Skip to content

PM-921 qa fixes #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 25 additions & 73 deletions src/api/admin-winning/adminWinning.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (body.externalIds && body.externalIds.length > 0) {
query = this.getQueryByExternalIDs(body);
orderBy = this.getOrderByWithExternalIDs(body);
} else if (winnerIds) {
query = this.getQueryByWinnerId(body, winnerIds);
orderBy = this.getOrderByWithWinnerId(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: {
Expand All @@ -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 = {
Expand Down Expand Up @@ -173,6 +168,7 @@ export class AdminWinningService {
private getQueryByWinnerId(
body: WinningRequestDto,
winnerIds: string[] | undefined,
externalIds: string[] | undefined,
) {
const filterDate: object | undefined = this.generateFilterDate(body);

Expand All @@ -183,6 +179,11 @@ export class AdminWinningService {
in: winnerIds,
}
: undefined,
external_id: externalIds
? {
in: body.externalIds,
}
: undefined,
category: body.type
? {
equals: body.type,
Expand Down Expand Up @@ -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;
Expand Down