2019-10-22 01:01:15 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2021-07-12 11:29:40 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-10-22 01:01:15 +00:00
|
|
|
module Util where
|
|
|
|
|
|
|
|
import Opaleye
|
|
|
|
|
2021-07-12 11:29:40 +00:00
|
|
|
import Data.ByteString.Lazy (fromStrict)
|
|
|
|
|
2019-12-12 02:15:56 +00:00
|
|
|
import Data.Maybe (fromMaybe)
|
2019-10-22 01:01:15 +00:00
|
|
|
|
|
|
|
import Data.Profunctor.Product.Default (Default)
|
|
|
|
|
2021-07-12 11:29:40 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
|
|
|
import qualified Data.Text.Encoding as E
|
|
|
|
|
2021-02-01 01:25:08 +00:00
|
|
|
import Database.PostgreSQL.Simple
|
|
|
|
|
|
|
|
import Control.Monad (void)
|
2021-07-12 11:29:40 +00:00
|
|
|
import Control.Monad.Reader (asks)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
|
|
|
|
import Network.Mail.Mime
|
|
|
|
|
|
|
|
import System.Directory (doesFileExist)
|
2021-02-01 01:25:08 +00:00
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Model
|
|
|
|
|
2021-07-12 11:29:40 +00:00
|
|
|
import Types
|
|
|
|
|
2019-10-22 01:01:15 +00:00
|
|
|
printSql :: Default Unpackspec a a => Select a -> IO ()
|
2019-12-12 02:15:56 +00:00
|
|
|
printSql = putStrLn . fromMaybe "Empty query" . showSqlForPostgres
|
2021-02-01 01:25:08 +00:00
|
|
|
|
|
|
|
initDB :: Connection -> IO ()
|
|
|
|
initDB conn = do
|
|
|
|
execute_ conn initAvatar
|
|
|
|
execute_ conn initUser
|
|
|
|
execute_ conn initProduct
|
|
|
|
execute_ conn initToken
|
|
|
|
execute_ conn initAuthData
|
|
|
|
execute_ conn initAmount
|
|
|
|
execute_ conn initJournal
|
|
|
|
execute_ conn initRole
|
|
|
|
execute_ conn initUserToRole
|
|
|
|
void $ runInsertInitialRoles conn
|
2021-06-24 04:33:05 +00:00
|
|
|
|
|
|
|
-- This is only a dummy function.
|
|
|
|
-- TODO: Replace with proper translation function(s)!
|
|
|
|
__ = id
|
2021-07-12 11:29:40 +00:00
|
|
|
|
|
|
|
sendUserNotification
|
|
|
|
:: UserDetails -- ^ The recipient
|
|
|
|
-> T.Text -- ^ The mail subject
|
|
|
|
-> T.Text -- ^ The mail body
|
|
|
|
-> MateHandler ()
|
|
|
|
sendUserNotification recipient subject message =
|
|
|
|
case userDetailsEmail recipient of
|
|
|
|
Just email ->
|
|
|
|
sendNotification (Mail
|
|
|
|
{ mailFrom = Address Nothing "noreply"
|
|
|
|
, mailTo =
|
|
|
|
[ Address
|
|
|
|
(Just $ userDetailsIdent recipient)
|
|
|
|
(email)
|
|
|
|
]
|
|
|
|
, mailCc = []
|
|
|
|
, mailBcc = []
|
|
|
|
, mailHeaders = [("Subject", subject)]
|
|
|
|
, mailParts =
|
|
|
|
[[ Part
|
|
|
|
{ partType = "text/plain; charset=utf-8"
|
|
|
|
, partEncoding = None
|
|
|
|
, partDisposition = DefaultDisposition
|
|
|
|
, partHeaders = []
|
|
|
|
, partContent = PartContent (fromStrict $ E.encodeUtf8 message)
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Nothing ->
|
|
|
|
return ()
|
|
|
|
|
|
|
|
sendNotification mail = do
|
|
|
|
sendmail <- asks rsSendmailPath
|
|
|
|
liftIO $ do
|
|
|
|
existence <- doesFileExist sendmail
|
|
|
|
if existence
|
|
|
|
then
|
|
|
|
renderSendMailCustom sendmail [] mail
|
|
|
|
else
|
|
|
|
print "Warning: sending notification failed: Sendmail not present!"
|