-- SPDX-FileCopyrightText: 2022-24 Sarah Vaupel ,Steffen Jost ,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, postAdminAvsUserR , getProblemAvsSynchR, postProblemAvsSynchR , getProblemAvsErrorR ) 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 Handler.Utils.Users (getUserPrimaryCompany) import Handler.Utils.Company (switchAvsUserCompany) 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 exceptionWgt :: SomeException -> Widget exceptionWgt (SomeException e) = [whamlet|

Error:

#{tshow e}|] tryShow :: MonadCatch m => m Widget -> m Widget tryShow act = try act >>= \case Left err -> return $ exceptionWgt err Right res -> return res -- 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 = "Show all licence difference to current AVS" -- 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 AvsPersonId -> 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 :: AvsPersonId -> Text unparseAvsIds = tshow . avsPersonId validateAvsQueryStatus :: FormValidator AvsQueryStatus Handler () validateAvsQueryStatus = do AvsQueryStatus ids <- State.get guardValidation (MsgAvsQueryStatusInvalid $ tshow ids) $ not (null ids) makeAvsContactForm :: Maybe AvsPersonId -> 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 :: AvsPersonId -> Text unparseAvsIds = tshow . avsPersonId --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