2026-05-19 15:40:45 +03:30
|
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
2026-04-22 21:55:40 +03:30
|
|
|
import {
|
|
|
|
|
generateTrackingCode,
|
|
|
|
|
isTrackingCodeUniqueViolation,
|
|
|
|
|
} from '@/common/utils/tracking-code-generator.util'
|
2026-04-16 22:19:20 +03:30
|
|
|
import {
|
2026-04-23 20:59:39 +03:30
|
|
|
LicenseChargeTransactionSelect,
|
|
|
|
|
LicenseChargeTransactionWhereInput,
|
2026-04-16 22:19:20 +03:30
|
|
|
} from '@/generated/prisma/models'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
2026-04-16 22:19:20 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { RedisService } from '@/redis/redis.service'
|
2026-04-16 22:19:20 +03:30
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
2026-04-23 20:59:39 +03:30
|
|
|
export class PartnerLicenseChargeTransactionService {
|
2026-05-19 15:40:45 +03:30
|
|
|
constructor(
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
private readonly redisService: RedisService,
|
|
|
|
|
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
|
|
|
|
|
) {}
|
2026-04-16 22:19:20 +03:30
|
|
|
|
2026-04-22 21:55:40 +03:30
|
|
|
private readonly TRACKING_CODE_LENGTH = 8
|
|
|
|
|
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
2026-05-08 18:09:13 +03:30
|
|
|
private readonly LICENSE_BATCH_SIZE = 200
|
|
|
|
|
private readonly MAX_QUANTITY_PER_REQUEST = 1000
|
2026-04-22 21:55:40 +03:30
|
|
|
|
2026-04-16 22:19:20 +03:30
|
|
|
private readonly mappedTransaction = (transaction: any) => {
|
2026-04-23 20:59:39 +03:30
|
|
|
const { licenses, purchased_count, _count, ...rest } = transaction
|
|
|
|
|
|
|
|
|
|
const activation_count = _count.licenses
|
|
|
|
|
|
2026-04-16 22:19:20 +03:30
|
|
|
return {
|
|
|
|
|
...rest,
|
2026-04-23 20:59:39 +03:30
|
|
|
charged_license_count: purchased_count,
|
|
|
|
|
activation_count,
|
|
|
|
|
remained_license_count: purchased_count - activation_count,
|
2026-04-16 22:19:20 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 20:59:39 +03:30
|
|
|
private readonly defaultSelect: LicenseChargeTransactionSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
activation_expires_at: true,
|
|
|
|
|
tracking_code: true,
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
licenses: {
|
|
|
|
|
where: {
|
|
|
|
|
activation: {
|
|
|
|
|
isNot: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
async findAll(partner_id: string, page = 1, perPage = 10) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionsList(
|
|
|
|
|
partner_id,
|
|
|
|
|
page,
|
|
|
|
|
perPage,
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-23 20:59:39 +03:30
|
|
|
const defaultWhere: LicenseChargeTransactionWhereInput = {
|
2026-04-16 22:19:20 +03:30
|
|
|
partner_id,
|
|
|
|
|
}
|
2026-05-21 17:27:37 +03:30
|
|
|
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
|
|
|
|
const [transactions, total] = await this.prisma.$transaction(async tx => [
|
|
|
|
|
await tx.licenseChargeTransaction.findMany({
|
|
|
|
|
where: defaultWhere,
|
|
|
|
|
skip: (page - 1) * perPage,
|
|
|
|
|
take: perPage,
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
}),
|
|
|
|
|
await tx.licenseChargeTransaction.count({
|
|
|
|
|
where: defaultWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const mappedTransactions = transactions.map(this.mappedTransaction)
|
|
|
|
|
return {
|
|
|
|
|
items: mappedTransactions,
|
|
|
|
|
page,
|
|
|
|
|
perPage,
|
|
|
|
|
total,
|
|
|
|
|
}
|
2026-04-16 22:19:20 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(partner_id: string, id: string) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionDetail(partner_id, id)
|
2026-05-21 17:27:37 +03:30
|
|
|
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
|
|
|
|
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
partner_id,
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
return this.mappedTransaction(transaction)
|
2026-04-16 22:19:20 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(partner_id: string, data: ChargeLicenseDto) {
|
2026-05-08 18:09:13 +03:30
|
|
|
if (data.quantity > this.MAX_QUANTITY_PER_REQUEST) {
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
`تعداد درخواستی بیش از حد مجاز است. حداکثر ${this.MAX_QUANTITY_PER_REQUEST} عدد مجاز است.`,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 22:19:20 +03:30
|
|
|
try {
|
2026-05-08 18:09:13 +03:30
|
|
|
const transaction = await this.prisma.$transaction(async tx => {
|
|
|
|
|
let createdTransaction: { id: string; purchased_count: number } | null = null
|
2026-04-22 21:55:40 +03:30
|
|
|
|
|
|
|
|
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
|
|
|
|
try {
|
2026-05-08 18:09:13 +03:30
|
|
|
createdTransaction = await tx.licenseChargeTransaction.create({
|
2026-04-22 21:55:40 +03:30
|
|
|
data: {
|
|
|
|
|
activation_expires_at: data.activated_expires_at,
|
|
|
|
|
tracking_code: generateTrackingCode('LIC', this.TRACKING_CODE_LENGTH),
|
2026-04-23 20:59:39 +03:30
|
|
|
purchased_count: data.quantity,
|
2026-04-22 21:55:40 +03:30
|
|
|
partner: { connect: { id: partner_id } },
|
|
|
|
|
},
|
2026-05-08 18:09:13 +03:30
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
purchased_count: true,
|
|
|
|
|
},
|
2026-04-22 21:55:40 +03:30
|
|
|
})
|
|
|
|
|
break
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (
|
|
|
|
|
isTrackingCodeUniqueViolation(error) &&
|
|
|
|
|
attempt < this.TRACKING_CODE_MAX_ATTEMPTS - 1
|
|
|
|
|
) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-16 22:19:20 +03:30
|
|
|
|
2026-05-08 18:09:13 +03:30
|
|
|
if (!createdTransaction) {
|
2026-04-16 22:19:20 +03:30
|
|
|
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 18:09:13 +03:30
|
|
|
return createdTransaction
|
|
|
|
|
})
|
2026-04-16 22:19:20 +03:30
|
|
|
|
2026-05-08 18:09:13 +03:30
|
|
|
this.scheduleLicenseProvisioning(transaction.id, transaction.purchased_count)
|
2026-05-19 15:40:45 +03:30
|
|
|
await this.cacheInvalidationService.invalidatePartnersList()
|
|
|
|
|
await this.cacheInvalidationService.invalidatePartnerLicenses(partner_id)
|
2026-05-08 18:09:13 +03:30
|
|
|
return ResponseMapper.create({
|
|
|
|
|
transaction_id: transaction.id,
|
|
|
|
|
charged_license_count: transaction.purchased_count,
|
|
|
|
|
status: 'QUEUED',
|
2026-04-16 22:19:20 +03:30
|
|
|
})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-08 18:09:13 +03:30
|
|
|
|
|
|
|
|
private scheduleLicenseProvisioning(transactionId: string, quantity: number) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.provisionLicensesInBackground(transactionId, quantity).catch(() => null)
|
|
|
|
|
}, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async provisionLicensesInBackground(transactionId: string, quantity: number) {
|
|
|
|
|
let createdCount = 0
|
|
|
|
|
|
|
|
|
|
while (createdCount < quantity) {
|
|
|
|
|
const batchSize = Math.min(this.LICENSE_BATCH_SIZE, quantity - createdCount)
|
|
|
|
|
|
|
|
|
|
await this.prisma.license.createMany({
|
|
|
|
|
data: Array.from({ length: batchSize }, () => ({
|
|
|
|
|
charge_transaction_id: transactionId,
|
|
|
|
|
})),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
createdCount += batchSize
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-16 22:19:20 +03:30
|
|
|
}
|