aboutsummaryrefslogtreecommitdiff
path: root/server/src/Job/WeeklyReport.hs
blob: 34bbd3ac09513ba06b2bced246b77e343bc70578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Job.WeeklyReport
  ( weeklyReport
  ) where

import           Data.Time.Clock        (UTCTime, getCurrentTime)

import           Conf                   (Conf)
import qualified Model.Query            as Query
import qualified Persistence.Income     as IncomePersistence
import qualified Persistence.Payment    as PaymentPersistence
import qualified Persistence.User       as UserPersistence
import qualified SendMail
import qualified View.Mail.WeeklyReport as WeeklyReport

weeklyReport :: Conf -> Maybe UTCTime -> IO UTCTime
weeklyReport conf mbLastExecution = do
  now <- getCurrentTime

  case mbLastExecution of
    Nothing ->
      return ()

    Just lastExecution -> do
      (weekPayments, paymentRange, preIncomeRepartition, postIncomeRepartition, weekIncomes, users) <- Query.run $ do
        users <- UserPersistence.list
        paymentRange <- PaymentPersistence.getRange
        weekPayments <- PaymentPersistence.listModifiedSince lastExecution
        weekIncomes <- IncomePersistence.listModifiedSince lastExecution
        (preIncomeRepartition, postIncomeRepartition) <-
          PaymentPersistence.getPreAndPostPaymentRepartition paymentRange users
        return (weekPayments, paymentRange, preIncomeRepartition, postIncomeRepartition, weekIncomes, users)

      _ <-
        SendMail.sendMail
          conf
          (WeeklyReport.mail conf users weekPayments preIncomeRepartition postIncomeRepartition (fst <$> paymentRange) weekIncomes lastExecution now)

      return ()

  return now