From 43ace1c6a0394c550f686004866a03d3dc486ff1 Mon Sep 17 00:00:00 2001 From: Steffen Jost Date: Fri, 27 Jan 2023 11:48:37 +0100 Subject: [PATCH] chore(users): allow multiple company-user associations at once --- models/users.model | 4 ++-- src/Handler/Utils/Company.hs | 6 +++--- test/Database/Fill.hs | 13 ++++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/models/users.model b/models/users.model index abee2d5c6..5f15d937d 100644 --- a/models/users.model +++ b/models/users.model @@ -88,12 +88,12 @@ UserCompany user UserId company CompanyId OnDeleteCascade OnUpdateCascade supervisor Bool -- is this user a company supervisor? - UniqueUserCompany user -- only one company per user is currently allowed + UniqueUserCompany user company -- a user may belong to multiple companies, but to each one only once deriving Generic UserSupervisor supervisor UserId -- multiple supervisor per trainee possible user UserId rerouteNotifications Bool -- User can be his own supervisor to receive notifications as well - UniqueUserSupervisor supervisor user + UniqueUserSupervisor supervisor user -- each supervisor/user combination is unique (same supervisor can superviser the same user only once) deriving Generic diff --git a/src/Handler/Utils/Company.hs b/src/Handler/Utils/Company.hs index ad9b9e1a1..837bb5181 100644 --- a/src/Handler/Utils/Company.hs +++ b/src/Handler/Utils/Company.hs @@ -17,10 +17,10 @@ import qualified Data.Text as Text upsertUserCompany :: UserId -> Maybe Text -> DB () upsertUserCompany uid (Just cName) | notNull cName = do cid <- upsertCompany cName - void $ upsertBy (UniqueUserCompany uid) + void $ upsertBy (UniqueUserCompany uid cid) (UserCompany uid cid False) - [UserCompanyCompany =. cid, UserCompanySupervisor =. False] -upsertUserCompany uid _ = deleteBy (UniqueUserCompany uid) + [] +upsertUserCompany uid _ = deleteWhere [ UserCompanyUser ==. uid ] upsertCompany :: Text -> DB CompanyId upsertCompany cName = diff --git a/test/Database/Fill.hs b/test/Database/Fill.hs index 9de3b3292..969ce28b2 100644 --- a/test/Database/Fill.hs +++ b/test/Database/Fill.hs @@ -475,15 +475,18 @@ fillDb = do I am aware that violations in the form plagiarism or collaboration with third parties will lead to expulsion from the course. |] } - fraportAg <- insert' $ Company "Fraport AG" "Fraport" 1 False Nothing - _fraGround <- insert' $ Company "Fraport Ground Handling Professionals GmbH" "FraGround" 2 False Nothing -- TODO: better testcases - nice <- insert' $ Company "N*ICE Aircraft Services & Support GmbH" "N*ICE" 33 False Nothing - _ffacil <- insert' $ Company "Fraport Facility Services GmbH" "GCS" 44 False Nothing - bpol <- insert' $ Company "Bundespolizeidirektion Flughafen Frankfurt am Main" "BPol" 5555 False Nothing + fraportAg <- insert' $ Company "Fraport AG" "Fraport" 1 False Nothing + fraGround <- insert' $ Company "Fraport Ground Handling Professionals GmbH" "FraGround" 2 False Nothing -- TODO: better testcases + nice <- insert' $ Company "N*ICE Aircraft Services & Support GmbH" "N*ICE" 33 False Nothing + ffacil <- insert' $ Company "Fraport Facility Services GmbH" "GCS" 44 False Nothing + bpol <- insert' $ Company "Bundespolizeidirektion Flughafen Frankfurt am Main" "BPol" 5555 False Nothing void . insert' $ UserCompany jost fraportAg True void . insert' $ UserCompany svaupel nice True void . insert' $ UserCompany gkleen nice False + void . insert' $ UserCompany gkleen fraGround False void . insert' $ UserCompany fhamann bpol False + void . insert' $ UserCompany fhamann ffacil True + void . insert' $ UserCompany fhamann nice False void . insert' $ UserSupervisor jost gkleen True void . insert' $ UserSupervisor jost svaupel False void . insert' $ UserSupervisor jost sbarth False