diff --git a/src/shared/payments/payments.service.ts b/src/shared/payments/payments.service.ts index 047559b..e38ab7c 100644 --- a/src/shared/payments/payments.service.ts +++ b/src/shared/payments/payments.service.ts @@ -26,14 +26,24 @@ 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); + usersPayoutStatus = await this.prisma.$queryRaw< { userId: string; setupComplete: boolean; }[] - >` + >` + WITH u(user_id) AS ( + VALUES ${Prisma.join(ids.map((id) => Prisma.sql`(${id})`))} + ) SELECT - upm.user_id as "userId", + u.user_id as "userId", CASE WHEN utx.tax_form_status = 'ACTIVE' AND upm.status = 'CONNECTED' @@ -41,11 +51,12 @@ export class PaymentsService { 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 + 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[],