From f40ab5d63706698143151f656f3ae984c7e2d0aa Mon Sep 17 00:00:00 2001 From: Disketsa - Azuya Date: Tue, 24 Sep 2024 23:01:56 +0700 Subject: [PATCH 1/2] Update contact.ts, add condition to query when search parameter not provide contact result return [], null when search parameter not defined. --- src/controllers/contact.ts | 59 ++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/controllers/contact.ts b/src/controllers/contact.ts index aa873e5..9080d56 100644 --- a/src/controllers/contact.ts +++ b/src/controllers/contact.ts @@ -3,38 +3,53 @@ import { logger } from "@/utils"; import { makePhotoURLHandler } from "./misc"; import { prisma } from "@/config/database"; import WhatsappService from "@/whatsapp/service"; +import { Prisma } from "@prisma/client"; export const list: RequestHandler = async (req, res) => { try { const { sessionId } = req.params; const { cursor = undefined, limit = 25, search } = req.query; + + // Buat whereConditions + const whereConditions: Prisma.ContactWhereInput = { + id: { endsWith: "s.whatsapp.net" }, + sessionId, + }; + + // Tambahkan kondisi OR hanya jika search ada + if (search) { + whereConditions.OR = [ + { + name: { + contains: String(search), + }, + }, + { + verifiedName: { + contains: String(search), + }, + }, + { + notify: { + contains: String(search), + }, + }, + ]; + } + + console.log("Query Parameters:", { sessionId, cursor, limit, search }); + console.log("Where Conditions:", whereConditions); + const contacts = await prisma.contact.findMany({ cursor: cursor ? { pkId: Number(cursor) } : undefined, take: Number(limit), skip: cursor ? 1 : 0, - where: { - id: { endsWith: "s.whatsapp.net" }, - sessionId, - OR: [ - { - name: { - contains: String(search), - }, - }, - { - verifiedName: { - contains: String(search), - }, - }, - { - notify: { - contains: String(search), - }, - }, - ], - }, + where: whereConditions, }); + console.log("Manual query result:", contacts); + console.log("Session ID:", sessionId); + res.status(200).json({ data: contacts, cursor: @@ -43,7 +58,7 @@ export const list: RequestHandler = async (req, res) => { : null, }); } catch (e) { - const message = "An error occured during contact list"; + const message = "An error occurred during contact list"; logger.error(e, message); res.status(500).json({ error: message }); } From 6fbdc82748994d9fb82106e2681f23bbbfaa2e42 Mon Sep 17 00:00:00 2001 From: Disketsa - Azuya Date: Tue, 24 Sep 2024 23:04:51 +0700 Subject: [PATCH 2/2] Update contact.ts, add condition to query when search parameter not provide Add OR condition if only 'search' parameter is exist --- src/controllers/contact.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/controllers/contact.ts b/src/controllers/contact.ts index 9080d56..380c7c8 100644 --- a/src/controllers/contact.ts +++ b/src/controllers/contact.ts @@ -9,14 +9,12 @@ export const list: RequestHandler = async (req, res) => { try { const { sessionId } = req.params; const { cursor = undefined, limit = 25, search } = req.query; - - // Buat whereConditions + // Create whereConditions const whereConditions: Prisma.ContactWhereInput = { id: { endsWith: "s.whatsapp.net" }, sessionId, }; - - // Tambahkan kondisi OR hanya jika search ada + // Add OR condition if only search parameter is exist if (search) { whereConditions.OR = [ { @@ -36,20 +34,12 @@ export const list: RequestHandler = async (req, res) => { }, ]; } - - console.log("Query Parameters:", { sessionId, cursor, limit, search }); - console.log("Where Conditions:", whereConditions); - const contacts = await prisma.contact.findMany({ cursor: cursor ? { pkId: Number(cursor) } : undefined, take: Number(limit), skip: cursor ? 1 : 0, where: whereConditions, }); - - console.log("Manual query result:", contacts); - console.log("Session ID:", sessionId); - res.status(200).json({ data: contacts, cursor: