From 973414984801fa0cb7396d9a0a97200fde8387e8 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 20 Jan 2025 09:44:03 +0100 Subject: [PATCH 1/2] extend controller for search --- src/controller/admin/club/memberController.ts | 4 +++- src/helpers/newsletterHelper.ts | 2 +- src/service/club/member/memberService.ts | 24 ++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/controller/admin/club/memberController.ts b/src/controller/admin/club/memberController.ts index 807a911..bfb952a 100644 --- a/src/controller/admin/club/memberController.ts +++ b/src/controller/admin/club/memberController.ts @@ -61,8 +61,10 @@ 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 search = (req.query.search as string) ?? ""; + let noLimit = req.query.noLimit === "true"; + let ids = (req.query.ids as string).split(",").map((i) => parseInt(i)); - let [members, total] = await MemberService.getAll(offset, count, search); + let [members, total] = await MemberService.getAll({ offset, count, search, noLimit, ids }); res.json({ members: MemberFactory.mapToBase(members), diff --git a/src/helpers/newsletterHelper.ts b/src/helpers/newsletterHelper.ts index eed0fc5..cfdeb76 100644 --- a/src/helpers/newsletterHelper.ts +++ b/src/helpers/newsletterHelper.ts @@ -141,7 +141,7 @@ export abstract class NewsletterHelper { } } - let members = await MemberService.getAll(0, 1000); + let members = await MemberService.getAll({ noLimit: true }); return members[0].filter((m) => queryMemberIds.includes(m.id)); } diff --git a/src/service/club/member/memberService.ts b/src/service/club/member/memberService.ts index 969cbb9..c7da55e 100644 --- a/src/service/club/member/memberService.ts +++ b/src/service/club/member/memberService.ts @@ -8,7 +8,19 @@ export default abstract class MemberService { * @description get all members * @returns {Promise<[Array, number]>} */ - static async getAll(offset: number = 0, count: number = 25, search: string = ""): Promise<[Array, number]> { + static async getAll({ + offset = 0, + count = 25, + search = "", + noLimit = false, + ids = [], + }: { + offset?: number; + count?: number; + search?: string; + noLimit?: boolean; + ids?: Array; + }): Promise<[Array, number]> { let query = dataSource .getRepository(member) .createQueryBuilder("member") @@ -54,9 +66,15 @@ export default abstract class MemberService { }); } + if (ids.length != 0) { + query = query.where({ id: ids }); + } + + if (!noLimit) { + query = query.offset(offset).limit(count); + } + return await query - .offset(offset) - .limit(count) .orderBy("member.lastname") .addOrderBy("member.firstname") .addOrderBy("member.nameaffix") -- 2.45.2 From 0bbe22e9ae2bf841f52491b9c95c1026b8c4a9a1 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 20 Jan 2025 12:25:57 +0100 Subject: [PATCH 2/2] fix: undefined value on ids search --- src/controller/admin/club/memberController.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/controller/admin/club/memberController.ts b/src/controller/admin/club/memberController.ts index bfb952a..d4a9f7e 100644 --- a/src/controller/admin/club/memberController.ts +++ b/src/controller/admin/club/memberController.ts @@ -62,7 +62,10 @@ export async function getAllMembers(req: Request, res: Response): Promise { let count = parseInt((req.query.count as string) ?? "25"); let search = (req.query.search as string) ?? ""; let noLimit = req.query.noLimit === "true"; - let ids = (req.query.ids as string).split(",").map((i) => parseInt(i)); + let ids = ((req.query.ids ?? "") as string) + .split(",") + .filter((i) => i) + .map((i) => parseInt(i)); let [members, total] = await MemberService.getAll({ offset, count, search, noLimit, ids }); -- 2.45.2