diff --git a/src/pg.ml b/src/pg.ml index 9d73398f..196c74ac 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -221,6 +221,52 @@ module Caqti_type = struct { wire_fee; closing_fee; start_date; end_date; sig_ }) Caqti_type.(t5 amount amount time time master_sig) + let exchange_wire_account = + let master_sig = Bin_sig.MasterWireDetails.caqti in + Caqti_type.custom + ~encode:(fun + ExchangeWireAccount. + { + payto_uri; + conversion_url; + debit_restrictions; + credit_restrictions; + master_sig; + bank_label; + priority; + } + -> + Ok + ( payto_uri, + conversion_url, + debit_restrictions, + credit_restrictions, + master_sig, + bank_label, + priority )) + ~decode:(fun + ( payto_uri, + conversion_url, + debit_restrictions, + credit_restrictions, + master_sig, + bank_label, + priority ) + -> + Ok + { + payto_uri; + conversion_url; + debit_restrictions; + credit_restrictions; + master_sig; + bank_label; + priority; + }) + Caqti_type.( + t7 payto_uri (option string) account_restrictions account_restrictions + master_sig (option string) (option int)) + include Caqti_request.Infix end @@ -421,87 +467,34 @@ let lookup_wire_timestamp = let insert_wire = let insert_wire = - let master_sig = Bin_sig.MasterWireDetails.caqti in - Caqti_type.( - t8 payto_uri (option string) account_restrictions account_restrictions - master_sig time (option string) (option int) - ->. unit) + Caqti_type.(t3 exchange_wire_account bool time ->. unit) "INSERT INTO wire_accounts (payto_uri, conversion_url, \ - debit_restrictions, credit_restrictions, master_sig, is_active, \ - last_change, bank_label, priority) VALUES \ - ($1,$2,$3::TEXT::JSONB,$4::TEXT::JSONB,$5,true,$6,$7,$8)" + credit_restrictions, debit_restrictions, master_sig, bank_label, \ + priority, is_active, last_change) VALUES \ + ($1,$2,$3::TEXT::JSONB,$4::TEXT::JSONB,$5,$6,$7,true,$8)" in - fun (module Conn : CONN) - ~last_change - ExchangeWireAccount. - { - payto_uri; - conversion_url; - debit_restrictions; - credit_restrictions; - master_sig; - bank_label; - priority; - } - -> - Conn.exec insert_wire - ( payto_uri, - conversion_url, - debit_restrictions, - credit_restrictions, - master_sig, - last_change, - bank_label, - priority ) + fun (module Conn : CONN) ~last_change v -> + let is_active = true in + Conn.exec insert_wire (v, is_active, last_change) let update_wire = let update_wire = - let master_sig = Bin_sig.MasterWireDetails.caqti in - Caqti_type.( - t9 payto_uri bool (option string) account_restrictions - account_restrictions time master_sig (option string) (option int) - ->. unit) - "UPDATE wire_accounts SET is_active=$2, conversion_url=$3, \ - debit_restrictions=$4::TEXT::JSONB, \ - credit_restrictions=$5::TEXT::JSONB, last_change=$6, master_sig=$7, \ - bank_label=$8, priority=$9 WHERE payto_uri=$1" + Caqti_type.(t3 exchange_wire_account bool time ->. unit) + "UPDATE wire_accounts SET conversion_url=$2, \ + debit_restrictions=$3::TEXT::JSONB, \ + credit_restrictions=$4::TEXT::JSONB, master_sig=$5, bank_label=$6, \ + priority=$7, is_active=$8, last_change=$9 WHERE payto_uri=$1" in - fun (module Conn : CONN) - ~last_change - ExchangeWireAccount. - { - payto_uri; - conversion_url; - debit_restrictions; - credit_restrictions; - master_sig; - bank_label; - priority; - } - -> - let enabled = true in - Conn.exec update_wire - ( payto_uri, - enabled, - conversion_url, - debit_restrictions, - credit_restrictions, - last_change, - master_sig, - bank_label, - priority ) + fun (module Conn : CONN) ~is_active ~last_change v -> + Conn.exec update_wire (v, is_active, last_change) let disable_wire = let disable_wire = - (* TODO - check syntax on this - don't erase old values with null and just change `is_active` and `last_change`? - or re-use the update query *) + (* TODO check syntax on this *) Caqti_type.(t2 payto_uri time ->. unit) - "UPDATE wire_accounts SET is_active=false, conversion_url=NULL, \ - debit_restrictions=NULL::TEXT::JSONB, \ - credit_restrictions=NULL::TEXT::JSONB, last_change=$2, master_sig=NULL, \ - bank_label=NULL, priority=NULL WHERE payto_uri=$1" + "UPDATE wire_accounts SET conversion_url=NULL, debit_restrictions=NULL, \ + credit_restrictions=NULL, master_sig=NULL, bank_label=NULL, \ + priority=NULL, is_active=FALSE, last_change=$2 WHERE payto_uri=$1" in fun (module Conn : CONN) ~payto_uri ~validity_end -> Conn.exec disable_wire (payto_uri, validity_end)