aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Income.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Controller/Income.hs')
-rw-r--r--server/src/Controller/Income.hs28
1 files changed, 10 insertions, 18 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index 75d0133..784a2db 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -13,7 +13,7 @@ import qualified Network.HTTP.Types.Status as Status
import Web.Scotty hiding (delete)
import Common.Model (CreateIncomeForm (..),
- EditIncomeForm (..), Income (..),
+ EditIncomeForm (..),
IncomeHeader (..), IncomeId,
IncomePage (..), User (..))
@@ -21,7 +21,6 @@ import qualified Controller.Helper as ControllerHelper
import Model.CreateIncome (CreateIncome (..))
import Model.EditIncome (EditIncome (..))
import qualified Model.Query as Query
-import qualified Payer as Payer
import qualified Persistence.Income as IncomePersistence
import qualified Persistence.Payment as PaymentPersistence
import qualified Persistence.User as UserPersistence
@@ -36,26 +35,19 @@ list page perPage =
count <- IncomePersistence.count
users <- UserPersistence.list
- paymentRange <- PaymentPersistence.getRange
- allIncomes <- IncomePersistence.listAll -- TODO optimize
-
- let since =
- Payer.useIncomesFrom (map _user_id users) allIncomes (fst <$> paymentRange)
+ let userIds = _user_id <$> users
- let byUser =
- case since of
- Just s ->
- M.fromList . flip map users $ \user ->
- ( _user_id user
- , Payer.cumulativeIncomesSince currentTime s $
- filter ((==) (_user_id user) . _income_userId) allIncomes
- )
+ paymentRange <- PaymentPersistence.getRange
+ incomeDefinedForAll <- IncomePersistence.definedForAll userIds
+ let since = max <$> (fst <$> paymentRange) <*> incomeDefinedForAll
- Nothing ->
- M.empty
+ cumulativeIncome <-
+ case since of
+ Just s -> IncomePersistence.getCumulativeIncome s (Clock.utctDay currentTime)
+ Nothing -> return M.empty
incomes <- IncomePersistence.list page perPage
- return $ IncomePage (IncomeHeader since byUser) incomes count) >>= json
+ return $ IncomePage page (IncomeHeader since cumulativeIncome) incomes count) >>= json
)
create :: CreateIncomeForm -> ActionM ()