From baefda5a902a94cedf84cfcd2ae550267e5d932e Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 30 Mar 2016 00:28:55 +0200 Subject: Merge punctual and monthly payments in client model --- src/client/elm/Model/Payment.elm | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/client/elm/Model/Payment.elm') 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) -- cgit v1.2.3