Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v1/medical_shifts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def index
params: params.permit(:page, :per_page, :month, :year, :payd, :hospital_name).to_h
).medical_shifts

amount_cents = MedicalShifts::TotalAmountCents.call(user_id: current_user.id, month: params[:month])
amount_cents = MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts)

render json: {
total: amount_cents.total,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/pdf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def medical_shifts_pdf
scope: authorized_scope,
params: permitted_query_params
).medical_shifts
total_amount_cents = MedicalShifts::TotalAmountCents.call(user_id: current_user.id, month: params[:month])
total_amount_cents = MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts)

PdfGeneratorService.new(
relation: medical_shifts,
Expand Down
9 changes: 4 additions & 5 deletions app/operations/medical_shifts/total_amount_cents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

module MedicalShifts
class TotalAmountCents < Actor
input :user_id, type: Integer
input :month, type: String, allow_nil: true
input :medical_shifts

output :total, type: String
output :payd, type: String
output :unpaid, type: String

def call
self.total = Money.new(SumTotalAmountQuery.call(user_id:, month:), "BRL").format
self.payd = Money.new(SumPaydAmountQuery.call(user_id:, month:), "BRL").format
self.unpaid = Money.new(SumUnpaidAmountQuery.call(user_id:, month:), "BRL").format
self.total = Money.new(medical_shifts.sum(&:amount_cents), "BRL").format
self.payd = Money.new(medical_shifts.select(&:payd).sum(&:amount_cents), "BRL").format
self.unpaid = Money.new(medical_shifts.reject(&:payd).sum(&:amount_cents), "BRL").format
end
end
end
19 changes: 0 additions & 19 deletions app/queries/medical_shifts/sum_payd_amount_query.rb

This file was deleted.

19 changes: 0 additions & 19 deletions app/queries/medical_shifts/sum_total_amount_query.rb

This file was deleted.

19 changes: 0 additions & 19 deletions app/queries/medical_shifts/sum_unpaid_amount_query.rb

This file was deleted.

18 changes: 13 additions & 5 deletions spec/operations/medical_shifts/total_amount_cents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
describe ".result" do
it "is successful" do
user = create(:user)
medical_shift = create(:medical_shift, amount_cents: 1000, payd: true, user:, start_date: "25/02/2023")

expect(described_class.result(user_id: user.id, month: nil)).to be_success
expect(described_class.call(medical_shifts: [medical_shift])).to be_success
end

it "returns the total, payd and unpaid amount_cents of money" do
user = create(:user)
_medical_shift = create(:medical_shift, amount_cents: 1000, payd: true, user:, start_date: "25/02/2023")
medical_shift = create(:medical_shift, amount_cents: 1000, payd: true, user:, start_date: "25/02/2023")
_medical_shift2 = create(:medical_shift, amount_cents: 1000, payd: true, user:, start_date: "25/03/2023")
_payd_medical_shift = create(:medical_shift, amount_cents: 2000, payd: true, user:, start_date: "25/02/2023")
payd_medical_shift = create(:medical_shift, amount_cents: 2000, payd: true, user:, start_date: "25/02/2023")
_payd_medical_shift2 = create(:medical_shift, amount_cents: 2000, payd: true, user:, start_date: "25/03/2023")
_unpaid_medical_shift = create(:medical_shift, amount_cents: 1500, payd: false, user:, start_date: "25/02/2023")
unpaid_medical_shift = create(:medical_shift, amount_cents: 1500, payd: false, user:, start_date: "25/02/2023")
_unpaid_medical_shift2 = create(:medical_shift, amount_cents: 1500, payd: false, user:, start_date: "25/03/2023")

result = described_class.call(user_id: user.id, month: "2")
result = described_class.call(
medical_shifts:
[
medical_shift,
payd_medical_shift,
unpaid_medical_shift
]
)

expect(result.total).to eq("R$45.00")
expect(result.payd).to eq("R$30.00")
Expand Down
2 changes: 1 addition & 1 deletion spec/pdfs/medical_shifts_report_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it "generates a report with the correct content" do
user = create(:user)
pdf = Prawn::Document.new
amount = MedicalShifts::TotalAmountCents.call(user_id: user.id, month: nil)
medical_shifts = create_list(:medical_shift, 3, user_id: user.id)
amount = MedicalShifts::TotalAmountCents.call(medical_shifts: medical_shifts)

described_class.new(pdf: pdf, amount: amount, items: medical_shifts, title: "Plantões", email: user.email).generate
rendered_pdf = pdf.render
Expand Down
19 changes: 0 additions & 19 deletions spec/queries/medical_shifts/sum_payd_amount_query_spec.rb

This file was deleted.

18 changes: 0 additions & 18 deletions spec/queries/medical_shifts/sum_total_amount_query_spec.rb

This file was deleted.

18 changes: 0 additions & 18 deletions spec/queries/medical_shifts/sum_unpaid_amount_query_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/services/pdf_generator_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
it "generates medical_shifts pdf" do
user = create(:user)
medical_shifts = create_list(:medical_shift, 9, user: user)
amount = MedicalShifts::TotalAmountCents.call(user_id: medical_shifts.first.user_id, month: nil)
amount = MedicalShifts::TotalAmountCents.call(medical_shifts: [medical_shifts[0]])
pdf = described_class.new(
relation: medical_shifts, amount: amount, entity_name: "medical_shifts", email: user.email
).generate_pdf
Expand Down
Loading