summaryrefslogtreecommitdiff
path: root/lib/operation.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/operation.ml')
-rw-r--r--lib/operation.ml40
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)