Repository: CSCfi/rems
Pull request #1090 (2019-04-08 13:40:33)

Refactor: rename :endt to :end


Patch:
diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql
index 3492f7390..64061e46d 100644
--- a/resources/sql/queries.sql
+++ b/resources/sql/queries.sql
@@ -51,8 +51,8 @@ SET
 /*~ (when (boolean? (:archived params)) */
   archived = :archived,
 /*~ ) ~*/
-/*~ (when (contains? params :endt) */
-  endt = :endt,
+/*~ (when (contains? params :end) */
+  endt = :end,
 /*~ ) ~*/
   id = id
 WHERE id = :id;
@@ -75,7 +75,7 @@ SELECT
   organization,
   resid,
   start,
-  endt,
+  endt as "end",
   enabled,
   archived
 FROM resource;
@@ -88,7 +88,7 @@ SELECT
   organization,
   resid,
   start,
-  endt,
+  endt as "end",
   enabled,
   archived
 FROM resource
@@ -99,7 +99,7 @@ WHERE id = :id;
 INSERT INTO resource
 (resid, organization, ownerUserId, modifieruserid, endt)
 VALUES (:resid, :organization, :owneruserid, :modifieruserid,
- /*~ (if (:endt params) */ :endt /*~*/ NULL /*~ ) ~*/
+ /*~ (if (:end params) */ :end /*~*/ NULL /*~ ) ~*/
 );
 
 -- :name set-resource-state! :insert
@@ -133,7 +133,7 @@ SELECT
   organization,
   title,
   start,
-  endt,
+  endt as "end",
   enabled,
   archived
 FROM application_form;
@@ -187,7 +187,7 @@ FROM application_form form
 WHERE form.id = :id;
 
 -- :name get-all-form-items :? :*
-SELECT id, type, value, visibility, start, endt, owneruserid, modifieruserid
+SELECT id, type, value, visibility, start, endt as "end", owneruserid, modifieruserid
 FROM application_form_item;
 
 -- :name get-form-item-localizations :? :*
@@ -244,7 +244,7 @@ VALUES
  :user,
  :user,
  'public',
- /*~ (if (:endt params) */ :endt /*~*/ NULL /*~ ) ~*/
+ /*~ (if (:end params) */ :end /*~*/ NULL /*~ ) ~*/
 );
 
 -- :name set-form-state! :!
@@ -425,7 +425,7 @@ INSERT INTO license
 VALUES
 (:owneruserid, :modifieruserid, :title, :type::license_type, :textcontent,
 /*~ (if (:attachmentId params) */ :attachmentId /*~*/ NULL /*~ ) ~*/,
-/*~ (if (:endt params) */ :endt /*~*/ NULL /*~ ) ~*/
+/*~ (if (:end params) */ :end /*~*/ NULL /*~ ) ~*/
 );
 
 -- :name set-license-state! :!
@@ -463,7 +463,7 @@ VALUES
  :modifieruserid,
  :title,
  :fnlround,
- /*~ (if (:endt params) */ :endt /*~*/ NULL /*~ ) ~*/,
+ /*~ (if (:end params) */ :end /*~*/ NULL /*~ ) ~*/,
  /*~ (if (:workflow params) */ :workflow::jsonb /*~*/ NULL /*~ ) ~*/
 );
 
@@ -567,7 +567,7 @@ AND ci.id = :catid
 
 -- :name get-workflows :? :*
 SELECT
-  wf.id, wf.organization, wf.owneruserid, wf.modifieruserid, wf.title, wf.fnlround, wf.visibility, wf.start, wf.endt,
+  wf.id, wf.organization, wf.owneruserid, wf.modifieruserid, wf.title, wf.fnlround, wf.visibility, wf.start, wf.endt as "end",
   wf.workflowBody::TEXT as workflow, wf.enabled, wf.archived
 FROM workflow wf;
 
@@ -591,12 +591,12 @@ WHERE textvalues.catAppId = :application
 -- - Gets application licenses by workflow and catalogue item ids
 -- - :wfid workflow id for workflow licenses
 -- - :items vector of catalogue item ids for resource licenses
-SELECT lic.id, lic.title, lic.type, lic.textcontent, wl.start, wl.endt, lic.enabled, lic.archived
+SELECT lic.id, lic.title, lic.type, lic.textcontent, wl.start, wl.endt as "end", lic.enabled, lic.archived
 FROM license lic
 INNER JOIN workflow_licenses wl ON lic.id = wl.licid
 WHERE wl.wfid = :wfid
 UNION
