From 5375ad26dd78220185f1ffe05222250c06dc1a0c Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 21 Nov 2015 21:41:38 +0100 Subject: Get next week birthdays and send an empty mail for the moment --- src/Model/Mail.hs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Model/Mail.hs (limited to 'src/Model/Mail.hs') diff --git a/src/Model/Mail.hs b/src/Model/Mail.hs new file mode 100644 index 0000000..e730e32 --- /dev/null +++ b/src/Model/Mail.hs @@ -0,0 +1,60 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Model.Mail + ( mailSubject + , mailBody + ) where + +import Data.Text (Text) +import qualified Data.Text as T + +import Model.Date +import Model.Birthdate + +mailSubject :: [Birthdate] -> Text +mailSubject birthdates = + let count = length birthdates + in T.concat + [ "Hey, " + , if count > 1 then "there are" else "there is" + , " " + , T.pack . show $ count + , " birthday" + , if count > 1 then "s" else "" + , " today!" + ] + +mailBody :: Date -> [Birthdate] -> Text +mailBody currentDate birthdates = + T.concat $ map (mailLine currentDate) (attachLines birthdates) + +attachLines :: [Birthdate] -> [(Line, Birthdate)] +attachLines birthdates = + let count = length birthdates + lineKind 1 = if count == 1 then SingleLine else FirstLine + lineKind line = if line == count then LastLine else MiddleLine + mapFst f (x, y) = (f x, y) + in map (mapFst lineKind) . zip [1..] $ birthdates + +data Line = + SingleLine + | FirstLine + | MiddleLine + | LastLine + deriving (Eq, Show) + +mailLine :: Date -> (Line, Birthdate) -> Text +mailLine currDate (line, birthdate) = + T.concat + [ case line of + MiddleLine -> ", " + LastLine -> " and " + _ -> "" + , fullname birthdate + , case line of + x | x `elem` [SingleLine, FirstLine] -> " is now " + _ -> " is " + , T.pack . show $ age currDate birthdate + , " years old" + , if line == LastLine then "." else "" + ] -- cgit v1.2.3