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

Change catalogue items #3277

Merged
merged 17 commits into from
Apr 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ have notable changes.

Changes since v2.36

### Additions
- The "Change Form" function in administration has been expanded to be able to change both forms and workflows. From now on it is called "Update catalogue item". The old `change-form` API still exists but has been deprecated. (#3271)

## v2.36 "Laivapojankuja" 2024-03-13

### Additions
Expand Down
10 changes: 6 additions & 4 deletions resources/translations/da.edn
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@
:archive "Arkiver"
:back "Tilbage"
:blacklist "Sortliste"
:change-form "Rediger blanket"
:cancel "Afbryd"
:catalogue-item "Katalogpost"
:catalogue-items "Katalogposter"
:categories "Kategorier"
:category "Kategori"
:category-children "Underkategorier"
:change "Rediger"
:comment "Kommentar"
:copy-as-new "Kopier som ny"
:create-catalogue-item "Ny katalogpost"
Expand All @@ -113,6 +111,8 @@
:display-order "Visningsrækkefølge"
:display-own-organization-only-explanation nil
:display-own-organization-only nil
:do-not-change-form nil
:do-not-change-workflow nil
:edit "Rediger"
:edit-catalogue-item "Rediger katalogpost"
:edit-category "Rediger kategori"
Expand Down Expand Up @@ -170,6 +170,7 @@
:title "Navn"
:type "Type"
:unarchive "Dearkiver"
:update-catalogue-item nil
:user "Bruger"
:userid "Brugernavn"
:user-role "Brugerrolle"
Expand Down Expand Up @@ -321,8 +322,9 @@
:show "Vis"
:show-less "Vis mindre"
:show-more "Vis mere"}
:change-form {:change-form-intro "Du er ved at ændre blanketten for nedenstående ressourcer. Ændringerne foretages en ad gangen, så hver gammel ressource tages væk og en ny kopi vil benytte den valgte blanket."
:form-selection "Blanket"}
:update-catalogue-item {:update-catalogue-item-intro "Du er ved at opdatera nedenstående ressourcer. Ændringerne foretages en ad gangen, så hver gammel ressource tages væk og en ny kopi vil benytte den valgte blanket og arbejdsgang."
:form-selection "Blanket"
:workflow-selection "Arbejdsgang"}
:commands {:accept-invitation "Accepter invitation"
:accept-licenses "Accepter brugsvilkår"
:add-licenses "Tilføj brugsvilkår"
Expand Down
10 changes: 6 additions & 4 deletions resources/translations/en.edn
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@
:archive "Archive"
:back "Back"
:blacklist "Blacklist"
:change-form "Change form"
:cancel "Cancel"
:catalogue-item "Catalogue item"
:catalogue-items "Catalogue items"
:categories "Categories"
:category "Category"
:category-children "Subcategories"
:change "Change"
:comment "Comment"
:copy-as-new "Copy as new"
:create-catalogue-item "Create catalogue item"
Expand All @@ -113,6 +111,8 @@
:display-order "Display order"
:display-own-organization-only-explanation "Should only own organization items be shown?"
:display-own-organization-only "Own organization only"
:do-not-change-form "Don't change form"
:do-not-change-workflow "Don't change workflow"
:edit "Edit"
:edit-catalogue-item "Edit catalogue item"
:edit-category "Edit category"
Expand Down Expand Up @@ -170,6 +170,7 @@
:title "Title"
:type "Type"
:unarchive "Unarchive"
:update-catalogue-item "Update catalogue item"
:user "User"
:userid "User id"
:user-role "User role"
Expand Down Expand Up @@ -346,8 +347,9 @@
:show "Show"
:show-less "Show less"
:show-more "Show more"}
:change-form {:change-form-intro "You are about to change the form of the following catalogue items. The changes will be made one at a time, so that each old item is terminated and a new copy will use the chosen form."
:form-selection "Form"}
:update-catalogue-item {:update-catalogue-item-intro "You are about to update the following catalogue items. The changes will be made one at a time, so that each old item is terminated and a new copy will use the chosen form and workflow."
:form-selection "Form"
:workflow-selection "Workflow"}
:commands {:accept-invitation "Accept invitation"
:accept-licenses "Accept the terms of use"
:add-licenses "Add terms of use"
Expand Down
10 changes: 6 additions & 4 deletions resources/translations/fi.edn
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@
:archive "Arkistoi"
:back "Takaisin"
:blacklist "Kieltolista"
:change-form "Vaihda lomaketta"
:cancel "Peruuta"
:catalogue-item "Aineisto"
:catalogue-items "Aineistot"
:categories "Kategoriat"
:category "Kategoria"
:category-children "Alakategoriat"
:change "Vaihda"
:comment "Kommentti"
:copy-as-new "Kopioi uudeksi"
:create-catalogue-item "Uusi aineisto"
Expand All @@ -111,6 +109,8 @@
:display-order "Näyttöjärjestys"
:display-own-organization-only-explanation "Näytetäänkö vain oma organisaatio?"
:display-own-organization-only "Vain oma organisaatio"
:do-not-change-form "Älä vaihda lomaketta"
:do-not-change-workflow "Älä vaihda työvuota"
:edit "Muokkaa"
:edit-catalogue-item "Muokkaa aineistoa"
:edit-category "Muokkaa kategoriaa"
Expand Down Expand Up @@ -166,6 +166,7 @@
:title "Nimi"
:type "Tyyppi"
:unarchive "Palauta arkistosta"
:update-catalogue-item "Päivitä aineistoa"
:user "Käyttäjä"
:userid "Käyttäjätunnus"
:user-role "Käyttäjän rooli"
Expand Down Expand Up @@ -300,8 +301,9 @@
:show "Näytä"
:show-less "Näytä vähemmän"
:show-more "Näytä lisää"}
:change-form {:change-form-intro "Olet muuttamassa seuraavien aineistojen lomaketta. Muutokset tehdään yksi kerrallaan siten, että vanha aineisto poistetaan käytöstä ja uusi kopio käyttää valitsemaasi lomaketta."
:form-selection "Lomake"}
:update-catalogue-item {:update-catalogue-item-intro "Olet muuttamassa seuraavia aineistoja. Muutokset tehdään yksi kerrallaan siten, että vanha aineisto poistetaan käytöstä ja uusi kopio käyttää valitsemaasi lomaketta ja työvuota."
:form-selection "Lomake"
:workflow-selection "Workflow"}
:commands {:accept-invitation "Hyväksy kutsu"
:accept-licenses "Hyväksy käyttöehdot"
:add-licenses "Lisää käyttöehto"
Expand Down
10 changes: 6 additions & 4 deletions resources/translations/sv.edn
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@
:archive "Arkivera"
:back "Tillbaka"
:blacklist "Svartlista"
:change-form "Ändra blankett"
:cancel "Avbryt"
:catalogue-item "Katalogpost"
:catalogue-items "Katalogposter"
:categories "Kategorier"
:category "Kategori"
:category-children "Underkategorier"
:change "Ändra"
:comment "Kommentar"
:copy-as-new "Kopiera som ny"
:create-catalogue-item "Ny katalogpost"
Expand All @@ -111,6 +109,8 @@
:display-order "Visningsordning"
:display-own-organization-only-explanation "Visa endast egen organisation?"
:display-own-organization-only "Endast egen organisation"
:do-not-change-form "Ändra inte blanketten"
:do-not-change-workflow "Ändra inte arbetsflöden"
:edit "Ändra"
:edit-catalogue-item "Ändra katalogpost"
:edit-category "Ändra kategori"
Expand Down Expand Up @@ -166,6 +166,7 @@
:title "Namn"
:type "Typ"
:unarchive "Avarkivera"
:update-catalogue-item "Uppdatera katalogposten"
:user "Användare"
:userid "Användarnamn"
:user-role "Användarensroll"
Expand Down Expand Up @@ -300,8 +301,9 @@
:show "Visa"
:show-less "Visa mindre"
:show-more "Visa mer"}
:change-form {:change-form-intro "Du ändrar blanketten för resurserna nedan. Förändringarna görs en åt gången genom att den gamla resursen tas ur bruk och ersätts av en kopia som använder blanketten du väljer nedan."
:form-selection "Blankett"}
:update-catalogue-item {:update-catalogue-item-intro "Du uppdaterar katalogposterna nedan. Förändringarna görs en åt gången genom att den gamla katalogposten tas ur bruk och ersätts av en kopia som använder blanketten och arbetsflöden du väljer nedan."
:form-selection "Blankett"
:workflow-selection "Arbetsflöde"}
:commands {:accept-invitation "Acceptera inbjudan"
:accept-licenses "Acceptera licenserna"
:add-licenses "Lägg till en licens"
Expand Down
27 changes: 26 additions & 1 deletion src/clj/rems/api/catalogue_items.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
(s/optional-key :catalogue-item-id) s/Int
(s/optional-key :errors) [s/Any]})

(s/defschema UpdateCatalogueItemCommand
{(s/optional-key :form) (describe (s/maybe s/Int) "new form id")
(s/optional-key :workflow) (describe (s/maybe s/Int) "new workflow id")})

(s/defschema UpdateCatalogueItemResponse
{:success s/Bool
(s/optional-key :catalogue-item-id) s/Int
(s/optional-key :errors) [s/Any]})

;; TODO use declarative roles everywhere
(def catalogue-items-api
(context "/catalogue-items" []
Expand All @@ -81,7 +90,7 @@
:archived archived}))))

(POST "/:item-id/change-form" request
:summary "Change catalogue item form. Creates a copy and ends the old."
:summary "Change catalogue item form. Creates a copy and ends the old. DEPRECATED, will disappear, use /update instead"
:roles +admin-write-roles+
:path-params [item-id :- (describe s/Int "catalogue item")]
:body [command ChangeFormCommand]
Expand All @@ -92,6 +101,22 @@
(ok (catalogue/change-form! it (:form command)))
(not-found-json-response)))

(POST "/:item-id/update" request
:summary "Update a catalogue item allowing to change form and workflow. Creates a copy and ends the old."
:roles +admin-write-roles+
:path-params [item-id :- (describe s/Int "catalogue item")]
:body [command UpdateCatalogueItemCommand]
:responses {200 {:schema UpdateCatalogueItemResponse}
404 {:schema s/Any :description "Not found"}}
(extended-logging request)
(if-let [it (catalogue/get-localized-catalogue-item item-id)]
(ok (catalogue/update! it
(merge (when (contains? command :form)
{:form-id (:form command)})
(when (contains? command :workflow)
{:workflow-id (:workflow command)}))))
(not-found-json-response)))

(GET "/:item-id" []
:summary "Get a single catalogue item"
:path-params [item-id :- (describe s/Int "catalogue item")]
Expand Down
51 changes: 51 additions & 0 deletions src/clj/rems/service/catalogue.clj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,57 @@

{:success true :catalogue-item-id (:id new-item)})))

(defn update!
"Updates the catalogue item `item`.

Changes the form and/or workflow.

Since we don't want to modify the old item we must create
a new item that is the copy of the old item except for the changed details."
[item {:keys [form-id workflow-id] :or {form-id :do-not-change-form workflow-id :do-not-change-workflow}}]
(util/check-allowed-organization! (:organization item))
;; are we done already? could be retry
(if (and (or (= :do-not-change-form form-id)
(= (:formid item) form-id))
(or (= :do-not-change-workflow workflow-id)
(= (:wfid item) workflow-id)))
{:success true :catalogue-item-id (:id item)}

;; create a new item with the new form
(let [form (case form-id
:do-not-change-form (:formid item) ; preserve old
nil nil ; form is optional and can be unset
form-id)
wfid (case workflow-id
:do-not-change-workflow (:wfid item) ; preserve old
workflow-id)
new-item (db/create-catalogue-item! {:enabled true
:archived false
:form form
:organization (get-in item [:organization :organization/id])
:resid (:resource-id item)
:wfid wfid
:catalogueitemdata (catalogue/catalogueitemdata->json item)})]

;; copy localizations
(doseq [[langcode localization] (:localizations item)]
(db/upsert-catalogue-item-localization! {:id (:id new-item)
:langcode (name langcode)
:title (:title localization)
:infourl (:infourl localization)}))

;; reset cache so that next call to get localizations will get these ones
(catalogue/reset-cache!)

;; hide the old catalogue item
(db/set-catalogue-item-enabled! {:id (:id item) :enabled false})
(db/set-catalogue-item-archived! {:id (:id item) :archived true})

;; new dependencies introduced
(dependencies/reset-cache!)

{:success true :catalogue-item-id (:id new-item)})))

