update users service

This commit is contained in:
2026-04-16 09:50:59 +03:30
parent 44114b2436
commit 36a57db33b
4 changed files with 56 additions and 18 deletions
+2
View File
@@ -23,6 +23,7 @@ import { PaymentsRepository } from "./repositories/payments.repository";
import { zarinpalConfig } from "../../configs/zarinpal.config";
import { NotificationModule } from "../notifications/notifications.module";
import { ReferralsModule } from "../referrals/referrals.module";
import { UsersModule } from "../users/users.module";
@Module({
imports: [
@@ -49,6 +50,7 @@ import { ReferralsModule } from "../referrals/referrals.module";
WalletsModule,
NotificationModule,
ReferralsModule,
UsersModule
],
controllers: [PaymentsController],
providers: [
@@ -39,6 +39,7 @@ import { DepositRequestsRepository } from "../repositories/deposit-requests.repo
import { PaymentGatewaysRepository } from "../repositories/payment-gateway.repository";
import { PaymentsRepository } from "../repositories/payments.repository";
import { GatewayType } from "../types/gateway.type";
import { UsersService } from "../../users/providers/users.service";
@Injectable()
export class PaymentsService {
private readonly logger = new Logger(PaymentsService.name);
@@ -53,8 +54,9 @@ export class PaymentsService {
private readonly walletsService: WalletsService,
private readonly depositRequestsRepository: DepositRequestsRepository,
private readonly referralsService: ReferralsService,
private readonly usersService: UsersService,
private dataSource: DataSource,
) {}
) { }
//===============================================
async chargeWalletWithGateway(chargeDto: GatewayDepositDto, userId: string) {
@@ -453,34 +455,42 @@ export class PaymentsService {
return frontUrl;
}
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 isFirstPurchase(userId: string, queryRunner: QueryRunner) {
// const paymentCount = await queryRunner.manager.count(Payment, {
// where: { user: { id: userId }, status: PaymentStatus.COMPLETED },
// });
// return paymentCount === 0;
// }
//===============================================
private async handleReferralReward(payment: Payment, queryRunner: QueryRunner) {
const isFirstPurchase = await this.isFirstPurchase(payment.user.id, queryRunner);
if (!isFirstPurchase) return;
// const isFirstPurchase = await this.isFirstPurchase(payment.user.id, queryRunner);
// if (!isFirstPurchase) return;
////
const referral = await this.referralsService.getPendingReferral(payment.user.id, queryRunner);
if (!referral) return;
// 10%
const rewardAmount = new Decimal(payment.amount).mul(0.1);
const agentId = referral.referrer.id
const resellerOwner = await this.usersService.finResellerOwnerByAgentId(agentId)
const reward = await this.referralsService.createReferralReward(referral.referrer.id, referral.id, rewardAmount, queryRunner);
// 8%
const agentRewardAmount = new Decimal(payment.amount).mul(0.08);
const resellerOwnerRewardAmount = new Decimal(payment.amount).mul(0.02);
const agentReward = await this.referralsService.createReferralReward(agentId, referral.id, agentRewardAmount, queryRunner);
const resellerOwnerReward = await this.referralsService.createReferralReward(agentId, resellerOwner.id, resellerOwnerRewardAmount, queryRunner);
// Charge referrer's wallet
await this.walletsService.createReferralRewardTransaction(referral.referrer.id, rewardAmount, queryRunner);
await this.walletsService.createReferralRewardTransaction(agentId, agentRewardAmount, queryRunner);
await this.walletsService.createReferralRewardTransaction(resellerOwner.id, resellerOwnerRewardAmount, queryRunner);
// Update reward status
reward.status = ReferralRewardStatus.PAID;
reward.paidAt = dayjs().toDate();
await queryRunner.manager.save(reward);
agentReward.status = ReferralRewardStatus.PAID;
agentReward.status = ReferralRewardStatus.PAID;
resellerOwnerReward.paidAt = dayjs().toDate();
resellerOwnerReward.paidAt = dayjs().toDate();
await queryRunner.manager.save([agentReward,resellerOwnerReward]);
// Update referral status
referral.status = ReferralStatus.COMPLETED;
+12
View File
@@ -15,6 +15,7 @@ import { ResellerType } from '../auth/DTO/verify-reseller-otp.dto';
import { PaginationDto } from '../../common/DTO/pagination.dto';
import { WalletsService } from '../wallets/providers/wallets.service';
import Decimal from 'decimal.js';
import { IsNull } from 'typeorm';
@Injectable()
export class ResellerService {
@@ -186,6 +187,17 @@ export class ResellerService {
if (!user) {
throw new NotFoundException(AuthMessage.USER_NOT_FOUND)
}
const pendingWithdrawRequest = await this.withdrawRequestRepository.findOne({
where: {
user: { id: userId },
paidAt: IsNull()
}
})
if (pendingWithdrawRequest) {
throw new BadRequestException("امکان ایجاد درخواست برداشت جدید نیست")
}
const { balance } = await this.walletsService.getBalance(userId)
if (new Decimal(balance).lessThan(requestedAmount)) {
+15 -1
View File
@@ -427,6 +427,20 @@ export class UsersService {
}
/**************************** */
async finResellerOwnerByAgentId(agentId: string) {
const owner = await this.userRepository.findOne({
where: {
ownedReseller: {
agents: {
id: agentId
}
}
}
})
if (!owner) {
throw new BadRequestException("صاحب نمایندگی پیدا نشد")
}
return owner
}
}