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,74 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { NotificationsService } from './notifications.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('NotificationsService', () => {
|
||||
let service: NotificationsService;
|
||||
let prisma: {
|
||||
notification: {
|
||||
create: jest.Mock;
|
||||
findMany: jest.Mock;
|
||||
findUnique: jest.Mock;
|
||||
update: jest.Mock;
|
||||
};
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
prisma = {
|
||||
notification: {
|
||||
create: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
};
|
||||
service = new NotificationsService(prisma as unknown as PrismaService);
|
||||
});
|
||||
|
||||
it('records a notification with the given priority and message', async () => {
|
||||
const created = {
|
||||
id: 'n1',
|
||||
priority: 'KRITISCH',
|
||||
message: 'Keine Königin gesehen',
|
||||
};
|
||||
prisma.notification.create.mockResolvedValueOnce(created);
|
||||
|
||||
const result = await service.record({
|
||||
priority: 'KRITISCH',
|
||||
message: 'Keine Königin gesehen',
|
||||
});
|
||||
|
||||
expect(result).toEqual(created);
|
||||
expect(prisma.notification.create).toHaveBeenCalledWith({
|
||||
data: { priority: 'KRITISCH', message: 'Keine Königin gesehen' },
|
||||
});
|
||||
});
|
||||
|
||||
it('lists notifications for a user including broadcast notifications', async () => {
|
||||
await service.findAll({ userId: 'user-1' });
|
||||
|
||||
const [[queryArg]] = prisma.notification.findMany.mock.calls as [
|
||||
[{ where: { OR?: { userId: string | null }[] } }],
|
||||
];
|
||||
expect(queryArg.where.OR).toEqual([{ userId: 'user-1' }, { userId: null }]);
|
||||
});
|
||||
|
||||
it('throws NotFoundException when marking an unknown notification as read', async () => {
|
||||
prisma.notification.findUnique.mockResolvedValueOnce(null);
|
||||
await expect(service.markRead('missing')).rejects.toBeInstanceOf(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('marks a notification as read', async () => {
|
||||
prisma.notification.findUnique.mockResolvedValueOnce({
|
||||
id: 'n1',
|
||||
read: false,
|
||||
});
|
||||
prisma.notification.update.mockResolvedValueOnce({ id: 'n1', read: true });
|
||||
|
||||
const result = await service.markRead('n1');
|
||||
|
||||
expect(result.read).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user