{-|
Module      : Gargantext.Database.Action.Mail
Description :
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX


-}

{-# LANGUAGE TemplateHaskell    #-}

module Gargantext.Database.Action.Mail
  where

import Control.Lens (view)
import Gargantext.Core.Mail (mail, MailModel(..))
import Gargantext.Core.Mail.Types (mailSettings)
import Gargantext.Core.Types.Individu (User(..))
import Gargantext.Database.Action.User
import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
import Gargantext.Database.Schema.User
import Gargantext.Prelude
import Gargantext.System.Logging (MonadLogger, LogLevel(..), logLocM)

------------------------------------------------------------------------

sendMail :: (HasNodeError err, IsDBCmdExtra env err m, MonadLogger m)
         => User -> m ()
sendMail u = do
  cfg       <- view $ mailSettings
  userLight <- runDBQuery $ getUserLightDB u
  $(logLocM) DEBUG $ "[sendMail] sending mail to user " <> show userLight
  mail cfg (MailInfo { mailInfo_username = userLight_username userLight
                     , mailInfo_address  = userLight_email    userLight
                     }
           )
