aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model
diff options
context:
space:
mode:
authorJoris2016-03-30 00:28:55 +0200
committerJoris2016-03-30 00:28:55 +0200
commitbaefda5a902a94cedf84cfcd2ae550267e5d932e (patch)
tree72fa7b4e6c49d025563e3d7cba1ec13af43aa1c2 /src/client/elm/Model
parent76f8b85eb9f796d6df861a04f702ef5f48630795 (diff)
Merge punctual and monthly payments in client model
Diffstat (limited to 'src/client/elm/Model')
-rw-r--r--src/client/elm/Model/Init.elm1
-rw-r--r--src/client/elm/Model/Payment.elm32
2 files changed, 28 insertions, 5 deletions
diff --git a/src/client/elm/Model/Init.elm b/src/client/elm/Model/Init.elm
index 7028427..7fccf00 100644
--- a/src/client/elm/Model/Init.elm
+++ b/src/client/elm/Model/Init.elm
@@ -10,6 +10,5 @@ type alias Init =
{ users : Users
, me : UserId
, payments : Payments
- , monthlyPayments : Payments
, incomes : Incomes
}
diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm
index 80579e2..e792c6c 100644
--- a/src/client/elm/Model/Payment.elm
+++ b/src/client/elm/Model/Payment.elm
@@ -3,11 +3,13 @@ module Model.Payment
, Payments
, Payment
, PaymentId
+ , Frequency(..)
, paymentsDecoder
, paymentIdDecoder
, deletePayment
- , PaymentFrequency(..)
, totalPayments
+ , punctualPayments
+ , monthlyPayments
) where
import Date exposing (..)
@@ -27,33 +29,55 @@ type alias Payment =
, name : String
, cost : Int
, userId : UserId
+ , frequency : Frequency
}
type alias PaymentId = Int
-type PaymentFrequency = Punctual | Monthly
+type Frequency = Punctual | Monthly
paymentsDecoder : Json.Decoder Payments
paymentsDecoder = Json.list paymentDecoder
paymentDecoder : Json.Decoder Payment
paymentDecoder =
- Json.object5 Payment
+ Json.object6 Payment
("id" := paymentIdDecoder)
("creation" := dateDecoder)
("name" := Json.string)
("cost" := Json.int)
("userId" := userIdDecoder)
+ ("frequency" := frequencyDecoder)
paymentIdDecoder : Json.Decoder PaymentId
paymentIdDecoder = Json.int
+frequencyDecoder : Json.Decoder Frequency
+frequencyDecoder =
+ Json.customDecoder
+ Json.string
+ (\input -> case input of
+ "Punctual" -> Ok Punctual
+ "Monthly" -> Ok Monthly
+ _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input)
+ )
+
deletePayment : PaymentId -> Payments -> Payments
deletePayment paymentId = List.filter (((/=) paymentId) << .id)
totalPayments : (Payment -> Bool) -> UserId -> Payments -> Int
totalPayments paymentFilter userId payments =
payments
- |> List.filter (\payment -> paymentFilter payment && payment.userId == userId)
+ |> List.filter (\payment ->
+ paymentFilter payment
+ && payment.userId == userId
+ && payment.frequency == Punctual
+ )
|> List.map .cost
|> List.sum
+
+punctualPayments : Payments -> Payments
+punctualPayments = List.filter ((==) Punctual << .frequency)
+
+monthlyPayments : Payments -> Payments
+monthlyPayments = List.filter ((==) Monthly << .frequency)