feat: add delete notification
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 9s

This commit is contained in:
Methapon2001 2025-03-05 17:18:25 +07:00
parent 6265ac8a65
commit 9bf534adce
2 changed files with 35 additions and 5 deletions

View file

@ -29,7 +29,8 @@ model Notification {
createdAt DateTime @default(now())
readByUser User[]
readByUser User[] @relation(name: "NotificationRead")
deleteByUser User[] @relation(name: "NotificationDelete")
}
model NotificationGroup {
@ -484,7 +485,8 @@ model User {
invoiceCreated Invoice[]
paymentCreated Payment[]
notificationReceive Notification[] @relation("NotificationReceiver")
notificationRead Notification[]
notificationRead Notification[] @relation("NotificationRead")
notificationDelete Notification[] @relation("NotificationDelete")
taskOrderCreated TaskOrder[] @relation("TaskOrderCreatedByUser")
creditNoteCreated CreditNote[] @relation("CreditNoteCreatedByUser")

View file

@ -135,11 +135,39 @@ export class NotificationController extends Controller {
);
}
@Delete()
@Security("keycloak")
async deleteNotificationMany(@Request() req: RequestWithUser, @Body() notificationId: string[]) {
if (!notificationId.length) return;
return await prisma.notification
.findMany({ where: { id: { in: notificationId } } })
.then(async (v) => {
await prisma.$transaction(
v.map((v) =>
prisma.notification.update({
where: { id: v.id },
data: {
deleteByUser: { connect: { id: req.user.sub } },
},
}),
),
);
});
}
@Delete("{notificationId}")
@Security("keycloak")
async deleteNotification(@Request() req: RequestWithUser, @Path() notificationId: string) {
const record = await prisma.notification.deleteMany({ where: { id: notificationId } });
if (record.count === 0) throw notFoundError("Notification");
return record;
const record = await prisma.notification.findFirst({ where: { id: notificationId } });
if (!record) throw notFoundError("Notification");
return await prisma.notification.update({
where: { id: notificationId },
data: {
deleteByUser: {
disconnect: { id: req.user.sub },
},
},
});
}
}