aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model/Payment.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/elm/Model/Payment.elm')
-rw-r--r--src/client/elm/Model/Payment.elm30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/client/elm/Model/Payment.elm b/src/client/elm/Model/Payment.elm
index 7a6c630..ab3cbb7 100644
--- a/src/client/elm/Model/Payment.elm
+++ b/src/client/elm/Model/Payment.elm
@@ -11,7 +11,8 @@ module Model.Payment exposing
, punctual
, monthly
, groupAndSortByMonth
- , sortedFiltredPunctual
+ , search
+ , validateFrequency
)
import Date exposing (..)
@@ -19,6 +20,7 @@ import Date.Extra.Core exposing (monthToInt, intToMonth)
import Json.Decode as Json exposing ((:=))
import String
+import Form.Validate as Validate exposing (Validation)
import Model.User exposing (UserId, userIdDecoder)
import Model.Date exposing (dateDecoder)
@@ -77,7 +79,6 @@ totalPayments paymentFilter userId payments =
|> List.filter (\payment ->
paymentFilter payment
&& payment.userId == userId
- && payment.frequency == Punctual
)
|> List.map .cost
|> List.sum
@@ -85,8 +86,8 @@ totalPayments paymentFilter userId payments =
punctual : Payments -> Payments
punctual = List.filter ((==) Punctual << .frequency)
-monthly : UserId -> Payments -> Payments
-monthly userId = List.filter (\p -> p.frequency == Monthly && p.userId == userId)
+monthly : Payments -> Payments
+monthly = List.filter ((==) Monthly << .frequency)
groupAndSortByMonth : Payments -> List ((Month, Int), Payments)
groupAndSortByMonth payments =
@@ -96,12 +97,25 @@ groupAndSortByMonth payments =
|> List.map (\((year, month), payments) -> ((intToMonth month, year), payments))
|> List.reverse
-sortedFiltredPunctual : String -> Payments -> Payments
-sortedFiltredPunctual search payments =
- punctual payments
+search : String -> Frequency -> Payments -> Payments
+search name frequency payments =
+ payments
+ |> List.filter ((==) frequency << .frequency)
|> List.sortBy (Date.toTime << .creation)
- |> List.filter (searchSuccess search)
+ |> List.filter (searchSuccess name)
|> List.reverse
searchSuccess : String -> Payment -> Bool
searchSuccess text { name } = (String.toLower text) `String.contains` (String.toLower name)
+
+validateFrequency : Validation String Frequency
+validateFrequency =
+ Validate.customValidation Validate.string (\str ->
+ if str == toString Punctual
+ then
+ Ok Punctual
+ else
+ if str == toString Monthly
+ then Ok Monthly
+ else Err (Validate.customError "InvalidFrequency")
+ )