(defn get-catalogue-table [opts]
(get-localized-catalogue-items (merge {:archived false}
opts)))
47 changes: 21 additions & 26 deletions src/cljs/rems/administration/catalogue_items.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(ns rems.administration.catalogue-items
(:require [cljs-time.coerce :as time-coerce]
[medley.core :refer [index-by]]
[re-frame.core :as rf]
[rems.administration.administration :as administration]
[rems.administration.catalogue-item :as catalogue-item]
[rems.administration.status-flags :as status-flags]
[rems.atoms :as atoms :refer [readonly-checkbox document-title]]
[rems.flash-message :as flash-message]
[rems.common.roles :as roles]
[rems.common.util :refer [select-vals]]
[rems.spinner :as spinner]
[rems.table :as table]
[rems.text :refer [localize-time text get-localized-title]]
Expand All @@ -15,7 +17,7 @@
(rf/reg-event-fx
::enter-page
(fn [{:keys [db]}]
{:db (assoc db ::selected-items-ids (or (::selected-items-ids db) #{}))
{:db db
:dispatch-n [[::fetch-catalogue]
[:rems.table/reset]
[:rems.administration.administration/remember-current-page]]}))
Expand Down Expand Up @@ -63,39 +65,33 @@
:error-handler (flash-message/default-error-handler :top description)})
{}))

(rf/reg-event-db
::set-selected-items-ids
(fn [db [_ items]]
(assoc db ::selected-items-ids items)))

(rf/reg-sub
::selected-items-ids
(fn [db _]
(::selected-items-ids db)))

(defn- items-by-ids [items ids]
(filter (comp ids :id) items))
::catalogue-by-ids
:<- [::catalogue]
(fn [items]
(index-by :id items)))

(rf/reg-sub
::selected-catalogue-items
(fn [_ _]
[(rf/subscribe [::catalogue])
(rf/subscribe [::selected-items-ids])])
(fn [[catalogue selected-item-ids] _]
(items-by-ids catalogue selected-item-ids)))
:<- [::catalogue-by-ids]
:<- [:rems.table/selected-rows {:id ::catalogue}]
(fn [[catalogue-by-ids selected-item-ids] _]
(select-vals catalogue-by-ids selected-item-ids)))
Macroz marked this conversation as resolved.
Show resolved Hide resolved

(defn- create-catalogue-item-button []
[atoms/link {:class "btn btn-primary" :id :create-catalogue-item}
"/administration/catalogue-items/create"
(text :t.administration/create-catalogue-item)])

(defn- change-form-button [items]
[:button.btn.btn-primary
{:disabled (when (empty? items) :disabled)
(defn- update-catalogue-item-button [items]
[atoms/rate-limited-action-button
{:id :update-catalogue-item
:class "btn-primary"
:disabled (when (empty? items) :disabled)
:on-click (fn []
(rf/dispatch [:rems.administration.change-catalogue-item-form/enter-page items])
(navigate! "/administration/catalogue-items/change-form"))}
(text :t.administration/change-form)])
(rf/dispatch [:rems.administration.update-catalogue-item/enter-page items])
(navigate! "/administration/catalogue-items/update-catalogue-item"))
:label [text :t.administration/update-catalogue-item]}])

(defn- categories-button []
[atoms/link {:class "btn btn-primary" :id :manage-categories}
Expand Down Expand Up @@ -182,8 +178,7 @@
:rows [::catalogue-table-rows]
:default-sort-column :created
:default-sort-order :desc
:selectable? true
:on-select #(rf/dispatch [::set-selected-items-ids %])}])
:selectable? true}])

(defn catalogue-items-page []
(into [:div
Expand All @@ -196,7 +191,7 @@
[atoms/commands
[create-catalogue-item-button]
[categories-button]
[change-form-button @(rf/subscribe [::selected-catalogue-items])]]
[update-catalogue-item-button @(rf/subscribe [::selected-catalogue-items])]]
[status-flags/status-flags-intro #(do (rf/dispatch [::fetch-catalogue])
(rf/dispatch [:rems.table/set-selected-rows {:id ::catalogue} nil]))]]
[administration/own-organization-selection]
Expand Down
Loading