Files
thrilltrack-explorer/supabase/functions/trigger-notification/index.ts
gpt-engineer-app[bot] a1280ddd05 Migrate Novu functions to wrapEdgeFunction
Refactor Phase 3 Batch 2–4 Novu-related functions to use the createEdgeFunction wrapper, replacing explicit HTTP servers with edge wrapper, adding standardized logging, tracing, and error handling across subscriber management, topic/notification, and migration/sync functions.
2025-11-11 03:55:02 +00:00

74 lines
1.8 KiB
TypeScript

import { Novu } from "npm:@novu/api@1.6.0";
import { corsHeaders } from '../_shared/cors.ts';
import { edgeLogger } from "../_shared/logger.ts";
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
export default createEdgeFunction(
{
name: 'trigger-notification',
requireAuth: false,
corsHeaders: corsHeaders
},
async (req, context) => {
const novuApiKey = Deno.env.get('NOVU_API_KEY');
if (!novuApiKey) {
throw new Error('NOVU_API_KEY is not configured');
}
const novu = new Novu({
secretKey: novuApiKey
});
const { workflowId, subscriberId, topicKey, payload, overrides } = await req.json() as {
workflowId: string;
subscriberId?: string;
topicKey?: string;
payload: Record<string, unknown>;
overrides?: Record<string, unknown>;
};
// Support both individual subscribers and topics
if (!subscriberId && !topicKey) {
throw new Error('Either subscriberId or topicKey must be provided');
}
const recipient = subscriberId
? { subscriberId }
: { topicKey: topicKey! };
context.span.setAttribute('action', 'trigger_notification');
edgeLogger.info('Triggering notification', {
workflowId,
recipient,
requestId: context.requestId,
action: 'trigger_notification'
});
const result = await novu.trigger({
to: recipient,
workflowId,
payload,
overrides,
});
edgeLogger.info('Notification triggered successfully', {
requestId: context.requestId,
transactionId: result.data.transactionId
});
return new Response(
JSON.stringify({
success: true,
transactionId: result.data.transactionId,
}),
{
headers: {
'Content-Type': 'application/json',
},
status: 200,
}
);
}
);