Repository: CSCfi/rems
Pull request #2663 (2021-05-11 06:33:40)

feat: notification emails for applicant-changed
for #2581

# Checklist for author

Remove items that aren't applicable, check items that are done.

## Reviewability
- [x] Link to issue

## Documentation
- [ ] Update changelog if necessary
  - entry already there

## Testing
- [x] Complex logic is unit tested


Patch:
diff --git a/resources/translations/en.edn b/resources/translations/en.edn
index 0bcfbd756..fe20dfa0b 100644
--- a/resources/translations/en.edn
+++ b/resources/translations/en.edn
@@ -336,7 +336,11 @@
   ;; %4 - name of applicant
   ;; %5 - catalogue items of application, comma separated list
   ;; %6 - link to application
-  :email {:application-approved {:subject-to-applicant "Your application %3 has been approved"
+  :email {:applicant-changed {:subject-to-member "Applicant for application %3 changed"
+                              :message-to-member "Dear %1,\n\nThe applicant for application %3 has been changed to %4 by %2.\n\nYou can view the application at %6."
+                              :subject-to-handler "(%3) Applicant changed"
+                              :message-to-handler "Dear %1,\n\nThe applicant for application %3 has been changed to %4 by %2.\n\nYou can view the application at %6."}
+          :application-approved {:subject-to-applicant "Your application %3 has been approved"
                                  :message-to-applicant "Dear %1,\n\nYour application %3 has been approved.\n\nYou can view the application and the decision at %6"
                                  :subject-to-handler "(%3) Application has been approved"
                                  :message-to-handler "Dear %1,\n\n%2 has approved the application %3 from %4.\n\nYou can view the application and the decision at %6"}
diff --git a/resources/translations/fi.edn b/resources/translations/fi.edn
index badc9dac9..fbfe62a96 100644
--- a/resources/translations/fi.edn
+++ b/resources/translations/fi.edn
@@ -304,7 +304,11 @@
                     :title "Työvuon nimi"}
   :dropdown {:no-results "Ei tuloksia"
              :placeholder "Valitse..."}
-  :email {:application-approved {:subject-to-applicant "Hakemuksesi %3 on hyväksytty"
+  :email {:applicant-changed {:subject-to-member "Hakemuksen %3 hakija vaihtunut"
+                              :message-to-member "Hyvä %1,\n\n%2 on vaihtanut hakemuksen %3 hakijan käyttäjäksi %4.\n\nVoit tarkastella hakemusta: %6."
+                              :subject-to-handler "(%3) Hakija vaihtunut"
+                              :message-to-handler "Hyvä %1,\n\n%2 on vaihtanut hakemuksen %3 hakijan käyttäjäksi %4.\n\nVoit tarkastella hakemusta: %6."}
+          :application-approved {:subject-to-applicant "Hakemuksesi %3 on hyväksytty"
                                  :message-to-applicant "Hyvä %1,\n\nHakemuksesi %3 on hyväksytty.\n\nVoit tarkastella hakemusta ja päätöstä osoitteessa %6"
                                  :subject-to-handler "(%3) Hakemus on hyväksytty"
                                  :message-to-handler "Hyvä %1,\n\n%2 on hyväksynyt käyttäjän %4 hakemuksen %3.\n\nVoit tarkastella hakemusta ja päätöstä osoitteessa %6"}
diff --git a/resources/translations/sv.edn b/resources/translations/sv.edn
index 8fbfdf8cf..c8a2cc68e 100644
--- a/resources/translations/sv.edn
+++ b/resources/translations/sv.edn
@@ -304,7 +304,11 @@
                     :title "Arbetsflödets namn"}
   :dropdown {:no-results "Inga resultat"
              :placeholder "Välj..."}
-  :email {:application-approved {:subject-to-applicant "Din ansökan %3 är godkänd"
+  :email {:applicant-changed {:subject-to-member "Sökande för ansökan %3 bytte"
+                              :message-to-member "%1,\n\nSökande för ansökan %3 har bytts ut till %4 av %2.\n\nDu kan granska ansökningen här: %6." ; TODO check
+                              :subject-to-handler "(%3) Sökande bytte"
+                              :message-to-handler "%1,\n\nSökande för ansökan %3 har bytts ut till %4 av %2.\n\nDu kan granska ansökningen här: %6."} ; TODO check
+          :application-approved {:subject-to-applicant "Din ansökan %3 är godkänd"
                                  :message-to-applicant "%1,\n\nDin ansökan %3 är godkänd.\n\nDu kan granska ansökningen och beslutet här: %6"
                                  :subject-to-handler "(%3) Ansökan är godkänd"
                                  :message-to-handler "%1,\n\n%2 har godkänt ansökan %3 från %4.\n\nDu kan granska ansökningen och beslutet här: %6"}
diff --git a/src/clj/rems/email/template.clj b/src/clj/rems/email/template.clj
index ec4f1686d..6aab91fa5 100644
--- a/src/clj/rems/email/template.clj
+++ b/src/clj/rems/email/template.clj
@@ -236,6 +236,16 @@
                (text :t.email/regards)
                (text :t.email/footer))}])))
 
+(defmethod event-to-emails :application.event/applicant-changed [event application]
+  (concat (emails-to-recipients (application-util/applicant-and-members application)
+                                event application
+                                :t.email.applicant-changed/subject-to-member
+                                :t.email.applicant-changed/message-to-member)
+          (emails-to-recipients (other-handlers event application)
+                                event application
+                                :t.email.applicant-changed/subject-to-handler
+                                :t.email.applicant-changed/message-to-handler)))
+
 ;; TODO member-joined?
 
 (defn handler-reminder-email [lang handler applications]


Test patch:
diff --git a/test/clj/rems/email/test_template.clj b/test/clj/rems/email/test_template.clj
index 69da51cf9..e39bb6780 100644
--- a/test/clj/rems/email/test_template.clj
+++ b/test/clj/rems/email/test_template.clj
@@ -78,6 +78,9 @@
                  :email "assistant@example.com"}
     "remarker" {:userid "remarker"
                 :name "Random Remarker"}
+    "bob" {:userid "bob"
+           :name "Bob Boss"
+           :email "bob@corp.net"}
     {:userid userid}))
 
 (defn email-recipient [email]
@@ -395,6 +398,26 @@
                               :application/decider {:email "actor@example.com" :name "Adam Actor"}
                               :invitation/token "abc123"}))))
 
