Email applicants when resolving their applications

This commit is contained in:
Saku Laesvuori 2023-09-28 09:17:41 +03:00
parent 8cda69d30d
commit 49536ad05b
Signed by: slaesvuo
GPG Key ID: 257D284A2A1D3A32
2 changed files with 32 additions and 11 deletions

View File

@ -31,7 +31,7 @@ import Database.Persist (Entity, entityVal, entityKey, get, (=.))
import Datarekisteri.Core.Types import Datarekisteri.Core.Types
import Datarekisteri.Backend.DB import Datarekisteri.Backend.DB
import Datarekisteri.Backend.DB.Queries import Datarekisteri.Backend.DB.Queries
import Datarekisteri.Backend.Email (sendVerificationEmail) import Datarekisteri.Backend.Email (sendVerificationEmail, sendApplicationAcceptedEmail, sendApplicationRejectedEmail)
import Datarekisteri.Backend.Types import Datarekisteri.Backend.Types
import Datarekisteri.Backend.Utils import Datarekisteri.Backend.Utils
import qualified "base64" Data.ByteString.Base64 as B64 (encodeBase64) import qualified "base64" Data.ByteString.Base64 as B64 (encodeBase64)
@ -193,15 +193,25 @@ makeNewKey (KeyData {..}) user = do
, dBKeyIsPrimaryEncryptionKey = True , dBKeyIsPrimaryEncryptionKey = True
} }
acceptApplication :: (MonadDB m, MonadTime m) => UserID -> m Unit acceptApplication :: (MonadDB m, MonadTime m, MonadError GQLError m, MonadEmail m) => UserID -> m Unit
acceptApplication user = void $ do acceptApplication user = void $ do
time <- currentTime maybeEmail <- getUserEmail user
markAsAccepted user time case maybeEmail of
Nothing -> throwError $ "No valid application for " <> show user <> "!"
Just email -> do
time <- currentTime
markAsAccepted user time
void $ sendApplicationAcceptedEmail $ dBEmailEmail $ entityVal email
rejectApplication :: (MonadDB m, MonadTime m) => UserID -> m Unit rejectApplication :: (MonadDB m, MonadTime m, MonadError GQLError m, MonadEmail m) => UserID -> m Unit
rejectApplication user = void $ do rejectApplication user = void $ do
time <- currentTime maybeEmail <- getUserEmail user
markAsRejected user time case maybeEmail of
Nothing -> throwError $ "No valid application for " <> show user <> "!"
Just email -> do
time <- currentTime
markAsRejected user time
void $ sendApplicationRejectedEmail $ dBEmailEmail $ entityVal email
resolveQuery :: (MonadRequest m, MonadDB m, MonadError GQLError m, MonadPermissions m) => Query m resolveQuery :: (MonadRequest m, MonadDB m, MonadError GQLError m, MonadPermissions m) => Query m
resolveQuery = Query resolveQuery = Query

View File

@ -8,11 +8,22 @@ import Data.Text.Lazy (fromStrict)
import Datarekisteri.Core.Types import Datarekisteri.Core.Types
import Network.Mail.Mime import Network.Mail.Mime
sendVerificationEmail :: MonadEmail m => Email -> Text -> m () sendDatarekisteriEmail :: MonadEmail m => Email -> Text -> Text -> m ()
sendVerificationEmail to secret = do sendDatarekisteriEmail to subject content = do
from <- fromAddress from <- fromAddress
sendEmail $ simpleMail' (Address Nothing $ renderEmail to) from sendEmail $ simpleMail' (Address Nothing $ renderEmail to) from subject $ fromStrict content
"Sähköpostin vahvistuskoodi" $ fromStrict $
sendVerificationEmail :: MonadEmail m => Email -> Text -> m ()
sendVerificationEmail to secret = sendDatarekisteriEmail to "Sähköpostin vahvistuskoodi" $
"Vahvista sähköpostisi syöttämällä alla oleva koodi rekisteriin:\n\n" "Vahvista sähköpostisi syöttämällä alla oleva koodi rekisteriin:\n\n"
<> secret <> "\n\n" <> secret <> "\n\n"
<> "Mikäli et odottanut tätä viestiä, voit jättää sen turvallisesti huomiotta." <> "Mikäli et odottanut tätä viestiä, voit jättää sen turvallisesti huomiotta."
sendApplicationAcceptedEmail :: MonadEmail m => Email -> m ()
sendApplicationAcceptedEmail to = sendDatarekisteriEmail to "Jäsenhakemus hyväksytty" $
"Jäsenhakemuksesi Datat RY:lle on hyväksytty. Olet nyt yhdistyksen jäsen."
sendApplicationRejectedEmail :: MonadEmail m => Email -> m ()
sendApplicationRejectedEmail to = sendDatarekisteriEmail to "Jäsenhakemus hylätty" $
"Jäsenhakemuksesi Datat RY:lle on hylätty. Voit halutessasi kysyä hakemuksen"
<> " hylkäämisen syistä yhdistyksen hallitukselta <hallitus@datat.fi>."