Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -33,6 +33,52 @@ export class SalesInvoiceTspService {
|
||||
private sharedSaleInvoiceCreateService: SharedSaleInvoiceCreateService,
|
||||
) {}
|
||||
|
||||
private mapProviderError(error: any) {
|
||||
const isProviderFailureObject =
|
||||
error &&
|
||||
typeof error === 'object' &&
|
||||
('provider_request_payload' in error ||
|
||||
'provider_response_payload' in error ||
|
||||
'sent_at' in error ||
|
||||
'received_at' in error)
|
||||
|
||||
if (isProviderFailureObject) {
|
||||
return {
|
||||
hasError: true,
|
||||
status: error.status || TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_request_payload: error.provider_request_payload,
|
||||
provider_response_payload: error.provider_response_payload,
|
||||
sent_at: error.sent_at || new Date().toISOString(),
|
||||
received_at: error.received_at || new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
hasError: true,
|
||||
status: TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error?.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_response_payload: {
|
||||
error: error?.message || 'fetch failed',
|
||||
cause: error?.cause || null,
|
||||
},
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
private async runProviderCall(fn: () => Promise<any>) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error: any) {
|
||||
return this.mapProviderError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async originalSend(
|
||||
posId: string,
|
||||
invoice_id: string,
|
||||
@@ -40,6 +86,7 @@ export class SalesInvoiceTspService {
|
||||
const payload = await buildPayload(this.prisma, invoice_id, posId)
|
||||
|
||||
const attemptNumber = await getOriginalResendAttemptNumber(this.prisma, invoice_id)
|
||||
console.log('attemptNumber', attemptNumber)
|
||||
|
||||
const attempt = await this.prisma.saleInvoiceTspAttempts.create({
|
||||
data: {
|
||||
@@ -51,7 +98,15 @@ export class SalesInvoiceTspService {
|
||||
},
|
||||
})
|
||||
|
||||
const result = await trySend(this.tspSwitchService, payload)
|
||||
const result = await this.runProviderCall(() =>
|
||||
trySend(this.tspSwitchService, payload),
|
||||
)
|
||||
if (result?.hasError) {
|
||||
result.provider_request_payload =
|
||||
result.provider_request_payload || JSON.parse(JSON.stringify(payload))
|
||||
result.sent_at = result.sent_at || new Date().toISOString()
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
@@ -260,7 +315,16 @@ export class SalesInvoiceTspService {
|
||||
},
|
||||
)
|
||||
|
||||
const result = await trySend(this.tspSwitchService, correctionPayload)
|
||||
const result = await this.runProviderCall(() =>
|
||||
trySend(this.tspSwitchService, correctionPayload),
|
||||
)
|
||||
if (result?.hasError) {
|
||||
result.provider_request_payload =
|
||||
result.provider_request_payload ||
|
||||
JSON.parse(JSON.stringify(correctionPayload))
|
||||
result.sent_at = result.sent_at || new Date().toISOString()
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
console.log('sendResult')
|
||||
console.log(result)
|
||||
@@ -447,7 +511,15 @@ export class SalesInvoiceTspService {
|
||||
},
|
||||
)
|
||||
|
||||
const result = await this.tspSwitchService.revoke(revokePayload)
|
||||
const result = await this.runProviderCall(() =>
|
||||
this.tspSwitchService.revoke(revokePayload),
|
||||
)
|
||||
if (result?.hasError) {
|
||||
result.provider_request_payload =
|
||||
result.provider_request_payload || JSON.parse(JSON.stringify(revokePayload))
|
||||
result.sent_at = result.sent_at || new Date().toISOString()
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user