aboutsummaryrefslogtreecommitdiff
path: root/src/backenddb/pg_insert_transfer_details.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/backenddb/pg_insert_transfer_details.sql')
-rw-r--r--src/backenddb/pg_insert_transfer_details.sql57
1 files changed, 33 insertions, 24 deletions
diff --git a/src/backenddb/pg_insert_transfer_details.sql b/src/backenddb/pg_insert_transfer_details.sql
index a64d0139..1650d157 100644
--- a/src/backenddb/pg_insert_transfer_details.sql
+++ b/src/backenddb/pg_insert_transfer_details.sql
@@ -43,13 +43,9 @@ DECLARE
my_affected_orders RECORD;
i INT8;
curs CURSOR (arg_coin_pub BYTEA) FOR
- SELECT mcon.order_serial
+ SELECT mcon.deposit_confirmation_serial,
+ mcon.order_serial
FROM merchant_deposits dep
- -- Next 2 joins ensure transfers exist in the first place
- JOIN merchant_deposit_to_transfer
- USING (deposit_serial)
- JOIN merchant_transfers mtrans
- USING (credit_serial)
JOIN merchant_deposit_confirmations mcon
USING (deposit_confirmation_serial)
WHERE dep.coin_pub=arg_coin_pub;
@@ -195,24 +191,37 @@ LOOP
EXIT WHEN NOT FOUND;
RAISE NOTICE 'checking affected order for completion';
- UPDATE merchant_contract_terms
- SET wired=TRUE
- WHERE order_serial IN
- (SELECT order_serial
- FROM merchant_deposit_confirmations dcon
- WHERE
- order_serial=my_affected_orders.order_serial
- AND NOT EXISTS
- (SELECT 1
- FROM merchant_deposit_confirmations dcon
- JOIN merchant_deposits dep
- USING (deposit_confirmation_serial)
- JOIN merchant_deposit_to_transfer
- USING (deposit_serial)
- JOIN merchant_transfers mtrans
- USING (credit_serial)
- WHERE dcon.order_serial=my_affected_orders.order_serial
- AND NOT mtrans.confirmed));
+
+ -- First, check if deposit confirmation is done.
+ UPDATE merchant_deposit_confirmations
+ SET wire_pending=FALSE
+ WHERE (deposit_confirmation_serial=my_affected_orders.deposit_confirmation_serial)
+ AND NOT EXISTS
+ (SELECT 1
+ FROM merchant_deposits md
+ LEFT JOIN merchant_deposit_to_transfer mdtt
+ USING (deposit_serial)
+ WHERE md.deposit_confirmation_serial=my_affected_orders.deposit_confirmation_serial
+ AND mdtt.wtid IS NULL);
+ -- wtid will be NULL due to LEFT JOIN
+ -- if we do not have an entry in mdtt for the deposit
+ -- and thus some entry in md was not yet wired.
+
+ IF FOUND
+ THEN
+ -- Also update contract terms, if all (other) associated
+ -- deposit_confirmations are also done.
+
+ UPDATE merchant_contract_terms
+ SET wired=TRUE
+ WHERE (order_serial=my_affected_orders.order_serial)
+ AND NOT EXISTS
+ (SELECT 1
+ FROM merchant_deposit_confirmations mdc
+ WHERE mdc.wire_pending
+ AND mdc.order_serial=my_affected_orders.order_serial);
+ END IF;
+
END LOOP; -- END curs LOOP
CLOSE curs;
END LOOP; -- END FOR loop