chore(AVS): (WIP) implement user avs update to primary company

This commit is contained in:
Steffen Jost 2024-03-22 12:24:08 +01:00
parent 7a5917131c
commit d213c8e4a1

View File

@ -517,41 +517,36 @@ updateAvsUserByIds apids = do
oldCompanyEnt <- getAvsCompany `traverseJoin` oldAvsFirmInfo
let oldCompanyId = entityKey <$> oldCompanyEnt
oldCompanyMb = entityVal <$> oldCompanyEnt
pst_up = mkUpdate usr newCompany oldCompanyMb $ CheckAvsUpdate UserPrefersPostal _companyPrefersPostal -- possibly change postal preference TODO: only if needed
pst_up = mkUpdate usr newCompany oldCompanyMb $ CheckAvsUpdate UserPrefersPostal _companyPrefersPostal -- possibly change postal preference
superReasonComDef = tshow SupervisorReasonCompanyDefault
_primaryCompanyId <- getUserPrimaryCompany usrId (Just . CompanyKey . companyShorthand)
newUserComp = UserCompany usrId newCompanyId False False 1 True -- default value for new company insertion, if no update can be done
primaryCompanyId <- getUserPrimaryCompany usrId (Just . CompanyKey . companyShorthand)
usr_ups <- case oldAvsFirmInfo of
_ | oldCompanyId == Just newCompanyId -- company unchanged entirely
_ | Just newCompanyId == oldCompanyId -- company unchanged entirely
-> return usr_up0 -- => do nothing
(Just oafi) | ((==) `on` view _avsFirmPostAddressSimple) oafi newAvsFirmInfo -- company address unchanged OR
|| ((==) `on` view _avsFirmPrimaryEmail) oafi newAvsFirmInfo -- company primary email unchanged
-> do -- => just update user company association, keeping supervision privileges
-- newUC <- getBy (UniqueUserCompany usrId oldCompanyId) >>= \case
-- Nothing -> return $ UserCompany usrId newCompanyId False False 1 True
-- Just Entity{entityVal=oldUCid, entityVal=oldUC} -> do
-- delete oldUCid
-- return $ oldUC & _userCompanyCompany .~ newCompanyId
-- void $ insertUnique newUC
let newUserComp = UserCompany usrId newCompanyId False False 1 True
-> do -- => just update user company association, keeping supervision privileges
case oldCompanyId of
Nothing -> void $ insertUnique newUserComp -- it's ok if this already exists
Just ocid -> do
void $ upsertBySafe (UniqueUserCompany usrId ocid) newUserComp (_userCompanyCompany .~ newCompanyId)
void $ upsertBySafe (UniqueUserCompany usrId ocid) newUserComp (_userCompanyCompany .~ newCompanyId)
void $ updateWhere [ UserSupervisorSupervisor ==. usrId
, UserSupervisorCompany ==. Just ocid
, UserSupervisorReason ==. Just superReasonComDef] -- to we want this last condition?
[ UserSupervisorCompany =. Just newCompanyId]
return usr_up0
-- _ | newCompanyId == primaryCompanyId -- Wechsel der AVS-Firma zur FRADrive-Primärfirma
-- alte CompanyUser entfernen
-- keep Supervision unchanged
-- keep postal preference
_ | Just newCompanyId == primaryCompanyId -- Wechsel der AVS-Firma zur FRADrive-Primärfirma
-> do
whenIsJust oldCompanyId $ deleteBy . UniqueUserCompany usrId
when (isJust oldCompanyId) $ deleteWhere $ (UserSupervisorUser ==. usrId):(UserSupervisorCompany ==. oldCompanyId):(UserSupervisorReason ~=. superReasonComDef)
return usr_up0
_ -- company changed completely
-> do -- switch company
-> do -- switch company, keeping priority
(getBy . UniqueUserCompany usrId) `traverseJoin` oldCompanyId >>= \case
Nothing -> do
void $ insertUnique $ UserCompany usrId newCompanyId False False 1 True
void $ insertUnique newUserComp
Just Entity{entityKey=ucidOld, entityVal=UserCompany{userCompanyCompany, userCompanySupervisor, userCompanySupervisorReroute, userCompanyPriority}} -> do
when userCompanySupervisor $ reportAdminProblem $ AdminProblemSupervisorNewCompany usrId userCompanyCompany newCompanyId userCompanySupervisorReroute
delete ucidOld