-
Notifications
You must be signed in to change notification settings - Fork 0
PM-1161 - reconcile payments on user details updates #39
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './payments.module'; | ||
export * from './payments.service'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PaymentsService } from './payments.service'; | ||
|
||
@Module({ | ||
providers: [PaymentsService], | ||
exports: [PaymentsService], | ||
}) | ||
export class PaymentsModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../global/prisma.service'; | ||
import { payment_status, Prisma } from '@prisma/client'; | ||
import { uniq } from 'lodash'; | ||
|
||
@Injectable() | ||
export class PaymentsService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
/** | ||
* Retrieves the payout setup status for a list of users. | ||
* | ||
* This method queries the database to determine whether each user's payout setup | ||
* is complete or still in progress. A user's payout setup is considered complete | ||
* if their tax form status is 'ACTIVE' and their payment method status is 'CONNECTED'. | ||
* | ||
* @param userIds - An array of user IDs for which to retrieve the payout setup status. | ||
* @returns A promise that resolves to an object containing two arrays: | ||
* - `complete`: An array of user IDs whose payout setup is complete. | ||
* - `inProgress`: An array of user IDs whose payout setup is still in progress. | ||
* | ||
* @throws Will throw an error if the database query fails. | ||
*/ | ||
private async getUsersPayoutStatus(userIds: string[]) { | ||
const usersPayoutStatus = await this.prisma.$queryRaw< | ||
{ | ||
userId: string; | ||
setupComplete: boolean; | ||
}[] | ||
>` | ||
SELECT | ||
upm.user_id as "userId", | ||
CASE WHEN utx.tax_form_status = 'ACTIVE' AND upm.status = 'CONNECTED' 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' | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
WHERE upm.user_id IN (${Prisma.join(uniq(userIds))}) | ||
`; | ||
|
||
const setupStatusMap = { | ||
complete: [] as string[], | ||
inProgress: [] as string[], | ||
}; | ||
|
||
usersPayoutStatus.forEach((user) => { | ||
setupStatusMap[user.setupComplete ? 'complete' : 'inProgress'].push( | ||
user.userId, | ||
); | ||
}); | ||
|
||
return setupStatusMap; | ||
} | ||
|
||
/** | ||
* Updates the payment status of users' payments based on the provided user IDs and the desired status. | ||
* | ||
* @param userIds - An array of user IDs whose payment statuses need to be updated. | ||
* @param setOnHold - A boolean indicating whether to set the payment status to "ON_HOLD" (true) | ||
* or revert it to "OWED" (false). | ||
* @returns A raw Prisma query that updates the payment statuses in the database. | ||
* | ||
* The function performs the following: | ||
* - Updates the `payment_status` field in the `payment` table. | ||
* - Changes the status to "ON_HOLD" if `setOnHold` is true, or to "OWED" if `setOnHold` is false. | ||
* - Ensures that only payments associated with the specified users' winnings are updated. | ||
*/ | ||
private updateUserPaymentsStatus(userIds: string[], setOnHold: boolean) { | ||
return this.prisma.$queryRaw` | ||
UPDATE payment | ||
SET payment_status = ${setOnHold ? payment_status.ON_HOLD : payment_status.OWED}::payment_status | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FROM winnings w | ||
WHERE payment.payment_status = ${setOnHold ? payment_status.OWED : payment_status.ON_HOLD}::payment_status | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AND payment.winnings_id = w.winning_id | ||
AND w.winner_id IN (${Prisma.join(uniq(userIds))}); | ||
`; | ||
} | ||
|
||
/** | ||
* Reconciles the payment statuses of the specified users by updating their payment records. | ||
* | ||
* @param userIds - A list of user IDs whose payment statuses need to be reconciled. | ||
* | ||
* The method performs the following steps: | ||
* 1. Retrieves the payout status of the specified users. | ||
* 2. Updates the payment status for users whose payments are complete to `OWED`. | ||
* 3. Updates the payment status for users whose payments are in progress to `ON_HOLD`. | ||
* | ||
* This ensures that the payment statuses are accurately reflected in the system. | ||
*/ | ||
async reconcileUserPayments(...userIds: string[]) { | ||
const usersPayoutStatus = await this.getUsersPayoutStatus(userIds); | ||
|
||
if (usersPayoutStatus.complete.length) { | ||
await this.updateUserPaymentsStatus(usersPayoutStatus.complete, false); | ||
} | ||
|
||
if (usersPayoutStatus.inProgress.length) { | ||
await this.updateUserPaymentsStatus(usersPayoutStatus.inProgress, true); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.