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

feat: allow creating items with mixed organizations in admin ui #2038

Merged
merged 4 commits into from Mar 6, 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
1 change: 0 additions & 1 deletion resources/translations/en.edn
Expand Up @@ -95,7 +95,6 @@
:resource "Resource"
:resources "Resources"
:save "Save"
:select-organization "Please select organization."
:start "Start"
:title "Title"
:type "Type"
Expand Down
1 change: 0 additions & 1 deletion resources/translations/fi.edn
Expand Up @@ -95,7 +95,6 @@
:resource "Resurssi"
:resources "Resurssit"
:save "Tallenna"
:select-organization "Valitse organisaatio."
:start "Alku"
:title "Nimi"
:type "Tyyppi"
Expand Down
6 changes: 4 additions & 2 deletions src/clj/rems/api/blacklist.clj
Expand Up @@ -33,7 +33,7 @@

(GET "/" []
:summary "Get blacklist entries"
:roles #{:handler :owner :reporter}
:roles #{:handler :owner :reporter :organization-owner}
:query-params [{user :- schema/UserId nil}
{resource :- s/Str nil}]
:return [BlacklistEntryWithDetails]
Expand All @@ -44,10 +44,12 @@

(GET "/users" []
:summary "Existing REMS users available for adding to the blacklist"
:roles #{:owner :handler}
:roles #{:owner :handler :organization-owner}
:return [schema/UserWithAttributes]
(ok (users/get-users)))

;; TODO write access to blacklist for organization-owner

(POST "/add" []
:summary "Add a blacklist entry"
:roles #{:owner :handler}
Expand Down
84 changes: 31 additions & 53 deletions src/cljs/rems/administration/create_catalogue_item.cljs
Expand Up @@ -78,17 +78,13 @@
(not (str/blank? (:title localization))))

(defn- valid-request? [form request languages]
(let [selected-organization (:organization request)]
(and (= (get-in form [:workflow :organization])
(get-in form [:resource :organization])
(get-in form [:form :form/organization])
selected-organization)
(number? (:wfid request))
(number? (:resid request))
(number? (:form request))
(= (set languages)
(set (keys (:localizations request))))
(every? valid-localization? (vals (:localizations request))))))
(and (string? (:organization request))
(number? (:wfid request))
(number? (:resid request))
(number? (:form request))
(= (set languages)
(set (keys (:localizations request))))
(every? valid-localization? (vals (:localizations request)))))

(defn- empty-string-to-nil [str]
(when-not (str/blank? str)
Expand Down Expand Up @@ -258,86 +254,68 @@
" (" (str/upper-case (name language)) ")")}])

