Merge pull request #1 from Frappet/feat/notification
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s

feat: notification endpoint
This commit is contained in:
Methapon Metanipat 2025-03-05 11:18:18 +07:00 committed by GitHub
commit 5842935a54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 12 deletions

View file

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Notification" ADD COLUMN "registeredBranchId" TEXT;
-- AddForeignKey
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_registeredBranchId_fkey" FOREIGN KEY ("registeredBranchId") REFERENCES "Branch"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View file

@ -21,6 +21,9 @@ model Notification {
groupReceiver NotificationGroup[] groupReceiver NotificationGroup[]
registeredBranchId String?
registeredBranch Branch? @relation(fields: [registeredBranchId], references: [id])
receiver User? @relation(name: "NotificationReceiver", fields: [receiverId], references: [id], onDelete: Cascade) receiver User? @relation(name: "NotificationReceiver", fields: [receiverId], references: [id], onDelete: Cascade)
receiverId String? receiverId String?
@ -313,6 +316,7 @@ model Branch {
quotation Quotation[] quotation Quotation[]
workflowTemplate WorkflowTemplate[] workflowTemplate WorkflowTemplate[]
taskOrder TaskOrder[] taskOrder TaskOrder[]
notification Notification[]
} }
model BranchBank { model BranchBank {

View file

@ -14,10 +14,19 @@ import {
} from "tsoa"; } from "tsoa";
import { RequestWithUser } from "../interfaces/user"; import { RequestWithUser } from "../interfaces/user";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import prisma from "../db";
import { Prisma } from "@prisma/client";
import { queryOrNot } from "../utils/relation";
import { notFoundError } from "../utils/error";
import dayjs from "dayjs";
import HttpError from "../interfaces/http-error";
import { createPermCondition } from "../services/permission";
type NotificationCreate = {}; type NotificationCreate = {};
type NotificationUpdate = {}; type NotificationUpdate = {};
const permissionCondCompany = createPermCondition((_) => true);
@Route("/api/v1/notification") @Route("/api/v1/notification")
@Tags("Notification") @Tags("Notification")
export class NotificationController extends Controller { export class NotificationController extends Controller {
@ -29,12 +38,44 @@ export class NotificationController extends Controller {
@Query() pageSize: number = 30, @Query() pageSize: number = 30,
@Query() query = "", @Query() query = "",
) { ) {
const total = 0; const where: Prisma.NotificationWhereInput = {
AND: [
// TODO: implement {
OR: queryOrNot<(typeof where)[]>(query, [
{ title: { contains: query } },
{ detail: { contains: query } },
]),
},
{
OR: [
{ receiverId: req.user.sub },
req.user.roles.length > 0
? {
groupReceiver: { some: { name: { in: req.user.roles } } },
registeredBranch: { OR: permissionCondCompany(req.user) },
}
: {},
],
},
],
NOT: {
readByUser: { some: { id: req.user.sub } },
createdAt: dayjs().subtract(7, "days").toDate(),
},
};
const [result, total] = await prisma.$transaction([
prisma.notification.findMany({ where, include: { readByUser: true } }),
prisma.notification.count({ where }),
]);
return { return {
result: [], 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),
})),
page, page,
pageSize, pageSize,
total, total,
@ -44,9 +85,18 @@ export class NotificationController extends Controller {
@Get("{notificationId}") @Get("{notificationId}")
@Security("keycloak") @Security("keycloak")
async getNotification(@Request() req: RequestWithUser, @Path() notificationId: string) { async getNotification(@Request() req: RequestWithUser, @Path() notificationId: string) {
// TODO: implement const record = await prisma.notification.update({
where: { id: notificationId },
data: {
readByUser: {
connect: { id: req.user.sub },
},
},
});
return {}; if (!record) throw notFoundError("Notification");
return record;
} }
@Post() @Post()
@ -54,8 +104,9 @@ export class NotificationController extends Controller {
async createNotification(@Request() req: RequestWithUser, @Body() body: NotificationCreate) { async createNotification(@Request() req: RequestWithUser, @Body() body: NotificationCreate) {
// TODO: implement // TODO: implement
this.setStatus(HttpStatus.CREATED); // this.setStatus(HttpStatus.CREATED);
return {};
throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "Not implemented.", "notImplemented");
} }
@Put("{notificationId}") @Put("{notificationId}")
@ -67,14 +118,14 @@ export class NotificationController extends Controller {
) { ) {
// TODO: implement // TODO: implement
return {}; throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "Not implemented.", "notImplemented");
} }
@Delete("{notificationId}") @Delete("{notificationId}")
@Security("keycloak") @Security("keycloak")
async deleteNotification(@Request() req: RequestWithUser, @Path() notificationId: string) { async deleteNotification(@Request() req: RequestWithUser, @Path() notificationId: string) {
// TODO: implement const record = await prisma.notification.deleteMany({ where: { id: notificationId } });
if (record.count === 0) throw notFoundError("Notification");
return {}; return record;
} }
} }