From 0a3dbdfb10c5615e599d827d2f96edce0d29c349 Mon Sep 17 00:00:00 2001 From: Vasilica Olariu Date: Thu, 10 Apr 2025 14:58:24 +0300 Subject: [PATCH 1/2] PM-1038 - fix search by externalid --- src/api/admin-winning/adminWinning.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/admin-winning/adminWinning.service.ts b/src/api/admin-winning/adminWinning.service.ts index adcdb59..14717ee 100644 --- a/src/api/admin-winning/adminWinning.service.ts +++ b/src/api/admin-winning/adminWinning.service.ts @@ -59,12 +59,12 @@ export class AdminWinningService { let query; let orderBy; - if (body.externalIds && body.externalIds.length > 0) { - query = this.getQueryByExternalIDs(body); - orderBy = this.getOrderByWithExternalIDs(body); - } else if (winnerIds) { + if (winnerIds?.length) { query = this.getQueryByWinnerId(body, winnerIds); orderBy = this.getOrderByWithWinnerId(body); + } else if (body.externalIds && body.externalIds.length > 0) { + query = this.getQueryByExternalIDs(body); + orderBy = this.getOrderByWithExternalIDs(body); } else { query = this.getQueryByWinnerId(body, undefined); orderBy = this.getOrderByWithExternalIDs(body); From 8f873117bfdd8a251473ae07753d94cd8c0ec370 Mon Sep 17 00:00:00 2001 From: Vasilica Olariu Date: Thu, 10 Apr 2025 15:31:38 +0300 Subject: [PATCH 2/2] update search winnings --- src/api/admin-winning/adminWinning.service.ts | 98 +++++-------------- 1 file changed, 25 insertions(+), 73 deletions(-) diff --git a/src/api/admin-winning/adminWinning.service.ts b/src/api/admin-winning/adminWinning.service.ts index 14717ee..3326f75 100644 --- a/src/api/admin-winning/adminWinning.service.ts +++ b/src/api/admin-winning/adminWinning.service.ts @@ -50,29 +50,24 @@ export class AdminWinningService { try { let winnerIds: string[] | undefined; + let externalIds: string[] | undefined; if (body.winnerId) { winnerIds = [body.winnerId]; } else if (body.winnerIds) { winnerIds = [...body.winnerIds]; + } else if (body.externalIds?.length > 0) { + externalIds = body.externalIds; } - let query; - let orderBy; - - if (winnerIds?.length) { - query = this.getQueryByWinnerId(body, winnerIds); - orderBy = this.getOrderByWithWinnerId(body); - } else if (body.externalIds && body.externalIds.length > 0) { - query = this.getQueryByExternalIDs(body); - orderBy = this.getOrderByWithExternalIDs(body); - } else { - query = this.getQueryByWinnerId(body, undefined); - orderBy = this.getOrderByWithExternalIDs(body); - } + const queryWhere = this.getQueryByWinnerId(body, winnerIds, externalIds); + const orderBy = this.getOrderByWithWinnerId( + body, + !winnerIds && !!externalIds?.length, + ); const [winnings, count] = await this.prisma.$transaction([ this.prisma.winnings.findMany({ - ...query, + ...queryWhere, include: { payment: { where: { @@ -90,7 +85,7 @@ export class AdminWinningService { skip: body.offset, take: body.limit, }), - this.prisma.winnings.count({ where: query.where }), + this.prisma.winnings.count({ where: queryWhere.where }), ]); result.data = { @@ -173,6 +168,7 @@ export class AdminWinningService { private getQueryByWinnerId( body: WinningRequestDto, winnerIds: string[] | undefined, + externalIds: string[] | undefined, ) { const filterDate: object | undefined = this.generateFilterDate(body); @@ -183,6 +179,11 @@ export class AdminWinningService { in: winnerIds, } : undefined, + external_id: externalIds + ? { + in: body.externalIds, + } + : undefined, category: body.type ? { equals: body.type, @@ -213,70 +214,21 @@ export class AdminWinningService { return query; } - private getOrderByWithWinnerId(body: WinningRequestDto) { - let orderBy: object = [ + private getOrderByWithWinnerId( + body: WinningRequestDto, + externalIds?: boolean, + ) { + const orderBy: object = [ { created_at: 'desc', }, - { - external_id: 'asc', - }, + ...(externalIds ? [{ external_id: 'asc' }] : []), ]; - if (body.sortBy && body.sortOrder) { - orderBy = [ - { - [body.sortBy]: body.sortOrder.toString(), - }, - { - external_id: 'asc', - }, - ]; - } - - return orderBy; - } - - private getQueryByExternalIDs(body: WinningRequestDto) { - const filterDate: object | undefined = this.generateFilterDate(body); - - const query = { - where: { - external_id: { - in: body.externalIds, - }, - category: body.type - ? { - equals: body.type, - } - : undefined, - created_at: filterDate, - payment: body.status - ? { - some: { - payment_status: { - equals: body.status, - }, - }, - } - : undefined, - }, - }; - - return query; - } - private getOrderByWithExternalIDs(body: WinningRequestDto) { - let orderBy: object = [ - { - created_at: 'desc', - }, - ]; if (body.sortBy && body.sortOrder) { - orderBy = [ - { - [body.sortBy]: body.sortOrder.toString(), - }, - ]; + orderBy[0] = { + [body.sortBy]: body.sortOrder.toString(), + }; } return orderBy;