max / makenotwork
| 1 | -- Reverse settled platform-funded credits when their sale is refunded (Run 21, |
| 2 | -- money-loss finding). |
| 3 | -- |
| 4 | -- When a Fan+ platform credit discounts a paid sale, the scheduler transfers |
| 5 | -- the discounted amount MNW -> creator so the creator nets full price |
| 6 | -- (migration 158). If that sale is later refunded, the buyer's charge is |
| 7 | -- refunded but nothing reversed the MNW -> creator transfer: the creator kept |
| 8 | -- the reimbursement for a returned item and MNW absorbed the loss silently. |
| 9 | -- |
| 10 | -- To reverse the transfer we need its Stripe id, which the settle path now |
| 11 | -- captures (`platform_credit_transfer_id`). `platform_credit_reversed_at` marks |
| 12 | -- a completed reversal so the reversal sweep runs exactly once per transaction. |
| 13 | transactions |
| 14 | ADD COLUMN IF NOT EXISTS platform_credit_transfer_id TEXT, |
| 15 | ADD COLUMN IF NOT EXISTS platform_credit_reversed_at TIMESTAMPTZ; |
| 16 | |
| 17 | -- Drives the reversal sweep: settled credits on refunded transactions that |
| 18 | -- haven't been reversed yet. |
| 19 | NOT EXISTS idx_transactions_platform_credit_reversible |
| 20 | ON transactions (completed_at) |
| 21 | WHERE platform_credit_cents > 0 |
| 22 | AND platform_credit_settled_at IS NOT NULL |
| 23 | AND platform_credit_reversed_at IS NULL; |
| 24 |