diff options
author | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-05 20:39:19 -0300 |
---|---|---|
committer | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-05 20:39:19 -0300 |
commit | 81e194d07f6ddd653b8bef4d50ba454df41c2702 (patch) | |
tree | 0d1e79bd0d42b2130d1745a278dee6b8c84d713a /lib/operation.ml | |
parent | b2aed14fd4a252d77b7ebaf42407472c47d1c98b (diff) |
add debit logic
Diffstat (limited to 'lib/operation.ml')
-rw-r--r-- | lib/operation.ml | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/lib/operation.ml b/lib/operation.ml index 1fb1dd5..3c8df2a 100644 --- a/lib/operation.ml +++ b/lib/operation.ml @@ -1,29 +1,34 @@ module TransactionType = struct type t = - | Credit - | Debit + [ `Credit + | `Debit + ] let t = let encode = function - | Credit -> "credit" - | Debit -> "debit" + | `Credit -> "credit" + | `Debit -> "debit" in let decode = function - | "credit" -> Ok Credit - | "debit" -> Ok Debit + | "credit" -> Ok `Credit + | "debit" -> Ok `Debit | _ -> Error "Invalid transaction type" in Caqti_type.(enum ~encode ~decode "transaction_type") ;; end -type t = - | Transaction of - { transaction_type : TransactionType.t - ; value : int - ; description : string - } - | Balance of { client_id : int } +type transaction_payload = + { value : int + ; description : string + } + +type transaction_op = + [ `Credit of transaction_payload + | `Debit of transaction_payload + ] + +type t = Balance of { client_id : int } type transaction = { id : int @@ -34,15 +39,14 @@ type transaction = ; created_at : Ptime.t } -let decoder = +let decoder : transaction_op Utils.Decoder.decoder = let open Utils.Decoder in let open Syntax in let transaction_type_decoder = - literal "c" *> return TransactionType.Credit - <|> literal "d" *> return TransactionType.Debit + literal "c" *> return (fun p -> `Credit p) + <|> literal "d" *> return (fun p -> `Debit p) in - (fun value transaction_type description -> - Transaction { value; description; transaction_type }) + (fun value transaction_type description -> transaction_type { value; description }) <$> ("valor" <: int) <*> ("tipo" <: transaction_type_decoder) <*> ("descricao" <: string) |