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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def recurrence_params
:workload,
:start_hour,
:hospital_name,
:amount_cents
:amount_cents,
:color
).to_h
end
end
Expand Down
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 @@ -84,7 +84,7 @@ def medical_shift
end

def medical_shift_params
params.permit(:hospital_name, :workload, :start_date, :start_hour, :amount_cents, :paid).to_h
params.permit(:hospital_name, :workload, :start_date, :start_hour, :amount_cents, :paid, :color).to_h
end

def serialized_medical_shifts(medical_shifts)
Expand Down
1 change: 1 addition & 0 deletions app/models/medical_shift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MedicalShift < ApplicationRecord
validates :amount_cents, presence: true

validates :amount_cents, numericality: { greater_than_or_equal_to: 0 }
validates :color, format: { with: /\A#[a-fA-F0-9]{6}\z/ }

def shift
daytime_start = Time.new(2000, 0o1, 0o1, 0o7, 0o0, 0o0, 0o0)
Expand Down
1 change: 1 addition & 0 deletions app/models/medical_shift_recurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MedicalShiftRecurrence < ApplicationRecord
validates :start_hour, presence: true
validates :hospital_name, presence: true
validates :amount_cents, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :color, format: { with: /\A#[a-fA-F0-9]{6}\z/ }

validates :day_of_week, presence: true, if: lambda {
frequency.in?(%w[weekly biweekly])
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/medical_shift_recurrence_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class MedicalShiftRecurrenceSerializer < ActiveModel::Serializer
:hospital_name,
:amount_cents,
:last_generated_until,
:user_id
:user_id,
:color

def amount_cents
object.amount.format
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/medical_shift_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MedicalShiftSerializer < ActiveModel::Serializer
attributes :id, :hospital_name, :workload, :date, :hour, :amount_cents, :paid, :shift, :title,
:medical_shift_recurrence_id
:medical_shift_recurrence_id, :color

def date
object.start_date.strftime("%d/%m/%Y")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddColorToMedicalShiftsAndRecurrences < ActiveRecord::Migration[7.1]
def change
add_column :medical_shifts, :color, :string, default: "#ffffff", null: false
add_column :medical_shift_recurrences, :color, :string, default: "#ffffff", null: false
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/medical_shift_recurrences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
start_hour { "19:00:00" }
hospital_name { create(:hospital).name }
amount_cents { 120_000 }
color { "#ffffff" }

trait :weekly do
frequency { "weekly" }
Expand Down
1 change: 1 addition & 0 deletions spec/factories/medical_shifts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
start_hour { "10:51:23" }
amount_cents { 1 }
paid { false }
color { "#ffffff" }

traits_for_enum(:workload, MedicalShifts::Workloads.list)

Expand Down
8 changes: 8 additions & 0 deletions spec/models/medical_shift_recurrence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

it { is_expected.to validate_numericality_of(:amount_cents).is_greater_than_or_equal_to(0) }

it { is_expected.to allow_value("#FF00aa").for(:color) }
it { is_expected.not_to allow_value("FF00aa").for(:color) }
it { is_expected.not_to allow_value("#fff").for(:color) }
it { is_expected.not_to allow_value("#ff00aaff").for(:color) }
it { is_expected.not_to allow_value("#ff00zz").for(:color) }
it { is_expected.not_to allow_value("random value").for(:color) }
it { is_expected.not_to allow_value("").for(:color) }

context "when frequency is weekly" do
it "requires day_of_week to be present and valid" do
weekly = build(:medical_shift_recurrence, :weekly, day_of_week: nil)
Expand Down
8 changes: 8 additions & 0 deletions spec/models/medical_shift_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
it { is_expected.to validate_presence_of(:amount_cents) }

it { is_expected.to validate_numericality_of(:amount_cents).is_greater_than_or_equal_to(0) }

it { is_expected.to allow_value("#FF00aa").for(:color) }
it { is_expected.not_to allow_value("FF00aa").for(:color) }
it { is_expected.not_to allow_value("#fff").for(:color) }
it { is_expected.not_to allow_value("#ff00aaff").for(:color) }
it { is_expected.not_to allow_value("#ff00zz").for(:color) }
it { is_expected.not_to allow_value("random value").for(:color) }
it { is_expected.not_to allow_value("").for(:color) }
end

describe ".enumerations" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"start_date",
"workload",
"hospital_name",
"amount_cents"
"amount_cents",
"color"
)
end

Expand Down Expand Up @@ -121,7 +122,8 @@
workload: MedicalShifts::Workloads::SIX,
start_hour: "19:00:00",
hospital_name: "Hospital Teste",
amount_cents: 120_000
amount_cents: 120_000,
color: "#ffffff"
}
}
end
Expand All @@ -142,7 +144,7 @@
end.to change(MedicalShiftRecurrence, :count).by(1)
end

