diff --git a/src/modules/referrals/repositories/referral-codes.repository.ts b/src/modules/referrals/repositories/referral-codes.repository.ts index 63c7791..31ea501 100755 --- a/src/modules/referrals/repositories/referral-codes.repository.ts +++ b/src/modules/referrals/repositories/referral-codes.repository.ts @@ -13,8 +13,11 @@ export class ReferralCodesRepository extends Repository { } async findPaginatedList(userIds: string[], queryDto: PaginationDto) { + if (!userIds.length) { + return [[], 0] as [ReferralCode[], number]; + } + const { limit, skip } = PaginationUtils(queryDto); - console.log(userIds) const qb = this.createQueryBuilder("rr") .where("rr.userId IN (:...userIds)", { userIds }) .leftJoinAndSelect("rr.user", "user") diff --git a/src/modules/referrals/repositories/referral-rewards.repository.ts b/src/modules/referrals/repositories/referral-rewards.repository.ts index 68bc179..07226fc 100755 --- a/src/modules/referrals/repositories/referral-rewards.repository.ts +++ b/src/modules/referrals/repositories/referral-rewards.repository.ts @@ -13,6 +13,10 @@ export class ReferralRewardsRepository extends Repository { } async findPaginatedList(userIds: string[], queryDto: SearchWithdrawRequestQueryDto) { + if (!userIds.length) { + return [[], 0] as [ReferralReward[], number]; + } + const { limit, skip } = PaginationUtils(queryDto); const qb = this.createQueryBuilder("rr") .where("rr.userId IN (:...userIds)", { userIds }) diff --git a/src/modules/referrals/repositories/referrals.repository.ts b/src/modules/referrals/repositories/referrals.repository.ts index f50c8fe..b1ae0dd 100755 --- a/src/modules/referrals/repositories/referrals.repository.ts +++ b/src/modules/referrals/repositories/referrals.repository.ts @@ -12,6 +12,11 @@ export class ReferralsRepository extends Repository { } async findPaginatedList(userIds: string[], queryDto: SearchCustomersQueryDto) { + + if (userIds.length == 0) { + return [[], 0] + } + const { limit, skip } = PaginationUtils(queryDto); const qb = this.createQueryBuilder("referral") diff --git a/src/modules/reseller/entities/rewards-withdraw-request.entity.ts b/src/modules/reseller/entities/rewards-withdraw-request.entity.ts index f1cb634..0bce48d 100755 --- a/src/modules/reseller/entities/rewards-withdraw-request.entity.ts +++ b/src/modules/reseller/entities/rewards-withdraw-request.entity.ts @@ -16,7 +16,7 @@ export class RewardWithdrawRequest extends BaseEntity { @Column({ type: "timestamp", nullable: true }) paidAt: Date; - @Column({ type: "varchar", length: 255, nullable: true }) + @Column({ type: "varchar", length: 255, nullable: true, unique: true }) transactionId?: string; @OneToMany(() => WalletTransaction, (walletTransaction) => walletTransaction.withdrawRequest) diff --git a/src/modules/reseller/reseller.service.ts b/src/modules/reseller/reseller.service.ts index 0dca433..68e0a1a 100644 --- a/src/modules/reseller/reseller.service.ts +++ b/src/modules/reseller/reseller.service.ts @@ -95,10 +95,18 @@ export class ResellerService { return user } - async removeResellerAgent(userId: string, agentToBeDeleted: string) { - await this.FindOneOrFailByUserId(userId) + async removeResellerAgent(userId: string, agentId: string) { + const reseller = await this.FindOneOrFailByUserId(userId) + + const { user } = await this.usersService.findOneById(agentId) - const { user } = await this.usersService.findOneById(agentToBeDeleted) + if (!user) { + throw new NotFoundException(AuthMessage.USER_NOT_FOUND) + } + + if (user.reseller?.id !== reseller.id) { + throw new BadRequestException("بازاریاب به این نمایندگی تعلق ندارد") + } user.reseller = undefined @@ -112,15 +120,19 @@ export class ResellerService { const agentIds = reselller.agents.map(ag => ag.id) + if (!agentIds.length) { + return { customers: [], count: 0, paginate: true }; + } + return this.referralService.findPaginatedReferedUsers(agentIds, query) } // *************** Invoice *************** async findInvoices(userId: string, type: ResellerType, query: SearchResellerInvoicesQueryDto) { if (type == ResellerType.agent) { - return this.findResellerCustomersInvoices(userId, query) - } else if (type == ResellerType.reseller) { return this.findAgentCustomersInvoices(userId, query) + } else if (type == ResellerType.reseller) { + return this.findResellerCustomersInvoices(userId, query) } } @@ -129,6 +141,10 @@ export class ResellerService { const agentIds = reselller.agents.map(ag => ag.id) + if (!agentIds.length) { + return { invoices: [], count: 0, paginate: true }; + } + const customerIds = await this.referralService.findReferedUsersIds(agentIds) const [invoices, count] = await this.invoicesService.findInvoicesByCustomerIds(customerIds, query) @@ -163,6 +179,10 @@ export class ResellerService { const agentIds = reselller.agents.map(ag => ag.id) + if (!agentIds.length) { + return { rewards: [], count: 0, paginate: true }; + } + return this.referralService.findRewards(agentIds, query) } @@ -234,7 +254,7 @@ export class ResellerService { return this.withdrawRequestRepository.remove(request) } - async confirmWithdrawRequest(requestId: string,transactionId:string) { + async confirmWithdrawRequest(requestId: string, transactionId: string) { const request = await this.withdrawRequestRepository.findOne({ where: { id: requestId }, relations: ['user'] }) if (!request) { throw new BadRequestException("درخواست برداشت یافت نشد .") @@ -326,11 +346,15 @@ export class ResellerService { const agentIds = reselller.agents.map(ag => ag.id) - return this.referralService.findRewards(agentIds, query) + if (!agentIds.length) { + return { codes: [], count: 0, paginate: true }; + } + + return this.referralService.getReferralCodesByUserIds(agentIds, query) } private async findAgentReferralCodes(userId: string, query: SearchWithdrawRequestQueryDto) { - return this.referralService.findRewards([userId], query) + return this.referralService.getReferralCodesByUserIds([userId], query) } }