aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller
diff options
context:
space:
mode:
authorJoris2019-11-03 11:28:42 +0100
committerJoris2019-11-03 11:28:42 +0100
commit9dbb4e6f7c2f0edc1126626e2ff498144c6b9947 (patch)
treeda352e0861a2786a3a57dab2397ec7d678b5919b /server/src/Controller
parenta267f0bb4566389342c3244d3c082dc2453f4615 (diff)
Show income header
Diffstat (limited to 'server/src/Controller')
-rw-r--r--server/src/Controller/Income.hs49
1 files changed, 34 insertions, 15 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs
index 3272cbf..d8d3d89 100644
--- a/server/src/Controller/Income.hs
+++ b/server/src/Controller/Income.hs
@@ -1,42 +1,61 @@
module Controller.Income
( list
- , listv2
, create
, edit
, delete
) where
import Control.Monad.IO.Class (liftIO)
+import qualified Data.Map as M
+import qualified Data.Time.Clock as Clock
import Data.Validation (Validation (Failure, Success))
import qualified Network.HTTP.Types.Status as Status
import Web.Scotty hiding (delete)
import Common.Model (CreateIncomeForm (..),
- EditIncomeForm (..), IncomeId,
- IncomesAndCount (..), User (..))
+ EditIncomeForm (..), Income (..),
+ IncomeHeader (..), IncomeId,
+ IncomePage (..), User (..))
+import qualified Common.Model as CM
import qualified Controller.Helper as ControllerHelper
import Model.CreateIncome (CreateIncome (..))
import Model.EditIncome (EditIncome (..))
import qualified Model.Query as Query
import qualified Persistence.Income as IncomePersistence
+import qualified Persistence.Payment as PaymentPersistence
+import qualified Persistence.User as UserPersistence
import qualified Secure
import qualified Validation.Income as IncomeValidation
-list :: ActionM ()
-list =
- Secure.loggedAction (\_ ->
- (liftIO . Query.run $ IncomePersistence.list) >>= json
- )
-
-listv2 :: Int -> Int -> ActionM ()
-listv2 page perPage =
- Secure.loggedAction (\_ ->
+list :: Int -> Int -> ActionM ()
+list page perPage =
+ Secure.loggedAction (\_ -> do
+ currentTime <- liftIO Clock.getCurrentTime
(liftIO . Query.run $ do
count <- IncomePersistence.count
- incomes <- IncomePersistence.listv2 page perPage
- return $ IncomesAndCount incomes count
- ) >>= json
+
+ users <- UserPersistence.list
+ allPayments <- PaymentPersistence.listPunctual -- TODO: get first payment defined for all
+ allIncomes <- IncomePersistence.listAll
+
+ let since =
+ CM.useIncomesFrom (map _user_id users) allIncomes allPayments
+
+ let byUser =
+ case since of
+ Just s ->
+ M.fromList . flip map users $ \user ->
+ ( _user_id user
+ , CM.cumulativeIncomesSince currentTime s $
+ filter ((==) (_user_id user) . _income_userId) allIncomes
+ )
+
+ Nothing ->
+ M.empty
+
+ incomes <- IncomePersistence.list page perPage
+ return $ IncomePage (IncomeHeader since byUser) incomes count) >>= json
)
create :: CreateIncomeForm -> ActionM ()