summaryrefslogtreecommitdiff
path: root/lib/operation.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/operation.ml')
-rw-r--r--lib/operation.ml49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/operation.ml b/lib/operation.ml
new file mode 100644
index 0000000..1fb1dd5
--- /dev/null
+++ b/lib/operation.ml
@@ -0,0 +1,49 @@
+module TransactionType = struct
+ type t =
+ | Credit
+ | Debit
+
+ let t =
+ let encode = function
+ | Credit -> "credit"
+ | Debit -> "debit"
+ in
+ let decode = function
+ | "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 =
+ { id : int
+ ; client_id : int
+ ; value : int
+ ; transaction_type : TransactionType.t
+ ; description : string
+ ; created_at : Ptime.t
+ }
+
+let decoder =
+ let open Utils.Decoder in
+ let open Syntax in
+ let transaction_type_decoder =
+ literal "c" *> return TransactionType.Credit
+ <|> literal "d" *> return TransactionType.Debit
+ in
+ (fun value transaction_type description ->
+ Transaction { value; description; transaction_type })
+ <$> ("valor" <: int)
+ <*> ("tipo" <: transaction_type_decoder)
+ <*> ("descricao" <: string)
+;;