Commit bf7757a2 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei

Add schedule by type report request

Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent 4dd2f78e
Pipeline #14131 passed with stage
in 7 minutes and 19 seconds
......@@ -646,5 +646,47 @@ module Api::V1
render json: response.as_json
end
# GET /forms/schedule_per_type_index
def schedule_per_type_index
citizen = current_user[0]
permission = Professional.get_permission(current_user[1])
response = Hash.new
case permission
when "adm_c3sl"
city_halls = CityHall.all_active
response[:city_halls] = city_halls.as_json(only: [:id, :name, :city_id])
else
render json: {
errors: ["You're not allowed to view this form."]
}, status: 403
return
end
render json: response.as_json
end
# GET /forms/schedule_per_type
def schedule_per_type
citizen = current_user[0]
permission = Professional.get_permission(current_user[1])
response = Hash.new
case permission
when "adm_c3sl"
city_halls = CityHall.all_active
response[:city_halls] = city_halls.as_json(only: [:id, :name, :city_id])
else
render json: {
errors: ["You're not allowed to view this form."]
}, status: 403
return
end
render json: response.as_json
end
end
end
......@@ -143,6 +143,32 @@ module Api::V1
end
# GET /schedules/schedule_by_type
def schedule_by_type
permission = Professional.get_permission(current_user[1])
if permission == 'adm_c3sl'
city_hall_id = params[:city_hall_id]
elsif permission == 'adm_prefeitura' or permission == 'adm_local'
professional = current_user[0].professional
city_hall_id = professional.professionals_service_places
.find(current_user[1]).service_place.city_hall_id
else
render json: {
errors: ["You're not allowed to view this report."]
}, status: 422
return
end
startt = params[:start_time]
endt = params[:end_time]
@schedules = Schedule.schedule_by_type(city_hall_id, startt, endt)
render json: @schedules
end
# NEVER USED (TODO: Check if is save to remove - definitely not safe to leave it there)
# POST /schedules
def create
......
......@@ -243,6 +243,52 @@ class Schedule < ApplicationRecord
return response
end
def self.schedule_by_type(city_hall_id, startt, endt)
schedules = Schedule.where(situation: Situation.compareceu)
.where('service_start_time >= ?', startt)
.where('service_start_time <= ?', endt)
.where(service_places: {city_hall_id: city_hall_id})
.includes(:service_place)
sps = schedules.pluck(:service_place_id)
service_places = ServicePlace.where(id: sps)
response = Array.new
service_places.each do |sp|
entry = Hash.new
entry[:service_place_id] = sp.id
entry[:service_place_name] = sp.name
entry[:professionals] = Professional
.where(professionals_service_places: {service_place_id: sp.id})
.includes(:professionals_service_places).simple_index_response
entry[:service_types] = []
service_types = sp.service_types
for i in service_types
st = Hash.new
st[:id] = i.id
st[:description] = i.description
st[:schedules] = []
for j in entry[:professionals]
st[:schedules].append(
schedules.where(shifts: {professional_performer_id: j["id"]})
.includes(:shift)
.where(shifts: {service_type_id: i.id}).includes(:shift).count
)
end
entry[:service_types].append(st)
end
response.append(entry)
end
return response.as_json
end
# @params params [ActionController::Parameters] Parameters for searching
# @params npage [String] number of page to be returned
......
......@@ -8,32 +8,41 @@ class Situation < ApplicationRecord
validates_presence_of :description
# Returns the situation with the description "Agendador" (scheduled)
# @return [Situation] Returns the situation with the description "Agendador"
# @return [Situation] situation with the description "Agendador"
def self.agendado
where(description: "Agendado").first
end
# Returns the situation with the description "Cancelado" (cancelled)
# @return [Situation] Returns the situation with the description "Cancelado"
# @return [Situation] situation with the description "Cancelado"
def self.cancelado
where(description: "Cancelado").first
end
# Returns the situation with the description "Cidadão não compareceu" (citizen not attended)
# @return [Situation] Returns the situation with the description "Cidadão não compareceu"
# @return [Situation] situation with the description "Cidadão não compareceu"
def self.citizen_absence
where(description: "Cidadão não compareceu").first
end
# Returns the situation with the description "Professional não compareceu" (professional not attended)
# @return [Situation] Returns the situation with the description "Professional não compareceu"
# @return [Situation] situation with the description "Professional não compareceu"
def self.professional_absence
where(description: "Profissional não compareceu").first
end
# Returns the situation with the description "available" (available)
# @return [Situation] returns the situation with the description "available"
# @return [Situation] situation with the description "available"
def self.disponivel
where(description: "Disponível").first
end
# Situations where the citizen showed up
def self.compareceu
where(description: [
"Atendimento realizado",
"Cidadão compareceu com atraso",
"Profissional compareceu com atraso"
])
end
end
......@@ -92,24 +92,24 @@ class SchedulePolicy < ApplicationPolicy
return case
when permission == "adm_c3sl"
return (record.situation.description == "Disponível")
return (record.situation.description == "Agendado")
when permission == "adm_prefeitura"
return ((record.service_place.city_hall_id == service_place.city_hall_id) and
(record.situation.description == "Disponível"))
(record.situation.description == "Agendado"))
when permission == "adm_local"
return ((record.service_place.id == service_place.id) and
(record.situation.description == "Disponível"))
(record.situation.description == "Agendado"))
when permission == "atendendente_local"
return ((record.service_place.id == service_place.id) and
(record.situation.description == "Disponível"))
(record.situation.description == "Agendado"))
when permission == "responsavel_atendimento"
return ((record.service_place.id == service_place.id) and
(record.shift.professional_performer_id == professional.id)
(record.situation.description == "Disponível"))
(record.situation.description == "Agendado"))
when permission == "citizen"
return ((record.situation.description == "Agendado") and
......
......@@ -60,6 +60,7 @@ Rails.application.routes.draw do
get "forms/occupation_index" => "forms#occupation_index"
get "forms/schedule_index" => "forms#schedule_index"
get "schedule_by_type_report" => "schedules#schedule_by_type"
get "check_create_professional" => "professionals#check_create_professional"
get "resource_details/:id" => "resources#details"
get "resource_more_info" => "resources#all_details"
......@@ -70,4 +71,4 @@ Rails.application.routes.draw do
end
end
end
\ No newline at end of file
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment