145 lines
3.4 KiB
TypeScript
145 lines
3.4 KiB
TypeScript
import { RedisKeyMaker, translateEnumValue } from '@/common/utils'
|
|
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
|
import { SalesInvoiceSelect } from '@/generated/prisma/models'
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
import { RedisService } from '@/redis/redis.service'
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
@Injectable()
|
|
export class InvoicesService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly redisService: RedisService,
|
|
) {}
|
|
|
|
private readonly summarySelect: SalesInvoiceSelect = {
|
|
id: true,
|
|
code: true,
|
|
invoice_date: true,
|
|
invoice_number: true,
|
|
notes: true,
|
|
tax_id: true,
|
|
total_amount: true,
|
|
type: true,
|
|
pos: {
|
|
select: {
|
|
name: true,
|
|
complex: {
|
|
select: {
|
|
name: true,
|
|
branch_code: true,
|
|
business_activity: {
|
|
select: {
|
|
name: true,
|
|
guild: {
|
|
select: {
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
payments: {
|
|
select: {
|
|
amount: true,
|
|
paid_at: true,
|
|
payment_method: true,
|
|
terminal_info: {
|
|
select: {
|
|
stan: true,
|
|
rrn: true,
|
|
transaction_date_time: true,
|
|
customer_card_no: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
unknown_customer: true,
|
|
customer: {
|
|
select: {
|
|
type: true,
|
|
legal: {
|
|
select: {
|
|
name: true,
|
|
economic_code: true,
|
|
postal_code: true,
|
|
registration_number: true,
|
|
},
|
|
},
|
|
individual: {
|
|
select: {
|
|
economic_code: true,
|
|
first_name: true,
|
|
last_name: true,
|
|
mobile_number: true,
|
|
national_id: true,
|
|
postal_code: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
items: {
|
|
select: {
|
|
good_snapshot: true,
|
|
measure_unit_text: true,
|
|
notes: true,
|
|
quantity: true,
|
|
total_amount: true,
|
|
unit_price: true,
|
|
discount_amount: true,
|
|
tax_amount: true,
|
|
payload: true,
|
|
sku_code: true,
|
|
sku_vat: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
private readonly select: SalesInvoiceSelect = {
|
|
...this.summarySelect,
|
|
tsp_attempts: {
|
|
orderBy: {
|
|
created_at: 'desc',
|
|
},
|
|
select: {
|
|
status: true,
|
|
},
|
|
take: 1,
|
|
},
|
|
}
|
|
|
|
private readonly where = () => ({})
|
|
|
|
private readonly invoiceMapper = (invoice: any) => {
|
|
const { tsp_attempts, type, ...rest } = invoice || {}
|
|
|
|
return {
|
|
...rest,
|
|
type: translateEnumValue('TspProviderRequestType', type),
|
|
status: translateEnumValue(
|
|
'TspProviderResponseStatus',
|
|
invoice.tsp_attempts?.[0]?.status || TspProviderResponseStatus.NOT_SEND,
|
|
),
|
|
}
|
|
}
|
|
|
|
async findOne(id: string) {
|
|
await this.redisService.delete(RedisKeyMaker.publicSaleInvoice(id))
|
|
return await this.redisService.getAndSet(
|
|
RedisKeyMaker.publicSaleInvoice(id),
|
|
'single',
|
|
async () => {
|
|
const item = await this.prisma.salesInvoice.findUnique({
|
|
where: { ...this.where(), id },
|
|
select: this.select,
|
|
})
|
|
return this.invoiceMapper(item)
|
|
},
|
|
60 * 60,
|
|
)
|
|
}
|
|
}
|