aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 0aa2910fe9f98e5dd68a744e230e2a059c71ce7b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE OverloadedStrings #-}

module Main
  ( main
  ) where

import System.IO (stderr)

import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T

import Date (getCurrentDate)
import Birthdate (Birthdate, filterBirthdayAt)
import BirthdateParser (parseBirthdates)
import Mail (mailSubject, mailBody)
import SendMail (sendMail)
import Config

birthdatePath :: FilePath
birthdatePath = "birthdates.csv"

configPath :: FilePath
configPath = "config.txt"

main :: IO ()
main = do
  eitherBirthdates <- parseBirthdates <$> T.readFile birthdatePath
  eitherConfig <- getConfig configPath
  case (eitherBirthdates, eitherConfig) of
    (Left err, _) ->
      birthdateError err
    (_, Left err) ->
      configError err
    (Right birthdates, Right config) ->
      sendNotificationForBirthdayToday birthdates config

birthdateError :: Text -> IO ()
birthdateError err =
  T.hPutStr stderr $
    T.concat
      [ "Error while parsing file "
      , T.pack birthdatePath
      , ":\n"
      , err
      ]

configError :: Text -> IO ()
configError err =
  T.hPutStr stderr $
    T.concat
      [ "Error while parsing config file "
      , T.pack configPath
      , ":\n"
      , err
      ]

sendNotificationForBirthdayToday :: [Birthdate] -> Config -> IO ()
sendNotificationForBirthdayToday birthdates config = do
  currentDate <- getCurrentDate
  let birthdays = filterBirthdayAt currentDate birthdates
  if not (null birthdays)
    then
      sendMail
        (mailTo config)
        (mailFrom config)
        (mailSubject birthdays)
        (mailBody currentDate birthdays)
    else
      return ()