generated from espoo-dev/rails_boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent_procedures_controller.rb
More file actions
127 lines (107 loc) · 3.44 KB
/
event_procedures_controller.rb
File metadata and controls
127 lines (107 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true
module Api
module V1
class EventProceduresController < ApiController
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
def index
authorized_scope = policy_scope(EventProcedure)
event_procedures = EventProcedures::List.result(
scope: authorized_scope,
params: event_procedure_permitted_query_params
).event_procedures
total_amount_cents = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)
render json: {
total: total_amount_cents.total,
total_payd: total_amount_cents.payd,
total_unpayd: total_amount_cents.unpaid,
event_procedures: serialized_event_procedures(event_procedures)
}, status: :ok
end
def create
authorize(EventProcedure)
result = EventProcedures::Create.result(attributes: event_procedure_params, user_id: current_user.id)
if result.success?
render json: result.event_procedure, status: :created
else
render json: result.error, status: :unprocessable_entity
end
end
def update
authorize(event_procedure)
result = EventProcedures::Update.result(id: event_procedure.id.to_s, attributes: event_procedure_params)
if result.success?
render json: result.event_procedure, status: :ok
else
render json: result.error, status: :unprocessable_entity
end
end
def destroy
authorize(event_procedure)
result = EventProcedures::Destroy.result(id: event_procedure.id.to_s)
if result.success?
deleted_successfully_render(result.event_procedure)
else
render json: result.error, status: :unprocessable_entity
end
end
private
def event_procedure_permitted_params
params.permit(
:hospital_id,
:cbhpm_id,
:patient_service_number,
:date,
:urgency,
:amount_cents,
:payd,
:room_type,
:payment,
patient_attributes: %i[
id name
],
procedure_attributes: %i[
id name code amount_cents description custom
],
health_insurance_attributes: %i[
id name custom
]
).to_h
end
def event_procedure_permitted_query_params
params.permit(
:page,
:per_page,
:month,
:year,
:payd,
hospital: [:name],
health_insurance: [:name]
).to_h
end
def event_procedure_params
result_params = event_procedure_permitted_params
%i[patient_attributes health_insurance_attributes procedure_attributes].each do |attribute|
result_params[attribute]&.merge!(user_id: current_user.id)
end
result_params
end
def event_procedure
@event_procedure ||= EventProcedures::Find.result(id: params[:id]).event_procedure
end
def serialized_event_procedures(event_procedures)
ActiveModelSerializers::SerializableResource.new(
event_procedures,
each_serializer: EventProcedureSerializer
)
end
def total_amount_cents_params
{
user_id: current_user.id,
month: params[:month],
year: params[:year]
}
end
end
end
end