aboutsummaryrefslogtreecommitdiff
path: root/src/Notification.hs
blob: 60062cc7fc5445448ed52d2f30fab2a8245c031c (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.Event (Event, filterBirthdayAt, filterBirthdayInside)
import Model.Mail (mailSubject, mailBody)
import Model.Conf

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

filterBirthdaysNextWeek :: Conf -> SuccessiveDates -> [Event] -> IO [Event]
filterBirthdaysNextWeek conf nextWeek events =
  (\currentDayOfWeek ->
    if T.toLower currentDayOfWeek == T.toLower (dayForNextWeekNotification conf)
      then filterBirthdayInside nextWeek events
      else []
  ) <$> formatCurrentLocale "%A"