aboutsummaryrefslogtreecommitdiff
path: root/src/server/MonthlyPaymentJob.hs
blob: f5f68786f2d771807c62397cb050c03fafcb7954 (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
module MonthlyPaymentJob
  ( monthlyPaymentJobListener
  ) where

import Control.Monad.IO.Class (liftIO)

import Data.Time.Clock

import Database.Persist (entityVal, insert)

import Job (jobListener)

import Model.Database
import Model.Payment (getMonthlyPayments)
import Model.JobKind
import Model.Frequency

import Utils.Time (belongToCurrentMonth)

monthlyPaymentJobListener :: IO ()
monthlyPaymentJobListener =
  let lastExecutionTooOld = fmap not . belongToCurrentMonth
      runJob () = monthlyPaymentJob
      msDelay = 1000000 * 60 * 60
  in  jobListener MonthlyPaymentJob lastExecutionTooOld runJob msDelay

monthlyPaymentJob :: Persist ()
monthlyPaymentJob = do
  monthlyPayments <- map entityVal <$> getMonthlyPayments
  now <- liftIO $ getCurrentTime
  let punctualPayments = map (\p -> p { paymentFrequency = Punctual, paymentCreation = now }) monthlyPayments
  sequence_ $ map insert punctualPayments