diff options
Diffstat (limited to 'src/server/Model/Payment.hs')
-rw-r--r-- | src/server/Model/Payment.hs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index 51f09b9..300f6b8 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -3,11 +3,13 @@ module Model.Payment , createPayment , paymentKeyToText , deleteOwnPayment + , getTotalPayments ) where import Data.Text (Text) import qualified Data.Text as T import Data.Time.Clock (getCurrentTime) +import Data.Maybe (catMaybes) import Control.Monad.IO.Class (liftIO) @@ -18,6 +20,7 @@ import qualified Database.Esqueleto as E import Model.Database import qualified Model.Json.Payment as P +import qualified Model.Json.TotalPayment as TP getPayments :: Persist [P.Payment] getPayments = do @@ -62,3 +65,20 @@ deleteOwnPayment user paymentId = do return False Nothing -> return False + +getTotalPayments :: Persist [TP.TotalPayment] +getTotalPayments = do + values <- select $ + from $ \(payment `InnerJoin` user) -> do + on (payment ^. PaymentUserId E.==. user ^. UserId) + where_ (isNothing (payment ^. PaymentDeletedAt)) + groupBy (payment ^. PaymentUserId) + return (user ^. UserName, sum_ (payment ^. PaymentCost)) + return $ catMaybes . map (getTotalPayment . unValueTuple) $ values + +getTotalPayment :: (Text, Maybe Int) -> Maybe TP.TotalPayment +getTotalPayment (userName, Just cost) = Just (TP.TotalPayment userName cost) +getTotalPayment (_, Nothing) = Nothing + +unValueTuple :: (Value a, Value b) -> (a, b) +unValueTuple (Value a, Value b) = (a, b) |