jws-backend/src/controllers/00-notification-controller.ts

173 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-10-29 15:21:09 +07:00
import {
Body,
Controller,
Delete,
Get,
Path,
Post,
Query,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { RequestWithUser } from "../interfaces/user";
2024-10-30 13:48:18 +07:00
import prisma from "../db";
import { Prisma } from "@prisma/client";
import { queryOrNot } from "../utils/relation";
2024-10-30 14:08:39 +07:00
import { notFoundError } from "../utils/error";
2025-03-05 11:04:21 +07:00
import dayjs from "dayjs";
2025-03-05 11:16:18 +07:00
import { createPermCondition } from "../services/permission";
2024-10-29 15:21:09 +07:00
2025-03-05 11:16:18 +07:00
const permissionCondCompany = createPermCondition((_) => true);
2024-10-29 15:21:09 +07:00
@Route("/api/v1/notification")
@Tags("Notification")
export class NotificationController extends Controller {
@Get()
@Security("keycloak")
async getNotificationList(
@Request() req: RequestWithUser,
@Query() page: number = 1,
@Query() pageSize: number = 30,
@Query() query = "",
) {
2024-10-30 13:48:18 +07:00
const where: Prisma.NotificationWhereInput = {
AND: [
{
OR: queryOrNot<(typeof where)[]>(query, [
{ title: { contains: query } },
{ detail: { contains: query } },
]),
},
{
OR: [
{ receiverId: req.user.sub },
req.user.roles.length > 0
2025-03-05 11:16:18 +07:00
? {
groupReceiver: { some: { name: { in: req.user.roles } } },
registeredBranch: { OR: permissionCondCompany(req.user) },
}
2024-10-30 13:48:18 +07:00
: {},
],
},
],
2025-03-05 11:04:21 +07:00
NOT: {
2025-03-06 08:41:33 +07:00
OR: [
{
readByUser: { some: { id: req.user.sub } },
createdAt: { lte: dayjs().subtract(7, "days").toDate() },
},
{ deleteByUser: { some: { id: req.user.sub } } },
],
2025-03-05 11:04:21 +07:00
},
2024-10-30 13:48:18 +07:00
};
const [result, total] = await prisma.$transaction([
2025-03-05 15:25:52 +07:00
prisma.notification.findMany({
where,
include: { readByUser: true },
orderBy: { createdAt: "desc" },
}),
2024-10-30 13:48:18 +07:00
prisma.notification.count({ where }),
]);
2024-10-29 15:21:09 +07:00
return {
2025-03-05 11:04:21 +07:00
result: result.map((v) => ({
id: v.id,
title: v.title,
detail: v.detail,
createdAt: v.createdAt,
read: v.readByUser.some((v) => v.id === req.user.sub),
})),
2024-10-29 15:21:09 +07:00
page,
pageSize,
total,
};
}
@Get("{notificationId}")
@Security("keycloak")
async getNotification(@Request() req: RequestWithUser, @Path() notificationId: string) {
2025-03-05 10:49:08 +07:00
const record = await prisma.notification.update({
where: { id: notificationId },
data: {
readByUser: {
connect: { id: req.user.sub },
},
},
});
2024-10-29 15:21:09 +07:00
if (!record) throw notFoundError("Notification");
return record;
2024-10-29 15:21:09 +07:00
}
2025-03-05 17:42:10 +07:00
@Post("mark-read")
2024-10-29 15:21:09 +07:00
@Security("keycloak")
2025-03-05 15:17:23 +07:00
async markRead(@Request() req: RequestWithUser, @Body() body?: { id: string[] }) {
const record = await prisma.notification.findMany({
where: {
id: body ? { in: body.id } : undefined,
OR: !body
? [
{ receiverId: req.user.sub },
req.user.roles.length > 0
? {
groupReceiver: { some: { name: { in: req.user.roles } } },
registeredBranch: { OR: permissionCondCompany(req.user) },
}
: {},
]
: undefined,
},
});
2024-10-29 15:21:09 +07:00
2025-03-05 15:17:23 +07:00
await prisma.$transaction(
record.map((v) =>
prisma.notification.update({
where: { id: v.id },
data: {
readByUser: { connect: { id: req.user.sub } },
},
}),
),
);
2024-10-29 15:21:09 +07:00
}
2025-03-05 17:18:25 +07:00
@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 } },
},
}),
),
);
});
}
2024-10-29 15:21:09 +07:00
@Delete("{notificationId}")
@Security("keycloak")
async deleteNotification(@Request() req: RequestWithUser, @Path() notificationId: string) {
2025-03-05 17:18:25 +07:00
const record = await prisma.notification.findFirst({ where: { id: notificationId } });
if (!record) throw notFoundError("Notification");
return await prisma.notification.update({
where: { id: notificationId },
data: {
deleteByUser: {
2025-03-06 08:41:33 +07:00
connect: { id: req.user.sub },
2025-03-05 17:18:25 +07:00
},
},
});
2024-10-29 15:21:09 +07:00
}
}