Created
October 3, 2018 09:50
-
-
Save martink-io/8264bd24b36fe954a478e1f9de289442 to your computer and use it in GitHub Desktop.
client_command/jobs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ClientCommand | |
module Jobs | |
class CancelFilled < BaseCommand | |
include Concerns::Common::WithUser | |
def call | |
job.cancel_client_filled! do | |
JobItemCommand::CreateCancellationPenaltyCharge::call(job: job) | |
if refund.save | |
AdminCommand::StripeRefund::Create::call(job: job) | |
JobMailer.cancelled_filled_confirmation_email(job, current_user).deliver_later | |
notify_admins | |
# TODO: Refactor to consider multiple guards | |
JobMailer.cancelled_filled_notification_email(job, guard).deliver_later | |
return true | |
end | |
end | |
return false | |
end | |
private | |
def job | |
@job ||= params[:job] | |
end | |
def notify_admins | |
User.by_role('admin').each do |admin_user| | |
JobMailer.cancelled_filled_notification_email(job, admin_user).deliver_later | |
end | |
end | |
def guard | |
@guard ||= User.find(job.guard_jobs.first.guard_id) | |
end | |
def refund | |
@guard_booking ||= ::JobItems::GuardBooking.where(job_id: job.id).first! | |
@late_booking_penalty ||= ::JobItems::LateBookingPenalty.where(job_id: job.id).first | |
@cancellation_penalty ||= ::JobItems::CancelationPenalty.where(job_id: job.id).first | |
::JobItems::Refund.new(charge_rate: @guard_booking.charge_rate, | |
pay_rate: @guard_booking.pay_rate, | |
job: job, | |
resource: job.guard_jobs.first, | |
quantity: quantity) | |
end | |
def quantity | |
if @cancellation_penalty.present? | |
@guard_booking.quantity - @cancellation_penalty.quantity | |
elsif @late_booking_penalty.present? | |
job.number_of_hours + @late_booking_penalty.quantity | |
else | |
job.number_of_hours | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We miss one more scenario: Both LBP & CP to be present, right?