update
This commit is contained in:
+32
-6
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
generateTrackingCode,
|
||||
isTrackingCodeUniqueViolation,
|
||||
} from '@/common/utils/tracking-code-generator.util'
|
||||
import {
|
||||
ChargedLicenseTransactionsWhereInput,
|
||||
LicenseCreateInput,
|
||||
@@ -11,11 +15,16 @@ import { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
|
||||
export class PartnerChargedLicenseTransactionsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private readonly TRACKING_CODE_LENGTH = 8
|
||||
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
||||
|
||||
private readonly mappedTransaction = (transaction: any) => {
|
||||
const { licenses, ...rest } = transaction
|
||||
return {
|
||||
...rest,
|
||||
charged_license_count: licenses.length,
|
||||
activated_license_count: licenses.filter(license => license?.activated_license)
|
||||
.length,
|
||||
remained_license_count: licenses.filter(license => !license?.activated_license)
|
||||
.length,
|
||||
}
|
||||
@@ -35,6 +44,7 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
id: true,
|
||||
created_at: true,
|
||||
activation_expires_at: true,
|
||||
tracking_code: true,
|
||||
licenses: {
|
||||
select: {
|
||||
activated_license: {
|
||||
@@ -88,12 +98,28 @@ export class PartnerChargedLicenseTransactionsService {
|
||||
async create(partner_id: string, data: ChargeLicenseDto) {
|
||||
try {
|
||||
return await this.prisma.$transaction(async tx => {
|
||||
const transaction = await tx.chargedLicenseTransactions.create({
|
||||
data: {
|
||||
activation_expires_at: data.activated_expires_at,
|
||||
partner: { connect: { id: partner_id } },
|
||||
},
|
||||
})
|
||||
let transaction: { id: string } | null = null
|
||||
|
||||
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
transaction = await tx.chargedLicenseTransactions.create({
|
||||
data: {
|
||||
activation_expires_at: data.activated_expires_at,
|
||||
tracking_code: generateTrackingCode('LIC', this.TRACKING_CODE_LENGTH),
|
||||
partner: { connect: { id: partner_id } },
|
||||
},
|
||||
})
|
||||
break
|
||||
} catch (error) {
|
||||
if (
|
||||
isTrackingCodeUniqueViolation(error) &&
|
||||
attempt < this.TRACKING_CODE_MAX_ATTEMPTS - 1
|
||||
) {
|
||||
continue
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
if (!transaction) {
|
||||
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
|
||||
|
||||
Reference in New Issue
Block a user