diff --git a/prisma/migrations/20250423135831_truncate_payment_methods/migration.sql b/prisma/migrations/20250423135831_truncate_payment_methods/migration.sql index 3eff7e0..8be6a35 100644 --- a/prisma/migrations/20250423135831_truncate_payment_methods/migration.sql +++ b/prisma/migrations/20250423135831_truncate_payment_methods/migration.sql @@ -7,5 +7,11 @@ ALTER TABLE "paypal_payment_method" ALTER COLUMN "user_payment_method_id" DROP N UPDATE payoneer_payment_method SET user_payment_method_id = NULL; UPDATE paypal_payment_method SET user_payment_method_id = NULL; -DELETE FROM user_default_payment_method; -DELETE FROM user_payment_methods; \ No newline at end of file +DROP TABLE user_default_payment_method; + +DELETE FROM user_payment_methods +WHERE payment_method_id NOT IN ( + SELECT payment_method_id + FROM payment_method + WHERE payment_method_type IN ('Wipro Payroll', 'Trolley') +); diff --git a/src/api/payment-providers/trolley.service.ts b/src/api/payment-providers/trolley.service.ts index 5fce55f..a4bacfe 100644 --- a/src/api/payment-providers/trolley.service.ts +++ b/src/api/payment-providers/trolley.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { UserInfo } from 'src/dto/user.dto'; import { TrolleyService as Trolley } from 'src/shared/global/trolley.service'; import { PrismaService } from 'src/shared/global/prisma.service'; @@ -157,6 +157,12 @@ export class TrolleyService { * @returns A URL string to the Trolley user portal. */ async getPortalUrlForUser(user: UserInfo) { + if (user.email.toLowerCase().indexOf('@wipro.com') > -1) { + throw new BadRequestException( + 'Please contact Topgear support to withdrawal your payments', + ); + } + const recipient = await this.getPayeeRecipient(user); const link = this.trolley.getRecipientPortalUrl({ email: user.email, diff --git a/src/api/winnings/winnings.service.ts b/src/api/winnings/winnings.service.ts index e1fd8c9..3aae5ff 100644 --- a/src/api/winnings/winnings.service.ts +++ b/src/api/winnings/winnings.service.ts @@ -1,5 +1,10 @@ import { Injectable, HttpStatus, Logger } from '@nestjs/common'; -import { Prisma, payment, payment_status } from '@prisma/client'; +import { + Prisma, + payment, + payment_method_status, + payment_status, +} from '@prisma/client'; import { PrismaService } from 'src/shared/global/prisma.service'; @@ -28,6 +33,47 @@ export class WinningsService { private readonly originRepo: OriginRepository, ) {} + private async setPayrollPaymentMethod(userId: string) { + const payrollPaymentMethod = await this.prisma.payment_method.findFirst({ + where: { + payment_method_type: 'Wipro Payroll', + }, + }); + + if (!payrollPaymentMethod) { + this.logger.error(`Failed to retrieve Wipro Payroll payment method!`); + return; + } + + if ( + await this.prisma.user_payment_methods.findFirst({ + where: { + user_id: userId, + payment_method_id: payrollPaymentMethod.payment_method_id, + }, + }) + ) { + return; + } + + this.logger.debug(`Enrolling wipro user ${userId} with Wipro Payroll.`); + + try { + await this.prisma.user_payment_methods.create({ + data: { + user_id: userId, + status: payment_method_status.CONNECTED, + payment_method_id: payrollPaymentMethod.payment_method_id, + }, + }); + } catch (error) { + this.logger.error( + `Failed to enroll wipro user ${userId} with Wipro Payrol! ${error.message}`, + error, + ); + } + } + /** * Create winnings with parameters * @param body the request body @@ -97,6 +143,7 @@ export class WinningsService { if (payrollPayment) { paymentModel.payment_status = PaymentStatus.PAID; + await this.setPayrollPaymentMethod(body.winnerId); } winningModel.payment.create.push(paymentModel);