summaryrefslogtreecommitdiff
path: root/lib/operation.ml
blob: 1fb1dd5e079fa87fdb02c1993186f8e8e4e0add5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)
;;