Skip to content
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
25 changes: 18 additions & 7 deletions src/shared/payments/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,37 @@ export class PaymentsService {
* @throws Will throw an error if the database query fails.
*/
private async getUsersPayoutStatus(userIds: string[]) {
const usersPayoutStatus = await this.prisma.$queryRaw<
let usersPayoutStatus: {
userId: string;
setupComplete: boolean;
}[] = [];

if (userIds.length > 0) {
const ids = uniq(userIds);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The use of uniq to filter userIds is a good practice to avoid redundant database queries. However, ensure that uniq is imported from a reliable library like lodash to prevent potential runtime errors.

usersPayoutStatus = await this.prisma.$queryRaw<
{
userId: string;
setupComplete: boolean;
}[]
>`
>`
WITH u(user_id) AS (
VALUES ${Prisma.join(ids.map((id) => Prisma.sql`(${id})`))}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ security]
Using Prisma.sql to safely interpolate values into the SQL query is a good practice for preventing SQL injection. However, ensure that ids is properly sanitized and validated before this step.

)
SELECT
upm.user_id as "userId",
u.user_id as "userId",
CASE
WHEN utx.tax_form_status = 'ACTIVE'
AND upm.status = 'CONNECTED'
AND uiv.verification_status::text = 'ACTIVE'
THEN TRUE
ELSE FALSE
END as "setupComplete"
FROM user_payment_methods upm
LEFT JOIN user_tax_form_associations utx ON upm.user_id = utx.user_id AND utx.tax_form_status = 'ACTIVE'
LEFT JOIN user_identity_verification_associations uiv ON upm.user_id = uiv.user_id
WHERE upm.user_id IN (${Prisma.join(uniq(userIds))})
FROM u
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ correctness]
The alias u for the table is not defined in the FROM clause. Ensure that u is a valid alias for a table or subquery. This could lead to a runtime error if u is not properly defined.

LEFT JOIN user_payment_methods upm ON u.user_id = upm.user_id
LEFT JOIN user_tax_form_associations utx ON u.user_id = utx.user_id AND utx.tax_form_status = 'ACTIVE'
LEFT JOIN user_identity_verification_associations uiv ON u.user_id = uiv.user_id
`;
}

const setupStatusMap = {
complete: [] as string[],
Expand Down
Loading