update switch providers and nama provider. fix original send
This commit is contained in:
@@ -9,11 +9,11 @@ import {
|
||||
import {
|
||||
TspProviderActionResponseDto,
|
||||
TspProviderCorrectionInvoicePayloadDto,
|
||||
} from './dto/provider-switch.dto'
|
||||
} from './dto'
|
||||
import { SalesInvoiceTspSwitchService } from './sales-invoice-tsp-switch.service'
|
||||
import {
|
||||
buildCorrectionPayload,
|
||||
buildPayload,
|
||||
buildOriginalPayload,
|
||||
buildRevokePayload,
|
||||
getOriginalResendAttemptNumber,
|
||||
onResult,
|
||||
@@ -33,81 +33,27 @@ 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,
|
||||
): Promise<TspProviderActionResponseDto> {
|
||||
const payload = await buildPayload(this.prisma, invoice_id, posId)
|
||||
const payload = await buildOriginalPayload(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: {
|
||||
attempt_no: attemptNumber,
|
||||
invoice_id,
|
||||
provider_request_payload: {},
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
raw_request_payload: JSON.parse(JSON.stringify(payload)),
|
||||
sent_at: new Date().toISOString(),
|
||||
message: 'در حال ارسال به سامانه مالیاتی...',
|
||||
},
|
||||
})
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
const result = await trySend(this.tspSwitchService, payload)
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
|
||||
@@ -206,8 +152,6 @@ export class SalesInvoiceTspService {
|
||||
business_activity.partner_token,
|
||||
)
|
||||
|
||||
console.log('getResult', result)
|
||||
|
||||
if (!result) {
|
||||
throw new NotFoundException('نتیجه ارسال فاکتور یافت نشد.')
|
||||
}
|
||||
@@ -217,7 +161,7 @@ export class SalesInvoiceTspService {
|
||||
id: attempt.id,
|
||||
},
|
||||
data: {
|
||||
provider_response_payload: JSON.parse(JSON.stringify(result)),
|
||||
provider_response: JSON.parse(JSON.stringify(result)),
|
||||
status: result.status,
|
||||
received_at: result.received_at,
|
||||
},
|
||||
@@ -309,6 +253,7 @@ export class SalesInvoiceTspService {
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
sent_at: new Date().toISOString(),
|
||||
raw_request_payload: JSON.parse(JSON.stringify(correctionPayload)),
|
||||
provider_request_payload: {},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -326,8 +271,6 @@ export class SalesInvoiceTspService {
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
console.log('sendResult')
|
||||
console.log(result)
|
||||
return onResult(this.prisma, result, attempt.id)
|
||||
|
||||
// const countByGoodId = (goodIds: string[]): Map<string, number> => {
|
||||
@@ -505,6 +448,7 @@ export class SalesInvoiceTspService {
|
||||
message: 'در حال ارسال به سامانه مالیاتی...',
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
raw_request_payload: JSON.parse(JSON.stringify(payload)),
|
||||
provider_request_payload: {},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -524,4 +468,28 @@ export class SalesInvoiceTspService {
|
||||
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
|
||||
private mapProviderError(error: any) {
|
||||
return {
|
||||
hasError: true,
|
||||
status: error.status || TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_response_payload: error.provider_response_payload || {
|
||||
error: error?.message || 'fetch failed',
|
||||
cause: error?.cause || null,
|
||||
},
|
||||
sent_at: error.sent_at || new Date().toISOString(),
|
||||
received_at: error.received_at || new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
private async runProviderCall(fn: () => Promise<any>) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error: any) {
|
||||
return this.mapProviderError(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user