Skip to content

Commit

Permalink
Merge pull request #1111 from CSCfi/add-licenses
Browse files Browse the repository at this point in the history
Add licenses
  • Loading branch information
Macroz committed Apr 15, 2019
2 parents 7728d13 + 97f0794 commit 9683ef8
Show file tree
Hide file tree
Showing 14 changed files with 693 additions and 383 deletions.
5 changes: 4 additions & 1 deletion resources/translations/en.edn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:accept-invitation-success [:div [:p "Joining the application was successful."] [:p "Next you will be taken to the application."]]
:accept-invitation-already-member [:div [:p "You are already a member."] [:p "Next you will be taken to the application."]]
:accept-licenses "Accept terms of use"
:add-licenses "Add terms of use"
:add-member "Add member"
:applicant "Applicant"
:application "Application"
Expand All @@ -68,6 +69,7 @@
:handled-approvals "Handled applications"
:invite-member "Invite member"
:last-activity "Last activity"
:licenses-selection "Require following additional terms of use"
:load-application-states "Load application states"
:member "Member"
:member-name "Name"
Expand All @@ -79,7 +81,7 @@
:remove-member "Remove member"
:request-comment "Request comment"
:request-decision "Request decision"
:request-selection "Send request to user(s):"
:request-selection "Send request to users"
:resource "Resource"
:return "Return to applicant"
:show-publications "Show publications"
Expand Down Expand Up @@ -144,6 +146,7 @@
:decided "Decided"
:decision-requested "Decision requested"
:draft-saved "Saved"
:licenses-added "Added tems of use"
:licenses-accepted "Accepted terms of use"
:member-added "Member added"
:member-invited "Member invited"
Expand Down
5 changes: 4 additions & 1 deletion resources/translations/fi.edn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:accept-invitation-success [:div [:p "Liittyminen hakemukseen onnistui."] [:p "Seuraavaksi pääset katsomaan hakemuksen tietoja."]]
:accept-invitation-already-member [:div. [:p "Olet jo jäsen."] [:p "Seuraavaksi pääset katsomaan hakemuksen tietoja."]]
:accept-licenses "Hyväksy käyttöehdot"
:add-licenses "Lisää käyttöehto"
:add-member "Lisää jäsen"
:applicant "Hakija"
:application "Hakemus"
Expand All @@ -68,6 +69,7 @@
:handled-approvals "Käsitellyt hakemukset"
:invite-member "Kutsu jäsen"
:last-activity "Muokattu"
:licenses-selection "Lisää seuraavat käyttöehdot"
:load-application-states "Lataa hakemusten tilat"
:member "Jäsen"
:member-name "Nimi"
Expand All @@ -79,7 +81,7 @@
:remove-member "Poista jäsen"
:request-comment "Pyydä kommenttia"
:request-decision "Pyydä päätöstä"
:request-selection "Lähetä pyyntö henkilölle/henkilöille:"
:request-selection "Lähetä pyyntö seuraaville henkilöille"
:resource "Resurssi"
:return "Palauta hakijalle"
:show-publications "Näytä julkaisut"
Expand Down Expand Up @@ -144,6 +146,7 @@
:decided "Päätetty"
:decision-requested "Pyydetty päätöstä"
:draft-saved "Tallennettu"
:licenses-added "Käyttöehtoja lisätty"
:licenses-accepted "Käyttöehdot hyväksytty"
:member-added "Lisätty jäsen"
:member-invited "Kutsuttu jäsen"
Expand Down
1 change: 1 addition & 0 deletions src/clj/rems/api/applications.clj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@

(command-endpoint :application.command/accept-invitation commands/AcceptInvitationCommand)
(command-endpoint :application.command/accept-licenses commands/AcceptLicensesCommand)
(command-endpoint :application.command/add-licenses commands/AddLicensesCommand)
(command-endpoint :application.command/add-member commands/AddMemberCommand)
(command-endpoint :application.command/invite-member commands/InviteMemberCommand)
(command-endpoint :application.command/approve commands/ApproveCommand)
Expand Down
2 changes: 1 addition & 1 deletion src/clj/rems/api/licenses.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