+(deftest test-change-applicant
+  (let [change {:application/id 7
+                :event/type :application.event/applicant-changed
+                :event/actor "assistant"
+                :application/applicant {:userid "bob"}}
+        mails (emails base-events change)]
+    (is (= #{"applicant" "handler" "bob"} (email-recipients mails)))
+    (is (= {:to-user "applicant"
+            :subject "Applicant for application 2001/3, \"Application title\" changed"
+            :body "Dear Alice Applicant,\n\nThe applicant for application 2001/3, \"Application title\" has been changed to Bob Boss by Amber Assistant.\n\nYou can view the application at http://example.com/application/7."}
+           (email-to "applicant" mails)))
+    (is (= {:to-user "bob"
+            :subject "Applicant for application 2001/3, \"Application title\" changed"
+            :body "Dear Bob Boss,\n\nThe applicant for application 2001/3, \"Application title\" has been changed to Bob Boss by Amber Assistant.\n\nYou can view the application at http://example.com/application/7."}
+           (email-to "bob" mails)))
+    (is (= {:to-user "handler"
+            :subject "(2001/3, \"Application title\") Applicant changed"
+            :body "Dear Hannah Handler,\n\nThe applicant for application 2001/3, \"Application title\" has been changed to Bob Boss by Amber Assistant.\n\nYou can view the application at http://example.com/application/7."}
+           (email-to "handler" mails)))))
+
 (deftest test-finnish-emails
   ;; only one test case so far, more of a smoke test
   (with-redefs [rems.locales/translations (assoc-in rems.locales/translations [:fi :t :email :regards] "")]