fix: ensure we dont bloat notification table with any other notification than app

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-09-03 21:39:21 +02:00
parent e5cacb73df
commit 52b86682e2
5 changed files with 33 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
import { useAppParams } from '@/hooks/useAppParams'; import { useAppParams } from '@/hooks/useAppParams';
import useWS from '@/hooks/useWS'; import useWS from '@/hooks/useWS';
import type { Notification } from '@openpanel/db'; import type { Prisma } from '@openpanel/db';
import { BellIcon } from 'lucide-react'; import { BellIcon } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
@@ -15,12 +15,15 @@ export function NotificationProvider() {
export function InnerNotificationProvider({ export function InnerNotificationProvider({
projectId, projectId,
}: { projectId: string }) { }: { projectId: string }) {
useWS<Notification>(`/live/notifications/${projectId}`, (notification) => { useWS<Prisma.NotificationUncheckedCreateInput>(
toast(notification.title, { `/live/notifications/${projectId}`,
description: notification.message, (notification) => {
icon: <BellIcon className="size-4" />, toast(notification.title, {
}); description: notification.message,
}); icon: <BellIcon className="size-4" />,
});
},
);
return null; return null;
} }

View File

@@ -46,14 +46,6 @@ export function useColumns() {
); );
}, },
}, },
{
accessorKey: 'integration',
header: 'Integration',
cell({ row }) {
const integration = row.original.integration;
return <div>{integration?.name}</div>;
},
},
{ {
accessorKey: 'notificationRule', accessorKey: 'notificationRule',
header: 'Rule', header: 'Rule',

View File

@@ -117,21 +117,28 @@ function getIntegration(integrationId: string | null) {
} }
export async function createNotification(notification: ICreateNotification) { export async function createNotification(notification: ICreateNotification) {
const res = await db.notification.create({ const data: Prisma.NotificationUncheckedCreateInput = {
data: { title: notification.title,
title: notification.title, message: notification.message,
message: notification.message, projectId: notification.projectId,
projectId: notification.projectId, payload: notification.payload || Prisma.DbNull,
payload: notification.payload || Prisma.DbNull, ...getIntegration(notification.integrationId),
...getIntegration(notification.integrationId), notificationRuleId: notification.notificationRuleId,
notificationRuleId: notification.notificationRuleId, };
},
});
return triggerNotification(res); // Only create notifications for app
if (data.sendToApp) {
await db.notification.create({
data,
});
}
return triggerNotification(data);
} }
export function triggerNotification(notification: Notification) { export function triggerNotification(
notification: Prisma.NotificationUncheckedCreateInput,
) {
return notificationQueue.add('sendNotification', { return notificationQueue.add('sendNotification', {
type: 'sendNotification', type: 'sendNotification',
payload: { payload: {

View File

@@ -1,6 +1,6 @@
import { Queue, QueueEvents } from 'bullmq'; import { Queue, QueueEvents } from 'bullmq';
import type { IServiceEvent, Notification } from '@openpanel/db'; import type { IServiceEvent, Notification, Prisma } from '@openpanel/db';
import { getRedisQueue } from '@openpanel/redis'; import { getRedisQueue } from '@openpanel/redis';
import type { TrackPayload } from '@openpanel/sdk'; import type { TrackPayload } from '@openpanel/sdk';
@@ -130,7 +130,7 @@ export const miscQueue = new Queue<MiscQueuePayload>('misc', {
export type NotificationQueuePayload = { export type NotificationQueuePayload = {
type: 'sendNotification'; type: 'sendNotification';
payload: { payload: {
notification: Notification; notification: Prisma.NotificationUncheckedCreateInput;
}; };
}; };

View File

@@ -1,6 +1,6 @@
import { type Redis, getRedisPub, getRedisSub } from './redis'; import { type Redis, getRedisPub, getRedisSub } from './redis';
import type { IServiceEvent, Notification } from '@openpanel/db'; import type { IServiceEvent, Notification, Prisma } from '@openpanel/db';
import { getSuperJson, setSuperJson } from '@openpanel/json'; import { getSuperJson, setSuperJson } from '@openpanel/json';
export type IPublishChannels = { export type IPublishChannels = {
@@ -14,7 +14,7 @@ export type IPublishChannels = {
saved: IServiceEvent; saved: IServiceEvent;
}; };
notification: { notification: {
created: Notification; created: Prisma.NotificationUncheckedCreateInput;
}; };
}; };