0

Update webhook.ts

This commit is contained in:
Nicolas 2024-06-05 10:38:05 -07:00
parent 1a16378fe8
commit beb7526d1d

View File

@ -2,53 +2,57 @@ import { supabase_service } from "./supabase";
export const callWebhook = async (teamId: string, data: any) => { export const callWebhook = async (teamId: string, data: any) => {
try { try {
const selfHostedUrl = process.env.SELF_HOSTED_WEBHOOK_URL; const selfHostedUrl = process.env.SELF_HOSTED_WEBHOOK_URL;
let webhookUrl = selfHostedUrl; let webhookUrl = selfHostedUrl;
if (!selfHostedUrl) { if (!selfHostedUrl) {
const { data: webhooksData, error } = await supabase_service const { data: webhooksData, error } = await supabase_service
.from('webhooks') .from("webhooks")
.select('url') .select("url")
.eq('team_id', teamId) .eq("team_id", teamId)
.limit(1); .limit(1);
if (error) { if (error) {
console.error(`Error fetching webhook URL for team ID: ${teamId}`, error.message); console.error(
return null; `Error fetching webhook URL for team ID: ${teamId}`,
} error.message
);
return null;
}
if (!webhooksData || webhooksData.length === 0) { if (!webhooksData || webhooksData.length === 0) {
return null; return null;
} }
webhookUrl = webhooksData[0].url; webhookUrl = webhooksData[0].url;
}
let dataToSend = [];
if (data.result.links && data.result.links.length !== 0) {
for (let i = 0; i < data.result.links.length; i++) {
dataToSend.push({
content: data.result.links[i].content.content,
markdown: data.result.links[i].content.markdown,
metadata: data.result.links[i].content.metadata,
});
} }
}
await fetch(webhookUrl, { let dataToSend = [];
method: 'POST', if (data.result.links && data.result.links.length !== 0) {
headers: { for (let i = 0; i < data.result.links.length; i++) {
'Content-Type': 'application/json', dataToSend.push({
}, content: data.result.links[i].content.content,
body: JSON.stringify({ markdown: data.result.links[i].content.markdown,
success: data.success, metadata: data.result.links[i].content.metadata,
project_id: data.project_id, });
data: dataToSend, }
error: data.error || undefined, }
}),
await fetch(webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
success: data.success,
data: dataToSend,
error: data.error || undefined,
}),
}); });
} catch (error) { } catch (error) {
console.error(`Error sending webhook for team ID: ${teamId}`, error.message); console.error(
`Error sending webhook for team ID: ${teamId}`,
error.message
);
} }
}; };