Repository: CSCfi/rems (clojure)

Issue:
Send email to handler on application submit

Pull request:
closes #1184

Patch:
diff --git a/resources/translations/en.edn b/resources/translations/en.edn
index cd33295544..77105ed11f 100644
--- a/resources/translations/en.edn
+++ b/resources/translations/en.edn
@@ -232,6 +232,8 @@
                                  :message "Dear %1,\nYour application %2 has been rejected.\nView your application: %3"}
           :application-closed {:subject "Your application has been closed"
                                :message "Dear %1,\nYour application %2 has been closed.\nView your application: %3"}
+          :application-submitted {:subject "A new application has been submitted"
+                                  :message "Dear %1,\nUser %2 has submitted a new application %3.\nView the application: %4"}
           :comment-requested {:subject "Comment request"
                               :message "Dear %1,\nUser %2 has requested your comment on application %3.\nComment here: %4"}
           :commented {:subject "New comment notification"
diff --git a/resources/translations/fi.edn b/resources/translations/fi.edn
index 79fd614abb..b950581404 100644
--- a/resources/translations/fi.edn
+++ b/resources/translations/fi.edn
@@ -232,6 +232,8 @@
                                  :message "Hyvä %1,\nHakemuksesi %2 on hylätty.\nKatsele hakemusta: %3"}
           :application-closed {:subject "Hakemuksesi on suljettu"
                                :message "Hyvä %1,\nHakemuksesi %2 on suljettu.\nKatsele hakemusta: %3"}
+          :application-submitted {:subject "Uusi hakemus"
+                                  :message "Hyvä %1,\nKäyttäjä %2 on lähettänyt uuden hakemuksen %3.\nKatsele hakemusta: %4"}
           :comment-requested {:subject "Kommenttipyyntö"
                               :message "Hyvä %1,\nKäyttäjä %2 on pyytänyt sinulta kommenttia hakemukseen %3.\nKommentoi täällä: %4"}
           :commented {:subject "Uusi kommentti"
diff --git a/src/clj/rems/poller/email.clj b/src/clj/rems/poller/email.clj
index 2c482dbf0d..f0844bff1d 100644
--- a/src/clj/rems/poller/email.clj
+++ b/src/clj/rems/poller/email.clj
@@ -35,9 +35,19 @@
 (defmethod event-to-emails-impl :default [_event _application]
   [])
 
+(defmethod event-to-emails-impl :application.event/submitted [_event application]
+  (vec
+   (for [handler (get-in application [:application/workflow :workflow.dynamic/handlers])]
+     {:to-user handler
+      :subject (text :t.email.application-submitted/subject)
+      :body (text-format :t.email.application-submitted/message
+                         handler
+                         (:application/applicant application)
+                         (application-id-for-email application)
+                         (link-to-application (:application/id application)))})))
+
 (defn- applicant-and-members [application]
-  ;; XXX: the tests depend on the order of members
-  (conj (reverse (sort-by :userid (:application/members application)))
+  (conj (:application/members application)
         {:userid (:application/applicant application)}))
 
 ;; There's a slight inconsistency here: we look at current members, so


Test patch:
diff --git a/test/clj/rems/poller/test_email.clj b/test/clj/rems/poller/test_email.clj
index 5e0e6ea92b..393512ce46 100644
--- a/test/clj/rems/poller/test_email.clj
+++ b/test/clj/rems/poller/test_email.clj
@@ -16,11 +16,14 @@
     (f)
     (mount/stop)))
 
