mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:51:13 -05:00
Improve error handling and input validation for notification preferences
Add input validation for userId and channelPreferences, and enhance error reporting for Novu API calls by returning detailed results for each channel update. Replit-Commit-Author: Agent Replit-Commit-Session-Id: a8c5cf3e-a80e-462f-b090-b081acdcf03a Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
@@ -31,6 +31,33 @@ serve(async (req) => {
|
|||||||
|
|
||||||
console.log('Updating preferences for user:', userId);
|
console.log('Updating preferences for user:', userId);
|
||||||
|
|
||||||
|
// Validate input
|
||||||
|
if (!userId) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
error: 'userId is required',
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
status: 400,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!preferences?.channelPreferences) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
error: 'channelPreferences is required in preferences object',
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
status: 400,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Get Novu subscriber ID from database
|
// Get Novu subscriber ID from database
|
||||||
const { data: prefData, error: prefError } = await supabase
|
const { data: prefData, error: prefError } = await supabase
|
||||||
.from('user_notification_preferences')
|
.from('user_notification_preferences')
|
||||||
@@ -49,38 +76,59 @@ serve(async (req) => {
|
|||||||
const channelPrefs = preferences.channelPreferences;
|
const channelPrefs = preferences.channelPreferences;
|
||||||
const workflowId = preferences.workflowId || 'default';
|
const workflowId = preferences.workflowId || 'default';
|
||||||
|
|
||||||
try {
|
const channelTypes = ['email', 'sms', 'in_app', 'push'] as const;
|
||||||
// Update each channel preference separately
|
const results: { channel: string; success: boolean; error?: string }[] = [];
|
||||||
const channelTypes = ['email', 'sms', 'in_app', 'push'] as const;
|
|
||||||
|
for (const channelType of channelTypes) {
|
||||||
for (const channelType of channelTypes) {
|
if (channelPrefs[channelType] !== undefined) {
|
||||||
if (channelPrefs[channelType] !== undefined) {
|
try {
|
||||||
try {
|
await novu.subscribers.updatePreference(
|
||||||
await novu.subscribers.updatePreference(
|
subscriberId,
|
||||||
subscriberId,
|
workflowId,
|
||||||
workflowId,
|
{
|
||||||
{
|
channel: {
|
||||||
channel: {
|
type: channelType as any, // Cast to any to handle SDK type limitations
|
||||||
type: channelType as any, // Cast to any to handle SDK type limitations
|
enabled: channelPrefs[channelType]
|
||||||
enabled: channelPrefs[channelType]
|
},
|
||||||
},
|
}
|
||||||
}
|
);
|
||||||
);
|
results.push({ channel: channelType, success: true });
|
||||||
} catch (channelError: any) {
|
} catch (channelError: any) {
|
||||||
console.warn(`Failed to update ${channelType} preference:`, channelError.message);
|
console.error(`Failed to update ${channelType} preference:`, channelError.message);
|
||||||
// Continue with other channels even if one fails
|
results.push({
|
||||||
}
|
channel: channelType,
|
||||||
|
success: false,
|
||||||
|
error: channelError.message || 'Unknown error'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('Preferences updated successfully');
|
|
||||||
} catch (novuError: any) {
|
|
||||||
console.error('Novu API error:', novuError);
|
|
||||||
throw new Error(`Failed to update Novu preferences: ${novuError.message || 'Unknown error'}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if any updates failed
|
||||||
|
const failedChannels = results.filter(r => !r.success);
|
||||||
|
const allSucceeded = failedChannels.length === 0;
|
||||||
|
|
||||||
|
if (!allSucceeded) {
|
||||||
|
console.warn(`Some channel preferences failed to update:`, failedChannels);
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
error: 'Some channel preferences failed to update',
|
||||||
|
results,
|
||||||
|
failedChannels: failedChannels.map(c => c.channel),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
status: 502, // Bad Gateway - external service failure
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('All preferences updated successfully');
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
|
results,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||||
|
|||||||
Reference in New Issue
Block a user