-- SPDX-FileCopyrightText: 2022 Sarah Vaupel ,Steffen Jost -- -- SPDX-License-Identifier: AGPL-3.0-or-later {-# OPTIONS_GHC -fno-warn-orphans #-} -- needed for HasEntity instances {-# OPTIONS -Wno-unused-top-binds #-} -- TODO: remove me, for debugging only {-# LANGUAGE TypeApplications #-} module Handler.Admin.Avs ( getAdminAvsR, postAdminAvsR , getProblemAvsSynchR, postProblemAvsSynchR ) where import Import import qualified Control.Monad.State.Class as State -- import Data.Aeson (encode) import qualified Data.Text as Text import qualified Data.CaseInsensitive as CI import qualified Data.Set as Set import qualified Data.Map as Map import Database.Persist.Sql (updateWhereCount) import Handler.Utils import Handler.Utils.Avs import Utils.Avs import Database.Esqueleto.Experimental ((:&)(..)) import qualified Database.Esqueleto.Legacy as E import qualified Database.Esqueleto.Experimental as E hiding (from, on) import qualified Database.Esqueleto.Experimental as X (from, on) -- needs TypeApplications Lang-Pragma import qualified Database.Esqueleto.Utils as E -- import Database.Esqueleto.Utils.TH -- avoids repetition of local definitions single :: (k,a) -> Map k a single = uncurry Map.singleton -- Button needed only here data ButtonAvsTest = BtnCheckLicences | BtnSynchLicences deriving (Enum, Eq, Ord, Bounded, Read, Show, Generic, Typeable) instance Universe ButtonAvsTest instance Finite ButtonAvsTest nullaryPathPiece ''ButtonAvsTest camelToPathPiece instance Button UniWorX ButtonAvsTest where btnLabel BtnCheckLicences = "Check all licences" -- could be msg btnLabel BtnSynchLicences = "Synchronize all licences" -- could be msg btnClasses BtnCheckLicences = [BCIsButton, BCPrimary] btnClasses BtnSynchLicences = [BCIsButton, BCDanger] -- END Button avsCardNoField :: Monad m => RenderMessage (HandlerSite m) FormMessage => Field m AvsCardNo avsCardNoField = convertField AvsCardNo avsCardNo textField avsInternalPersonalNoField :: Monad m => RenderMessage (HandlerSite m) FormMessage => Field m AvsInternalPersonalNo avsInternalPersonalNoField = convertField mkAvsInternalPersonalNo avsInternalPersonalNo textField makeAvsPersonForm :: Maybe AvsQueryPerson -> Form AvsQueryPerson makeAvsPersonForm tmpl = identifyForm FIDAvsQueryPerson . validateForm validateAvsQueryPerson $ \html -> flip (renderAForm FormStandard) html $ AvsQueryPerson <$> aopt avsCardNoField (fslI MsgAvsCardNo) (avsPersonQueryCardNo <$> tmpl) <*> aopt textField (fslI MsgAvsVersionNo) (avsPersonQueryVersionNo <$> tmpl) <*> aopt textField (fslI MsgAvsFirstName) (avsPersonQueryFirstName <$> tmpl) <*> aopt textField (fslI MsgAvsLastName) (avsPersonQueryLastName <$> tmpl) <*> aopt avsInternalPersonalNoField (fslI MsgAvsInternalPersonalNo) (avsPersonQueryInternalPersonalNo <$> tmpl) validateAvsQueryPerson :: FormValidator AvsQueryPerson Handler () validateAvsQueryPerson = do AvsQueryPerson{..} <- State.get guardValidation MsgAvsQueryEmpty $ is _Just avsPersonQueryCardNo || is _Just avsPersonQueryFirstName || is _Just avsPersonQueryLastName || is _Just avsPersonQueryInternalPersonalNo || is _Just avsPersonQueryVersionNo makeAvsStatusForm :: Maybe AvsQueryStatus -> Form AvsQueryStatus makeAvsStatusForm tmpl = identifyForm FIDAvsQueryStatus . validateForm validateAvsQueryStatus $ \html -> flip (renderAForm FormStandard) html $ parseAvsIds <$> areq textField (fslI MsgAvsPersonId) (unparseAvsIds <$> tmpl) where parseAvsIds :: Text -> AvsQueryStatus parseAvsIds txt = AvsQueryStatus $ Set.fromList ids where nonemptys = filter (not . Text.null) $ Text.strip <$> Text.split (==',') txt ids = catMaybes $ readMay <$> nonemptys unparseAvsIds :: AvsQueryStatus -> Text unparseAvsIds (AvsQueryStatus ids) = Text.intercalate ", " $ tshow <$> Set.toAscList ids validateAvsQueryStatus :: FormValidator AvsQueryStatus Handler () validateAvsQueryStatus = do AvsQueryStatus ids <- State.get guardValidation (MsgAvsQueryStatusInvalid $ tshow ids) $ not (null ids) avsLicenceOptions :: OptionList AvsLicence avsLicenceOptions = mkOptionList [ Option { optionDisplay = Text.singleton $ licence2char l , optionInternalValue = l , optionExternalValue = toJsonText l } | l <- universeF ] getAdminAvsR, postAdminAvsR :: Handler Html getAdminAvsR = postAdminAvsR postAdminAvsR = do mAvsQuery <- getsYesod $ view _appAvsQuery case mAvsQuery of Nothing -> return mempty Just AvsQuery{..} -> do ((presult, pwidget), penctype) <- runFormPost $ makeAvsPersonForm Nothing let procFormPerson fr = do addMessage Info $ text2Html $ "Query: " <> tshow (toJSON fr) res <- avsQueryPerson fr case res of Left err -> let msg = tshow err in return $ Just [whamlet|

Error:

#{msg}|] Right (AvsResponsePerson pns) -> return $ Just [whamlet|