+(defn sort-emails [emails]
+  (sort-by #(or (:to %) (:to-user %)) emails))
+
 (defn events-to-emails [events]
   (let [application (-> (dynamic/apply-events nil events)
                         (model/enrich-workflow-handlers {5 {:workflow {:handlers ["handler" "assistant"]}}}))]
     (text/with-language :en
-      (fn [] (mapv #(#'rems.poller.email/event-to-emails-impl % application) events)))))
+      (fn [] (mapv #(sort-emails (#'rems.poller.email/event-to-emails-impl % application)) events)))))
 
 (deftest test-event-to-emails-impl
   (let [base-events [{:application/id 7
@@ -72,7 +75,12 @@
                     :event/type :application.event/closed
                     :event/actor "assistant"}])]
       (is (= [[]
-              []
+              [{:to-user "assistant",
+                :subject "A new application has been submitted",
+                :body "Dear assistant,\nUser applicant has submitted a new application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
+               {:to-user "handler",
+                :subject "A new application has been submitted",
+                :body "Dear handler,\nUser applicant has submitted a new application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
               [{:to "somebody@example.com",
                 :subject "Invitation to participate in an application",
                 :body "Hello,\nThis email address (somebody@example.com) has been invited to participate in an application.\nParticipate with this link: http://localhost:3001/accept-invitation?token=abc"}]
@@ -83,49 +91,54 @@
                 :subject "Comment request",
                 :body "Dear commenter2,\nUser handler has requested your comment on application 2001/3.\nComment here: http://localhost:3001/#/application/7"}]
               []
-              [{:to-user "handler",
+              [{:to-user "assistant",
                 :subject "New comment notification",
-                :body "Dear handler,\nUser commenter2 has posted a comment on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
-               {:to-user "assistant",
+                :body "Dear assistant,\nUser commenter2 has posted a comment on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
+               {:to-user "handler",
                 :subject "New comment notification",
-                :body "Dear assistant,\nUser commenter2 has posted a comment on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
+                :body "Dear handler,\nUser commenter2 has posted a comment on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
               [{:to-user "member",
                 :subject "You've been added as a member to an application",
                 :body "Dear member,\nYou've been added as a member to application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
               [{:to-user "decider",
                 :subject "Decision request",
                 :body "Dear decider,\nUser assistant has requested your decision on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
-              [{:to-user "handler",
+              [{:to-user "assistant",
                 :subject "New decision notification",
-                :body "Dear handler,\nUser decider has sent a decision on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
-               {:to-user "assistant",
+                :body "Dear assistant,\nUser decider has sent a decision on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
+               {:to-user "handler",
                 :subject "New decision notification",
-                :body "Dear assistant,\nUser decider has sent a decision on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
+                :body "Dear handler,\nUser decider has sent a decision on application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
               [{:to-user "applicant",
                 :subject "Your application has been approved",
                 :body "Dear applicant,\nYour application 2001/3 has been approved.\nView your application: http://localhost:3001/#/application/7"}
-               {:to-user "somebody",
-                :subject "Your application has been approved",
-                :body "Dear somebody,\nYour application 2001/3 has been approved.\nView your application: http://localhost:3001/#/application/7"}
                {:to-user "member",
                 :subject "Your application has been approved",
-                :body "Dear member,\nYour application 2001/3 has been approved.\nView your application: http://localhost:3001/#/application/7"}]
+                :body "Dear member,\nYour application 2001/3 has been approved.\nView your application: http://localhost:3001/#/application/7"}
+               {:to-user "somebody",
+                :subject "Your application has been approved",
+                :body "Dear somebody,\nYour application 2001/3 has been approved.\nView your application: http://localhost:3001/#/application/7"}]
               [{:to-user "applicant",
                 :subject "Your application has been closed",
                 :body "Dear applicant,\nYour application 2001/3 has been closed.\nView your application: http://localhost:3001/#/application/7"}
-               {:to-user "somebody",
-                :subject "Your application has been closed",
-                :body "Dear somebody,\nYour application 2001/3 has been closed.\nView your application: http://localhost:3001/#/application/7"}
                {:to-user "member",
                 :subject "Your application has been closed",
-                :body "Dear member,\nYour application 2001/3 has been closed.\nView your application: http://localhost:3001/#/application/7"}]]
+                :body "Dear member,\nYour application 2001/3 has been closed.\nView your application: http://localhost:3001/#/application/7"}
+               {:to-user "somebody",
+                :subject "Your application has been closed",
+                :body "Dear somebody,\nYour application 2001/3 has been closed.\nView your application: http://localhost:3001/#/application/7"}]]
              (events-to-emails events))))
     (let [events (conj base-events
                        {:application/id 7
                         :event/type :application.event/rejected
                         :event/actor "handler"})]
       (is (= [[]
-              []
+              [{:to-user "assistant",
+                :subject "A new application has been submitted",
+                :body "Dear assistant,\nUser applicant has submitted a new application 2001/3.\nView the application: http://localhost:3001/#/application/7"}
+               {:to-user "handler",
+                :subject "A new application has been submitted",
+                :body "Dear handler,\nUser applicant has submitted a new application 2001/3.\nView the application: http://localhost:3001/#/application/7"}]
               [{:subject "Your application has been rejected",
                 :body "Dear applicant,\nYour application 2001/3 has been rejected.\nView your application: http://localhost:3001/#/application/7",
                 :to-user "applicant"}]]