chore: add referral logic to first payment
This commit is contained in:
@@ -10,6 +10,11 @@ import { DataSource, Not, QueryRunner } from "typeorm";
|
||||
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||
import { CommonMessage, PaymentMessage, UserMessage, WalletMessage } from "../../../common/enums/message.enum";
|
||||
import { NotificationsService } from "../../notifications/providers/notifications.service";
|
||||
import { ReferralReward } from "../../referrals/entities/referral-reward.entity";
|
||||
import { Referral } from "../../referrals/entities/referral.entity";
|
||||
import { ReferralRewardStatus } from "../../referrals/enums/referral-reward-status.enum";
|
||||
import { ReferralStatus } from "../../referrals/enums/referral-status.enum";
|
||||
// import { ReferralsService } from "../../referrals/providers/referrals.service";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { WalletsService } from "../../wallets/providers/wallets.service";
|
||||
@@ -47,6 +52,7 @@ export class PaymentsService {
|
||||
private readonly walletsService: WalletsService,
|
||||
private readonly depositRequestsRepository: DepositRequestsRepository,
|
||||
private dataSource: DataSource,
|
||||
// private readonly referralsService: ReferralsService,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
@@ -253,11 +259,69 @@ export class PaymentsService {
|
||||
async handleFailedPayment(paymentId: string, queryRunner: QueryRunner) {
|
||||
await queryRunner.manager.update(Payment, { id: paymentId }, { status: PaymentStatus.CANCELLED });
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
private async isFirstPurchase(userId: string, queryRunner: QueryRunner): Promise<boolean> {
|
||||
const paymentCount = await queryRunner.manager.count(Payment, {
|
||||
where: { user: { id: userId }, status: PaymentStatus.COMPLETED },
|
||||
});
|
||||
return paymentCount === 0;
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
private async getPendingReferral(userId: string, queryRunner: QueryRunner): Promise<Referral | null> {
|
||||
return queryRunner.manager.findOne(Referral, {
|
||||
where: {
|
||||
referredUser: { id: userId },
|
||||
status: ReferralStatus.PENDING,
|
||||
},
|
||||
relations: { referrer: true },
|
||||
});
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
private async handleReferralReward(payment: Payment, queryRunner: QueryRunner) {
|
||||
const isFirstPurchase = await this.isFirstPurchase(payment.user.id, queryRunner);
|
||||
if (!isFirstPurchase) return;
|
||||
|
||||
const referral = await this.getPendingReferral(payment.user.id, queryRunner);
|
||||
if (!referral) return;
|
||||
|
||||
// Reward amount is 10% of the purchase amount
|
||||
const rewardAmount = new Decimal(payment.amount).mul(0.1);
|
||||
|
||||
// Create reward record
|
||||
const reward = queryRunner.manager.create(ReferralReward, {
|
||||
user: { id: referral.referrer.id },
|
||||
referral: { id: referral.id },
|
||||
amount: rewardAmount,
|
||||
status: ReferralRewardStatus.PENDING,
|
||||
});
|
||||
await queryRunner.manager.save(reward);
|
||||
|
||||
// Charge referrer's wallet
|
||||
await this.walletsService.createReferralRewardTransaction(referral.referrer.id, rewardAmount, queryRunner);
|
||||
|
||||
// Update reward status
|
||||
reward.status = ReferralRewardStatus.PAID;
|
||||
reward.paidAt = new Date();
|
||||
await queryRunner.manager.save(reward);
|
||||
|
||||
// Update referral status
|
||||
referral.status = ReferralStatus.COMPLETED;
|
||||
referral.completedAt = new Date();
|
||||
await queryRunner.manager.save(referral);
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async handleSuccessfulPayment(payment: Payment, userId: string, amount: Decimal, transactionId: string, queryRunner: QueryRunner) {
|
||||
await queryRunner.manager.update(Payment, { id: payment.id }, { status: PaymentStatus.COMPLETED, transactionId });
|
||||
return this.walletsService.createPaymentTransaction(userId, amount, payment, queryRunner);
|
||||
const result = await this.walletsService.createPaymentTransaction(userId, amount, payment, queryRunner);
|
||||
|
||||
// Handle referral reward if applicable
|
||||
await this.handleReferralReward(payment, queryRunner);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
Reference in New Issue
Block a user