|
| 1 | +'use server' |
| 2 | + |
| 3 | +import { createAdminClient } from '@/lib/supabase/admin' |
| 4 | +import { getUserRole } from '@/lib/supabase/get-user-role' |
| 5 | +import { getCurrentTenantId } from '@/lib/supabase/tenant' |
| 6 | +import { revalidatePath } from 'next/cache' |
| 7 | + |
| 8 | +interface ActionResponse { |
| 9 | + success: boolean |
| 10 | + error?: string |
| 11 | +} |
| 12 | + |
| 13 | +export interface PendingBinancePersonalTransaction { |
| 14 | + transaction_id: number |
| 15 | + amount: number | null |
| 16 | + currency: string | null |
| 17 | + transaction_date: string | null |
| 18 | + user_id: string |
| 19 | + full_name: string |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Admin manually confirms an ambiguous `binance_personal` transaction (issue #482). |
| 24 | + * |
| 25 | + * Personal Binance Pay has no webhook, and when a buyer omits the note code and |
| 26 | + * the amount collides with another transfer, automated reconciliation can't |
| 27 | + * safely attribute the payment — it stays `pending`. Once the school admin has |
| 28 | + * verified the transfer in their own Binance app, they flip it here. |
| 29 | + * |
| 30 | + * Uses the service-role client (bypasses RLS), so admin role + tenant scope + |
| 31 | + * provider are validated below. The status-guarded update (`.eq('status','pending')`) |
| 32 | + * keeps it idempotent; the after_transaction_update trigger creates the |
| 33 | + * entitlements on the flip, so we never call enroll RPCs. No provider_charge_id |
| 34 | + * is set — a manual confirmation has no Binance orderId to consume. |
| 35 | + */ |
| 36 | +export async function confirmBinancePersonalTransaction( |
| 37 | + transactionId: number |
| 38 | +): Promise<ActionResponse> { |
| 39 | + try { |
| 40 | + const role = await getUserRole() |
| 41 | + if (role !== 'admin') { |
| 42 | + return { success: false, error: 'Unauthorized' } |
| 43 | + } |
| 44 | + |
| 45 | + const tenantId = await getCurrentTenantId() |
| 46 | + const supabase = createAdminClient() |
| 47 | + |
| 48 | + const { data: tx, error: loadError } = await supabase |
| 49 | + .from('transactions') |
| 50 | + .select('transaction_id, tenant_id, payment_provider, status') |
| 51 | + .eq('transaction_id', transactionId) |
| 52 | + .maybeSingle() |
| 53 | + |
| 54 | + if (loadError) throw loadError |
| 55 | + if (!tx || tx.tenant_id !== tenantId || tx.payment_provider !== 'binance_personal') { |
| 56 | + return { success: false, error: 'Transaction not found' } |
| 57 | + } |
| 58 | + |
| 59 | + const { error: updateError } = await supabase |
| 60 | + .from('transactions') |
| 61 | + .update({ status: 'successful' }) |
| 62 | + .eq('transaction_id', transactionId) |
| 63 | + .eq('tenant_id', tenantId) |
| 64 | + .eq('payment_provider', 'binance_personal') |
| 65 | + .eq('status', 'pending') |
| 66 | + |
| 67 | + if (updateError) throw updateError |
| 68 | + |
| 69 | + revalidatePath('/dashboard/admin/payment-requests') |
| 70 | + |
| 71 | + return { success: true } |
| 72 | + } catch (error) { |
| 73 | + console.error('Error confirming Binance personal transaction:', error) |
| 74 | + return { success: false, error: 'Failed to confirm payment' } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Admin cancels a pending `binance_personal` transaction (issue #482) — used |
| 80 | + * when they've determined no payment ever arrived. Flips pending → canceled |
| 81 | + * with the same gating, ownership, and provider checks as the confirm action. |
| 82 | + */ |
| 83 | +export async function cancelBinancePersonalTransaction( |
| 84 | + transactionId: number |
| 85 | +): Promise<ActionResponse> { |
| 86 | + try { |
| 87 | + const role = await getUserRole() |
| 88 | + if (role !== 'admin') { |
| 89 | + return { success: false, error: 'Unauthorized' } |
| 90 | + } |
| 91 | + |
| 92 | + const tenantId = await getCurrentTenantId() |
| 93 | + const supabase = createAdminClient() |
| 94 | + |
| 95 | + const { data: tx, error: loadError } = await supabase |
| 96 | + .from('transactions') |
| 97 | + .select('transaction_id, tenant_id, payment_provider, status') |
| 98 | + .eq('transaction_id', transactionId) |
| 99 | + .maybeSingle() |
| 100 | + |
| 101 | + if (loadError) throw loadError |
| 102 | + if (!tx || tx.tenant_id !== tenantId || tx.payment_provider !== 'binance_personal') { |
| 103 | + return { success: false, error: 'Transaction not found' } |
| 104 | + } |
| 105 | + |
| 106 | + const { error: updateError } = await supabase |
| 107 | + .from('transactions') |
| 108 | + .update({ status: 'canceled' }) |
| 109 | + .eq('transaction_id', transactionId) |
| 110 | + .eq('tenant_id', tenantId) |
| 111 | + .eq('payment_provider', 'binance_personal') |
| 112 | + .eq('status', 'pending') |
| 113 | + |
| 114 | + if (updateError) throw updateError |
| 115 | + |
| 116 | + revalidatePath('/dashboard/admin/payment-requests') |
| 117 | + |
| 118 | + return { success: true } |
| 119 | + } catch (error) { |
| 120 | + console.error('Error canceling Binance personal transaction:', error) |
| 121 | + return { success: false, error: 'Failed to cancel payment' } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * List this tenant's pending `binance_personal` transactions for the admin |
| 127 | + * manual-confirmation queue (issue #482). Oldest first. `profiles` is global |
| 128 | + * and has no email column, so the buyer's display name is looked up via |
| 129 | + * `full_name`. |
| 130 | + */ |
| 131 | +export async function listPendingBinancePersonalTransactions(): Promise< |
| 132 | + PendingBinancePersonalTransaction[] |
| 133 | +> { |
| 134 | + try { |
| 135 | + const role = await getUserRole() |
| 136 | + if (role !== 'admin') { |
| 137 | + return [] |
| 138 | + } |
| 139 | + |
| 140 | + const tenantId = await getCurrentTenantId() |
| 141 | + const supabase = createAdminClient() |
| 142 | + |
| 143 | + const { data, error } = await supabase |
| 144 | + .from('transactions') |
| 145 | + .select('transaction_id, amount, currency, transaction_date, user_id') |
| 146 | + .eq('tenant_id', tenantId) |
| 147 | + .eq('payment_provider', 'binance_personal') |
| 148 | + .eq('status', 'pending') |
| 149 | + .order('transaction_date', { ascending: true }) |
| 150 | + |
| 151 | + if (error) throw error |
| 152 | + |
| 153 | + const rows = data || [] |
| 154 | + if (rows.length === 0) return [] |
| 155 | + |
| 156 | + const userIds = [...new Set(rows.map((r) => r.user_id).filter(Boolean))] |
| 157 | + const { data: profiles } = await supabase |
| 158 | + .from('profiles') |
| 159 | + .select('id, full_name') |
| 160 | + .in('id', userIds) |
| 161 | + |
| 162 | + const nameById = new Map<string, string>( |
| 163 | + (profiles || []).map((p: { id: string; full_name: string | null }) => [ |
| 164 | + p.id, |
| 165 | + p.full_name || '', |
| 166 | + ]) |
| 167 | + ) |
| 168 | + |
| 169 | + return rows.map((r) => ({ |
| 170 | + transaction_id: r.transaction_id, |
| 171 | + amount: r.amount, |
| 172 | + currency: r.currency, |
| 173 | + transaction_date: r.transaction_date, |
| 174 | + user_id: r.user_id, |
| 175 | + full_name: nameById.get(r.user_id) || '', |
| 176 | + })) |
| 177 | + } catch (error) { |
| 178 | + console.error('Error listing pending Binance personal transactions:', error) |
| 179 | + return [] |
| 180 | + } |
| 181 | +} |
0 commit comments