Commit 71d3620a authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei

Add requests for solicitations

Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent 04d19c04
Pipeline #14191 failed with stage
in 6 minutes and 44 seconds
......@@ -667,5 +667,26 @@ module Api::V1
render json: response.as_json
end
# GET /forms/solicitation_index
def solicitation_index
citizen = current_user[0]
permission = Professional.get_permission(current_user[1])
response = Hash.new
case permission
when "adm_c3sl"
response[:city_ids] = Solicitation.all
.as_json(only: [:city_id], methods: %w(city_name))
else
render json: {
errors: ["You're not allowed to view this form."]
}, status: 403
return
end
render json: response.as_json
end
end
end
module Api::V1
class SolicitationsController < ApplicationController
include Authenticable
before_action :set_solicitation, only: [:show, :update, :destroy]
# Had to be specified instead of "include Authenticable" because of the create
# method exception
before_action :authenticate_v1_account!, :verify_permission, except: [:create]
# GET /solicitations
def index
@solicitations = Solicitation.all
@solicitations = policy_scope(Solicitation.filter(params[:q], params[:page],
Professional.get_permission(current_user[1])))
render json: @solicitations
if @solicitations.nil?
render json: {
errors: ["You don't have the permission to view solicitations."]
}, status: 403
return
else
response = Hash.new
response[:num_entries] = @solicitations.total_count
response[:entries] = @solicitations.index_response
render json: response.to_json
return
end
end
# GET /solicitations/1
def show
if @solicitation.nil?
......@@ -18,46 +37,31 @@ module Api::V1
errors: ["Solicitation #{params[:id]} does not exist."]
}, status: 404
else
render json: @solicitation
begin
authorize @solicitation, :show?
rescue
render json: {
errors: ["You are not allowed to view solicitations"]
}, status: 403
return
end
render json: @solicitation.complete_info_response
end
end
# POST /solicitations
def create
@solicitation = Solicitation.new(solicitation_params)
if @solicitation.save
render json: @solicitation, status: :created#, location: @solicitation
render json: @solicitation.complete_info_response, status: :created
else
render json: @solicitation.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /solicitations/1
def update
if @solicitation.nil?
render json: {
errors: ["Solicitation #{params[:id]} does not exist."]
}, status: 404
else
if @solicitation.update(solicitation_params)
render json: @solicitation
else
render json: @solicitation.errors, status: :unprocessable_entity
end
end
end
# DELETE /solicitations/1
def destroy
if @solicitation.nil?
render json: {
errors: ["Solicitation #{params[:id]} does not exist."]
}, status: 404
else
@solicitation.destroy
end
end
private
......@@ -70,17 +74,15 @@ module Api::V1
end
end
# Only allow a trusted parameter "white list" through.
def solicitation_params
params.require(:solicitation).permit(
:id,
:cep,
:city_id,
:cpf,
:email,
:name,
:phone,
:sent
)
end
end
......
class Solicitation < ApplicationRecord
include Searchable
# Associations #
belongs_to :city, optional: true
......@@ -7,4 +8,87 @@ class Solicitation < ApplicationRecord
validates_presence_of :cpf, :name, :email, :phone, :cep
validates_uniqueness_of :cpf, scope: :city_id
validates :cpf, cpf: true
before_validation :create_solicitation
delegate :name, to: :city, prefix: true
delegate :state_name, to: :city
# Returns json response to index solicitations
# @return [Json] response
def self.index_response
self.all.as_json(only: [:id, :name, :cep, :phone, :city_id, :email, :sent],
methods: %w(city_name state_name))
end
# @return [Json] detailed service_type's data
def complete_info_response
city = City.find(self.city_id)
state = city.state
address = Address.get_address(self.cep)
return self.as_json(only: [
:id, :name, :cpf, :cep, :phone, :email
])
.merge({city: city.as_json(except: [
:ibge_code, :state_id, :created_at, :updated_at
])})
.merge({state: state.as_json(except: [
:ibge_code, :created_at, :updated_at
])})
.merge({address: address.as_json(except: [
:created_at, :updated_at, :state_id, :city_id
])})
end
# @params params [ActionController::Parameters] Parameters for searching
# @params npage [String] number of page to be returned
# @params permission [String] Permission of current user
# @return [ActiveRecords] filtered citizens
def self.filter(params, npage, permission)
return search(search_params(params, permission), npage)
end
private
# Translates incoming search parameters to ransack patterns
# @params params [ActionController::Parameters] Parameters for searching
# @params permission [String] Permission of current user
# @return [Hash] filtered and translated parameters
def self.search_params(params, permission)
case permission
when "adm_c3sl"
sortable = ["name", "cpf", "email", "phone", "city_name", "city_state_name"]
filter = {"city_id" => "city_id_eq", "sent" => "sent_eq", "s" => "s"}
end
return filter_search_params(params, filter, sortable)
end
# Method called when creating a solicitation. It associates
# the address to the service place given a cep
def create_solicitation
address = Address.get_address(self.cep)
self.sent = true
if not address.nil?
self.city_id = address.city_id
city = City.find(self.city_id)
city_hall = CityHall.find_by(city_id: self.city_id)
if not city_hall.nil?
self.errors["cep"] << "CityHall already exists for city #{city.name}"
end
else
self.errors["cep"] << "#{self.cep} is invalid."
return false
end
end
end
class SolicitationPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope
citizen = user[0]
permission = Professional.get_permission(user[1])
if permission == "citizen"
return nil
end
return case permission
when "adm_c3sl"
scope.all
else
nil
end
end
end
def show?
citizen = user[0]
permission = Professional.get_permission(user[1])
if permission == "citizen"
return false
end
return case
when permission == "adm_c3sl"
return true
else
false
end
end
end
......@@ -34,7 +34,7 @@ Rails.application.routes.draw do
resources :service_places
resources :service_types, except: :destroy
resources :shifts
resources :solicitations
resources :solicitations, only: [:create, :index, :show]
resources :notifications
......@@ -60,6 +60,7 @@ Rails.application.routes.draw do
get "forms/occupation_index" => "forms#occupation_index"
get "forms/schedule_index" => "forms#schedule_index"
get "forms/schedule_per_type_index" => "forms#schedule_per_type_index"
get "forms/solicitation_index" => "forms#solicitation_index"
get "schedule_per_type_report" => "schedules#schedule_per_type"
get "check_create_professional" => "professionals#check_create_professional"
......
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