-SELECT lic.id, lic.title, lic.type, lic.textcontent, rl.start, rl.endt, lic.enabled, lic.archived
+SELECT lic.id, lic.title, lic.type, lic.textcontent, rl.start, rl.endt as "end", lic.enabled, lic.archived
 FROM license lic
 INNER JOIN resource_licenses rl ON lic.id = rl.licid
 INNER JOIN catalogue_item item ON (item.resid = rl.resid)
@@ -604,17 +604,17 @@ WHERE item.id IN (:v*:items)
 ORDER BY id;
 
 -- :name get-resource-licenses :? :*
-SELECT lic.id, lic.title, lic.type, lic.textcontent, rl.start, rl.endt, lic.enabled, lic.archived
+SELECT lic.id, lic.title, lic.type, lic.textcontent, rl.start, rl.endt as "end", lic.enabled, lic.archived
 FROM license lic
 INNER JOIN resource_licenses rl ON lic.id = rl.licid
 WHERE rl.resid = :id;
 
 -- :name get-all-licenses :? :*
-SELECT lic.id, lic.title, lic.type, lic.textcontent, lic.start, lic.endt, lic.enabled, lic.archived, lic.attachmentid
+SELECT lic.id, lic.title, lic.type, lic.textcontent, lic.start, lic.endt as "end", lic.enabled, lic.archived, lic.attachmentid
 FROM license lic;
 
 -- :name get-license :? :1
-SELECT lic.id, lic.title, lic.type, lic.textcontent, lic.start, lic.endt, lic.enabled, lic.archived, lic.attachmentid
+SELECT lic.id, lic.title, lic.type, lic.textcontent, lic.start, lic.endt as "end", lic.enabled, lic.archived, lic.attachmentid
 FROM license lic
 WHERE lic.id = :id;
 
diff --git a/src/clj/rems/api/forms.clj b/src/clj/rems/api/forms.clj
index 7458415ef..2a9f8d46d 100644
--- a/src/clj/rems/api/forms.clj
+++ b/src/clj/rems/api/forms.clj
@@ -38,12 +38,12 @@
   [Form])
 
 (defn- format-form
-  [{:keys [id organization title start endt expired enabled archived]}]
+  [{:keys [id organization title start end expired enabled archived]}]
   {:id id
    :organization organization
    :title title
    :start start
-   :end endt
+   :end end
    :expired expired
    :enabled enabled
    :archived archived})
diff --git a/src/clj/rems/api/resources.clj b/src/clj/rems/api/resources.clj
index ad2cb3116..3aa384bf2 100644
--- a/src/clj/rems/api/resources.clj
+++ b/src/clj/rems/api/resources.clj
@@ -37,14 +37,14 @@
    (s/optional-key :errors) [s/Any]})
 
 (defn- format-resource
-  [{:keys [id owneruserid modifieruserid organization resid start endt expired enabled archived]}]
+  [{:keys [id owneruserid modifieruserid organization resid start end expired enabled archived]}]
   {:id id
    :owneruserid owneruserid
    :modifieruserid modifieruserid
    :organization organization
    :resid resid
    :start start
-   :end endt
+   :end end
    :expired expired
    :enabled enabled
    :archived archived})
diff --git a/src/clj/rems/api/workflows.clj b/src/clj/rems/api/workflows.clj
index 1fd7573ec..2e5dba8db 100644
--- a/src/clj/rems/api/workflows.clj
+++ b/src/clj/rems/api/workflows.clj
@@ -40,7 +40,7 @@
   (-> license
       (select-keys [:type :textcontent :localizations])
       (assoc :start (parse-db-time (:start license)))
-      (assoc :end (parse-db-time (:endt license)))))
+      (assoc :end (parse-db-time (:end license)))))
 
 (s/defschema Workflow
   {:id s/Num
@@ -62,7 +62,7 @@
   [Workflow])
 
 (defn- format-workflow
-  [{:keys [id organization owneruserid modifieruserid title fnlround workflow start endt expired enabled archived licenses]}]
+  [{:keys [id organization owneruserid modifieruserid title fnlround workflow start end expired enabled archived licenses]}]
   {:id id
    :organization organization
    :owneruserid owneruserid
@@ -71,7 +71,7 @@
    :final-round fnlround
    :workflow workflow
    :start start
-   :end endt
+   :end end
    :expired expired
    :enabled enabled
    :archived archived
diff --git a/src/clj/rems/db/core.clj b/src/clj/rems/db/core.clj
index e4d354dc5..39cf5e210 100644
--- a/src/clj/rems/db/core.clj
+++ b/src/clj/rems/db/core.clj
@@ -38,11 +38,10 @@
             (time/before? now end)))))
 
 (defn assoc-expired
-  "Calculates and assocs :expired attribute based on current time and :start and :endt attributes.
+  "Calculates and assocs :expired attribute based on current time and :start and :end attributes.
 
    Current time can be passed in optionally."
   ([x]
    (assoc-expired (time/now) x))
   ([now x]
-   ;; TODO: rename endt to end in all places
-   (assoc x :expired (not (now-active? now (:start x) (or (:endt x) (:end x)))))))
+   (assoc x :expired (not (now-active? now (:start x) (:end x))))))
diff --git a/src/clj/rems/db/licenses.clj b/src/clj/rems/db/licenses.clj
index 0b9e43bf5..13d917f88 100644
--- a/src/clj/rems/db/licenses.clj
+++ b/src/clj/rems/db/licenses.clj
@@ -10,7 +10,7 @@
   {:id (:id license)
    :licensetype (:type license)
    :start (:start license)
-   :end (:endt license)
+   :end (:end license)
    :enabled (:enabled license)
    :archived (:archived license)
    :expired (:expired license)


Test patch:
diff --git a/src/clj/rems/db/test_data.clj b/src/clj/rems/db/test_data.clj
index 45129ab03..91b5ffc89 100644
--- a/src/clj/rems/db/test_data.clj
+++ b/src/clj/rems/db/test_data.clj
@@ -84,12 +84,12 @@
 (defn- create-expired-form! []
   (let [yesterday (time/minus (time/now) (time/days 1))]
     ;; only used from create-test-data!
-    (db/create-form! {:organization "nbn" :title "Expired form, should not be seen by applicants" :user (+fake-users+ :owner) :endt yesterday})))
+    (db/create-form! {:organization "nbn" :title "Expired form, should not be seen by applicants" :user (+fake-users+ :owner) :end yesterday})))
 
 (defn- create-expired-license! []
   (let [owner (+fake-users+ :owner) ; only used from create-test-data!
         yesterday (time/minus (time/now) (time/days 1))]
-    (db/create-license! {:modifieruserid owner :owneruserid owner :title "expired license" :type "link" :textcontent "http://expired" :endt yesterday})))
+    (db/create-license! {:modifieruserid owner :owneruserid owner :title "expired license" :type "link" :textcontent "http://expired" :end yesterday})))
 
 (defn- create-basic-form!
   "Creates a bilingual form with all supported field types. Returns id of the form meta."
@@ -377,7 +377,7 @@
         with-review (:id (db/create-workflow! {:organization "nbn" :owneruserid owner :modifieruserid owner :title "with review" :fnlround 1}))
         two-round (:id (db/create-workflow! {:organization "nbn" :owneruserid owner :modifieruserid owner :title "two rounds" :fnlround 1}))
         different (:id (db/create-workflow! {:organization "nbn" :owneruserid owner :modifieruserid owner :title "two rounds, different approvers" :fnlround 1}))
-        expired (:id (db/create-workflow! {:organization "nbn" :owneruserid owner :modifieruserid owner :title "workflow has already expired, should not be seen" :fnlround 0 :endt (time/minus (time/now) (time/years 1))}))
+        expired (:id (db/create-workflow! {:organization "nbn" :owneruserid owner :modifieruserid owner :title "workflow has already expired, should not be seen" :fnlround 0 :end (time/minus (time/now) (time/years 1))}))
         dynamic (:id (workflow/create-workflow! {:user-id owner
                                                  :organization "nbn"
                                                  :title "dynamic workflow"
@@ -632,7 +632,7 @@
     (create-users-and-roles!)
     (let [res1 (:id (db/create-resource! {:resid "urn:nbn:fi:lb-201403262" :organization "nbn" :owneruserid (+fake-users+ :owner) :modifieruserid (+fake-users+ :owner)}))
           res2 (:id (db/create-resource! {:resid "Extra Data" :organization "nbn" :owneruserid (+fake-users+ :owner) :modifieruserid (+fake-users+ :owner)}))
-          _ (:id (db/create-resource! {:resid "Expired Resource, should not be seen" :organization "nbn" :owneruserid (+fake-users+ :owner) :modifieruserid (+fake-users+ :owner) :endt (time/minus (time/now) (time/years 1))}))
+          _ (:id (db/create-resource! {:resid "Expired Resource, should not be seen" :organization "nbn" :owneruserid (+fake-users+ :owner) :modifieruserid (+fake-users+ :owner) :end (time/minus (time/now) (time/years 1))}))
           form (create-basic-form! +fake-users+)
           _ (create-expired-form!)
           workflows (create-workflows! +fake-users+)
@@ -676,7 +676,7 @@
       (let [dynamic-expired (create-catalogue-item! res1 (:dynamic workflows) form
                                                     {"en" "Dynamic workflow (expired)"
                                                      "fi" "Dynaaminen työvuo (vanhentunut)"})]
-        (db/set-catalogue-item-state! {:id dynamic-expired :endt (time/now)})))
+        (db/set-catalogue-item-state! {:id dynamic-expired :end (time/now)})))
     (finally
       (DateTimeUtils/setCurrentMillisSystem))))
 
diff --git a/test/clj/rems/db/test_core.clj b/test/clj/rems/db/test_core.clj
index ab9288413..3db2b2c39 100644
--- a/test/clj/rems/db/test_core.clj
+++ b/test/clj/rems/db/test_core.clj
@@ -23,15 +23,15 @@
 
 (deftest test-assoc-expired
   (is (= {:expired false} (assoc-expired nil)))
-  (is (= {:expired false :start nil :endt nil :foobar 42} (assoc-expired {:start nil :endt nil :foobar 42})))
+  (is (= {:expired false :start nil :end nil :foobar 42} (assoc-expired {:start nil :end nil :foobar 42})))
   (let [today (time/now)
         yesterday (time/minus today (time/days 1))
         tomorrow (time/plus today (time/days 1))]
-    (is (= {:expired true :start tomorrow :endt nil} (assoc-expired {:start tomorrow :endt nil})))
-    (is (= {:expired true :start nil :endt yesterday} (assoc-expired {:start nil :endt yesterday})))
-    (is (= {:expired false :start yesterday :endt tomorrow} (assoc-expired {:start yesterday :endt tomorrow})))
-    (is (= {:expired false :start yesterday :endt nil} (assoc-expired {:start yesterday :endt nil})))
-    (is (= {:expired false :start nil :endt tomorrow} (assoc-expired {:start nil :endt tomorrow})))))
+    (is (= {:expired true :start tomorrow :end nil} (assoc-expired {:start tomorrow :end nil})))
+    (is (= {:expired true :start nil :end yesterday} (assoc-expired {:start nil :end yesterday})))
+    (is (= {:expired false :start yesterday :end tomorrow} (assoc-expired {:start yesterday :end tomorrow})))
+    (is (= {:expired false :start yesterday :end nil} (assoc-expired {:start yesterday :end nil})))
+    (is (= {:expired false :start nil :end tomorrow} (assoc-expired {:start nil :end tomorrow})))))
 
 (defn- take-ids [items]
   (map :id items))