(GET "/" []
:summary "Get licenses"
:roles #{:owner}
:roles #{:handler :owner}
:query-params [{disabled :- (describe s/Bool "whether to include disabled licenses") false}
{expired :- (describe s/Bool "whether to include expired licenses") false}
{archived :- (describe s/Bool "whether to include archived licenses") false}]
Expand Down
6 changes: 6 additions & 0 deletions src/clj/rems/application/commands.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
(assoc CommandBase
:comment s/Str))

(s/defschema AddLicensesCommand
(assoc CommandBase
:comment s/Str
:licenses [s/Num]))

(s/defschema AddMemberCommand
(assoc CommandBase
:member {:userid UserId}))
Expand Down Expand Up @@ -90,6 +95,7 @@
{#_:application.command/require-license
:application.command/accept-invitation AcceptInvitationCommand
:application.command/accept-licenses AcceptLicensesCommand
:application.command/add-licenses AddLicensesCommand
:application.command/add-member AddMemberCommand
:application.command/invite-member InviteMemberCommand
:application.command/approve ApproveCommand
Expand Down
6 changes: 6 additions & 0 deletions src/clj/rems/application/events.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
(assoc EventBase
:event/type (s/enum :application.event/licenses-accepted)
:application/accepted-licenses #{s/Int}))
(s/defschema LicensesAddedEvent
(assoc EventBase
:event/type (s/enum :application.event/licenses-added)
:application/comment s/Str
:application/licenses [{:license/id s/Int}]))
(s/defschema MemberAddedEvent
(assoc EventBase
:event/type (s/enum :application.event/member-added)
Expand Down Expand Up @@ -115,6 +120,7 @@
:application.event/decision-requested DecisionRequestedEvent
:application.event/draft-saved DraftSavedEvent
:application.event/licenses-accepted LicensesAcceptedEvent
:application.event/licenses-added LicensesAddedEvent
:application.event/member-added MemberAddedEvent
:application.event/member-invited MemberInvitedEvent
:application.event/member-joined MemberJoinedEvent
Expand Down
19 changes: 14 additions & 5 deletions src/clj/rems/application/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
:application.command/accept-licenses]
:member [:application.command/accept-licenses]
:handler [:see-everything
:application.command/add-licenses
:application.command/add-member
:application.command/remove-member
:application.command/invite-member
Expand Down Expand Up @@ -171,12 +172,9 @@
:application/members #{}
:application/past-members #{}
:application/invitation-tokens {}
:application/resources (map (fn [resource]
{:catalogue-item/id (:catalogue-item/id resource)
:resource/ext-id (:resource/ext-id resource)})
:application/resources (map #(select-keys % [:catalogue-item/id :resource/ext-id])
(:application/resources event))
:application/licenses (map (fn [license]
{:license/id (:license/id license)})
:application/licenses (map #(select-keys % [:license/id])
(:application/licenses event))
:application/accepted-licenses {}
:application/events []
Expand All @@ -201,6 +199,17 @@
(assoc :application/modified (:event/time event))
(assoc-in [:application/accepted-licenses (:event/actor event)] (:application/accepted-licenses event))))

(defmethod event-type-specific-application-view :application.event/licenses-added
[application event]
(-> application
(assoc :application/modified (:event/time event))
(update :application/licenses
(fn [licenses]
(-> licenses
(into (:application/licenses event))
distinct
vec)))))

(defmethod event-type-specific-application-view :application.event/member-invited
[application event]
(-> application
Expand Down
10 changes: 8 additions & 2 deletions src/clj/rems/workflow/dynamic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@
:application/request-id last-request-for-actor
:application/comment (:comment cmd)}))))

(defmethod command-handler :application.command/add-licenses
[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))
:application/comment (:comment cmd)})))

