feat(partners): add utility functions for partner business activity allocation limits and remaining licenses

- Implemented `getPartnerBusinessActivityAllocationLimits` to retrieve allocation limits for a partner's business activity.
- Added `ensurePartnerBusinessActivityHasRemainingAllocation` to validate remaining allocation credits.
- Created `getPartnerRemainingLicenses` to count remaining licenses for a partner.
- Developed `getPartnerFirstRemainingLicense` to fetch the first unused license for a partner.
- Introduced `ensurePartnerHasRemainingLicense` to ensure a partner has at least one unused license.
This commit is contained in:
2026-04-24 23:02:05 +03:30
parent 9b652a3603
commit 12506de863
43 changed files with 4645 additions and 2196 deletions
@@ -3,9 +3,9 @@ import {
isTrackingCodeUniqueViolation,
} from '@/common/utils/tracking-code-generator.util'
import {
PartnerAccountQuotaAllocationCreateInput,
PartnerAccountQuotaChargeTransactionSelect,
PartnerAccountQuotaChargeTransactionWhereInput,
PartnerAccountQuotaCreditCreateInput,
} from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
@@ -40,10 +40,10 @@ export class PartnerAccountChargeTransactionService {
purchased_count: true,
_count: {
select: {
allocations: {
credits: {
where: {
license_id: {
not: null,
allocation: {
isNot: null,
},
},
},
@@ -120,9 +120,9 @@ export class PartnerAccountChargeTransactionService {
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
}
const accountCreationPromises: any[] = []
const creditCreationPromises: any[] = []
for (let i = 0; i < data.quantity; i++) {
const account: PartnerAccountQuotaAllocationCreateInput = {
const credit: PartnerAccountQuotaCreditCreateInput = {
charge_transaction: {
connect: {
id: transaction.id,
@@ -130,20 +130,16 @@ export class PartnerAccountChargeTransactionService {
},
}
accountCreationPromises.push(
tx.partnerAccountQuotaAllocation.create({ data: account }),
creditCreationPromises.push(
tx.partnerAccountQuotaCredit.create({ data: credit }),
)
}
const createdAllocations = await Promise.all(accountCreationPromises)
const createdCredits = await Promise.all(creditCreationPromises)
if (
createdAllocations.some(
createdAllocation => createdAllocation.activation_id === null,
)
) {
if (createdCredits.some(createdCredit => createdCredit.activation_id === null)) {
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
}
return { transaction, createdAllocations }
return { transaction, createdCredits }
})
} catch (error) {
throw error