@@ -41,12 +41,12 @@
         yesterday (time/minus today (time/days 1))
         all-items [{:id :normal
                     :start nil
-                    :endt nil}
+                    :end nil}
                    {:id :expired
                     :start nil
-                    :endt yesterday}]
+                    :end yesterday}]
 
-        ; the following idiom can be used when reading database entries with 'endt' field
+        ; the following idiom can be used when reading database entries with 'end' field
         get-items (fn [filters]
                     (->> all-items
                          (map assoc-expired)
diff --git a/test/clj/rems/db/test_licenses.clj b/test/clj/rems/db/test_licenses.clj
index ebb6f1bdd..08003b7c7 100644
--- a/test/clj/rems/db/test_licenses.clj
+++ b/test/clj/rems/db/test_licenses.clj
@@ -29,10 +29,10 @@
         expired-license-end (time/plus yesterday (time/hours 1))
         just-created-license-start (time/minus today (time/hours 1))]
     (with-redefs [db/get-license-localizations (constantly [])
-                  db/get-licenses (fn [params] [{:id :always :start nil :endt nil}
-                                                {:id :always :start nil :endt nil}
-                                                {:id :expired :start nil :endt expired-license-end}
-                                                {:id :just-created :start just-created-license-start :endt nil}])]
+                  db/get-licenses (fn [params] [{:id :always :start nil :end nil}
+                                                {:id :always :start nil :end nil}
+                                                {:id :expired :start nil :end expired-license-end}
+                                                {:id :just-created :start just-created-license-start :end nil}])]
       (is (= 1 (count (:always (group-by :id (get-active-licenses today nil))))) "should be deduplicated")
       (is (= #{:always :just-created} (set (map :id (get-active-licenses today nil)))))
       (is (= #{:always :expired} (set (map :id (get-active-licenses yesterday nil))))))))