From 0bfe80b9a0b6e747eb35fd62c2a11c8edb060d31 Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Wed, 12 Jul 2023 16:24:11 +0200 Subject: [PATCH] refacto GET method with search params for users --- src/app/api/admin/UsersController.ts | 19 +++++++++++++++++-- src/app/api/super-admin/UsersController.ts | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/app/api/admin/UsersController.ts b/src/app/api/admin/UsersController.ts index 707a8ccf..8c2beb0e 100644 --- a/src/app/api/admin/UsersController.ts +++ b/src/app/api/admin/UsersController.ts @@ -27,9 +27,24 @@ export default class UsersController extends ApiController { if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } + + if (req.query["search"] && typeof req.query["search"] === "string") { + const filter = req.query["search"]; + query = { + where: { + contact: { + OR: [ + { first_name: { contains: filter, mode: "insensitive" } }, + { last_name: { contains: filter, mode: "insensitive" } }, + ], + }, + }, + }; + } + const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; - if(!query.where) query.where = { office_membership: officeWhereInput}; + const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; + if (!query.where) query.where = { office_membership: officeWhereInput }; query.where.office_membership = officeWhereInput; //call service to get prisma entity diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index 629add48..695725dd 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -27,6 +27,20 @@ export default class UsersController extends ApiController { query = JSON.parse(req.query["q"] as string); } + if (req.query["search"] && typeof req.query["search"] === "string") { + const filter = req.query["search"]; + query = { + where: { + contact: { + OR: [ + { first_name: { contains: filter, mode: "insensitive" } }, + { last_name: { contains: filter, mode: "insensitive" } }, + ], + }, + }, + }; + } + //call service to get prisma entity const usersEntities = await this.usersService.get(query);