From b68e5467880ae5d2170eefd6a176c4e2e0e6b70b Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 3 Jan 2025 15:20:05 +0100 Subject: [PATCH 1/2] member pagination search --- .gitignore | 4 +++- src/controller/admin/memberController.ts | 4 +++- src/service/memberService.ts | 24 +++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a7fe266..89904f3 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,6 @@ dist .yarn/install-state.gz .pnp.* -export \ No newline at end of file +export + +.idea \ No newline at end of file diff --git a/src/controller/admin/memberController.ts b/src/controller/admin/memberController.ts index 8728311..c60ecc3 100644 --- a/src/controller/admin/memberController.ts +++ b/src/controller/admin/memberController.ts @@ -58,7 +58,9 @@ import CommunicationCommandHandler from "../../command/communicationCommandHandl export async function getAllMembers(req: Request, res: Response): Promise { let offset = parseInt((req.query.offset as string) ?? "0"); let count = parseInt((req.query.count as string) ?? "25"); - let [members, total] = await MemberService.getAll(offset, count); + let search = (req.query.search as string) ?? ""; + + let [members, total] = await MemberService.getAll(offset, count, search); res.json({ members: MemberFactory.mapToBase(members), diff --git a/src/service/memberService.ts b/src/service/memberService.ts index 633497d..531c4b6 100644 --- a/src/service/memberService.ts +++ b/src/service/memberService.ts @@ -8,8 +8,8 @@ export default abstract class MemberService { * @description get all members * @returns {Promise<[Array, number]>} */ - static async getAll(offset: number = 0, count: number = 25): Promise<[Array, number]> { - return await dataSource + static async getAll(offset: number = 0, count: number = 25, search: string = ""): Promise<[Array, number]> { + let query = await dataSource .getRepository(member) .createQueryBuilder("member") .leftJoinAndMapOne( @@ -36,7 +36,25 @@ export default abstract class MemberService { ) .leftJoinAndSelect("preferredCommunication.type", "communicationtype_preferred") .leftJoinAndMapMany("member.smsAlarming", "member.communications", "smsAlarming", "smsAlarming.isSMSAlarming = 1") - .leftJoinAndSelect("smsAlarming.type", "communicationtype_smsAlarming") + .leftJoinAndSelect("smsAlarming.type", "communicationtype_smsAlarming"); + + if (search != "") { + search.split(" ").forEach((term, index) => { + const searchQuery = `%${term}%`; + const dynamic = "searchQuery" + Math.random().toString(36).substring(2); + if (index == 0) { + query = query.where(`member.firstname LIKE :${dynamic} OR member.lastname LIKE :${dynamic}`, { + [dynamic]: searchQuery, + }); + } else { + query = query.orWhere(`member.firstname LIKE :${dynamic} OR member.lastname LIKE :${dynamic}`, { + [dynamic]: searchQuery, + }); + } + }); + } + + return query .offset(offset) .limit(count) .orderBy("member.lastname") -- 2.45.2 From 14677d2030e2eee2fa47af901ec478b61bf2db37 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 3 Jan 2025 15:20:22 +0100 Subject: [PATCH 2/2] fix: queryBuilder now hides undefined strings --- src/helpers/dynamicQueryBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/dynamicQueryBuilder.ts b/src/helpers/dynamicQueryBuilder.ts index 79930fd..a97e7a2 100644 --- a/src/helpers/dynamicQueryBuilder.ts +++ b/src/helpers/dynamicQueryBuilder.ts @@ -263,7 +263,7 @@ export default abstract class DynamicQueryBuilder { results = tempResults; } else { results.forEach((res) => { - res[newKey] = String(value); + if (String(value) != "undefined") res[newKey] = String(value); }); } } -- 2.45.2