Files
psp_api/src/modules/consumer/customers/sale-invoices/sale-invoices.service.ts
T

124 lines
3.4 KiB
TypeScript
Raw Normal View History

import { QUERY_CONSTANTS } from '@/common/queryConstants'
import { SharedSaleInvoicePaginationService } from '@/common/services/saleInvoices/sale-invoice-pagination.service'
import { translateEnumValue } from '@/common/utils'
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
import { SalesInvoiceWhereInput } from '@/generated/prisma/models'
2026-04-08 18:15:44 +03:30
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable, NotFoundException } from '@nestjs/common'
2026-04-08 18:15:44 +03:30
import { ResponseMapper } from 'common/response/response-mapper'
@Injectable()
export class CustomerSaleInvoicesService {
constructor(
private readonly prisma: PrismaService,
private readonly sharedSaleInvoicePaginationService: SharedSaleInvoicePaginationService,
) {}
2026-04-08 18:15:44 +03:30
async findAll(
consumer_id: string,
customer_id: string,
requestedPage = 1,
requestedPerPage = 10,
) {
const { page, perPage, skip, take } =
this.sharedSaleInvoicePaginationService.normalize(requestedPage, requestedPerPage)
2026-04-08 18:15:44 +03:30
const salesWhere: SalesInvoiceWhereInput = {
customer_id,
pos: {
complex: {
business_activity: {
consumer_id,
2026-04-08 18:15:44 +03:30
},
},
},
}
const [saleInvoices, total] = await this.prisma.$transaction(async tx => [
2026-04-08 18:15:44 +03:30
await tx.salesInvoice.findMany({
where: salesWhere,
select: {
...QUERY_CONSTANTS.SALE_INVOICE.summarySelect,
pos: {
select: {
name: true,
complex: {
select: {
name: true,
business_activity: {
select: {
name: true,
},
},
},
},
},
},
2026-04-08 18:15:44 +03:30
_count: {
select: {
items: true,
},
},
},
skip,
take,
2026-04-08 18:15:44 +03:30
}),
await tx.salesInvoice.count({
where: salesWhere,
}),
])
const mappedAccounts = saleInvoices.map(saleInvoice => {
const { _count, consumer_account, last_tsp_status, ...rest } = saleInvoice
2026-04-08 18:15:44 +03:30
return {
...rest,
items_count: _count.items,
status: translateEnumValue('TspProviderResponseStatus', last_tsp_status),
2026-04-08 18:15:44 +03:30
}
})
return ResponseMapper.paginate(mappedAccounts, {
total,
2026-04-08 18:15:44 +03:30
page,
perPage,
2026-04-08 18:15:44 +03:30
})
}
async findOne(consumer_id: string, customer_id: string, id: string) {
const invoice = await this.prisma.salesInvoice.findUnique({
2026-04-08 18:15:44 +03:30
where: {
id,
customer_id,
pos: {
complex: {
business_activity: {
consumer_id,
2026-04-08 18:15:44 +03:30
},
},
},
},
select: {
...QUERY_CONSTANTS.SALE_INVOICE.select,
2026-04-08 18:15:44 +03:30
},
})
if (invoice) {
const { type, ...rest } = invoice
const mappedInvoice = {
...rest,
type: translateEnumValue('TspProviderRequestType', type),
status: translateEnumValue(
'TspProviderResponseStatus',
invoice.last_tsp_status || TspProviderResponseStatus.NOT_SEND,
),
settlement_type: translateEnumValue(
'InvoiceSettlementType',
invoice.settlement_type,
),
}
return ResponseMapper.single(mappedInvoice)
}
throw new NotFoundException('صورت‌حساب مورد نظر شما یافت نشد.')
2026-04-08 18:15:44 +03:30
}
}