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
+169 -15
View File
@@ -1,5 +1,5 @@
import { ResponseMapper } from '@/common/response/response-mapper'
import { PartnerSelect } from '@/generated/prisma/models'
import { PartnerAccountSelect } from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { UpdatePartnerProfileDto } from './dto/partner.dto'
@@ -8,23 +8,31 @@ import { UpdatePartnerProfileDto } from './dto/partner.dto'
export class PartnerService {
constructor(private readonly prisma: PrismaService) {}
private readonly defaultSelect: PartnerSelect = {
id: true,
name: true,
status: true,
account_quota_charge_transactions: {
private readonly defaultSelect: PartnerAccountSelect = {}
async me(partner_id: string) {
const partner = await this.prisma.partnerAccount.findUniqueOrThrow({
where: {
partner_id,
},
select: {
_count: {
id: true,
role: true,
account: {
select: {
allocations: {
where: {
license_id: undefined,
},
},
username: true,
},
},
partner: {
select: {
id: true,
name: true,
},
},
},
},
})
return ResponseMapper.single(partner)
}
async getInfo(partner_id: string) {
@@ -32,10 +40,156 @@ export class PartnerService {
where: {
id: partner_id,
},
select: this.defaultSelect,
select: {
id: true,
name: true,
code: true,
created_at: true,
license_charge_transactions: {
select: {
purchased_count: true,
activation_expires_at: true,
_count: {
select: {
licenses: {
where: {
activation: {
isNot: null,
},
},
},
},
},
},
},
account_quota_charge_transactions: {
select: {
purchased_count: true,
activation_expires_at: true,
_count: {
select: {
credits: {
where: {
charge_transaction: {
isNot: null,
},
allocation: {
isNot: null,
},
},
},
},
},
},
},
license_renew_charge_transactions: {
select: {
purchased_count: true,
activation_expires_at: true,
_count: {
select: {
license_renews: {
where: {
activation: {
isNot: undefined,
},
},
},
},
},
},
},
},
})
return ResponseMapper.single(partner)
const {
license_charge_transactions,
account_quota_charge_transactions,
license_renew_charge_transactions,
...partnerInfo
} = partner
const licenses_status = {
total_purchased: 0,
total_activated: 0,
total_expired: 0,
}
const account_quotas_status = {
total_purchased: 0,
total_activated: 0,
total_expired: 0,
}
const license_renews_status = {
total_purchased: 0,
total_activated: 0,
total_expired: 0,
}
const now = new Date()
const checkExpired = (expires_at: Date) => expires_at < now
const prepareData = (
items_count: number,
activation_expires_at: Date,
purchased_count: number,
) => {
let totalActivated = 0
let totalExpired = 0
if (!checkExpired(activation_expires_at)) {
totalActivated = items_count
} else {
totalExpired = items_count
}
return {
total_purchased: purchased_count,
total_activated: totalActivated,
total_expired: totalExpired,
}
}
license_charge_transactions.forEach(
({ _count, activation_expires_at, purchased_count }) => {
const status = prepareData(
_count.licenses,
activation_expires_at,
purchased_count,
)
licenses_status.total_purchased += status.total_purchased
licenses_status.total_activated += status.total_activated
licenses_status.total_expired += status.total_expired
},
)
account_quota_charge_transactions.forEach(
({ _count, activation_expires_at, purchased_count }) => {
const status = prepareData(_count.credits, activation_expires_at, purchased_count)
account_quotas_status.total_purchased += status.total_purchased
account_quotas_status.total_activated += status.total_activated
account_quotas_status.total_expired += status.total_expired
},
)
license_renew_charge_transactions.forEach(
({ _count, activation_expires_at, purchased_count }) => {
const status = prepareData(
_count.license_renews,
activation_expires_at,
purchased_count,
)
license_renews_status.total_purchased += status.total_purchased
license_renews_status.total_activated += status.total_activated
license_renews_status.total_expired += status.total_expired
},
)
return ResponseMapper.single({
licenses_status,
license_renews_status,
account_quotas_status,
...partnerInfo,
})
}
async updateInfo(partner_id: string, data: UpdatePartnerProfileDto) {