Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparation #2033

Merged
merged 9 commits into from Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/clj/rems/application/commands.clj
Expand Up @@ -191,7 +191,7 @@
(not get-catalogue-item) {:errors [{:type :missing-injection :injection :get-catalogue-item}]}
(not (get-catalogue-item catalogue-item-id)) {:errors [{:type :invalid-catalogue-item :catalogue-item-id catalogue-item-id}]}))

(defn- disabled-catalogue-items-error [application {:keys [get-catalogue-item]}]
(defn- disabled-catalogue-items-error [application]
(let [errors (for [item (:application/resources application)
:when (or (not (getx item :catalogue-item/enabled))
(getx item :catalogue-item/archived)
Expand Down Expand Up @@ -334,7 +334,7 @@
(defmethod command-handler :application.command/submit
[cmd application injections]
(or (merge-with concat
(disabled-catalogue-items-error application injections)
(disabled-catalogue-items-error application)
(licenses-not-accepted-error application (:actor cmd))
(validation-error application injections))
(ok {:event/type :application.event/submitted})))
Expand All @@ -360,7 +360,7 @@
:application/comment (:comment cmd)}))

(defmethod command-handler :application.command/revoke
[cmd application _injections]
[cmd _application _injections]
(ok {:event/type :application.event/revoked
:application/comment (:comment cmd)}))

Expand Down Expand Up @@ -421,7 +421,7 @@
:application/public (:public cmd)}))

(defmethod command-handler :application.command/add-licenses
[cmd _application injections]
[cmd _application _injections]
(or (must-not-be-empty cmd :licenses)
(ok {:event/type :application.event/licenses-added
:application/licenses (mapv (fn [id] {:license/id id}) (:licenses cmd))
Expand Down Expand Up @@ -512,7 +512,7 @@
:application/copied-to (select-keys created-event [:application/id :application/external-id])}])))))

(defmethod command-handler :application.command/assign-external-id
[cmd application _injections]
[cmd _application _injections]
(ok {:event/type :application.event/external-id-assigned
:application/external-id (:external-id cmd)}))

Expand Down
7 changes: 3 additions & 4 deletions src/clj/rems/application/model.clj
@@ -1,9 +1,9 @@
(ns rems.application.model
(:require [clojure.test :refer [deftest is testing]]
[medley.core :refer [map-vals update-existing]]
[rems.common.application-util :as application-util]
[medley.core :refer [find-first map-vals update-existing]]
[rems.application.events :as events]
[rems.application.master-workflow :as master-workflow]
[rems.common.application-util :as application-util]
[rems.common.form :refer [field-visible?]]
[rems.permissions :as permissions]
[rems.util :refer [getx conj-vec]]))
Expand Down Expand Up @@ -373,8 +373,7 @@
(defn- set-application-description [application]
(let [fields (get-in application [:application/form :form/fields])
description (->> fields
(filter #(= :description (:field/type %)))
first
(find-first #(= :description (:field/type %)))
:field/value)]
(assoc application :application/description (str description))))

Expand Down
4 changes: 2 additions & 2 deletions src/clj/rems/db/applications.clj
Expand Up @@ -116,7 +116,7 @@
(update applications app-id model/application-view event)
applications))

