aboutsummaryrefslogtreecommitdiff
path: root/src/server/Job/Model.hs
diff options
context:
space:
mode:
authorJoris2016-11-13 00:49:32 +0100
committerJoris2016-11-13 00:49:32 +0100
commit86a96decdb8892b10c5314eb916ef15a64204450 (patch)
tree6f41742d0466f77948680964188144fbff036902 /src/server/Job/Model.hs
parentbf6a0a0b32a7efb88f75c2e89b84d6907aeb10bc (diff)
downloadbudget-86a96decdb8892b10c5314eb916ef15a64204450.tar.gz
budget-86a96decdb8892b10c5314eb916ef15a64204450.tar.bz2
budget-86a96decdb8892b10c5314eb916ef15a64204450.zip
Send weekly activity at start of week about previous week
Diffstat (limited to 'src/server/Job/Model.hs')
-rw-r--r--src/server/Job/Model.hs33
1 files changed, 33 insertions, 0 deletions
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]