(defmethod command-handler :application.command/add-member
[cmd application injections]
(or (invalid-user-error (:userid (:member cmd)) injections)
Expand All @@ -204,8 +211,7 @@

(defmethod command-handler :application.command/accept-invitation
[cmd application injections]
(or (invalid-user-error (:actor cmd) injections)
(already-member-error application (:actor cmd))
(or (already-member-error application (:actor cmd))
(invitation-token-error application (:token cmd))
(ok {:event/type :application.event/member-joined
:application/id (:application-id cmd)
Expand Down
1 change: 1 addition & 0 deletions src/cljc/rems/text.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
:application.event/decided :t.applications.events/decided
:application.event/decision-requested :t.applications.events/decision-requested
:application.event/draft-saved :t.applications.events/draft-saved
:application.event/licenses-added :t.applications.events/licenses-added
:application.event/licenses-accepted :t.applications.events/licenses-accepted
:application.event/member-added :t.applications.events/member-added
:application.event/member-invited :t.applications.events/member-invited
Expand Down
116 changes: 116 additions & 0 deletions src/cljs/rems/actions/add_licenses.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
(ns rems.actions.add-licenses
(:require [clojure.string :as str]
[re-frame.core :as rf]
[rems.actions.action :refer [action-button action-form-view action-comment button-wrapper collapse-action-form]]
[rems.autocomplete :as autocomplete]
[rems.status-modal :as status-modal]
[rems.text :refer [text]]
[rems.util :refer [fetch post!]]))

(rf/reg-fx
::fetch-licenses
(fn [[user on-success]]
(fetch "/api/licenses"
{:handler on-success
:headers {"x-rems-user-id" (:eppn user)}})))

(rf/reg-event-fx
::open-form
(fn
[{:keys [db]} _]
{:db (assoc db
::comment ""
::potential-licenses #{}
::selected-licenses #{})
::fetch-licenses [(get-in db [:identity :user])
#(rf/dispatch [::set-potential-licenses %])]}))

(defn- assoc-all-titles
"Prepopulate `:all-titles` property to facilitate searching with localized names and unlocalized title"
[license]
(assoc license :all-titles (str/join "" (conj (mapcat :title (vals (:localizations license)))
(:title license)))))

(rf/reg-sub ::potential-licenses (fn [db _] (::potential-licenses db)))
(rf/reg-event-db
::set-potential-licenses
(fn [db [_ licenses]]
(assoc db
::potential-licenses (set (map assoc-all-titles licenses))
::selected-licenses #{})))

(rf/reg-sub ::selected-licenses (fn [db _] (::selected-licenses db)))
(rf/reg-event-db ::set-selected-licenses (fn [db [_ licenses]] (assoc db ::selected-licenses licenses)))
(rf/reg-event-db ::add-selected-licenses (fn [db [_ license]] (update db ::selected-licenses conj license)))
(rf/reg-event-db ::remove-selected-license (fn [db [_ license]] (update db ::selected-licenses disj license)))

(rf/reg-sub ::comment (fn [db _] (::comment db)))
(rf/reg-event-db ::set-comment (fn [db [_ value]] (assoc db ::comment value)))

(def ^:private action-form-id "add-licenses")

(rf/reg-event-fx
::send-add-licenses
(fn [_ [_ {:keys [application-id licenses comment on-finished]}]]
(status-modal/common-pending-handler! (text :t.actions/add-licenses))
(post! "/api/applications/add-licenses"
{:params {:application-id application-id
:comment comment
:licenses (map :id licenses)}
:handler (partial status-modal/common-success-handler! (fn [_]
(collapse-action-form action-form-id)
(on-finished)))
:error-handler status-modal/common-error-handler!})
{}))

(defn add-licenses-action-button []
[action-button {:id action-form-id
:text (text :t.actions/add-licenses)
:on-click #(rf/dispatch [::open-form])}])

(defn title-of-license [license language]
(or (get-in license [:localizations language :title])
(:title license)))

(defn add-licenses-view
[{:keys [selected-licenses potential-licenses comment language on-set-comment on-add-licenses on-remove-license on-send]}]
[action-form-view action-form-id
(text :t.actions/add-licenses)
[[button-wrapper {:id "add-licenses"
:text (text :t.actions/add-licenses)
:class "btn-primary"
:on-click on-send}]]
[:div
[action-comment {:id action-form-id
:label (text :t.form/add-comments-shown-to-applicant)
:comment comment
:on-comment on-set-comment}]
[:div.form-group
[:label (text :t.actions/licenses-selection)]
[autocomplete/component
{:value (sort-by #(title-of-license % language) selected-licenses)
:items potential-licenses
:value->text #(title-of-license %2 language)
:item->key :id
:item->text #(title-of-license % language)
:item->value identity
:search-fields [:all-titles]
:add-fn on-add-licenses
:remove-fn on-remove-license}]]]])

(defn add-licenses-form [application-id on-finished]
(let [selected-licenses @(rf/subscribe [::selected-licenses])
potential-licenses @(rf/subscribe [::potential-licenses])
comment @(rf/subscribe [::comment])
language @(rf/subscribe [:language])]
[add-licenses-view {:selected-licenses selected-licenses
:potential-licenses potential-licenses
:comment comment
:language language
:on-set-comment #(rf/dispatch [::set-comment %])
:on-add-licenses #(rf/dispatch [::add-selected-licenses %])
:on-remove-license #(rf/dispatch [::remove-selected-license %])
:on-send #(rf/dispatch [::send-add-licenses {:application-id application-id
:licenses selected-licenses
:comment comment
:on-finished on-finished}])}]))
3 changes: 3 additions & 0 deletions src/cljs/rems/application.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[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.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]]
[rems.actions.close :refer [close-action-button close-form]]
Expand Down Expand Up @@ -522,6 +523,7 @@
:application.command/decide [decide-action-button]
:application.command/request-comment [request-comment-action-button]
:application.command/comment [comment-action-button]
:application.command/add-licenses [add-licenses-action-button]
:application.command/approve [approve-reject-action-button]
:application.command/reject [approve-reject-action-button]
:application.command/close [close-action-button]]]
Expand All @@ -540,6 +542,7 @@
[close-form app-id reload]
[decide-form app-id reload]
[return-form app-id reload]
[add-licenses-form app-id reload]
[approve-reject-form app-id reload]]]]
(when (seq actions)
[collapsible/component
Expand Down
21 changes: 21 additions & 0 deletions test/clj/rems/api/test_applications.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
handler-id "developer"
commenter-id "carl"
decider-id "bob"
license-id 5 ;; additional licenses from test data
application-id 11] ;; submitted dynamic application from test data

(testing "getting dynamic application as applicant"
Expand All @@ -195,6 +196,7 @@
"application.command/reject"
"application.command/approve"
"application.command/return"
"application.command/add-licenses"
"application.command/add-member"
"application.command/remove-member"
"application.command/invite-member"
Expand Down Expand Up @@ -243,6 +245,21 @@
comment-event (get-in application [:application/events (inc eventcount)])]
(is (= (:application/request-id request-event)
(:application/request-id comment-event)))))))

(testing "adding and then accepting additonal licenses"
(let [eventcount (count (get (get-application application-id handler-id) :events))]
(testing "add licenses"
(is (= {:success true} (send-command handler-id
{:type :application.command/add-licenses
:application-id application-id
:licenses [license-id]
:comment "Please approve these new terms"}))))
(testing "applicant can now accept licenses"
(is (= {:success true} (send-command user-id
{:type :application.command/accept-licenses
:application-id application-id
:accepted-licenses [license-id]}))))))

(testing "request-decision"
(is (= {:success true} (send-command handler-id
{:type :application.command/request-decision
Expand Down Expand Up @@ -273,6 +290,8 @@
"application.event/submitted"
"application.event/comment-requested"
"application.event/commented"
"application.event/licenses-added"
"application.event/licenses-accepted"
"application.event/decision-requested"
"application.event/decided"
"application.event/approved"]
Expand All @@ -282,6 +301,8 @@
"application.event/licenses-accepted"
"application.event/draft-saved"
"application.event/submitted"
"application.event/licenses-added"
"application.event/licenses-accepted"
"application.event/approved"]
applicant-event-types))))))))

Expand Down
Loading

0 comments on commit 9683ef8

Please sign in to comment.