Skip to content

Commit

Permalink
Prevent changing work history for received reference
Browse files Browse the repository at this point in the history
If the reference has already been received it shouldn't be possible to
change the name of the contact as it would then be incorrect, suggesting
a different person responded to the reference.
  • Loading branch information
thomasleese committed Aug 27, 2024
1 parent d1b7b1e commit afda5b8
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions app/services/update_work_history_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def initialize(work_history:, user:, name: nil, job: nil, email: nil)
end

def call
if reference_request.present? && reference_request.received?
raise InvalidState, "Reference has already been received."
end

change_value("contact_name", name) if name.present?

change_value("contact_job", job) if job.present?
Expand All @@ -27,33 +31,33 @@ def call
contact_email_domain: email_address.host_name,
},
)
end

if email.present? && (reference_request = work_history.reference_request)
reference_request.regenerate_slug
if reference_request.present?
reference_request.regenerate_slug

RequestRequestable.call(
requestable: reference_request,
user:,
allow_already_requested: true,
)
RequestRequestable.call(
requestable: reference_request,
user:,
allow_already_requested: true,
)

ApplicationFormStatusUpdater.call(application_form:, user:)
ApplicationFormStatusUpdater.call(application_form:, user:)

DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :references_requested,
reference_requests: [reference_request],
)
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :references_requested,
reference_requests: [reference_request],
)
end
end
end

private

attr_reader :work_history, :user, :name, :job, :email

delegate :application_form, to: :work_history
delegate :application_form, :reference_request, to: :work_history

def change_value(column_name, new_value, additional_updates: {})
old_value = work_history.send(column_name)
Expand All @@ -75,4 +79,7 @@ def create_timeline_event(column_name, old_value, new_value)
new_value:,
)
end

class InvalidState < StandardError
end
end

0 comments on commit afda5b8

Please sign in to comment.