summaryrefslogtreecommitdiff
path: root/lib/operation.ml
diff options
context:
space:
mode:
authorMateus Cruz <mateuscolvr@gmail.com>2024-02-05 03:31:35 -0300
committerMateus Cruz <mateuscolvr@gmail.com>2024-02-05 03:31:35 -0300
commit5a2f5bf9a12b270e15757df48afeb1c1db5b34b3 (patch)
tree2f1797364d78d935b4eb79269fd6b100fa77ec21 /lib/operation.ml
parentf0142aa1c3b4d7c2fd0b70c9e62c1ec033e445c2 (diff)
add db queries
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)
+;;