mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:31:12 -05:00
Consolidate CORS handling by introducing a shared supabase/functions/_shared/cors.ts and migrate edge functions to import from it. Remove inline cors.ts usage across functions, standardize headers (including traceparent and x-request-id), and prepare for environment-aware origins.
103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
import { Novu } from "npm:@novu/api@1.6.0";
|
|
import { corsHeaders } from '../_shared/cors.ts';
|
|
import { edgeLogger, startRequest, endRequest } from "../_shared/logger.ts";
|
|
|
|
serve(async (req) => {
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, { headers: corsHeaders });
|
|
}
|
|
|
|
const tracking = startRequest('trigger-notification');
|
|
|
|
try {
|
|
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! };
|
|
|
|
edgeLogger.info('Triggering notification', {
|
|
workflowId,
|
|
recipient,
|
|
requestId: tracking.requestId,
|
|
action: 'trigger_notification'
|
|
});
|
|
|
|
const result = await novu.trigger({
|
|
to: recipient,
|
|
workflowId,
|
|
payload,
|
|
overrides,
|
|
});
|
|
|
|
edgeLogger.info('Notification triggered successfully', {
|
|
requestId: tracking.requestId,
|
|
transactionId: result.data.transactionId
|
|
});
|
|
|
|
endRequest(tracking, 200);
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
transactionId: result.data.transactionId,
|
|
requestId: tracking.requestId
|
|
}),
|
|
{
|
|
headers: {
|
|
...corsHeaders,
|
|
'Content-Type': 'application/json',
|
|
'X-Request-ID': tracking.requestId
|
|
},
|
|
status: 200,
|
|
}
|
|
);
|
|
} catch (error: unknown) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
edgeLogger.error('Error triggering notification', {
|
|
requestId: tracking.requestId,
|
|
error: errorMessage
|
|
});
|
|
|
|
endRequest(tracking, 500, errorMessage);
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
error: errorMessage,
|
|
requestId: tracking.requestId
|
|
}),
|
|
{
|
|
headers: {
|
|
...corsHeaders,
|
|
'Content-Type': 'application/json',
|
|
'X-Request-ID': tracking.requestId
|
|
},
|
|
status: 500,
|
|
}
|
|
);
|
|
}
|
|
});
|