refactor: remove SalesInvoiceItemsService and update sales invoice handling

- Deleted SalesInvoiceItemsService as it was not implemented.
- Changed variable declaration from `const` to `let` in SalesInvoicesService for sales invoice creation.
- Updated response handling after sending to TSP provider in SalesInvoicesService.
- Renamed payload properties in TspProvider DTOs for clarity.
- Enhanced error handling in SalesInvoiceTspSwitchService for unsupported providers.
- Refactored SalesInvoiceTspService to utilize utility functions for payload building and sending.
- Updated PrismaService to increase connection limit and utilize dynamic options.
- Added new migration scripts to update database schema for sale_invoice_tsp_attempts.
- Introduced utility functions for building payloads and handling responses in sales invoice processing.
This commit is contained in:
2026-05-08 18:09:13 +03:30
parent fbe7230865
commit 4e61ff618e
28 changed files with 944 additions and 713 deletions
@@ -5,7 +5,6 @@ import {
import {
LicenseChargeTransactionSelect,
LicenseChargeTransactionWhereInput,
LicenseCreateInput,
} from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
@@ -18,6 +17,8 @@ export class PartnerLicenseChargeTransactionService {
private readonly TRACKING_CODE_LENGTH = 8
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
private readonly LICENSE_BATCH_SIZE = 200
private readonly MAX_QUANTITY_PER_REQUEST = 1000
private readonly mappedTransaction = (transaction: any) => {
const { licenses, purchased_count, _count, ...rest } = transaction
@@ -78,7 +79,7 @@ export class PartnerLicenseChargeTransactionService {
}
async findOne(partner_id: string, id: string) {
const transaction = this.prisma.licenseChargeTransaction.findUniqueOrThrow({
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
where: {
id,
partner_id,
@@ -89,19 +90,29 @@ export class PartnerLicenseChargeTransactionService {
}
async create(partner_id: string, data: ChargeLicenseDto) {
if (data.quantity > this.MAX_QUANTITY_PER_REQUEST) {
throw new BadRequestException(
`تعداد درخواستی بیش از حد مجاز است. حداکثر ${this.MAX_QUANTITY_PER_REQUEST} عدد مجاز است.`,
)
}
try {
return await this.prisma.$transaction(async tx => {
let transaction: { id: string } | null = null
const transaction = await this.prisma.$transaction(async tx => {
let createdTransaction: { id: string; purchased_count: number } | null = null
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
try {
transaction = await tx.licenseChargeTransaction.create({
createdTransaction = await tx.licenseChargeTransaction.create({
data: {
activation_expires_at: data.activated_expires_at,
tracking_code: generateTrackingCode('LIC', this.TRACKING_CODE_LENGTH),
purchased_count: data.quantity,
partner: { connect: { id: partner_id } },
},
select: {
id: true,
purchased_count: true,
},
})
break
} catch (error) {
@@ -115,33 +126,43 @@ export class PartnerLicenseChargeTransactionService {
}
}
if (!transaction) {
if (!createdTransaction) {
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
}
const licenseCreationPromises: any[] = []
for (let i = 0; i < data.quantity; i++) {
const license: LicenseCreateInput = {
charge_transaction: {
connect: {
id: transaction.id,
},
},
}
return createdTransaction
})
licenseCreationPromises.push(tx.license.create({ data: license }))
}
const createdLicenses = await Promise.all(licenseCreationPromises)
if (
createdLicenses.some(createdLicense => createdLicense.activation_id === null)
) {
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
}
return { transaction, createdLicenses }
this.scheduleLicenseProvisioning(transaction.id, transaction.purchased_count)
return ResponseMapper.create({
transaction_id: transaction.id,
charged_license_count: transaction.purchased_count,
status: 'QUEUED',
})
} catch (error) {
throw error
}
}
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
}
}
}