aboutsummaryrefslogtreecommitdiff
path: root/src/server/Model/Payment.hs
blob: 300f6b893ba3e3e5c848279b0fe7c0d66f9af436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Model.Payment
  ( getPayments
  , 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)

import Database.Persist
import qualified Database.Persist as P
import Database.Esqueleto
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
  xs <- select $
        from $ \(payment `InnerJoin` user) -> do
          on (payment ^. PaymentUserId E.==. user ^. UserId)
          where_ (isNothing (payment ^. PaymentDeletedAt))
          return (payment, user)
  return (map getJsonPayment xs)

getJsonPayment :: (Entity Payment, Entity User) -> P.Payment
getJsonPayment (paymentEntity, userEntity) =
  let payment = entityVal paymentEntity
      user = entityVal userEntity
  in  P.Payment
        { P.id = paymentKeyToText . entityKey $ paymentEntity
        , P.creation = paymentCreation payment
        , P.name = paymentName payment
        , P.cost = paymentCost payment
        , P.userName = userName user
        }

paymentKeyToText :: Key Payment -> Text
paymentKeyToText = T.pack . show . unSqlBackendKey . unPaymentKey

createPayment :: UserId -> Text -> Int -> Persist PaymentId
createPayment userId name cost = do
  now <- liftIO getCurrentTime
  insert $ Payment userId now name cost Nothing

deleteOwnPayment :: Entity User -> PaymentId -> Persist Bool
deleteOwnPayment user paymentId = do
  mbPayment <- get paymentId
  case mbPayment of
    Just payment ->
      if paymentUserId payment == entityKey user
        then do
          now <- liftIO getCurrentTime
          P.update paymentId [PaymentDeletedAt P.=. Just now]
          return True
        else
          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)