-- 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 , getAdminAvsUserR , getProblemAvsSynchR, postProblemAvsSynchR ) where import Import import qualified Control.Monad.State.Class as State -- import Data.Aeson (encode) import qualified Data.Aeson.Encode.Pretty as Pretty 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 Handler.Utils.Qualification 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 only needed in AVS TEST; further buttons see below data ButtonAvsTest = BtnCheckLicences -- | BtnSynchLicences deriving (Enum, Eq, Ord, Bounded, Read, Show, Generic) 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 = mapMaybe 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) makeAvsContactForm :: Maybe AvsQueryContact -> Form AvsQueryContact makeAvsContactForm tmpl = identifyForm FIDAvsQueryContact . validateForm validateAvsQueryContact $ \html -> flip (renderAForm FormStandard) html $ parseAvsIds <$> areq textField (fslI MsgAvsPersonId) (unparseAvsIds <$> tmpl) -- consider using cfAnySeparatedSet here where parseAvsIds :: Text -> AvsQueryContact parseAvsIds txt = AvsQueryContact $ Set.fromList ids where nonemptys = filter (not . Text.null) $ Text.strip <$> Text.split (==',') txt ids = mapMaybe (fmap AvsObjPersonId . readMay) nonemptys unparseAvsIds :: AvsQueryContact -> Text unparseAvsIds (AvsQueryContact ids) = Text.intercalate ", " $ tshow <$> Set.toAscList ids validateAvsQueryContact :: FormValidator AvsQueryContact Handler () validateAvsQueryContact = do AvsQueryContact 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 mbAvsConf <- getsYesod $ view _appAvsConf let avsWgt = [whamlet| $maybe avsConf <- mbAvsConf AVS Konfiguration ist #{decodeUtf8 (avsUser avsConf)}@#{avsHost avsConf}:#{avsPort avsConf} $nothing AVS nicht konfiguriert! |] mAvsQuery <- getsYesod $ view _appAvsQuery case mAvsQuery of Nothing -> siteLayoutMsg MsgMenuAvs [whamlet|Error: AVS interface configuration is incomplete.|] -- should never occur after initilisation 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|