(defn- exclude-unnecessary-keys-from-overview [application]
(defn- ->ApplicationOverview [application]
(dissoc application
:application/events
:application/form
Expand Down Expand Up @@ -228,7 +228,7 @@
(defn get-all-applications [user-id]
(-> (refresh-all-applications-cache!)
(get-in [::apps-by-user user-id])
(->> (map exclude-unnecessary-keys-from-overview))))
(->> (map ->ApplicationOverview))))

(defn get-all-application-roles [user-id]
(-> (refresh-all-applications-cache!)
Expand Down
40 changes: 33 additions & 7 deletions src/cljc/rems/common/util.cljc
@@ -1,5 +1,6 @@
(ns rems.common.util
(:require [clojure.string :as str]
(:require [medley.core :refer [map-vals]]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]))

;; TODO remove separate clj and cljs implementations of getx and getx-in
Expand All @@ -26,6 +27,36 @@
[m ks & [default-value]]
(vec (reduce #(conj %1 (get m %2 default-value)) [] ks)))

(defn build-index
"Index the `coll` with given keys `ks` and map values with given `f`.

Results is nested map, `(count ks)` levels deep, e.g.
(build-index [:a :b] :c [{:a 1 :b \"x\" :c :a} {:a 1 :b \"y\" :c :b}])
==> {1 {\"x\" :a
\"y\" :b}}

In case of non-unique keys, `build-index` picks the first value, e.g.

(build-index [:a] identity [{:a 1 :b \"x\"} {:a 1 :b \"y\"}])
==> {1 {:a 1 :b \"x\"}}"
[ks f coll]
(if-let [[k & ks] (seq ks)]
(->> coll
Macroz marked this conversation as resolved.
Show resolved Hide resolved
(group-by k)
(map-vals #(build-index ks f %)))
(f (first coll))))

(deftest test-build-index
(is (= {:a 1} (build-index [] identity [{:a 1} {:b 2}])))
(is (= {1 {"x" :a "y" :b}}
(build-index [:a :b] :c [{:a 1 :b "x" :c :a} {:a 1 :b "y" :c :b}])))
(is (= {1 {:a 1 :b "x" :c :a}}
(build-index [:a] identity [{:a 1 :b "x" :c :a} {:a 1 :b "y" :c :b}]))))

(comment
(if-let [[k & ks] []]
(list k ks)))

(defn index-by
"Index the collection coll with given keys `ks`.
Result is a nested map, `(count ks)` levels deep, e.g.
Expand All @@ -39,12 +70,7 @@
(index-by [:a] [{:a 1 :b \"x\"} {:a 1 :b \"y\"}])
==> {1 {:a 1 :b \"x\"}}"
[ks coll]
(if (empty? ks)
(first coll)
(->> coll
(group-by (first ks))
(map (fn [[k v]] [k (index-by (rest ks) v)]))
(into {}))))
(build-index ks identity coll))

(deftest test-index-by
(is (= {1 {"x" {:a 1 :b "x"}
Expand Down
12 changes: 5 additions & 7 deletions src/cljs/rems/application.cljs
@@ -1,11 +1,9 @@
(ns rems.application
(:require [clojure.set :refer [union]]
[clojure.string :as str]
(:require [clojure.string :as str]
[goog.string]
[medley.core :refer [map-vals]]
[re-frame.core :as rf]
[rems.actions.accept-licenses :refer [accept-licenses-action-button]]
[rems.actions.action :refer [action-button action-form-view action-comment action-collapse-id button-wrapper]]
[rems.actions.action :refer [button-wrapper]]
[rems.actions.add-licenses :refer [add-licenses-action-button add-licenses-form]]
[rems.actions.add-member :refer [add-member-action-button add-member-form]]
[rems.actions.approve-reject :refer [approve-reject-action-button approve-reject-form]]
Expand All @@ -23,15 +21,15 @@
[rems.actions.revoke :refer [revoke-action-button revoke-form]]
[rems.application-list :as application-list]
[rems.common.application-util :refer [accepted-licenses? form-fields-editable? get-member-name]]
[rems.atoms :refer [external-link file-download info-field readonly-checkbox textarea document-title success-symbol empty-symbol]]
[rems.atoms :refer [external-link file-download info-field readonly-checkbox document-title success-symbol empty-symbol]]
[rems.common.catalogue-util :refer [urn-catalogue-item-link]]
[rems.collapsible :as collapsible]
[rems.common.form :refer [field-visible?]]
[rems.common.util :refer [index-by parse-int]]
[rems.common.util :refer [build-index index-by parse-int]]
[rems.fetcher :as fetcher]
[rems.fields :as fields]
[rems.flash-message :as flash-message]
[rems.guide-utils :refer [lipsum lipsum-short lipsum-paragraphs]]
[rems.guide-utils :refer [lipsum lipsum-paragraphs]]
[rems.phase :refer [phases]]
[rems.search :as search]
[rems.spinner :as spinner]
Expand Down
1 change: 0 additions & 1 deletion test/clj/rems/api/test_catalogue_items.clj
Expand Up @@ -129,7 +129,6 @@
(authenticate api-key user)
handler
read-ok-body)]
(prn data)
(is (= id (:id data)))
(is (= {:title "En title"
:infourl nil}
Expand Down
1 change: 0 additions & 1 deletion test/clj/rems/application/test_commands.clj
Expand Up @@ -3,7 +3,6 @@
[rems.application.commands :as commands]
[rems.application.events :as events]
[rems.application.model :as model]
[rems.common.util :refer [distinct-by]]
[rems.form-validation :as form-validation]
[rems.permissions :as permissions]
[rems.util :refer [assert-ex getx]])
Expand Down