fix(broadcasts): attach user_id on send, surface errors with toast

The INSERT into broadcasts in use-broadcast-sending.ts never set
user_id, but broadcasts.user_id is NOT NULL + RLS-guarded. The
INSERT rejected (23502 / 42501), the hook threw, handleSend caught
with console.error only. UI showed nothing — the wizard stayed on
Step 4 and the flow appeared dead.

Fix:
- Resolve session.user at the top of createAndSendBroadcast and
  include user_id on the broadcasts INSERT.
- handleSend in /broadcasts/new now toasts the error via sonner so
  future failures are visible immediately instead of vanishing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Arnas Donauskas 2026-04-17 13:55:18 +03:00
parent 5dbb26e4fc
commit cd04e0682c
No known key found for this signature in database
2 changed files with 21 additions and 1 deletions

View File

@ -59,7 +59,11 @@ export default function NewBroadcastPage() {
});
router.push(`/broadcasts/${broadcastId}`);
} catch (err) {
// Previously swallowed with console.error — the wizard would
// just no-op, leaving the user confused. Surface the reason.
const message = err instanceof Error ? err.message : 'Broadcast failed';
console.error('Broadcast failed:', err);
toast.error(message);
}
}

View File

@ -249,6 +249,19 @@ export function useBroadcastSending(): UseBroadcastSendingReturn {
const supabase = createClient();
try {
// ── Step 0: Resolve current user ──────────────────────────────
// broadcasts.user_id is NOT NULL + guarded by RLS
// (auth.uid() = user_id). Without this, the INSERT below was
// silently failing with 23502 / 42501 — the wizard would
// no-op with no feedback.
const {
data: { session },
} = await supabase.auth.getSession();
const user = session?.user;
if (!user) {
throw new Error('You are not signed in.');
}
// ── Step 1: Resolve audience contacts ─────────────────────────
setProgress(5);
const contacts = await resolveAudience(payload.audience);
@ -262,6 +275,7 @@ export function useBroadcastSending(): UseBroadcastSendingReturn {
const { data: broadcast, error: broadcastError } = await supabase
.from('broadcasts')
.insert({
user_id: user.id,
name: payload.name,
template_name: payload.template.name,
template_language: payload.template.language ?? 'en_US',
@ -284,7 +298,9 @@ export function useBroadcastSending(): UseBroadcastSendingReturn {
.single();
if (broadcastError || !broadcast) {
throw new Error(`Failed to create broadcast: ${broadcastError?.message}`);
throw new Error(
`Failed to create broadcast: ${broadcastError?.message ?? 'unknown error'}`,
);
}
// ── Step 3: Insert recipient rows ─────────────────────────────