refactor(avs): using MaybeT
This commit is contained in:
parent
b5340a18a2
commit
cb807fce98
@ -572,13 +572,13 @@ updateReceivers uid = do
|
|||||||
-- CR3 Functions
|
-- CR3 Functions
|
||||||
|
|
||||||
|
|
||||||
-- data CheckAvsUpdate record iavs = forall typ f. (Eq typ, PersistField typ, Functor f) => CheckAvsUpdate ((typ -> f typ) -> iavs -> f iavs) (EntityField record typ) -- A Lens and a User Field; does not work.
|
-- data CheckAvsUpdate record iavs = forall typ f. (Eq typ, PersistField typ, Functor f) => CheckAvsUpdate (EntityField record typ) ((typ -> f typ) -> iavs -> f iavs) -- An Record Field and fitting Lens
|
||||||
data CheckAvsUpdate record iavs = forall typ. (Eq typ, PersistField typ) => CheckAvsUpdate ((typ -> Const typ typ) -> iavs -> Const typ iavs) (EntityField record typ) -- A Lens and a User Field
|
data CheckAvsUpdate record iavs = forall typ. (Eq typ, PersistField typ) => CheckAvsUpdate (EntityField record typ) ((typ -> Const typ typ) -> iavs -> Const typ iavs) -- An Record Field and fitting Lens
|
||||||
|
|
||||||
-- | Compute necessary updates. Given an database record, a new and an old avs response and a pair consisting of a getter from avs response to a value and and EntityField of the same value,
|
-- | Compute necessary updates. Given an database record, a new and an old avs response and a pair consisting of a getter from avs response to a value and and EntityField of the same value,
|
||||||
-- an update is returned, if the current value is identical to the old avs value, which changed in the new avs query
|
-- an update is returned, if the current value is identical to the old avs value, which changed in the new avs query
|
||||||
mkUpdate :: PersistEntity record => record -> iavs -> iavs -> CheckAvsUpdate record iavs -> Maybe (Update record)
|
mkUpdate :: PersistEntity record => record -> iavs -> iavs -> CheckAvsUpdate record iavs -> Maybe (Update record)
|
||||||
mkUpdate usr npi opi (CheckAvsUpdate la up)
|
mkUpdate usr npi opi (CheckAvsUpdate up la)
|
||||||
| let newval = npi ^. la
|
| let newval = npi ^. la
|
||||||
, let oldval = opi ^. la
|
, let oldval = opi ^. la
|
||||||
, let usrval = getField up usr
|
, let usrval = getField up usr
|
||||||
@ -596,24 +596,23 @@ updateAvsUserByIds apids = do
|
|||||||
where
|
where
|
||||||
procResp (AvsDataContact apid avsPersonInfo _avsFirmInfo)
|
procResp (AvsDataContact apid avsPersonInfo _avsFirmInfo)
|
||||||
| apid `Set.notMember` apids = return mempty -- should not occur, neither should one apid occur multiple times withtin the response (if so, all responses processed here in random order)
|
| apid `Set.notMember` apids = return mempty -- should not occur, neither should one apid occur multiple times withtin the response (if so, all responses processed here in random order)
|
||||||
| otherwise = runDB $
|
| otherwise = fmap maybeMonoid . runDB . runMaybeT $ do
|
||||||
getBy (UniqueUserAvsId apid) >>= foldMapM ( \(Entity _ usravs) ->
|
(Entity _ usravs) <- MaybeT $ getBy $ UniqueUserAvsId apid
|
||||||
continueJust (userAvsLastPersonInfo usravs) ( \oldAvsPersonInfo ->
|
oldAvsPersonInfo <- hoistMaybe $ userAvsLastPersonInfo usravs
|
||||||
let usrId = userAvsUser usravs in
|
let usrId = userAvsUser usravs
|
||||||
get usrId >>= foldMapM ( \usr -> do
|
usr <- MaybeT $ get usrId
|
||||||
let ups = mapMaybe (mkUpdate usr avsPersonInfo oldAvsPersonInfo)
|
let ups = mapMaybe (mkUpdate usr avsPersonInfo oldAvsPersonInfo)
|
||||||
[ CheckAvsUpdate _avsInfoFirstName UserFirstName
|
[ CheckAvsUpdate UserFirstName _avsInfoFirstName
|
||||||
, CheckAvsUpdate _avsInfoLastName UserSurname
|
, CheckAvsUpdate UserSurname _avsInfoLastName
|
||||||
, CheckAvsUpdate _avsInfoDisplayName UserDisplayName
|
, CheckAvsUpdate UserDisplayName _avsInfoDisplayName
|
||||||
, CheckAvsUpdate _avsInfoDateOfBirth UserBirthday
|
, CheckAvsUpdate UserBirthday _avsInfoDateOfBirth
|
||||||
, CheckAvsUpdate _avsInfoPersonMobilePhoneNo UserMobile
|
, CheckAvsUpdate UserMobile _avsInfoPersonMobilePhoneNo
|
||||||
, CheckAvsUpdate (_avsInfoPersonNo . re _Just) UserMatrikelnummer -- Maybe im User, aber nicht im AvsInfo
|
, CheckAvsUpdate UserMatrikelnummer $ _avsInfoPersonNo . re _Just -- Maybe im User, aber nicht im AvsInfo; also: `re _Just` work like `to Just`
|
||||||
, CheckAvsUpdate (_avsInfoPersonEMail . to (fromMaybe mempty) . from _CI) UserDisplayEmail -- Maybe nicht im AvsInfo, aber im AvsInfo
|
, CheckAvsUpdate UserDisplayEmail $ _avsInfoPersonEMail . to (fromMaybe mempty) . from _CI -- Maybe nicht im User, aber im AvsInfo
|
||||||
, CheckAvsUpdate (_avsInfoInternalPersonalNo . _Just . _avsInternalPersonalNo . to Just) UserCompanyPersonalNumber -- Maybe im User und im AvsInfo
|
, CheckAvsUpdate UserCompanyPersonalNumber $ _avsInfoInternalPersonalNo . _Just . _avsInternalPersonalNo . re _Just -- Maybe im User und im AvsInfo
|
||||||
]
|
]
|
||||||
update usrId ups
|
lift $ update usrId ups
|
||||||
return $ Set.singleton usrId
|
return $ Set.singleton usrId
|
||||||
)))
|
|
||||||
|
|
||||||
|
|
||||||
updateAvsUserById :: ( MonadThrow m, MonadHandler m, HandlerSite m ~ UniWorX )
|
updateAvsUserById :: ( MonadThrow m, MonadHandler m, HandlerSite m ~ UniWorX )
|
||||||
|
|||||||
@ -1214,11 +1214,17 @@ ofoldl1M _ _ = error "otoList of NonNull is empty"
|
|||||||
foldMapM :: (Foldable f, Monad m, Monoid b) => (a -> m b) -> f a -> m b
|
foldMapM :: (Foldable f, Monad m, Monoid b) => (a -> m b) -> f a -> m b
|
||||||
foldMapM f = foldrM (\x xs -> (<> xs) <$> f x) mempty
|
foldMapM f = foldrM (\x xs -> (<> xs) <$> f x) mempty
|
||||||
|
|
||||||
-- | convenient synonym for `flip foldMapM`
|
{- left as a remineder: if you need these, use MaybeT instead!
|
||||||
|
-- convenient synonym for `flip foldMapM`
|
||||||
continueJust :: (Applicative m, Monoid b) => Maybe a -> (a -> m b) -> m b
|
continueJust :: (Applicative m, Monoid b) => Maybe a -> (a -> m b) -> m b
|
||||||
continueJust (Just x) f = f x
|
continueJust (Just x) f = f x
|
||||||
continueJust Nothing _ = pure mempty
|
continueJust Nothing _ = pure mempty
|
||||||
|
|
||||||
|
maybeContinue :: (Monoid b, Monad m) => m (Maybe a) -> (a -> m b) -> m b
|
||||||
|
maybeContinue mx f = mx >>= \case
|
||||||
|
Nothing -> return mempty
|
||||||
|
Just x -> f x
|
||||||
|
-}
|
||||||
|
|
||||||
ifoldMapM :: (FoldableWithIndex i f, Monad m, Monoid b) => (i -> a -> m b) -> f a -> m b
|
ifoldMapM :: (FoldableWithIndex i f, Monad m, Monoid b) => (i -> a -> m b) -> f a -> m b
|
||||||
ifoldMapM f = ifoldrM (\i x xs -> (<> xs) <$> f i x) mempty
|
ifoldMapM f = ifoldrM (\i x xs -> (<> xs) <$> f i x) mempty
|
||||||
|
|||||||
Reference in New Issue
Block a user