Skip to content

Instantly share code, notes, and snippets.

@martink-io
Created September 25, 2018 14:37
Show Gist options
  • Save martink-io/3e0547cb923532c1e0fc5c9b3c3bbd59 to your computer and use it in GitHub Desktop.
Save martink-io/3e0547cb923532c1e0fc5c9b3c3bbd59 to your computer and use it in GitHub Desktop.
Client Comamnd, Cancel Job Filled
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: charge_rate,
pay_rate: pay_rate,
job: job,
resource: job.guard_jobs.first,
quantity: quantity)
end
def quantity
if @cancellation_penalty.present?
@guard_booking.quantity - @cancellation_penalty.quantity
else
job.number_of_hours
end
end
def charge_rate
if @late_booking_penalty.present?
@guard_booking.charge_rate + @late_booking_penalty.charge_rate
else
@guard_booking.charge_rate
end
end
def pay_rate
if @late_booking_penalty.present?
@guard_booking.pay_rate + @late_booking_penalty.pay_rate
else
@guard_booking.pay_rate
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment