From 86a96decdb8892b10c5314eb916ef15a64204450 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 13 Nov 2016 00:49:32 +0100 Subject: Send weekly activity at start of week about previous week --- src/server/Job/Model.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/server/Job/Model.hs (limited to 'src/server/Job/Model.hs') diff --git a/src/server/Job/Model.hs b/src/server/Job/Model.hs new file mode 100644 index 0000000..cd7297a --- /dev/null +++ b/src/server/Job/Model.hs @@ -0,0 +1,33 @@ +module Job.Model + ( getLastExecution + , actualizeLastExecution + , actualizeLastCheck + ) where + +import Control.Monad.IO.Class (liftIO) + +import Data.Time.Clock (UTCTime, getCurrentTime) +import Data.Maybe (isJust) + +import Database.Persist + +import Model.Database + +import Job.Kind + +getLastExecution :: Kind -> Persist (Maybe UTCTime) +getLastExecution kind = do + mbJob <- fmap entityVal <$> selectFirst [JobKind ==. kind] [] + return (mbJob >>= jobLastExecution) + +actualizeLastExecution :: Kind -> UTCTime -> Persist () +actualizeLastExecution kind time = do + jobKindDefined <- isJust <$> selectFirst [JobKind ==. kind] [] + if jobKindDefined + then updateWhere [JobKind ==. kind] [JobLastExecution =. Just time] + else insert (Job kind (Just time) (Just time)) >> return () + +actualizeLastCheck :: Kind -> Persist () +actualizeLastCheck kind = do + now <- liftIO getCurrentTime + updateWhere [JobKind ==. kind] [JobLastCheck =. Just now] -- cgit v1.2.3