From b4b7d633d17e09c59353bf193f76d2d8838cda89 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Wed, 30 Oct 2024 13:48:18 +0700 Subject: [PATCH] feat: add query notification --- src/controllers/00-notification-controller.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/controllers/00-notification-controller.ts b/src/controllers/00-notification-controller.ts index 1718e04..4fdfce8 100644 --- a/src/controllers/00-notification-controller.ts +++ b/src/controllers/00-notification-controller.ts @@ -14,6 +14,9 @@ import { } from "tsoa"; import { RequestWithUser } from "../interfaces/user"; import HttpStatus from "../interfaces/http-status"; +import prisma from "../db"; +import { Prisma } from "@prisma/client"; +import { queryOrNot } from "../utils/relation"; type NotificationCreate = {}; type NotificationUpdate = {}; @@ -29,12 +32,31 @@ export class NotificationController extends Controller { @Query() pageSize: number = 30, @Query() query = "", ) { - const total = 0; - - // TODO: implement + 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 + ? { groupReceiver: { some: { name: { in: req.user.roles } } } } + : {}, + ], + }, + ], + }; + const [result, total] = await prisma.$transaction([ + prisma.notification.findMany({ where }), + prisma.notification.count({ where }), + ]); return { - result: [], + result, page, pageSize, total,