From afda5b8355e17cd49e38dfd9c233de70fc506ab0 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 20 Aug 2024 15:13:17 +0100 Subject: [PATCH] Prevent changing work history for received reference 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. --- app/services/update_work_history_contact.rb | 39 ++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/app/services/update_work_history_contact.rb b/app/services/update_work_history_contact.rb index 996ace773..6809eb6b1 100644 --- a/app/services/update_work_history_contact.rb +++ b/app/services/update_work_history_contact.rb @@ -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? @@ -27,25 +31,25 @@ 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 @@ -53,7 +57,7 @@ def call 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) @@ -75,4 +79,7 @@ def create_timeline_event(column_name, old_value, new_value) new_value:, ) end + + class InvalidState < StandardError + end end