(defn- catalogue-item-workflow-field []
(let [organization @(rf/subscribe [::selected-organization])
workflows @(rf/subscribe [::workflows])
compatible-workflows (filter #(= organization (% :organization)) workflows)
(let [workflows @(rf/subscribe [::workflows])
editing? @(rf/subscribe [::editing?])
selected-workflow @(rf/subscribe [::selected-workflow])
item-selected? #(= (:id %) (:id selected-workflow))]
[:div.form-group
[:label {:for workflow-dropdown-id} (text :t.create-catalogue-item/workflow-selection)]
(cond
(nil? organization)
[fields/readonly-field {:id workflow-dropdown-id
:value (text :t.administration/select-organization)}]

editing?
(let [workflow (item-by-id compatible-workflows :id (:id selected-workflow))]
(if editing?
(let [workflow (item-by-id workflows :id (:id selected-workflow))]
[fields/readonly-field {:id workflow-dropdown-id
:value (:title workflow)}])

:else
[dropdown/dropdown
{:id workflow-dropdown-id
:items compatible-workflows
:items workflows
:item-key :id
:item-label :title
:item-label #(str (:title %)
" (org: "
(:organization %)
")")
:item-selected? item-selected?
:on-change #(rf/dispatch [::set-selected-workflow %])}])]))

(defn- catalogue-item-resource-field []
(let [organization @(rf/subscribe [::selected-organization])
resources @(rf/subscribe [::resources])
compatible-resources (filter #(= organization (% :organization)) resources)
(let [resources @(rf/subscribe [::resources])
editing? @(rf/subscribe [::editing?])
selected-resource @(rf/subscribe [::selected-resource])
item-selected? #(= (:id %) (:id selected-resource))]
[:div.form-group
[:label {:for resource-dropdown-id} (text :t.create-catalogue-item/resource-selection)]
(cond
(nil? organization)
[fields/readonly-field {:id resource-dropdown-id
:value (text :t.administration/select-organization)}]

editing?
(let [resource (item-by-id compatible-resources :id (:id selected-resource))]
(if editing?
(let [resource (item-by-id resources :id (:id selected-resource))]
[fields/readonly-field {:id resource-dropdown-id
:value (:resid resource)}])

:else
[dropdown/dropdown
{:id resource-dropdown-id
:items compatible-resources
:items resources
:item-key :id
:item-label :resid
:item-label #(str (:resid %)
" (org: "
(:organization %)
")")
:item-selected? item-selected?
:on-change #(rf/dispatch [::set-selected-resource %])}])]))

(defn- catalogue-item-form-field []
(let [organization @(rf/subscribe [::selected-organization])
forms @(rf/subscribe [::forms])
compatible-forms (filter #(= organization (% :form/organization)) forms)
(let [forms @(rf/subscribe [::forms])
editing? @(rf/subscribe [::editing?])
selected-form @(rf/subscribe [::selected-form])
item-selected? #(= (:form/id %) (:form/id selected-form))]
[:div.form-group
[:label {:for form-dropdown-id} (text :t.create-catalogue-item/form-selection)]
(cond
(nil? organization)
[fields/readonly-field {:id form-dropdown-id
:value (text :t.administration/select-organization)}]

editing?
(let [form (item-by-id compatible-forms :form/id (:form/id selected-form))]
(if editing?
(let [form (item-by-id forms :form/id (:form/id selected-form))]
[fields/readonly-field {:id form-dropdown-id
:value (:form/title form)}])

:else
[dropdown/dropdown
{:id form-dropdown-id
:items compatible-forms
:items forms
:item-key :form/id
:item-label :form/title
:item-label #(str (:form/title %)
" (org: "
(:form/organization %)
")")
:item-selected? item-selected?
:on-change #(rf/dispatch [::set-selected-form %])}])]))

Expand Down
32 changes: 14 additions & 18 deletions src/cljs/rems/administration/create_resource.cljs
Expand Up @@ -53,8 +53,7 @@
;; form submit

(defn- valid-request? [form request]
(and (every? #(= (:organization request) %) (map :organization (:licenses form)))
(not (str/blank? (:organization request)))
(and (not (str/blank? (:organization request)))
(not (str/blank? (:resid request)))))

(defn build-request [form]
Expand Down Expand Up @@ -119,33 +118,30 @@
{:id organization-dropdown-id
:items organizations
:item-selected? item-selected?
:on-change #(do (rf/dispatch [::set-selected-organization %])
(rf/dispatch [::set-licenses []]))}])]))
:on-change #(rf/dispatch [::set-selected-organization %])}])]))

(defn- resource-id-field []
[text-field context {:keys [:resid]
:label (text :t.create-resource/resid)
:placeholder (text :t.create-resource/resid-placeholder)}])

(defn- resource-licenses-field []
(let [organization @(rf/subscribe [::selected-organization])
licenses @(rf/subscribe [::licenses])
compatible-licenses (filter #(= organization (% :organization)) licenses)
(let [licenses @(rf/subscribe [::licenses])
selected-licenses @(rf/subscribe [::selected-licenses])
language @(rf/subscribe [:language])]
[:div.form-group
[:label {:for licenses-dropdown-id} (text :t.create-resource/licenses-selection)]
(if (nil? organization)
[fields/readonly-field {:id licenses-dropdown-id
:value (text :t.administration/select-organization)}]
[dropdown/dropdown
{:id licenses-dropdown-id
:items compatible-licenses
:item-key :id
:item-label #(get-localized-title % language)
:item-selected? #(contains? (set selected-licenses) %)
:multi? true
:on-change #(rf/dispatch [::set-licenses %])}])]))
[dropdown/dropdown
{:id licenses-dropdown-id
:items licenses
:item-key :id
:item-label #(str (get-localized-title % language)
" (org: "
(:organization %)
")")
:item-selected? #(contains? (set selected-licenses) %)
:multi? true
:on-change #(rf/dispatch [::set-licenses %])}]]))

(defn- save-resource-button [form]
(let [request (build-request form)]
Expand Down
29 changes: 5 additions & 24 deletions test/cljs/rems/administration/test_create_catalogue_item.cljs
Expand Up @@ -32,6 +32,11 @@
languages)))
(is (nil? (build-request (assoc-in form [:title :fi] "")
languages))))

(testing "missing organization"
(is (nil? (build-request (dissoc form :organization)
languages))))

(testing "missing workflow"
(is (nil? (build-request (assoc form :workflow nil)
languages))))
Expand All @@ -40,28 +45,4 @@
languages))))
(testing "missing form"
(is (nil? (build-request (assoc form :form nil)
languages))))

(testing "incorrect workflow organization"
(is (nil? (build-request (assoc-in form [:workflow :organization] "organization2")
languages)))
(is (nil? (build-request (assoc-in form [:workflow :organization] "")
languages)))
(is (nil? (build-request (assoc-in form [:workflow :organization] nil)
languages))))

(testing "incorrect resource organization"
(is (nil? (build-request (assoc-in form [:resource :organization] "organization2")
languages)))
(is (nil? (build-request (assoc-in form [:resource :organization] "")
languages)))
(is (nil? (build-request (assoc-in form [:resource :organization] nil)
languages))))

(testing "incorrect form organization"
(is (nil? (build-request (assoc-in form [:form :form/organization] "organization2")
languages)))
(is (nil? (build-request (assoc-in form [:form :form/organization] "")
languages)))
(is (nil? (build-request (assoc-in form [:form :form/organization] nil)
languages))))))
12 changes: 1 addition & 11 deletions test/cljs/rems/administration/test_create_resource.cljs
Expand Up @@ -24,14 +24,4 @@
(is (nil? (build-request (assoc form :organization "")))))

(testing "missing resource id"
(is (nil? (build-request (assoc form :resid "")))))

(testing "incorrect organization"
(is (nil? (build-request (assoc form :licenses [{:id 123
:organization "organization2"}])))))

(testing "incorrect organization in one of several licenses"
(is (nil? (build-request (assoc form :licenses [{:id 123
:organization "organization1"}
{:id 124
:organization "organization2"}])))))))
(is (nil? (build-request (assoc form :resid "")))))))