feat: Implement notification edge functions

This commit is contained in:
gpt-engineer-app[bot]
2025-10-22 12:35:13 +00:00
parent ee2b03b832
commit 199f57e00e
3 changed files with 308 additions and 0 deletions

View File

@@ -448,6 +448,125 @@ class NotificationService {
}
}
/**
* Trigger a system announcement to all users via the "users" topic
* Requires admin or superuser role
*/
async sendSystemAnnouncement(payload: {
title: string;
message: string;
severity: 'info' | 'warning' | 'critical';
actionUrl?: string;
}): Promise<{ success: boolean; error?: string; announcementId?: string }> {
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' };
}
const { data, error, requestId } = await invokeWithTracking(
'notify-system-announcement',
payload
);
if (error) {
logger.error('Failed to send system announcement', {
action: 'send_system_announcement',
title: payload.title,
requestId,
error: error.message
});
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)
});
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to send system announcement'
};
}
}
/**
* Notify moderators about a new report via the "moderation-reports" topic
*/
async notifyModeratorsReport(payload: {
reportId: string;
reportType: string;
reportedEntityType: string;
reportedEntityId: string;
reporterName: string;
reason: string;
entityPreview: string;
reportedAt: string;
}): Promise<{ success: boolean; error?: string }> {
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' };
}
const { data, error, requestId } = await invokeWithTracking(
'notify-moderators-report',
payload
);
if (error) {
logger.error('Failed to notify moderators about report', {
action: 'notify_moderators_report',
reportId: payload.reportId,
requestId,
error: error.message
});
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)
});
return {
success: false,
error: 'Failed to notify moderators about report'
};
}
}
/**
* Check if notifications are enabled
*/