module Jobs.Handler.Invitation
  ( dispatchJobInvitation
  ) where

import Import
import Utils.Lens
import Handler.Utils.Mail

import qualified Data.CaseInsensitive as CI
import Text.Hamlet


dispatchJobInvitation :: UserId
                      -> UserEmail
                      -> Text
                      -> Text
                      -> Html
                      -> Handler ()
dispatchJobInvitation jInviter jInvitee jInvitationUrl jInvitationSubject jInvitationExplanation = do
  mInviter <- runDB $ get jInviter

  whenIsJust mInviter $ \jInviter' -> mailT def $ do
    _mailTo .= [Address Nothing $ CI.original jInvitee]
    replaceMailHeader "Reply-To" . Just . renderAddress $ userAddress jInviter'
    replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
    replaceMailHeader "Subject" $ Just jInvitationSubject
    addPart ($(ihamletFile "templates/mail/invitation.hamlet") :: HtmlUrlI18n UniWorXMessage (Route UniWorX))
