Skip to content

Commit d815017

Browse files
authored
Merge pull request #38 from topcoder-platform/PM-1155_update-winnings-endpoints
PM-1155 - add payment status to winnings
2 parents f4e6eba + 1680290 commit d815017

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/api/repository/winnings.repo.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { HttpStatus, Injectable } from '@nestjs/common';
2-
import { payment_status, Prisma, winnings_category } from '@prisma/client';
2+
import {
3+
payment_status,
4+
Prisma,
5+
winnings,
6+
winnings_category,
7+
} from '@prisma/client';
8+
import { uniq } from 'lodash';
39
import { ResponseDto } from 'src/dto/api-response.dto';
410
import { DateFilterType } from 'src/dto/date-filter.type';
511
import { PaymentStatus } from 'src/dto/payment.dto';
@@ -110,6 +116,32 @@ export class WinningsRepository {
110116
return orderBy;
111117
}
112118

119+
private async getUsersPayoutStatusForWinnings(winnings: winnings[]) {
120+
const usersPayoutStatus = await this.prisma.$queryRaw<
121+
{
122+
userId: string;
123+
taxFormSetupComplete: boolean;
124+
paymentMethodSetupComplete: boolean;
125+
}[]
126+
>`
127+
SELECT
128+
upm.user_id as "userId",
129+
CASE WHEN utx.tax_form_status = 'ACTIVE' THEN TRUE ELSE FALSE END as "taxFormSetupComplete",
130+
CASE WHEN upm.status = 'CONNECTED' THEN TRUE ELSE FALSE END as "payoutSetupComplete"
131+
FROM user_payment_methods upm
132+
LEFT JOIN user_tax_form_associations utx ON upm.user_id = utx.user_id
133+
WHERE upm.user_id IN (${Prisma.join(uniq(winnings.map((w) => w.winner_id)))})
134+
`;
135+
136+
return usersPayoutStatus.reduce(
137+
(map, userPayoutStatus) =>
138+
Object.assign(map, {
139+
[userPayoutStatus.userId]: { ...userPayoutStatus, userId: undefined },
140+
}),
141+
{},
142+
);
143+
}
144+
113145
/**
114146
* Search winnings with parameters
115147
* @param searchProps the request body
@@ -168,6 +200,9 @@ export class WinningsRepository {
168200
this.prisma.winnings.count({ where: queryWhere }),
169201
]);
170202

203+
const usersPayoutStatusMap =
204+
await this.getUsersPayoutStatusForWinnings(winnings);
205+
171206
result.data = {
172207
winnings: winnings.map((item) => ({
173208
id: item.winning_id,
@@ -197,6 +232,7 @@ export class WinningsRepository {
197232
item.payment?.[0].updated_at ??
198233
undefined) as Date,
199234
releaseDate: item.payment?.[0]?.release_date as Date,
235+
paymentStatus: usersPayoutStatusMap[item.winner_id],
200236
})),
201237
pagination: {
202238
totalItems: count,

src/dto/winning.dto.ts

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export enum WinningsCategory {
9898
TASK_COPILOT_PAYMENT = 'TASK_COPILOT_PAYMENT',
9999
}
100100

101+
export class PayoutStatus {
102+
payoutSetupComplete: boolean;
103+
taxFormSetupComplete: boolean;
104+
}
105+
101106
export class WinningDto {
102107
id: string;
103108
type: string;
@@ -109,6 +114,7 @@ export class WinningDto {
109114
externalId: string;
110115
attributes: object;
111116
details: PaymentDetailDto[];
117+
paymentStatus: PayoutStatus;
112118
createdAt: Date;
113119
updatedAt: Date;
114120
releaseDate: Date;

0 commit comments

Comments
 (0)