Skip to content

Commit

Permalink
Merge pull request #3245 from CSCfi/fix-roles-after-delete
Browse files Browse the repository at this point in the history
Fix roles after delete
  • Loading branch information
Macroz authored Jan 15, 2024
2 parents 8c52b4a + 25c1c09 commit 08caee1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ have notable changes.
Changes since v2.35

### Fixes
- Fix issue with user roles after deletion. This was introduced in the previous release that optimizes cache updates. (#3243)
- Mark form as optional in catalogue item creation. Also consider categories optional unless the catalogue tree is enabled. (#3244)

## v2.35 "Selkämerenkatu" 2023-12-13
Expand Down
20 changes: 15 additions & 5 deletions src/clj/rems/db/applications.clj
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,29 @@
updated-users))

;; update all the users that are in this round
updated-roles-by-user (->> updated-users
(mapcat new-updated-apps-by-user)
(distinct-by :application/id)
group-roles-by-user)
updated-roles-by-user (into {}
(for [userid updated-users
:let [apps (->> userid
new-personalized-apps-by-user
(distinct-by :application/id))
roles (->> apps
(mapcat :application/roles)
set)]]
[userid roles]))
new-roles-by-user (doall (merge (apply dissoc old-roles-by-user updated-users)
updated-roles-by-user))

;; now calculate the reverse, i.e. users by role
new-updated-users-by-role (->> (for [user updated-users
role (updated-roles-by-user user)]
{role #{user}})
(apply merge-with set/union))
;; update all the users that are in this round
new-users-by-role (->> old-users-by-role
;; remove users updated in this round
(map-vals (fn [users] (apply disj users updated-users)))
;; add users back to correct groups
(merge-with set/union (group-users-by-role (vals new-updated-enriched-apps)))
(merge-with set/union new-updated-users-by-role)
doall)]

{::raw-apps new-raw-apps
Expand Down
32 changes: 32 additions & 0 deletions test/clj/rems/db/test_applications.clj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,38 @@
(testing "db entry for application is gone"
(is (not (contains? (set (map :id (db/get-application-ids {}))) app-id1)))))

(testing "with two applications"
(let [app-id1 (test-helpers/create-application! {:actor "applicant1"})
app-id2 (test-helpers/create-application! {:actor "applicant1"})]
(test-helpers/command! {:application-id app-id2
:type :application.command/submit
:actor "applicant1"})
(is (applications/get-application app-id1))
(is (applications/get-application app-id2))
(is (= [app-id1 app-id2] (sort (map :application/id (applications/get-my-applications "applicant1")))))
(is (= #{:applicant} (applications/get-all-application-roles "applicant1")))
(is (= #{"applicant1" "applicant2"} (applications/get-users-with-role :applicant)))

(applications/delete-application! app-id1)

(testing "application disappears from my applications"
(is (= [app-id2] (map :application/id (applications/get-my-applications "applicant1")))))
(testing "application disappears from all-applications-cache (apps-by-user)"
(is (= [app-id2] (map :application/id (applications/get-all-applications "applicant1")))))
(testing "role persists in roles-by-user"
(is (= #{:applicant} (applications/get-all-application-roles "applicant1"))))
(testing "role persists in users-by-role"
(is (= #{"applicant1" "applicant2"} (applications/get-users-with-role :applicant))))
(testing "deleted draft is gone"
(is (not (applications/get-application app-id1))))
(is (applications/get-application app-id2))
(testing "events are gone from event cache"
(is (empty? (db-events/get-application-events app-id1))))
(testing "events are gone from DB"
(is (empty? (db/get-application-events {:application app-id1}))))
(testing "db entry for application is gone"
(is (not (contains? (set (map :id (db/get-application-ids {}))) app-id1))))))

(let [app-id1 (test-helpers/create-application! {:actor "applicant1"})]
(test-helpers/command! {:application-id app-id1
:type :application.command/submit
Expand Down

0 comments on commit 08caee1

Please sign in to comment.