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:
+11
-15
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user