module Model.View.LoggedIn.Account ( Account , IncomeEdition , initAccount , initIncomeEdition , getCurrentIncome , validateIncome ) where import Result as Result exposing (Result(..)) import Dict import String import Utils.Dict exposing (mapValues) import Model.Translations exposing (..) import Model.Payer exposing (..) import Model.User exposing (UserId) type alias Account = { me : UserId , payers : Payers , visibleDetail : Bool , incomeEdition : Maybe IncomeEdition } initAccount : UserId -> Payers -> Account initAccount me payers = { me = me , payers = payers |> mapValues (\payer -> { payer | incomes = List.sortBy .creation payer.incomes } ) , visibleDetail = False , incomeEdition = Nothing } getCurrentIncome : Account -> Maybe Int getCurrentIncome account = case Dict.get account.me account.payers of Just payer -> payer.incomes |> List.sortBy .creation |> List.reverse |> List.head |> Maybe.map .amount Nothing -> Nothing type alias IncomeEdition = { income : String , error : Maybe String } initIncomeEdition : Int -> IncomeEdition initIncomeEdition income = { income = toString income , error = Nothing } validateIncome : String -> Translations -> Result String Int validateIncome amount translations = case String.toInt amount of Ok number -> if number > 0 then Ok number else Err <| getMessage "IncomeMustBePositiveNumber" translations Err _ -> Err <| getMessage "IncomeRequired" translations