fix(firm): supervisor changes led to inconsistent DB

This commit is contained in:
Steffen Jost 2023-12-01 16:55:51 +01:00
parent 34c0928718
commit 1d3345cbba

View File

@ -1102,10 +1102,14 @@ postFirmSupersR fsh = do
addMessageI Info $ MsgRemoveSupervisors nrRmSuper nrRmActual
reloadKeepGetParams $ FirmUsersR fsh -- reload to reflect changes
(FirmSuperActSwitchSuperData{..}, Set.toList -> uids) -> do
let fltrSpr = guardMonoid (isNothing firmSuperActSwitchSuper) [UserCompanySupervisor ==. True]
changes = maybeEmpty firmSuperActSwitchSuper (pure . (UserCompanySupervisor =.))
<> guardMonoid (firmSuperActSwitchSuper /= Just False || firmSuperActSwitchReroute == Just False) (
maybeEmpty firmSuperActSwitchReroute (pure . (UserCompanySupervisorReroute =.)))
let (fltrSpr, changes) = case (firmSuperActSwitchSuper, firmSuperActSwitchReroute) of
(Just True, Nothing) -> ([UserCompanySupervisor ==. False], [UserCompanySupervisor =. True ])
(Just True, Just rer) -> ([UserCompanySupervisor ==. False] ||. [UserCompanySupervisorReroute !=. rer]
, [UserCompanySupervisor =. True , UserCompanySupervisorReroute =. rer ])
(Just False, _) -> ([UserCompanySupervisor ==. True ], [UserCompanySupervisor =. False, UserCompanySupervisorReroute =. False])
(Nothing, Just True) -> ([UserCompanySupervisor ==. True, UserCompanySupervisorReroute ==. False], [UserCompanySupervisorReroute =. True ])
(Nothing, Just False) -> ([ UserCompanySupervisorReroute ==. True ], [UserCompanySupervisorReroute =. False])
(Nothing, Nothing ) -> ([],[])
nrSuperChanges <- runDB $ updateWhereCount (fltrSpr <> [UserCompanyUser <-. uids, UserCompanyCompany ==. cid]) changes
addMessageI Info $ MsgFirmActAddSupersSet nrSuperChanges Nothing
reloadKeepGetParams $ FirmUsersR fsh -- reload to reflect changes