Fix order_created email by waiting for send-notification before navigation
The early termination issue was caused by the page navigating away before the send-notification function call could complete. Changed from fire-and-forget to awaiting the email send result before redirecting. Changes: - Checkout.tsx: Changed send-notification call to use await instead of .then()/.catch() - Now waits for email send to complete before navigating to order detail page - Email failures are caught in try/catch but navigation still proceeds Technical details: - Browser was terminating pending requests when navigate() was called immediately - Early termination: isolate warning indicated function was being killed mid-execution - Awaiting the function call ensures it completes before page navigation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -116,43 +116,43 @@ export default function Checkout() {
|
|||||||
throw new Error(paymentError.message || 'Gagal membuat pembayaran');
|
throw new Error(paymentError.message || 'Gagal membuat pembayaran');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send order_created email with QR code (non-blocking, fire-and-forget)
|
// Send order_created email with QR code (wait for completion before navigating)
|
||||||
console.log('[CHECKOUT] About to send order_created email for order:', order.id);
|
console.log('[CHECKOUT] About to send order_created email for order:', order.id);
|
||||||
console.log('[CHECKOUT] User email:', user.email);
|
console.log('[CHECKOUT] User email:', user.email);
|
||||||
console.log('[CHECKOUT] Payment data QR string:', paymentData?.qr_string);
|
console.log('[CHECKOUT] Payment data QR string:', paymentData?.qr_string);
|
||||||
|
|
||||||
supabase.functions.invoke('send-notification', {
|
try {
|
||||||
body: {
|
const result = await supabase.functions.invoke('send-notification', {
|
||||||
template_key: 'order_created',
|
body: {
|
||||||
recipient_email: user.email,
|
template_key: 'order_created',
|
||||||
recipient_name: user.user_metadata.name || user.email?.split('@')[0] || 'Pelanggan',
|
recipient_email: user.email,
|
||||||
variables: {
|
recipient_name: user.user_metadata.name || user.email?.split('@')[0] || 'Pelanggan',
|
||||||
nama: user.user_metadata.name || user.email?.split('@')[0] || 'Pelanggan',
|
variables: {
|
||||||
email: user.email,
|
nama: user.user_metadata.name || user.email?.split('@')[0] || 'Pelanggan',
|
||||||
order_id: order.id,
|
email: user.email,
|
||||||
tanggal_pesanan: new Date().toLocaleDateString('id-ID', {
|
order_id: order.id,
|
||||||
day: '2-digit',
|
tanggal_pesanan: new Date().toLocaleDateString('id-ID', {
|
||||||
month: 'short',
|
day: '2-digit',
|
||||||
year: 'numeric'
|
month: 'short',
|
||||||
}),
|
year: 'numeric'
|
||||||
total: formatIDR(total),
|
}),
|
||||||
metode_pembayaran: 'QRIS',
|
total: formatIDR(total),
|
||||||
produk: items.map(item => item.title).join(', '),
|
metode_pembayaran: 'QRIS',
|
||||||
payment_link: `${window.location.origin}/orders/${order.id}`,
|
produk: items.map(item => item.title).join(', '),
|
||||||
thank_you_page: `${window.location.origin}/orders/${order.id}`,
|
payment_link: `${window.location.origin}/orders/${order.id}`,
|
||||||
qr_string: paymentData?.qr_string || '',
|
thank_you_page: `${window.location.origin}/orders/${order.id}`,
|
||||||
qr_expiry_time: paymentData?.expired_at ? new Date(paymentData.expired_at).toLocaleString('id-ID') : ''
|
qr_string: paymentData?.qr_string || '',
|
||||||
|
qr_expiry_time: paymentData?.expired_at ? new Date(paymentData.expired_at).toLocaleString('id-ID') : ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}).then(result => {
|
|
||||||
console.log('[CHECKOUT] send-notification called successfully:', result);
|
console.log('[CHECKOUT] send-notification called successfully:', result);
|
||||||
}).catch(err => {
|
} catch (emailErr) {
|
||||||
console.error('[CHECKOUT] Failed to send order_created email:', err);
|
console.error('[CHECKOUT] Failed to send order_created email:', emailErr);
|
||||||
console.error('[CHECKOUT] Error details:', JSON.stringify(err));
|
|
||||||
// Don't block checkout flow if email fails
|
// Don't block checkout flow if email fails
|
||||||
});
|
}
|
||||||
|
|
||||||
console.log('[CHECKOUT] Order creation email call initiated');
|
console.log('[CHECKOUT] Order creation email call completed');
|
||||||
|
|
||||||
// Clear cart and redirect to order detail page to show QR code
|
// Clear cart and redirect to order detail page to show QR code
|
||||||
clearCart();
|
clearCart();
|
||||||
|
|||||||
Reference in New Issue
Block a user