Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36790 amit 1
-- ============================================================================
2
-- Backfill: PO-rejected orders stranded in status=0 (PAYMENT_PENDING)
3
-- ============================================================================
4
-- Root cause: OrderController.updateApprovalStatus (reject branch) refunded the
5
-- wallet ("Order canceled") but never updated the order rows, so rejected bulk-PO
6
-- orders stayed status=0 with billing/refund NULL and were counted by every
7
-- pending-indent / "active order" query forever (e.g. account statement float).
8
--
9
-- This backfill only covers GROUP A: rejected transactions whose wallet WAS
10
-- refunded (a "+PURCHASE Order canceled" entry exists). Group B (rejected but no
11
-- wallet refund, ~55 rows) is intentionally EXCLUDED -- cancelling those would
12
-- drop pending indent with no offsetting wallet credit and wrongly debit the
13
-- partner; they need separate review.
14
--
15
-- refund_timestamp is set to the actual "Order canceled" wallet timestamp so the
16
-- order leaves pending indent exactly when the money was returned (historically
17
-- accurate per period). status is set to PO_REJECTED(91).
18
--
19
-- PRECONDITIONS
20
--   1. Deploy the PO_REJECTED(91) enum + reject-path code to EVERY service that
21
--      reads this DB BEFORE running this. OrderStatus.findByValue(91) returns null
22
--      on old code -> NPE on order.getStatus(). DEPLOY FIRST, BACKFILL SECOND.
23
--   2. Run on every environment the apps connect to (prod 192.168.142.141 AND any
24
--      reporting replica). Verify counts match the dry-run (485 rows / 287 txns).
25
--
26
-- IDEMPOTENT: guarded on status=0 AND refund_timestamp IS NULL. Safe to re-run
27
-- (a second run matches 0 rows because status is no longer 0).
28
-- ============================================================================
29
 
30
UPDATE transaction.`order` o
31
JOIN transaction.transaction_approval ta
32
        ON ta.id = o.transaction_id AND ta.status = 'REJECTED'
33
JOIN (
34
        SELECT reference, MIN(timestamp) AS cancel_ts
35
        FROM transaction.userwallethistory
36
        WHERE reference_type = 'PURCHASE'
37
          AND amount > 0
38
          AND description LIKE 'Order cancel%'
39
        GROUP BY reference
40
) r ON r.reference = CAST(o.transaction_id AS CHAR)
41
SET o.status           = 91,                       -- OrderStatus.PO_REJECTED
42
    o.refund_timestamp = r.cancel_ts,              -- actual wallet "Order canceled" time
43
    o.refunded_by      = 'system-backfill',
44
    o.refund_reason    = 'PO rejected (backfill: wallet already refunded, order stranded in status=0)'
45
WHERE o.status = 0
46
  AND o.billing_timestamp IS NULL
47
  AND o.refund_timestamp IS NULL;
48
 
49
-- Expected: 485 rows affected, 287 transactions, ~Rs 10.84 cr pending indent released.
50
 
51
-- ---------------------------------------------------------------------------
52
-- Verification (run after the UPDATE):
53
-- SELECT COUNT(*) FROM transaction.`order` o
54
--   JOIN transaction.transaction_approval ta ON ta.id=o.transaction_id AND ta.status='REJECTED'
55
--   WHERE o.status=0 AND o.billing_timestamp IS NULL AND o.refund_timestamp IS NULL;
56
--   -- Group A should now be 0 (only the ~55 Group-B no-wallet-refund rows may remain).
57
-- ---------------------------------------------------------------------------