chore: update referral logic
This commit is contained in:
@@ -2,6 +2,7 @@ import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { BadRequestException, HttpException, HttpStatus, Injectable, InternalServerErrorException } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Queue } from "bullmq";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { FastifyReply } from "fastify";
|
||||
@@ -10,11 +11,9 @@ 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 { 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";
|
||||
@@ -51,8 +50,8 @@ export class PaymentsService {
|
||||
private readonly bankAccountsRepository: BankAccountsRepository,
|
||||
private readonly walletsService: WalletsService,
|
||||
private readonly depositRequestsRepository: DepositRequestsRepository,
|
||||
private readonly referralsService: ReferralsService,
|
||||
private dataSource: DataSource,
|
||||
// private readonly referralsService: ReferralsService,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
@@ -261,55 +260,38 @@ export class PaymentsService {
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
private async isFirstPurchase(userId: string, queryRunner: QueryRunner): Promise<boolean> {
|
||||
private async isFirstPurchase(userId: string, queryRunner: QueryRunner) {
|
||||
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);
|
||||
const referral = await this.referralsService.getPendingReferral(payment.user.id, queryRunner);
|
||||
if (!referral) return;
|
||||
|
||||
// Reward amount is 10% of the purchase amount
|
||||
// 10%
|
||||
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);
|
||||
const reward = await this.referralsService.createReferralReward(referral.referrer.id, referral.id, rewardAmount, queryRunner);
|
||||
|
||||
// Charge referrer's wallet
|
||||
await this.walletsService.createReferralRewardTransaction(referral.referrer.id, rewardAmount, queryRunner);
|
||||
|
||||
// Update reward status
|
||||
reward.status = ReferralRewardStatus.PAID;
|
||||
reward.paidAt = new Date();
|
||||
reward.paidAt = dayjs().toDate();
|
||||
await queryRunner.manager.save(reward);
|
||||
|
||||
// Update referral status
|
||||
referral.status = ReferralStatus.COMPLETED;
|
||||
referral.completedAt = new Date();
|
||||
referral.completedAt = dayjs().toDate();
|
||||
await queryRunner.manager.save(referral);
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
Reference in New Issue
Block a user