feat: refactor sales invoice services and introduce pagination and filtering
- Added SharedSaleInvoicePaginationService for handling pagination logic. - Introduced SharedSaleInvoiceFilterService to centralize filtering logic for sales invoices. - Updated SalesInvoicesService to utilize the new pagination and filtering services. - Refactored findAll methods in SalesInvoicesService, CustomerSaleInvoicesService, and other related services to support pagination and filtering. - Enhanced DTOs for sales invoice filtering to extend shared filter properties. - Updated module imports to include new services. - Cleaned up redundant code related to filtering and pagination across various services.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
} from 'class-validator'
|
||||
import { TspProviderResponseStatus } from 'generated/prisma/enums'
|
||||
|
||||
export class SharedSaleInvoicesFilterDto {
|
||||
@ApiPropertyOptional({ default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
page?: number
|
||||
|
||||
@ApiPropertyOptional({ default: 10 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
perPage?: number
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
invoice_date_from?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
invoice_date_to?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
created_at_from?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
created_at_to?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
customer_name?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
customer_mobile?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
customer_national_id?: string
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
customer_economic_code?: string
|
||||
|
||||
@ApiPropertyOptional({ enum: TspProviderResponseStatus })
|
||||
@IsOptional()
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status?: TspProviderResponseStatus
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
total_amount?: number
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
total_amount_from?: number
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
total_amount_to?: number
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { SalesInvoiceWhereInput } from '@/generated/prisma/models'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { SharedSaleInvoicesFilterDto } from './sale-invoice-filter.dto'
|
||||
|
||||
@Injectable()
|
||||
export class SharedSaleInvoiceFilterService {
|
||||
buildWhere(filter: SharedSaleInvoicesFilterDto): SalesInvoiceWhereInput {
|
||||
const where: SalesInvoiceWhereInput = {}
|
||||
|
||||
if (filter.invoice_date_from || filter.invoice_date_to) {
|
||||
where.invoice_date = {
|
||||
...(filter.invoice_date_from ? { gte: new Date(filter.invoice_date_from) } : {}),
|
||||
...(filter.invoice_date_to ? { lte: new Date(filter.invoice_date_to) } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.created_at_from || filter.created_at_to) {
|
||||
where.created_at = {
|
||||
...(filter.created_at_from ? { gte: new Date(filter.created_at_from) } : {}),
|
||||
...(filter.created_at_to ? { lte: new Date(filter.created_at_to) } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
filter.total_amount !== undefined ||
|
||||
filter.total_amount_from !== undefined ||
|
||||
filter.total_amount_to !== undefined
|
||||
) {
|
||||
where.total_amount = {
|
||||
...(filter.total_amount !== undefined ? { equals: filter.total_amount } : {}),
|
||||
...(filter.total_amount_from !== undefined
|
||||
? { gte: filter.total_amount_from }
|
||||
: {}),
|
||||
...(filter.total_amount_to !== undefined ? { lte: filter.total_amount_to } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
filter.customer_name?.trim() ||
|
||||
filter.customer_mobile?.trim() ||
|
||||
filter.customer_national_id?.trim() ||
|
||||
filter.customer_economic_code?.trim()
|
||||
) {
|
||||
where.customer = {
|
||||
is: {
|
||||
OR: [
|
||||
...(filter.customer_name?.trim()
|
||||
? [
|
||||
{
|
||||
individual: {
|
||||
is: {
|
||||
OR: [
|
||||
{ first_name: { contains: filter.customer_name.trim() } },
|
||||
{ last_name: { contains: filter.customer_name.trim() } },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
legal: {
|
||||
is: {
|
||||
name: {
|
||||
contains: filter.customer_name.trim(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(filter.customer_mobile?.trim()
|
||||
? [
|
||||
{
|
||||
individual: {
|
||||
is: {
|
||||
mobile_number: { contains: filter.customer_mobile.trim() },
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(filter.customer_national_id?.trim()
|
||||
? [
|
||||
{
|
||||
individual: {
|
||||
is: {
|
||||
national_id: { contains: filter.customer_national_id.trim() },
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(filter.customer_economic_code?.trim()
|
||||
? [
|
||||
{
|
||||
legal: {
|
||||
is: {
|
||||
OR: [
|
||||
{
|
||||
economic_code: {
|
||||
contains: filter.customer_economic_code.trim(),
|
||||
},
|
||||
},
|
||||
{
|
||||
registration_number: {
|
||||
contains: filter.customer_economic_code.trim(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (filter.status) {
|
||||
if (filter.status === TspProviderResponseStatus.NOT_SEND) {
|
||||
where.OR = [
|
||||
{
|
||||
last_tsp_status: null,
|
||||
},
|
||||
{
|
||||
last_tsp_status: TspProviderResponseStatus.NOT_SEND,
|
||||
},
|
||||
]
|
||||
} else {
|
||||
where.last_tsp_status = filter.status
|
||||
}
|
||||
}
|
||||
|
||||
return where
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@Injectable()
|
||||
export class SharedSaleInvoicePaginationService {
|
||||
normalize(
|
||||
page: number = 1,
|
||||
perPage: number = 10,
|
||||
defaultPerPage: number = 10,
|
||||
maxPerPage: number = 50,
|
||||
) {
|
||||
const normalizedPageValue = Number(page ?? 1)
|
||||
const normalizedPage = Number.isFinite(normalizedPageValue)
|
||||
? Math.max(1, Math.floor(normalizedPageValue))
|
||||
: 1
|
||||
|
||||
const requestedPerPageValue = Number(perPage ?? defaultPerPage)
|
||||
const requestedPerPage = Number.isFinite(requestedPerPageValue)
|
||||
? Math.max(1, Math.floor(requestedPerPageValue))
|
||||
: defaultPerPage
|
||||
const normalizedPerPage = Math.min(requestedPerPage, maxPerPage)
|
||||
|
||||
return {
|
||||
page: normalizedPage,
|
||||
perPage: normalizedPerPage,
|
||||
skip: (normalizedPage - 1) * normalizedPerPage,
|
||||
take: normalizedPerPage,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user