Files
stats/apps/worker/src/jobs/notification.ts
Carl-Gerhard Lindesvärd 168ebc3430 feat(subscriptions): added polar as payment provider for subscriptions
* feature(dashboard): add polar / subscription

* wip(payments): manage subscription

* wip(payments): add free product, faq and some other improvements

* fix(root): change node to bundler in tsconfig

* wip(payments): display current subscription

* feat(dashboard): schedule project for deletion

* wip(payments): support custom products/subscriptions

* wip(payments): fix polar scripts

* wip(payments): add json package to dockerfiles
2025-02-26 11:24:00 +01:00

72 lines
2.0 KiB
TypeScript

import type { Job } from 'bullmq';
import { db } from '@openpanel/db';
import { sendDiscordNotification } from '@openpanel/integrations/src/discord';
import { sendSlackNotification } from '@openpanel/integrations/src/slack';
import { setSuperJson } from '@openpanel/json';
import type { NotificationQueuePayload } from '@openpanel/queue';
import { getRedisPub, publishEvent } from '@openpanel/redis';
export async function notificationJob(job: Job<NotificationQueuePayload>) {
switch (job.data.type) {
case 'sendNotification': {
const { notification } = job.data.payload;
if (notification.sendToApp) {
publishEvent('notification', 'created', 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',
),
});
}
}
}
}
}