aboutsummaryrefslogtreecommitdiff
path: root/src/Notification.hs
blob: 6e6677e29e4e23f55f038effc07aaf8572d1c9c9 (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
{-# LANGUAGE OverloadedStrings #-}

module Notification
  ( notifyTodayAndNextWeek
  ) where

import qualified Data.Text as T

import SendMail (sendMail)
import Time (formatCurrentLocale)

import Model.Date (getCurrentDate, getNextWeek, SuccessiveDates)
import Model.Birthdate (Birthdate, filterBirthdayAt, filterBirthdayInside)
import Model.Mail (mailSubject, mailBody)
import Model.Config

notifyTodayAndNextWeek :: [Birthdate] -> Config -> IO ()
notifyTodayAndNextWeek birthdates config = do
  currentDate <- getCurrentDate
  let birthdaysToday = filterBirthdayAt currentDate birthdates
  nextWeek <- getNextWeek
  birthdaysNextWeek <- filterBirthdaysNextWeek config nextWeek birthdates
  if length birthdaysToday > 0 || length birthdaysNextWeek > 0
    then
      sendMail
        (mailTo config)
        (mailFrom config)
        (mailSubject birthdaysToday birthdaysNextWeek)
        (mailBody currentDate nextWeek birthdaysToday birthdaysNextWeek)
    else
      return ()

filterBirthdaysNextWeek :: Config -> SuccessiveDates -> [Birthdate] -> IO [Birthdate]
filterBirthdaysNextWeek config nextWeek birthdates =
  (\currentDayOfWeek ->
    if T.toLower currentDayOfWeek == T.toLower (dayForNextWeekNotification config)
      then filterBirthdayInside nextWeek birthdates
      else []
  ) <$> formatCurrentLocale "%A"