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
|
|
|
LicenseCreateInput,
|
|
|
|
|
} from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
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-04-16 22:19:20 +03:30
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2026-04-22 21:55:40 +03:30
|
|
|
private readonly TRACKING_CODE_LENGTH = 8
|
|
|
|
|
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
|
|
|
|
|
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-16 22:19:20 +03:30
|
|
|
async findAll(partner_id: string, page = 1, pageSize = 10) {
|
2026-04-23 20:59:39 +03:30
|
|
|
const defaultWhere: LicenseChargeTransactionWhereInput = {
|
2026-04-16 22:19:20 +03:30
|
|
|
partner_id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [transactions, count] = await this.prisma.$transaction(async tx => [
|
2026-04-23 20:59:39 +03:30
|
|
|
await tx.licenseChargeTransaction.findMany({
|
2026-04-16 22:19:20 +03:30
|
|
|
where: defaultWhere,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
take: pageSize,
|
2026-04-23 20:59:39 +03:30
|
|
|
select: this.defaultSelect,
|
2026-04-16 22:19:20 +03:30
|
|
|
}),
|
2026-04-23 20:59:39 +03:30
|
|
|
await tx.licenseChargeTransaction.count({
|
2026-04-16 22:19:20 +03:30
|
|
|
where: defaultWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const mappedTransactions = transactions.map(this.mappedTransaction)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.paginate(mappedTransactions, {
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
count,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(partner_id: string, id: string) {
|
2026-04-23 20:59:39 +03:30
|
|
|
const transaction = this.prisma.licenseChargeTransaction.findUniqueOrThrow({
|
2026-04-16 22:19:20 +03:30
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
partner_id,
|
|
|
|
|
},
|
2026-04-23 20:59:39 +03:30
|
|
|
select: this.defaultSelect,
|
2026-04-16 22:19:20 +03:30
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(this.mappedTransaction(transaction))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(partner_id: string, data: ChargeLicenseDto) {
|
|
|
|
|
try {
|
|
|
|
|
return await this.prisma.$transaction(async tx => {
|
2026-04-22 21:55:40 +03:30
|
|
|
let transaction: { id: string } | null = null
|
|
|
|
|
|
|
|
|
|
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
|
|
|
|
try {
|
2026-04-23 20:59:39 +03:30
|
|
|
transaction = 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 } },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
break
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (
|
|
|
|
|
isTrackingCodeUniqueViolation(error) &&
|
|
|
|
|
attempt < this.TRACKING_CODE_MAX_ATTEMPTS - 1
|
|
|
|
|
) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-16 22:19:20 +03:30
|
|
|
|
|
|
|
|
if (!transaction) {
|
|
|
|
|
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const licenseCreationPromises: any[] = []
|
|
|
|
|
for (let i = 0; i < data.quantity; i++) {
|
|
|
|
|
const license: LicenseCreateInput = {
|
2026-04-23 20:59:39 +03:30
|
|
|
charge_transaction: {
|
2026-04-16 22:19:20 +03:30
|
|
|
connect: {
|
|
|
|
|
id: transaction.id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
licenseCreationPromises.push(tx.license.create({ data: license }))
|
|
|
|
|
}
|
|
|
|
|
const createdLicenses = await Promise.all(licenseCreationPromises)
|
|
|
|
|
|
2026-04-23 20:59:39 +03:30
|
|
|
if (
|
|
|
|
|
createdLicenses.some(createdLicense => createdLicense.activation_id === null)
|
|
|
|
|
) {
|
|
|
|
|
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
|
|
|
|
|
}
|
2026-04-16 22:19:20 +03:30
|
|
|
return { transaction, createdLicenses }
|
|
|
|
|
})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|