feat(backend): Benachrichtigungssystem (Modul 26)
NotificationsModule: Notification-Modell mit Prioritaeten INFO/ HINWEIS/WARNUNG/KRITISCH, GET /notifications (eigene + Broadcast), POST /notifications/:id/read. record() als interne API fuer kuenftige Automatisierungsregeln (Modul 30) vorgesehen.
This commit is contained in:
1 parent
9b078022ff
commit
a57cde1f9a
9 files changed
+299
-1
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Notification, NotificationPriority } from '../../generated/prisma';
|
||||
|
||||
export interface CreateNotificationInput {
|
||||
userId?: string;
|
||||
priority: NotificationPriority;
|
||||
message: string;
|
||||
relatedType?: string;
|
||||
relatedId?: string;
|
||||
}
|
||||
|
||||
export interface NotificationFilter {
|
||||
userId?: string;
|
||||
unreadOnly?: boolean;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class NotificationsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async record(input: CreateNotificationInput): Promise<Notification> {
|
||||
return this.prisma.notification.create({ data: { ...input } });
|
||||
}
|
||||
|
||||
async findAll(filter: NotificationFilter = {}): Promise<Notification[]> {
|
||||
return this.prisma.notification.findMany({
|
||||
where: {
|
||||
OR: filter.userId
|
||||
? [{ userId: filter.userId }, { userId: null }]
|
||||
: undefined,
|
||||
read: filter.unreadOnly ? false : undefined,
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 100,
|
||||
});
|
||||
}
|
||||
|
||||
async markRead(id: string): Promise<Notification> {
|
||||
const notification = await this.prisma.notification.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!notification) {
|
||||
throw new NotFoundException('Notification not found');
|
||||
}
|
||||
return this.prisma.notification.update({
|
||||
where: { id },
|
||||
data: { read: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user