diff options
author | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-05 20:39:19 -0300 |
---|---|---|
committer | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-05 20:39:19 -0300 |
commit | 81e194d07f6ddd653b8bef4d50ba454df41c2702 (patch) | |
tree | 0d1e79bd0d42b2130d1745a278dee6b8c84d713a /lib/query.ml | |
parent | b2aed14fd4a252d77b7ebaf42407472c47d1c98b (diff) |
add debit logic
Diffstat (limited to 'lib/query.ml')
-rw-r--r-- | lib/query.ml | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/lib/query.ml b/lib/query.ml index 85f91f0..6e359ce 100644 --- a/lib/query.ml +++ b/lib/query.ml @@ -31,7 +31,9 @@ module Q = struct [%rapper get_opt {sql| - SELECT @int{id}, @int{mov_limit} FROM clients WHERE id = %int{id} + SELECT clients.id as @int{id}, @int{mov_limit}, value as @int{balance} FROM clients + JOIN balances ON balances.client_id = %int{id} + WHERE clients.id = %int{id} |sql} record_out] ;; @@ -47,14 +49,19 @@ end let ( let* ) = Result.bind -let execute_operation ~client_id ~(op : Operation.t) conn = +(* TODO: Separar em duas funções *) +let execute_transaction ~client_id ~(op : Operation.transaction_op) conn = match op with - | Transaction { value; description; transaction_type } -> - let* () = Q.transaction ~client_id ~value ~description conn ~transaction_type in - (match transaction_type with - | Credit -> Q.credit ~value ~client_id conn - | Debit -> Q.debit ~value ~client_id conn) - | _ -> failwith "TODO" + | `Credit { value; description } -> + let* () = + Q.transaction ~client_id ~value ~description conn ~transaction_type:`Credit + in + Q.credit ~value ~client_id conn + | `Debit { value; description } -> + let* () = + Q.transaction ~client_id ~value ~description conn ~transaction_type:`Debit + in + Q.debit ~value ~client_id conn ;; let find_client id conn = Q.client ~id conn |