Skip to content

Commit bacb196

Browse files
authored
Merge pull request #21 from topcoder-platform/PM-921_qa-fixes
PM-921 qa fixes
2 parents c13faa4 + 8f87311 commit bacb196

File tree

1 file changed

+25
-73
lines changed

1 file changed

+25
-73
lines changed

src/api/admin-winning/adminWinning.service.ts

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,24 @@ export class AdminWinningService {
5050

5151
try {
5252
let winnerIds: string[] | undefined;
53+
let externalIds: string[] | undefined;
5354
if (body.winnerId) {
5455
winnerIds = [body.winnerId];
5556
} else if (body.winnerIds) {
5657
winnerIds = [...body.winnerIds];
58+
} else if (body.externalIds?.length > 0) {
59+
externalIds = body.externalIds;
5760
}
5861

59-
let query;
60-
let orderBy;
61-
62-
if (body.externalIds && body.externalIds.length > 0) {
63-
query = this.getQueryByExternalIDs(body);
64-
orderBy = this.getOrderByWithExternalIDs(body);
65-
} else if (winnerIds) {
66-
query = this.getQueryByWinnerId(body, winnerIds);
67-
orderBy = this.getOrderByWithWinnerId(body);
68-
} else {
69-
query = this.getQueryByWinnerId(body, undefined);
70-
orderBy = this.getOrderByWithExternalIDs(body);
71-
}
62+
const queryWhere = this.getQueryByWinnerId(body, winnerIds, externalIds);
63+
const orderBy = this.getOrderByWithWinnerId(
64+
body,
65+
!winnerIds && !!externalIds?.length,
66+
);
7267

7368
const [winnings, count] = await this.prisma.$transaction([
7469
this.prisma.winnings.findMany({
75-
...query,
70+
...queryWhere,
7671
include: {
7772
payment: {
7873
where: {
@@ -90,7 +85,7 @@ export class AdminWinningService {
9085
skip: body.offset,
9186
take: body.limit,
9287
}),
93-
this.prisma.winnings.count({ where: query.where }),
88+
this.prisma.winnings.count({ where: queryWhere.where }),
9489
]);
9590

9691
result.data = {
@@ -173,6 +168,7 @@ export class AdminWinningService {
173168
private getQueryByWinnerId(
174169
body: WinningRequestDto,
175170
winnerIds: string[] | undefined,
171+
externalIds: string[] | undefined,
176172
) {
177173
const filterDate: object | undefined = this.generateFilterDate(body);
178174

@@ -183,6 +179,11 @@ export class AdminWinningService {
183179
in: winnerIds,
184180
}
185181
: undefined,
182+
external_id: externalIds
183+
? {
184+
in: body.externalIds,
185+
}
186+
: undefined,
186187
category: body.type
187188
? {
188189
equals: body.type,
@@ -213,70 +214,21 @@ export class AdminWinningService {
213214
return query;
214215
}
215216

216-
private getOrderByWithWinnerId(body: WinningRequestDto) {
217-
let orderBy: object = [
217+
private getOrderByWithWinnerId(
218+
body: WinningRequestDto,
219+
externalIds?: boolean,
220+
) {
221+
const orderBy: object = [
218222
{
219223
created_at: 'desc',
220224
},
221-
{
222-
external_id: 'asc',
223-
},
225+
...(externalIds ? [{ external_id: 'asc' }] : []),
224226
];
225-
if (body.sortBy && body.sortOrder) {
226-
orderBy = [
227-
{
228-
[body.sortBy]: body.sortOrder.toString(),
229-
},
230-
{
231-
external_id: 'asc',
232-
},
233-
];
234-
}
235-
236-
return orderBy;
237-
}
238-
239-
private getQueryByExternalIDs(body: WinningRequestDto) {
240-
const filterDate: object | undefined = this.generateFilterDate(body);
241-
242-
const query = {
243-
where: {
244-
external_id: {
245-
in: body.externalIds,
246-
},
247-
category: body.type
248-
? {
249-
equals: body.type,
250-
}
251-
: undefined,
252-
created_at: filterDate,
253-
payment: body.status
254-
? {
255-
some: {
256-
payment_status: {
257-
equals: body.status,
258-
},
259-
},
260-
}
261-
: undefined,
262-
},
263-
};
264-
265-
return query;
266-
}
267227

268-
private getOrderByWithExternalIDs(body: WinningRequestDto) {
269-
let orderBy: object = [
270-
{
271-
created_at: 'desc',
272-
},
273-
];
274228
if (body.sortBy && body.sortOrder) {
275-
orderBy = [
276-
{
277-
[body.sortBy]: body.sortOrder.toString(),
278-
},
279-
];
229+
orderBy[0] = {
230+
[body.sortBy]: body.sortOrder.toString(),
231+
};
280232
}
281233

282234
return orderBy;

0 commit comments

Comments
 (0)