From 81e194d07f6ddd653b8bef4d50ba454df41c2702 Mon Sep 17 00:00:00 2001 From: Mateus Cruz Date: Mon, 5 Feb 2024 20:39:19 -0300 Subject: add debit logic --- lib/operation.ml | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'lib/operation.ml') 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) -- cgit v1.2.3