From b977bb5ba3d5ad8f8008aa4ceb60d1f988a82a0a Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 22 Nov 2015 01:56:20 +0100 Subject: Generate a mail that contains both birthdays today and birthdays next week --- src/Model/Mail.hs | 77 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 17 deletions(-) (limited to 'src/Model/Mail.hs') diff --git a/src/Model/Mail.hs b/src/Model/Mail.hs index e730e32..19ebaeb 100644 --- a/src/Model/Mail.hs +++ b/src/Model/Mail.hs @@ -11,22 +11,62 @@ import qualified Data.Text as T import Model.Date import Model.Birthdate -mailSubject :: [Birthdate] -> Text -mailSubject birthdates = +data Event = + Today + | NextWeek + deriving (Eq, Show) + +mailSubject :: [Birthdate] -> [Birthdate] -> Text +mailSubject birthdaysToday birthdaysNextWeek = + T.concat + [ "Hey, " + , if length birthdaysToday > 0 + then mailSubjectSentence Today birthdaysToday + else "" + , if length birthdaysNextWeek > 0 + then + T.concat + [ if length birthdaysToday > 0 then " and " else "" + , mailSubjectSentence NextWeek birthdaysNextWeek + ] + else + "" + , "!" + ] + +mailSubjectSentence :: Event -> [Birthdate] -> Text +mailSubjectSentence event birthdates = let count = length birthdates in T.concat - [ "Hey, " - , if count > 1 then "there are" else "there is" + [ case event of + Today -> if count > 1 then "there are" else "there is" + NextWeek -> "there will be" , " " , T.pack . show $ count , " birthday" , if count > 1 then "s" else "" - , " today!" + , " " + , if event == Today then "today" else "next week" ] -mailBody :: Date -> [Birthdate] -> Text -mailBody currentDate birthdates = - T.concat $ map (mailLine currentDate) (attachLines birthdates) +mailBody :: Date -> [Birthdate] -> [Birthdate] -> Text +mailBody currentDate birthdaysToday birthdaysNextWeek = + T.concat + [ if length birthdaysToday > 0 + then mailBodySentence Today currentDate birthdaysToday + else "" + , if length birthdaysNextWeek > 0 + then + T.concat + [ if length birthdaysToday > 0 then " " else "" + , mailBodySentence NextWeek currentDate birthdaysNextWeek + ] + else "" + ] + +mailBodySentence :: Event -> Date -> [Birthdate] -> Text +mailBodySentence event currentDate birthdates = + T.concat $ map (mailBodyPart event currentDate) (attachLines birthdates) attachLines :: [Birthdate] -> [(Line, Birthdate)] attachLines birthdates = @@ -43,18 +83,21 @@ data Line = | LastLine deriving (Eq, Show) -mailLine :: Date -> (Line, Birthdate) -> Text -mailLine currDate (line, birthdate) = +mailBodyPart :: Event -> Date -> (Line, Birthdate) -> Text +mailBodyPart event currDate (line, birthdate) = T.concat [ case line of - MiddleLine -> ", " - LastLine -> " and " - _ -> "" + x | x `elem` [SingleLine, FirstLine] -> + if event == Today then "Today, " else "Next week, " + MiddleLine -> + ", " + LastLine -> + " and " + _ -> + "" , fullname birthdate - , case line of - x | x `elem` [SingleLine, FirstLine] -> " is now " - _ -> " is " + , if event == Today then " is " else " will be " , T.pack . show $ age currDate birthdate , " years old" - , if line == LastLine then "." else "" + , if line == SingleLine || line == LastLine then "." else "" ] -- cgit v1.2.3