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
@@ -6,7 +6,6 @@ import {
POSStatus,
} from '@/generated/prisma/enums'
import { PosCreateInput, PosSelect, PosWhereInput } from '@/generated/prisma/models'
import { getBusinessActivityRemainingAccounts } from '@/modules/consumer/utils/getBusinessActivityRemainingAccounts.util'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@@ -24,6 +23,15 @@ export class ComplexPosesService {
status: true,
created_at: true,
pos_type: true,
account: {
select: {
account: {
select: {
username: true,
},
},
},
},
provider: {
select: {
@@ -74,6 +82,7 @@ export class ComplexPosesService {
defaultInsert = async (
consumer_id: string,
complex_id: string,
account_allocation_id: string,
data: CreatePosDto,
): Promise<PosCreateInput> => {
const { device_id, provider_id, username, password, ...rest } = data
@@ -88,6 +97,11 @@ export class ComplexPosesService {
account: {
create: {
role: ConsumerRole.OPERATOR,
account_allocation: {
connect: {
id: account_allocation_id,
},
},
account: {
create: {
username,
@@ -120,13 +134,25 @@ export class ComplexPosesService {
}
}
private readonly mapPos = (pos: any) => {
const { account, ...rest } = pos
const { account: accountInfo } = account
return {
...rest,
account: {
username: accountInfo.username,
},
}
}
async findAll(consumer_id: string, business_activity_id: string, complex_id: string) {
const poses = await this.prisma.pos.findMany({
where: this.defaultWhere(consumer_id, business_activity_id, complex_id),
select: this.defaultSelect,
})
return ResponseMapper.list(poses)
return ResponseMapper.list(poses.map(this.mapPos))
}
async findOne(
@@ -142,7 +168,7 @@ export class ComplexPosesService {
},
select: this.defaultSelect,
})
return ResponseMapper.single(pos)
return ResponseMapper.single(this.mapPos(pos))
}
async create(
@@ -151,13 +177,41 @@ export class ComplexPosesService {
complex_id: string,
data: CreatePosDto,
) {
const now = new Date()
const pos = await this.prisma.$transaction(async tx => {
const quota = await getBusinessActivityRemainingAccounts(tx, {
consumer_id,
business_activity_id,
const account_allocation = await tx.licenseAccountAllocation.findFirst({
where: {
account_id: null,
license_activation: {
business_activity_id,
business_activity: {
consumer_id,
},
OR: [
{
expires_at: {
gte: now,
},
},
{
license_renews: {
some: {
expires_at: {
gte: now,
},
},
},
},
],
},
},
select: {
id: true,
},
})
if (quota.remaining_accounts <= 0) {
if (!account_allocation) {
throw new BadRequestException(
`تعداد کاربرهای تخصیص داده شده برای این فعالیت تجاری به پایان رسیده است.`,
)
@@ -165,7 +219,12 @@ export class ComplexPosesService {
return await tx.pos.create({
data: {
...(await this.defaultInsert(consumer_id, complex_id, data)),
...(await this.defaultInsert(
consumer_id,
complex_id,
account_allocation.id,
data,
)),
status: POSStatus.ACTIVE,
},
})