aboutsummaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authorJoris2019-11-03 11:28:42 +0100
committerJoris2019-11-03 11:28:42 +0100
commit9dbb4e6f7c2f0edc1126626e2ff498144c6b9947 (patch)
treeda352e0861a2786a3a57dab2397ec7d678b5919b /server/src
parenta267f0bb4566389342c3244d3c082dc2453f4615 (diff)
downloadbudget-9dbb4e6f7c2f0edc1126626e2ff498144c6b9947.tar.gz
budget-9dbb4e6f7c2f0edc1126626e2ff498144c6b9947.tar.bz2
budget-9dbb4e6f7c2f0edc1126626e2ff498144c6b9947.zip
Show income header
Diffstat (limited to 'server/src')
-rw-r--r--server/src/Controller/Income.hs49
-rw-r--r--server/src/Job/WeeklyReport.hs2
-rw-r--r--server/src/Main.hs7
-rw-r--r--server/src/Persistence/Income.hs23
4 files changed, 50 insertions, 31 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 ()
diff --git a/server/src/Job/WeeklyReport.hs b/server/src/Job/WeeklyReport.hs
index 203c4e8..1a478dc 100644
--- a/server/src/Job/WeeklyReport.hs
+++ b/server/src/Job/WeeklyReport.hs
@@ -19,7 +19,7 @@ weeklyReport conf mbLastExecution = do
Nothing -> return ()
Just lastExecution -> do
(payments, incomes, users) <- Query.run $
- (,,) <$> PaymentPersistence.listPunctual <*> IncomePersistence.list <*> UserPersistence.list
+ (,,) <$> PaymentPersistence.listPunctual <*> IncomePersistence.listAll <*> UserPersistence.list
_ <- SendMail.sendMail conf (WeeklyReport.mail conf users payments incomes lastExecution now)
return ()
return now
diff --git a/server/src/Main.hs b/server/src/Main.hs
index 00e8d1c..40b53b6 100644
--- a/server/src/Main.hs
+++ b/server/src/Main.hs
@@ -54,13 +54,10 @@ main = do
paymentId <- S.param "id"
Payment.delete paymentId
- S.get "/api/v2/incomes" $ do
+ S.get "/api/incomes" $ do
page <- S.param "page"
perPage <- S.param "perPage"
- Income.listv2 page perPage
-
- S.get "/api/incomes" $
- Income.list
+ Income.list page perPage
S.post "/api/income" $
S.jsonData >>= Income.create
diff --git a/server/src/Persistence/Income.hs b/server/src/Persistence/Income.hs
index de55a18..4ae3228 100644
--- a/server/src/Persistence/Income.hs
+++ b/server/src/Persistence/Income.hs
@@ -1,7 +1,7 @@
module Persistence.Income
( count
, list
- , listv2
+ , listAll
, create
, edit
, delete
@@ -43,15 +43,8 @@ count =
SQLite.query_ conn "SELECT COUNT(*) FROM income WHERE deleted_at IS NULL"
)
-list :: Query [Income]
-list =
- Query (\conn ->
- map (\(Row i) -> i) <$>
- SQLite.query_ conn "SELECT * FROM income WHERE deleted_at IS NULL"
- )
-
-listv2 :: Int -> Int -> Query [Income]
-listv2 page perPage =
+list :: Int -> Int -> Query [Income]
+list page perPage =
Query (\conn ->
map (\(Row i) -> i) <$>
SQLite.query
@@ -60,6 +53,16 @@ listv2 page perPage =
(perPage, (page - 1) * perPage)
)
+listAll :: Query [Income]
+listAll =
+ Query (\conn ->
+ map (\(Row i) -> i) <$>
+ SQLite.query_ conn "SELECT * FROM income WHERE deleted_at IS NULL"
+ )
+
+-- firstIncomeByUser
+-- SELECT user_id, MIN(date) FROM income WHERE deleted_at IS NULL GROUP BY user_id;
+
create :: UserId -> Day -> Int -> Query Income
create userId date amount =
Query (\conn -> do