feature(dashboard): add integrations and notifications

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-10-02 22:12:05 +02:00
parent d920f6951c
commit f65a633403
94 changed files with 3692 additions and 127 deletions

View File

@@ -0,0 +1,71 @@
import type { Job } from 'bullmq';
import { setSuperJson } from '@openpanel/common';
import { db } from '@openpanel/db';
import { sendDiscordNotification } from '@openpanel/integrations/src/discord';
import { sendSlackNotification } from '@openpanel/integrations/src/slack';
import type { NotificationQueuePayload } from '@openpanel/queue';
import { getRedisPub } from '@openpanel/redis';
export async function notificationJob(job: Job<NotificationQueuePayload>) {
switch (job.data.type) {
case 'sendNotification': {
const { notification } = job.data.payload;
if (notification.sendToApp) {
getRedisPub().publish('notification', setSuperJson(notification));
// empty for now
return;
}
if (notification.sendToEmail) {
// empty for now
return;
}
if (!notification.integrationId) {
throw new Error('No integrationId provided');
}
const integration = await db.integration.findUniqueOrThrow({
where: {
id: notification.integrationId,
},
});
switch (integration.config.type) {
case 'webhook': {
return fetch(integration.config.url, {
method: 'POST',
headers: {
...(integration.config.headers ?? {}),
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: notification.title,
message: notification.message,
}),
});
}
case 'discord': {
return sendDiscordNotification({
webhookUrl: integration.config.url,
message: [
`🔔 **${notification.title}**`,
notification.message,
].join('\n'),
});
}
case 'slack': {
return sendSlackNotification({
webhookUrl: integration.config.incoming_webhook.url,
message: [`🔔 *${notification.title}*`, notification.message].join(
'\n',
),
});
}
}
}
}
}