diff options
author | Joris | 2016-03-27 17:36:33 +0200 |
---|---|---|
committer | Joris | 2016-03-27 17:59:32 +0200 |
commit | 869bab77e93e2a6c776a4b1fc35ef0fd5df22f5f (patch) | |
tree | 917a9e871eff1c487da63ea2407234d7e3829dda /src/server/Model/Payer.hs | |
parent | a8882071da12cbb5b0bf2f003322e42e181b0c82 (diff) |
Compute payers client side rather than server side
Diffstat (limited to 'src/server/Model/Payer.hs')
-rw-r--r-- | src/server/Model/Payer.hs | 46 |
1 files changed, 0 insertions, 46 deletions
diff --git a/src/server/Model/Payer.hs b/src/server/Model/Payer.hs deleted file mode 100644 index 3893765..0000000 --- a/src/server/Model/Payer.hs +++ /dev/null @@ -1,46 +0,0 @@ -module Model.Payer - ( getPayers - ) - where - -import Control.Monad.IO.Class (liftIO) - -import Data.Time.Clock (getCurrentTime) -import Data.List (find) -import Data.Maybe (fromMaybe, fromMaybe) - -import Database.Persist - -import Model.Database -import Model.Payer.Payment (getTotalPaymentsBefore, getTotalPaymentsAfter) -import Model.Payer.Income (incomeDefinedForAll) -import Model.User (getUsers) -import Model.Income (getIncomes) - -import qualified Model.Json.Payer as Json -import qualified Model.Json.Income as Json - -getPayers :: Persist [Json.Payer] -getPayers = do - userIds <- map entityKey <$> getUsers - incomes <- getIncomes - now <- liftIO getCurrentTime - incomeIsDefined <- fromMaybe now <$> incomeDefinedForAll - preIncomePaymentSums <- getTotalPaymentsBefore incomeIsDefined - postIncomePaymentSums <- getTotalPaymentsAfter incomeIsDefined - return $ map (getPayer incomes preIncomePaymentSums postIncomePaymentSums) userIds - -getPayer :: [Income] -> [(UserId, Int)] -> [(UserId, Int)] -> UserId -> Json.Payer -getPayer incomes preIncomePaymentSums postIncomePaymentSums userId = - Json.Payer - { Json.userId = userId - , Json.preIncomePaymentSum = findOrDefault userId 0 preIncomePaymentSums - , Json.postIncomePaymentSum = findOrDefault userId 0 postIncomePaymentSums - , Json.incomes = - map (\income -> Json.Income (incomeCreation income) (incomeAmount income)) - . filter ((==) userId . incomeUserId) - $ incomes - } - -findOrDefault :: (Eq a) => a -> b -> [(a, b)] -> b -findOrDefault a b = fromMaybe b . fmap snd . find ((==) a . fst) |