Last active
September 10, 2018 19:56
-
-
Save martink-io/b45aa1df4feaae88957aee1b712df761 to your computer and use it in GitHub Desktop.
Cancel assignment(guard_job) by guard/admin
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 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 |
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 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