Repository: CSCfi/rems
Pull request #1513 (2019-08-16 16:01:27)

Refactor commands to return 0..N events
Needed for #832


Patch:
diff --git a/src/clj/rems/application/commands.clj b/src/clj/rems/application/commands.clj
index 6376d73f2..f87c3f936 100644
--- a/src/clj/rems/application/commands.clj
+++ b/src/clj/rems/application/commands.clj
@@ -246,9 +246,9 @@
   (when (contains? (all-members application) userid)
     {:errors [{:type :already-member :application-id (:id application)}]}))
 
-(defn- ok [event]
+(defn- ok [& events]
   {:success true
-   :result event})
+   :events events})
 
 (defmethod command-handler :application.command/save-draft
   [cmd _application _injections]
@@ -425,7 +425,8 @@
 
 (defn- enrich-result [result cmd]
   (if (:success result)
-    (update result :result add-common-event-fields-from-command cmd)
+    (update result :events (fn [events]
+                             (mapv #(add-common-event-fields-from-command % cmd) events)))
     result))
 
 (defn ^:dynamic postprocess-command-result-for-tests [result _cmd _application]
diff --git a/src/clj/rems/db/applications.clj b/src/clj/rems/db/applications.clj
index 4a6d90352..cdf3ba717 100644
--- a/src/clj/rems/db/applications.clj
+++ b/src/clj/rems/db/applications.clj
@@ -125,7 +125,8 @@
   (let [app (get-unrestricted-application (:application-id cmd))
         result (commands/handle-command cmd app db-injections)]
     (if (:success result)
-      (events/add-event! (:result result))
+      (doseq [event (:events result)]
+        (events/add-event! event))
       result)))
 
 (defn accept-invitation [user-id invitation-token]


Test patch:
diff --git a/test/clj/rems/application/test_commands.clj b/test/clj/rems/application/test_commands.clj
index 55fd0a6dd..2d620269e 100644
--- a/test/clj/rems/application/test_commands.clj
+++ b/test/clj/rems/application/test_commands.clj
@@ -60,7 +60,12 @@
    (let [cmd (merge command-defaults cmd)
          result (commands/handle-command cmd application injections)]
      (assert-ex (:success result) {:cmd cmd :result result})
-     (events/validate-event (getx result :result)))))
+     (let [events (:events result)]
+       (events/validate-events events)
+       ;; most tests expect only one event, so this avoids having to wrap the expectation to a list
+       (if (= 1 (count events))
+         (first events)
+         events)))))
 
 (defn- apply-command
   ([application cmd]
diff --git a/test/clj/rems/db/test_transactions.clj b/test/clj/rems/db/test_transactions.clj
index c0dfd479f..9b540e785 100644
--- a/test/clj/rems/db/test_transactions.clj
+++ b/test/clj/rems/db/test_transactions.clj
@@ -62,8 +62,8 @@
         observed-app-version-marker 999
         mark-observed-app-version (fn [result _cmd application]
                                     (if (and (:success result)
-                                             (= :application.event/draft-saved (:event/type (:result result))))
-                                      (assoc-in result [:result :application/field-values observed-app-version-marker]
+                                             (= :application.event/draft-saved (:event/type (first (:events result)))))
+                                      (assoc-in result [:events 0 :application/field-values observed-app-version-marker]
                                                 (str (count (:application/events application))))
                                       result))
         write-event (fn [app-id]