aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorJoris2019-11-03 15:47:11 +0100
committerJoris2019-11-03 15:47:14 +0100
commit0f85cbd8ee736b1996e3966bac1f5e47ed7d27a9 (patch)
tree661049f0876e097c739bd65579ca550a88945796 /server
parent182f3d3fea9985c0e403087fe253981c68e57102 (diff)
Fetch the first payment date instead of every payment to get cumulative income
Diffstat (limited to 'server')
-rw-r--r--server/src/Controller/Income.hs4
-rw-r--r--server/src/Persistence/Payment.hs16
2 files changed, 18 insertions, 2 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index 4a41bd3..127e3b3 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -37,11 +37,11 @@ list page perPage =
count <- IncomePersistence.count
users <- UserPersistence.list
- allPayments <- PaymentPersistence.listPunctual -- TODO: get first payment defined for all
+ firstPayment <- PaymentPersistence.firstPunctualDay
allIncomes <- IncomePersistence.listAll
let since =
- CM.useIncomesFrom (map _user_id users) allIncomes allPayments
+ CM.useIncomesFrom (map _user_id users) allIncomes firstPayment
let byUser =
case since of
diff --git a/server/src/Persistence/Payment.hs b/server/src/Persistence/Payment.hs
index bcd7eb8..eb238d4 100644
--- a/server/src/Persistence/Payment.hs
+++ b/server/src/Persistence/Payment.hs
@@ -1,6 +1,7 @@
module Persistence.Payment
( Payment(..)
, find
+ , firstPunctualDay
, listActive
, listPunctual
, listActiveMonthlyOrderedByName
@@ -60,6 +61,21 @@ find paymentId =
SQLite.query conn "SELECT * FROM payment WHERE id = ?" (Only paymentId)
)
+data DayRow = DayRow Day
+
+instance FromRow DayRow where
+ fromRow = DayRow <$> SQLite.field
+
+firstPunctualDay :: Query (Maybe Day)
+firstPunctualDay =
+ Query (\conn -> do
+ fmap (\(DayRow d) -> d) . listToMaybe <$>
+ SQLite.query
+ conn
+ "SELECT date FROM payment WHERE frequency = ? AND deleted_at IS NULL ORDER BY date LIMIT 1"
+ (Only (FrequencyField Punctual))
+ )
+
listActive :: Query [Payment]
listActive =
Query (\conn -> do