Repository: CSCfi/rems (clojure)

Issue:
LBR: ID not used in Email
Email still uses year/running number instead of ID.
The mail does not say what resource(s) were applied for (unlike present REMS):

Dear matthies@csc.fi,
User matthies@csc.fi has submitted a new application 2019/82.
View the application: https://rems-lbr03.csc.fi/#/application/910
Please do not reply to this automatically generated message.

Pull request:
fixes #1266

Patch:
diff --git a/resources/translations/en.edn b/resources/translations/en.edn
index f8ade00d44..2afa204c6c 100644
--- a/resources/translations/en.edn
+++ b/resources/translations/en.edn
@@ -199,7 +199,7 @@
           :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"}
+                                  :message "Dear %1,\nUser %2 has submitted a new application %3 for the resource(s) %4.\nView the application: %5"}
           :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 4019961dd9..a8d3f9f42f 100644
--- a/resources/translations/fi.edn
+++ b/resources/translations/fi.edn
@@ -199,7 +199,7 @@
           :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"}
+                                  :message "Hyvä %1,\nKäyttäjä %2 on lähettänyt uuden hakemuksen %3 resurss(e)ille %4.\nKatsele hakemusta: %5"}
           :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 8a28f9087a..96ce866724 100644
--- a/src/clj/rems/poller/email.clj
+++ b/src/clj/rems/poller/email.clj
@@ -1,10 +1,12 @@
 (ns rems.poller.email
   "Sending emails based on application events."
-  (:require [clojure.test :refer :all]
+  (:require [clojure.string :as str]
+            [clojure.test :refer :all]
             [clojure.tools.logging :as log]
             [mount.core :as mount]
             [postal.core :as postal]
             [rems.config :refer [env]]
+            [rems.context :as context]
             [rems.db.applications :as applications]
             [rems.db.events :as events]
             [rems.db.users :as users]
@@ -27,7 +29,14 @@
   (str (:public-url env) "accept-invitation?token=" token))
 
 (defn- application-id-for-email [application]
-  (or (:application/external-id application) (:application/id application)))
+  (case (util/getx env :application-id-column)
+    :external-id (:application/external-id application)
+    :id (:application/id application)))
+
+(defn- resources-for-email [application]
+  (->> (:application/resources application)
+       (map #(get-in % [:catalogue-item/title context/*lang*]))
+       (str/join ", ")))
 
 (defmulti ^:private event-to-emails-impl
   (fn [event _application] (:event/type event)))
@@ -44,6 +53,7 @@
                          handler
                          (:application/applicant application)
                          (application-id-for-email application)
+                         (resources-for-email application)
                          (link-to-application (:application/id application)))})))
 
 (defn- applicant-and-members [application]


Test patch:
diff --git a/test/clj/rems/poller/test_email.clj b/test/clj/rems/poller/test_email.clj
index 2103a1ecc0..28997bc9fb 100644
--- a/test/clj/rems/poller/test_email.clj
+++ b/test/clj/rems/poller/test_email.clj
@@ -18,9 +18,31 @@
 (defn sort-emails [emails]
   (sort-by #(or (:to %) (:to-user %)) emails))
 
+(def ^:private get-catalogue-item
+  {10 {:localizations {:en {:langcode :en
+                            :title "en title 11"}
+                       :fi {:langcode :fi
+                            :title "fi title 11"}}}
+   20 {:localizations {:en {:langcode :en
+                            :title "en title 21"}
+                       :fi {:langcode :fi
+                            :title "fi title 21"}}}})
+
+(def ^:private get-workflow
+  {5 {:workflow {:handlers ["handler" "assistant"]}}})
+
+(defn ^:private get-nothing [& _]
+  nil)
+
 (defn events-to-emails [events]
   (let [application (-> (reduce model/application-view nil events)
-                        (model/enrich-workflow-handlers {5 {:workflow {:handlers ["handler" "assistant"]}}}))]
+                        (model/enrich-with-injections {:get-workflow get-workflow
+                                                       :get-catalogue-item get-catalogue-item
+                                                       :get-form get-nothing
+                                                       :get-license get-nothing
+                                                       :get-user get-nothing
+                                                       :get-users-with-role get-nothing
+                                                       :get-attachments-for-application get-nothing}))]
     (text/with-language :en
       (fn [] (mapv #(sort-emails (#'rems.poller.email/event-to-emails-impl % application)) events)))))
 
@@ -29,6 +51,10 @@
                       :application/external-id "2001/3"
                       :event/type :application.event/created
                       :event/actor "applicant"
+                      :application/resources [{:catalogue-item/id 10
+                                               :resource/ext-id "urn:11"}
+                                              {:catalogue-item/id 20
+                                               :resource/ext-id "urn:21"}]
                       :workflow/id 5
                       :workflow/type :workflow/dynamic}
                      {:application/id 7
@@ -76,10 +102,10 @@
       (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"}
+                :body "Dear assistant,\nUser applicant has submitted a new application 2001/3 for the resource(s) en title 11, en title 21.\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"}]
+                :body "Dear handler,\nUser applicant has submitted a new application 2001/3 for the resource(s) en title 11, en title 21.\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"}]
@@ -134,11 +160,21 @@
       (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"}
+                :body "Dear assistant,\nUser applicant has submitted a new application 2001/3 for the resource(s) en title 11, en title 21.\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"}]
+                :body "Dear handler,\nUser applicant has submitted a new application 2001/3 for the resource(s) en title 11, en title 21.\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"}]]
-             (events-to-emails events))))))
+             (events-to-emails events))))
+    (testing "id field can be overrided"
+      (with-redefs [rems.config/env (assoc rems.config/env :application-id-column :id)]
+        (is (= [[]
+                [{:to-user "assistant"
+                  :subject "A new application has been submitted"
+                  :body "Dear assistant,\nUser applicant has submitted a new application 7 for the resource(s) en title 11, en title 21.\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 7 for the resource(s) en title 11, en title 21.\nView the application: http://localhost:3001/#/application/7"}]]
+               (events-to-emails base-events)))))))