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:
+47
-23
@@ -5,7 +5,6 @@ import {
|
||||
import {
|
||||
PartnerAccountQuotaChargeTransactionSelect,
|
||||
PartnerAccountQuotaChargeTransactionWhereInput,
|
||||
PartnerAccountQuotaCreditCreateInput,
|
||||
} from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
@@ -18,6 +17,8 @@ export class PartnerAccountChargeTransactionService {
|
||||
|
||||
private readonly TRACKING_CODE_LENGTH = 8
|
||||
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
|
||||
private readonly QUOTA_BATCH_SIZE = 200
|
||||
private readonly MAX_QUANTITY_PER_REQUEST = 1000
|
||||
|
||||
private readonly mappedTransaction = (transaction: any) => {
|
||||
const { allocations, purchased_count, _count, ...rest } = transaction
|
||||
@@ -90,19 +91,28 @@ export class PartnerAccountChargeTransactionService {
|
||||
}
|
||||
|
||||
async create(partner_id: string, data: ChargeAccountQuotaDto) {
|
||||
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 } | null = null
|
||||
|
||||
for (let attempt = 0; attempt < this.TRACKING_CODE_MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
transaction = await tx.partnerAccountQuotaChargeTransaction.create({
|
||||
createdTransaction = await tx.partnerAccountQuotaChargeTransaction.create({
|
||||
data: {
|
||||
activation_expires_at: data.activated_expires_at,
|
||||
tracking_code: generateTrackingCode('AQC', this.TRACKING_CODE_LENGTH),
|
||||
purchased_count: data.quantity,
|
||||
partner: { connect: { id: partner_id } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
break
|
||||
} catch (error) {
|
||||
@@ -116,33 +126,47 @@ export class PartnerAccountChargeTransactionService {
|
||||
}
|
||||
}
|
||||
|
||||
if (!transaction) {
|
||||
if (!createdTransaction) {
|
||||
throw new BadRequestException('متاسفانه مشکلی پیش آمده است.')
|
||||
}
|
||||
|
||||
const creditCreationPromises: any[] = []
|
||||
for (let i = 0; i < data.quantity; i++) {
|
||||
const credit: PartnerAccountQuotaCreditCreateInput = {
|
||||
charge_transaction: {
|
||||
connect: {
|
||||
id: transaction.id,
|
||||
},
|
||||
},
|
||||
}
|
||||
return createdTransaction
|
||||
})
|
||||
|
||||
creditCreationPromises.push(
|
||||
tx.partnerAccountQuotaCredit.create({ data: credit }),
|
||||
)
|
||||
}
|
||||
const createdCredits = await Promise.all(creditCreationPromises)
|
||||
this.scheduleQuotaCreditProvisioning(transaction.id, data.quantity)
|
||||
|
||||
if (createdCredits.some(createdCredit => createdCredit.activation_id === null)) {
|
||||
throw new BadRequestException('متاسفانه مشکلی پیش آمده است. ')
|
||||
}
|
||||
return { transaction, createdCredits }
|
||||
return ResponseMapper.create({
|
||||
transaction_id: transaction.id,
|
||||
charged_license_count: data.quantity,
|
||||
status: 'QUEUED',
|
||||
})
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleQuotaCreditProvisioning(transactionId: string, quantity: number) {
|
||||
setTimeout(() => {
|
||||
this.provisionQuotaCreditsInBackground(transactionId, quantity).catch(() => null)
|
||||
}, 0)
|
||||
}
|
||||
|
||||
private async provisionQuotaCreditsInBackground(
|
||||
transactionId: string,
|
||||
quantity: number,
|
||||
) {
|
||||
let createdCount = 0
|
||||
|
||||
while (createdCount < quantity) {
|
||||
const batchSize = Math.min(this.QUOTA_BATCH_SIZE, quantity - createdCount)
|
||||
|
||||
await this.prisma.partnerAccountQuotaCredit.createMany({
|
||||
data: Array.from({ length: batchSize }, () => ({
|
||||
charge_transaction_id: transactionId,
|
||||
})),
|
||||
})
|
||||
|
||||
createdCount += batchSize
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+46
-25
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { translateEnumValue } from '@/common/utils'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import {
|
||||
AccountStatus,
|
||||
@@ -87,6 +88,7 @@ export class PartnersService {
|
||||
license_charge_transactions,
|
||||
account_quota_charge_transactions,
|
||||
license_renew_charge_transactions,
|
||||
status,
|
||||
...rest
|
||||
} = partner
|
||||
|
||||
@@ -127,6 +129,7 @@ export class PartnersService {
|
||||
})
|
||||
return {
|
||||
...rest,
|
||||
status: translateEnumValue('PartnerStatus', status),
|
||||
licenses_status,
|
||||
license_renew_status,
|
||||
account_quota_status,
|
||||
|
||||
Reference in New Issue
Block a user