feat: Implement final error coverage

This commit is contained in:
gpt-engineer-app[bot]
2025-11-04 19:50:06 +00:00
parent a9334c7a3a
commit 0df047d56b
9 changed files with 291 additions and 403 deletions

View File

@@ -1,7 +1,6 @@
import { supabase } from "@/lib/supabaseClient";
import { invokeWithTracking } from "@/lib/edgeFunctionTracking";
import { logger } from "@/lib/logger";
import { AppError } from "@/lib/errorHandler";
import { handleNonCriticalError, AppError } from "@/lib/errorHandler";
import { z } from "zod";
import type {
NotificationPayload,
@@ -29,9 +28,9 @@ class NotificationService {
return !!data?.setting_value;
} catch (error: unknown) {
logger.error('Failed to check Novu status', {
action: 'check_novu_status',
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Check Novu Status',
metadata: { returnedFalse: true }
});
return false;
}
@@ -47,10 +46,6 @@ class NotificationService {
const novuEnabled = await this.isNovuEnabled();
if (!novuEnabled) {
logger.warn('Novu not configured, skipping subscriber update', {
action: 'update_novu_subscriber',
userId: validated.subscriberId
});
return { success: false, error: 'Novu not configured' };
}
@@ -60,11 +55,10 @@ class NotificationService {
);
if (error) {
logger.error('Edge function error updating Novu subscriber', {
action: 'update_novu_subscriber',
handleNonCriticalError(error, {
action: 'Update Novu Subscriber (Edge Function)',
userId: validated.subscriberId,
requestId,
error: error.message
metadata: { requestId }
});
throw new AppError(
'Failed to update notification subscriber',
@@ -73,18 +67,11 @@ class NotificationService {
);
}
logger.info('Novu subscriber updated successfully', {
action: 'update_novu_subscriber',
userId: validated.subscriberId,
requestId
});
return { success: true };
} catch (error: unknown) {
logger.error('Error in updateSubscriber', {
action: 'update_novu_subscriber',
userId: subscriberData.subscriberId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Update Novu Subscriber',
userId: subscriberData.subscriberId
});
return {
@@ -104,10 +91,6 @@ class NotificationService {
const novuEnabled = await this.isNovuEnabled();
if (!novuEnabled) {
logger.warn('Novu not configured, skipping subscriber creation', {
action: 'create_novu_subscriber',
userId: validated.subscriberId
});
return { success: false, error: 'Novu not configured' };
}
@@ -117,11 +100,10 @@ class NotificationService {
);
if (error) {
logger.error('Edge function error creating Novu subscriber', {
action: 'create_novu_subscriber',
handleNonCriticalError(error, {
action: 'Create Novu Subscriber (Edge Function)',
userId: validated.subscriberId,
requestId,
error: error.message
metadata: { requestId }
});
throw new AppError(
'Failed to create notification subscriber',
@@ -146,27 +128,18 @@ class NotificationService {
});
if (dbError) {
logger.error('Failed to store subscriber preferences', {
action: 'store_subscriber_preferences',
userId: validated.subscriberId,
error: dbError.message,
errorCode: dbError.code
handleNonCriticalError(dbError, {
action: 'Store Subscriber Preferences',
userId: validated.subscriberId
});
throw dbError;
}
logger.info('Novu subscriber created successfully', {
action: 'create_novu_subscriber',
userId: validated.subscriberId,
requestId
});
return { success: true };
} catch (error: unknown) {
logger.error('Error in createSubscriber', {
action: 'create_novu_subscriber',
userId: subscriberData.subscriberId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Create Novu Subscriber',
userId: subscriberData.subscriberId
});
return {
@@ -207,11 +180,10 @@ class NotificationService {
);
if (novuError) {
logger.error('Failed to update Novu preferences', {
action: 'update_novu_preferences',
handleNonCriticalError(novuError, {
action: 'Update Novu Preferences',
userId,
requestId,
error: novuError.message
metadata: { requestId }
});
throw novuError;
}
@@ -228,11 +200,9 @@ class NotificationService {
});
if (dbError) {
logger.error('Failed to save notification preferences', {
action: 'save_notification_preferences',
userId,
error: dbError.message,
errorCode: dbError.code
handleNonCriticalError(dbError, {
action: 'Save Notification Preferences',
userId
});
throw dbError;
}
@@ -268,17 +238,11 @@ class NotificationService {
});
}
logger.info('Notification preferences updated', {
action: 'update_notification_preferences',
userId
});
return { success: true };
} catch (error: unknown) {
logger.error('Error updating notification preferences', {
action: 'update_notification_preferences',
userId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Update Notification Preferences',
userId
});
if (error instanceof z.ZodError) {
@@ -307,20 +271,14 @@ class NotificationService {
.maybeSingle();
if (error && error.code !== 'PGRST116') {
logger.error('Failed to fetch notification preferences', {
action: 'fetch_notification_preferences',
userId,
error: error.message,
errorCode: error.code
handleNonCriticalError(error, {
action: 'Fetch Notification Preferences',
userId
});
throw error;
}
if (!data) {
logger.info('No preferences found, returning defaults', {
action: 'fetch_notification_preferences',
userId
});
return DEFAULT_NOTIFICATION_PREFERENCES;
}
@@ -331,10 +289,9 @@ class NotificationService {
frequencySettings: data.frequency_settings
});
} catch (error: unknown) {
logger.error('Error fetching notification preferences', {
action: 'fetch_notification_preferences',
userId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Get Notification Preferences',
userId
});
return null;
}
@@ -352,10 +309,8 @@ class NotificationService {
.order('category', { ascending: true });
if (error) {
logger.error('Failed to fetch notification templates', {
action: 'fetch_notification_templates',
error: error.message,
errorCode: error.code
handleNonCriticalError(error, {
action: 'Fetch Notification Templates'
});
throw error;
}
@@ -367,9 +322,8 @@ class NotificationService {
novu_workflow_id: t.novu_workflow_id || null,
}));
} catch (error: unknown) {
logger.error('Error fetching notification templates', {
action: 'fetch_notification_templates',
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Get Notification Templates'
});
return [];
}
@@ -382,11 +336,6 @@ class NotificationService {
try {
const novuEnabled = await this.isNovuEnabled();
if (!novuEnabled) {
logger.warn('Novu not configured, skipping notification', {
action: 'trigger_notification',
workflowId: payload.workflowId,
subscriberId: payload.subscriberId
});
return { success: false, error: 'Novu not configured' };
}
@@ -396,31 +345,18 @@ class NotificationService {
);
if (error) {
logger.error('Failed to trigger notification', {
action: 'trigger_notification',
workflowId: payload.workflowId,
subscriberId: payload.subscriberId,
requestId,
error: error.message
handleNonCriticalError(error, {
action: 'Trigger Notification',
metadata: { workflowId: payload.workflowId, subscriberId: payload.subscriberId, requestId }
});
throw error;
}
logger.info('Notification triggered successfully', {
action: 'trigger_notification',
workflowId: payload.workflowId,
subscriberId: payload.subscriberId,
transactionId: data?.transactionId,
requestId
});
return { success: true };
} catch (error: unknown) {
logger.error('Error triggering notification', {
action: 'trigger_notification',
workflowId: payload.workflowId,
subscriberId: payload.subscriberId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Trigger Notification',
metadata: { workflowId: payload.workflowId, subscriberId: payload.subscriberId }
});
return {
@@ -446,25 +382,16 @@ class NotificationService {
);
if (error) {
logger.error('Failed to notify moderators', {
action: 'notify_moderators',
submissionId: payload.submission_id,
requestId,
error: error.message
handleNonCriticalError(error, {
action: 'Notify Moderators (Submission)',
metadata: { submissionId: payload.submission_id, requestId }
});
throw error;
}
logger.info('Moderators notified successfully', {
action: 'notify_moderators',
submissionId: payload.submission_id,
requestId
});
} catch (error: unknown) {
logger.error('Error notifying moderators', {
action: 'notify_moderators',
submissionId: payload.submission_id,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Notify Moderators (Submission)',
metadata: { submissionId: payload.submission_id }
});
}
}
@@ -482,10 +409,6 @@ class NotificationService {
try {
const novuEnabled = await this.isNovuEnabled();
if (!novuEnabled) {
logger.warn('Novu not configured, skipping system announcement', {
action: 'send_system_announcement',
title: payload.title
});
return { success: false, error: 'Novu not configured' };
}
@@ -495,31 +418,21 @@ class NotificationService {
);
if (error) {
logger.error('Failed to send system announcement', {
action: 'send_system_announcement',
title: payload.title,
requestId,
error: error.message
handleNonCriticalError(error, {
action: 'Send System Announcement',
metadata: { title: payload.title, requestId }
});
throw error;
}
logger.info('System announcement sent successfully', {
action: 'send_system_announcement',
title: payload.title,
announcementId: data?.announcementId,
requestId
});
return {
success: true,
announcementId: data?.announcementId
};
} catch (error: unknown) {
logger.error('Error sending system announcement', {
action: 'send_system_announcement',
title: payload.title,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Send System Announcement',
metadata: { title: payload.title }
});
return {
@@ -545,10 +458,6 @@ class NotificationService {
try {
const novuEnabled = await this.isNovuEnabled();
if (!novuEnabled) {
logger.warn('Novu not configured, skipping report notification', {
action: 'notify_moderators_report',
reportId: payload.reportId
});
return { success: false, error: 'Novu not configured' };
}
@@ -558,27 +467,18 @@ class NotificationService {
);
if (error) {
logger.error('Failed to notify moderators about report', {
action: 'notify_moderators_report',
reportId: payload.reportId,
requestId,
error: error.message
handleNonCriticalError(error, {
action: 'Notify Moderators (Report)',
metadata: { reportId: payload.reportId, requestId }
});
throw error;
}
logger.info('Moderators notified about report successfully', {
action: 'notify_moderators_report',
reportId: payload.reportId,
requestId
});
return { success: true };
} catch (error: unknown) {
logger.error('Error notifying moderators about report', {
action: 'notify_moderators_report',
reportId: payload.reportId,
error: error instanceof Error ? error.message : String(error)
handleNonCriticalError(error, {
action: 'Notify Moderators (Report)',
metadata: { reportId: payload.reportId }
});
return {