Skip to content

Instantly share code, notes, and snippets.

@martink-io
Last active September 10, 2018 19:56
Show Gist options
  • Save martink-io/b45aa1df4feaae88957aee1b712df761 to your computer and use it in GitHub Desktop.
Save martink-io/b45aa1df4feaae88957aee1b712df761 to your computer and use it in GitHub Desktop.
Cancel assignment(guard_job) by guard/admin
module AdminCommand
module GuardJobs
class Cancel < BaseCommand
def call
if job.present? && job.filled?
if current_user.guard? && current_user.id != guard_job.guard_id
return false
end
guard_job.update(guard_id: nil)
job.guard_jobs.count == job.guard_jobs.where(guard_id: nil).count do
job.update(state: 'unfilled') do
# send an email about job status change
end
end
if current_user.admin?
# send an email to guard
end
if current_user.guard?
# send an email to admin
end
AdminCommand::Jobs::InviteGuards.call(guard_id: guard_job.guard_id)
return true
end
return false
end
private
def guard_job
@guard_job ||= GuardJob.find(params[:id])
end
def job
@job ||= guard_job.job
end
end
end
end
module AdminCommand
module Jobs
class InviteGuards < BaseCommand
def call
suitable_guards(params[:guard_id]).each do |guard|
InvitationMailer.job_invitation_email(guard, job).deliver_later
end
end
def suitable_guards(guard_id)
job = params[:job]
suitable_guards = AdminCommand::Jobs::FindAvailableGuards.call(job: job) if guard_id.blank?
suitable_guards = AdminCommand::Jobs::FindAvailableGuards.call(job: job)
.where.not(guard_id) if guard_id.present?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment