aboutsummaryrefslogtreecommitdiff
path: root/src/Notification.hs
blob: e5abe45d2bfd196259fc2f8679791a37876cf1d8 (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
41
42
43
44
45
{-# LANGUAGE OverloadedStrings #-}

module Notification
  ( todayAndNextWeek
  ) where

import qualified Data.Text as T

import SendMail (sendMail)
import Time (formatCurrentLocale)

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

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

filterBirthdaysToday :: [Birthdate] -> IO [Birthdate]
filterBirthdaysToday birthdates = do
  currentDate <- getCurrentDate
  return (filterBirthdayAt currentDate birthdates)

filterBirthdaysNextWeek :: [Birthdate] -> Config -> IO [Birthdate]
filterBirthdaysNextWeek birthdates config = do
  currentDayOfWeek <- formatCurrentLocale "%A"
  if T.toLower currentDayOfWeek == T.toLower (dayForNextWeekNotification config)
    then do
      (begin, end) <- getNextWeek
      return (filterBirthdayBetween begin end birthdates)
    else
      return []