|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { PrismaService } from '../global/prisma.service'; |
| 3 | +import { payment_status, Prisma } from '@prisma/client'; |
| 4 | +import { uniq } from 'lodash'; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class PaymentsService { |
| 8 | + constructor(private readonly prisma: PrismaService) {} |
| 9 | + |
| 10 | + /** |
| 11 | + * Retrieves the payout setup status for a list of users. |
| 12 | + * |
| 13 | + * This method queries the database to determine whether each user's payout setup |
| 14 | + * is complete or still in progress. A user's payout setup is considered complete |
| 15 | + * if their tax form status is 'ACTIVE' and their payment method status is 'CONNECTED'. |
| 16 | + * |
| 17 | + * @param userIds - An array of user IDs for which to retrieve the payout setup status. |
| 18 | + * @returns A promise that resolves to an object containing two arrays: |
| 19 | + * - `complete`: An array of user IDs whose payout setup is complete. |
| 20 | + * - `inProgress`: An array of user IDs whose payout setup is still in progress. |
| 21 | + * |
| 22 | + * @throws Will throw an error if the database query fails. |
| 23 | + */ |
| 24 | + private async getUsersPayoutStatus(userIds: string[]) { |
| 25 | + const usersPayoutStatus = await this.prisma.$queryRaw< |
| 26 | + { |
| 27 | + userId: string; |
| 28 | + setupComplete: boolean; |
| 29 | + }[] |
| 30 | + >` |
| 31 | + SELECT |
| 32 | + upm.user_id as "userId", |
| 33 | + CASE WHEN utx.tax_form_status = 'ACTIVE' AND upm.status = 'CONNECTED' THEN TRUE ELSE FALSE END as "setupComplete" |
| 34 | + FROM user_payment_methods upm |
| 35 | + LEFT JOIN user_tax_form_associations utx ON upm.user_id = utx.user_id AND utx.tax_form_status = 'ACTIVE' |
| 36 | + WHERE upm.user_id IN (${Prisma.join(uniq(userIds))}) |
| 37 | + `; |
| 38 | + |
| 39 | + const setupStatusMap = { |
| 40 | + complete: [] as string[], |
| 41 | + inProgress: [] as string[], |
| 42 | + }; |
| 43 | + |
| 44 | + usersPayoutStatus.forEach((user) => { |
| 45 | + setupStatusMap[user.setupComplete ? 'complete' : 'inProgress'].push( |
| 46 | + user.userId, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + return setupStatusMap; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Updates the payment status of users' payments based on the provided user IDs and the desired status. |
| 55 | + * |
| 56 | + * @param userIds - An array of user IDs whose payment statuses need to be updated. |
| 57 | + * @param setOnHold - A boolean indicating whether to set the payment status to "ON_HOLD" (true) |
| 58 | + * or revert it to "OWED" (false). |
| 59 | + * @returns A raw Prisma query that updates the payment statuses in the database. |
| 60 | + * |
| 61 | + * The function performs the following: |
| 62 | + * - Updates the `payment_status` field in the `payment` table. |
| 63 | + * - Changes the status to "ON_HOLD" if `setOnHold` is true, or to "OWED" if `setOnHold` is false. |
| 64 | + * - Ensures that only payments associated with the specified users' winnings are updated. |
| 65 | + */ |
| 66 | + private updateUserPaymentsStatus(userIds: string[], setOnHold: boolean) { |
| 67 | + return this.prisma.$queryRaw` |
| 68 | + UPDATE payment |
| 69 | + SET payment_status = ${setOnHold ? payment_status.ON_HOLD : payment_status.OWED}::payment_status |
| 70 | + FROM winnings w |
| 71 | + WHERE payment.payment_status = ${setOnHold ? payment_status.OWED : payment_status.ON_HOLD}::payment_status |
| 72 | + AND payment.winnings_id = w.winning_id |
| 73 | + AND w.winner_id IN (${Prisma.join(uniq(userIds))}); |
| 74 | + `; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Reconciles the payment statuses of the specified users by updating their payment records. |
| 79 | + * |
| 80 | + * @param userIds - A list of user IDs whose payment statuses need to be reconciled. |
| 81 | + * |
| 82 | + * The method performs the following steps: |
| 83 | + * 1. Retrieves the payout status of the specified users. |
| 84 | + * 2. Updates the payment status for users whose payments are complete to `OWED`. |
| 85 | + * 3. Updates the payment status for users whose payments are in progress to `ON_HOLD`. |
| 86 | + * |
| 87 | + * This ensures that the payment statuses are accurately reflected in the system. |
| 88 | + */ |
| 89 | + async reconcileUserPayments(...userIds: string[]) { |
| 90 | + const usersPayoutStatus = await this.getUsersPayoutStatus(userIds); |
| 91 | + |
| 92 | + if (usersPayoutStatus.complete.length) { |
| 93 | + await this.updateUserPaymentsStatus(usersPayoutStatus.complete, false); |
| 94 | + } |
| 95 | + |
| 96 | + if (usersPayoutStatus.inProgress.length) { |
| 97 | + await this.updateUserPaymentsStatus(usersPayoutStatus.inProgress, true); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments