aboutsummaryrefslogtreecommitdiff
path: root/server/src/Job/WeeklyReport.hs
diff options
context:
space:
mode:
authorJoris2019-11-24 16:19:53 +0100
committerJoris2019-11-24 16:19:53 +0100
commit54628c70cb33de5e4309c35b9f6b57bbe9f7a07b (patch)
tree57e331cadfdf81b5598d21f76302f5269fd58344 /server/src/Job/WeeklyReport.hs
parent3c67fcf1d524811a18f0c4db3ef6eed1270b9a12 (diff)
downloadbudget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.tar.gz
budget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.tar.bz2
budget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.zip
Compute cumulative income with a DB query
Diffstat (limited to 'server/src/Job/WeeklyReport.hs')
-rw-r--r--server/src/Job/WeeklyReport.hs17
1 files changed, 14 insertions, 3 deletions
diff --git a/server/src/Job/WeeklyReport.hs b/server/src/Job/WeeklyReport.hs
index 34bbd3a..16be396 100644
--- a/server/src/Job/WeeklyReport.hs
+++ b/server/src/Job/WeeklyReport.hs
@@ -2,8 +2,11 @@ module Job.WeeklyReport
( weeklyReport
) where
+import qualified Data.Map as M
import Data.Time.Clock (UTCTime, getCurrentTime)
+import Common.Model (User (..))
+
import Conf (Conf)
import qualified Model.Query as Query
import qualified Persistence.Income as IncomePersistence
@@ -21,19 +24,27 @@ weeklyReport conf mbLastExecution = do
return ()
Just lastExecution -> do
- (weekPayments, paymentRange, preIncomeRepartition, postIncomeRepartition, weekIncomes, users) <- Query.run $ do
+ (weekPayments, cumulativeIncome, preIncomeRepartition, postIncomeRepartition, weekIncomes, users) <- Query.run $ do
users <- UserPersistence.list
paymentRange <- PaymentPersistence.getRange
+ incomeDefinedForAll <- IncomePersistence.definedForAll (_user_id <$> users)
+ cumulativeIncome <-
+ case (incomeDefinedForAll, paymentRange) of
+ (Just incomeStart, Just (paymentStart, paymentEnd)) ->
+ IncomePersistence.getCumulativeIncome (max incomeStart paymentStart) paymentEnd
+
+ _ ->
+ return M.empty
weekPayments <- PaymentPersistence.listModifiedSince lastExecution
weekIncomes <- IncomePersistence.listModifiedSince lastExecution
(preIncomeRepartition, postIncomeRepartition) <-
PaymentPersistence.getPreAndPostPaymentRepartition paymentRange users
- return (weekPayments, paymentRange, preIncomeRepartition, postIncomeRepartition, weekIncomes, users)
+ return (weekPayments, cumulativeIncome, preIncomeRepartition, postIncomeRepartition, weekIncomes, users)
_ <-
SendMail.sendMail
conf
- (WeeklyReport.mail conf users weekPayments preIncomeRepartition postIncomeRepartition (fst <$> paymentRange) weekIncomes lastExecution now)
+ (WeeklyReport.mail conf users weekIncomes weekPayments cumulativeIncome preIncomeRepartition postIncomeRepartition lastExecution now)
return ()