105 lines
4.9 KiB
Haskell
105 lines
4.9 KiB
Haskell
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-} -- ihamletFile discards do results
|
|
|
|
module Jobs.Handler.SendNotification.Qualification
|
|
( dispatchNotificationQualificationExpiry
|
|
, dispatchNotificationQualificationRenewal
|
|
) where
|
|
|
|
import Import
|
|
|
|
import Utils.Print
|
|
import Handler.Utils
|
|
import Handler.Utils.Users
|
|
import Jobs.Handler.SendNotification.Utils
|
|
|
|
import qualified Data.ByteString.Lazy as LBS
|
|
|
|
-- import Handler.Info (FAQItem(..))
|
|
import qualified Data.CaseInsensitive as CI
|
|
import Text.Hamlet
|
|
|
|
-- import qualified Database.Esqueleto.Experimental as E
|
|
-- import qualified Database.Esqueleto.Utils as E
|
|
|
|
|
|
dispatchNotificationQualificationExpiry :: QualificationId -> Day -> UserId -> Handler ()
|
|
dispatchNotificationQualificationExpiry nQualification _nExpiry jRecipient = userMailT jRecipient $ do
|
|
(User{..}, Qualification{..}, Entity _ QualificationUser{..}) <- liftHandler . runDB $ (,,)
|
|
<$> getJust jRecipient
|
|
<*> getJust nQualification
|
|
<*> getJustBy (UniqueQualificationUser nQualification jRecipient)
|
|
|
|
let qname = CI.original qualificationName
|
|
$logDebugS "LMS" $ "Notify " <> tshow jRecipient <> " about expiry of qualification " <> qname
|
|
replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
|
|
setSubjectI $ MsgMailSubjectQualificationExpiry qname
|
|
|
|
editNotifications <- mkEditNotifications jRecipient
|
|
|
|
addHtmlMarkdownAlternatives $(ihamletFile "templates/mail/qualificationExpiry.hamlet")
|
|
|
|
|
|
-- NOTE: qualificationRenewal expects that LmsUser already exists for recipient
|
|
dispatchNotificationQualificationRenewal :: QualificationId -> UserId -> Handler ()
|
|
dispatchNotificationQualificationRenewal nQualification jRecipient = do
|
|
(recipient@User{..}, Qualification{..}, Entity _ QualificationUser{..}, Entity _ LmsUser{..}) <- runDB $ (,,,)
|
|
<$> getJust jRecipient
|
|
<*> getJust nQualification
|
|
<*> getJustBy (UniqueQualificationUser nQualification jRecipient)
|
|
<*> getJustBy (UniqueLmsQualificationUser nQualification jRecipient)
|
|
let entRecipient = Entity jRecipient recipient
|
|
qname = CI.original qualificationName
|
|
-- content = $(i18nWidgetFile "qualification/renewal")
|
|
$logDebugS "LMS" $ "Notify " <> tshow jRecipient <> " for renewal of qualification " <> qname
|
|
|
|
now <- liftIO getCurrentTime
|
|
letterDate <- formatTimeUser SelFormatDate now $ Just entRecipient
|
|
|
|
let prepAddress upa = userDisplayName : (upa & html2textlines) -- TODO: use supervisor's address
|
|
pdfMeta = mkMeta
|
|
[ toMeta "date" letterDate
|
|
, toMeta "lang" $ selectDeEn userLanguages -- select German or English, see Utils.Lang
|
|
, toMeta "login" (lmsUserIdent & getLmsIdent)
|
|
, toMeta "pin" lmsUserPin
|
|
, toMeta "recipient" userDisplayName
|
|
, mbMeta "address" (prepAddress <$> userPostAddress)
|
|
]
|
|
pdfRenewal pdfMeta >>= \case
|
|
Left err -> do
|
|
let msg = "Notify " <> tshow jRecipient <> " PDF generation failed with error: " <> err
|
|
$logErrorS "LMS" msg
|
|
error $ unpack msg
|
|
Right pdf | userPrefersEmail recipient -> userMailT jRecipient $ do
|
|
|
|
replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
|
|
setSubjectI $ MsgMailSubjectQualificationRenewal qname
|
|
|
|
editNotifications <- mkEditNotifications jRecipient -- TODO: add to hamlet file again
|
|
-- let msgrenewal = $(i18nHamletFile "qualification/renewal") -- :: HtmlUrlI18n (SomeMessage UniWorX) (Route UniWorX)
|
|
-- addHtmlMarkdownAlternatives' msgrenewal
|
|
|
|
encryptPDF "tomatenmarmelade" pdf >>= \case
|
|
Left err -> do
|
|
let msg = "Notify " <> tshow jRecipient <> " PDF encryption failed with error: " <> err
|
|
$logErrorS "LMS" msg
|
|
error $ unpack msg
|
|
Right pdffile -> do
|
|
addPart (File { fileTitle = "RenewalPinLetter.pdf" -- TODO: better file title!
|
|
, fileModified = now
|
|
, fileContent = Just $ yield $ LBS.toStrict pdffile
|
|
} :: PureFile)
|
|
-- TODO: this is just a dummy to continue while i18nHamletFile usage is unclear
|
|
addHtmlMarkdownAlternatives $(ihamletFile "templates/mail/qualificationRenewal.hamlet")
|
|
|
|
Right pdf | otherwise -> do
|
|
let printJobName = mempty --TODO
|
|
printSender = Nothing --TODO
|
|
runDB (sendLetter printJobName pdf printSender (Just jRecipient) Nothing (Just nQualification)) >>= \case
|
|
-- lprPDF printJobName pdf >>= \case
|
|
Left err -> do
|
|
let msg = "Notify " <> tshow jRecipient <> " PDF printing to send letter failed with error: " <> err
|
|
$logErrorS "LMS" msg
|
|
error $ unpack msg
|
|
Right (msg,_)
|
|
| null msg -> return ()
|
|
| otherwise -> $logWarnS "LMS" $ "PDF printing to send letter with lpr returned ExitSucces and the following message: " <> msg |