it "returns the created recurrence" do
it "returns the created recurrence" do # rubocop:disable Rspec/MultipleExpectations
post "/api/v1/medical_shift_recurrences",
params: valid_params,
headers: auth_headers
Expand All @@ -152,6 +154,7 @@
expect(body["medical_shift_recurrence"]).to be_present
expect(body["medical_shift_recurrence"]["frequency"]).to eq("weekly")
expect(body["medical_shift_recurrence"]["day_of_week"]).to eq(1)
expect(body["medical_shift_recurrence"]["color"]).to eq("#ffffff")
end

it "generates shifts immediately" do
Expand Down
24 changes: 16 additions & 8 deletions spec/requests/api/v1/medical_shifts_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"amount_cents" => medical_shifts.second.amount.format,
"paid" => medical_shifts.second.paid,
"shift" => medical_shifts.second.shift,
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}"
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}",
"color" => medical_shifts.second.color
},
{
"id" => medical_shifts.first.id,
Expand All @@ -53,7 +54,8 @@
"amount_cents" => medical_shifts.first.amount.format,
"paid" => medical_shifts.first.paid,
"shift" => medical_shifts.first.shift,
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}"
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}",
"color" => medical_shifts.first.color
}
)
end
Expand Down Expand Up @@ -135,7 +137,8 @@
"amount_cents" => paid_medical_shifts.second.amount.format,
"paid" => paid_medical_shifts.second.paid,
"shift" => paid_medical_shifts.second.shift,
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}"
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}",
"color" => paid_medical_shifts.second.color
},
{
"id" => paid_medical_shifts.first.id,
Expand All @@ -147,7 +150,8 @@
"amount_cents" => paid_medical_shifts.first.amount.format,
"paid" => paid_medical_shifts.first.paid,
"shift" => paid_medical_shifts.first.shift,
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}"
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}",
"color" => paid_medical_shifts.first.color
}
)
end
Expand Down Expand Up @@ -176,7 +180,8 @@
"amount_cents" => unpaid_medical_shifts.second.amount.format,
"paid" => unpaid_medical_shifts.second.paid,
"shift" => unpaid_medical_shifts.second.shift,
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}"
"title" => "#{second_hospital_name} | #{second_workload} | #{second_shift}",
"color" => unpaid_medical_shifts.second.color
},
{
"id" => unpaid_medical_shifts.first.id,
Expand All @@ -188,7 +193,8 @@
"amount_cents" => unpaid_medical_shifts.first.amount.format,
"paid" => unpaid_medical_shifts.first.paid,
"shift" => unpaid_medical_shifts.first.shift,
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}"
"title" => "#{first_hospital_name} | #{first_workload} | #{first_shift}",
"color" => unpaid_medical_shifts.first.color
}
)
end
Expand Down Expand Up @@ -255,7 +261,8 @@
start_date: "2024-01-29",
start_hour: "10:51:23",
amount_cents: 1,
paid: false
paid: false,
color: "#000000"
}

post path, params: params, headers: headers
Expand All @@ -273,7 +280,8 @@
amount_cents: MedicalShift.last.amount.format,
paid: params[:paid],
shift: MedicalShift.last.shift,
title: "#{hospital_name} | #{workload} | #{shift}"
title: "#{hospital_name} | #{workload} | #{shift}",
color: "#000000"
)
end
end
Expand Down