feat(customers): enhance customer retrieval with filtering options

- Updated `findAll` method in `CustomersController` to accept filtering parameters.
- Implemented filtering logic in `CustomersService` to retrieve customers based on type and search query.
- Added `PosCustomerFilterDto` for query validation.
- Updated customer-related DTOs to make fields optional and added length validation where necessary.
- Modified customer-related logic in `sales-invoice-tsp.utils.ts` to accommodate new DTO structure.
- Added unique constraints to `customer_individuals` and `customer_legal` tables in the database migration.
- Removed commented-out code in `PosMiddleware` for cleaner codebase.
- Set default value for `is_default_guild_good` to false in `OwnedGoodsService`.
This commit is contained in:
2026-06-03 18:00:06 +03:30
parent b2d8fdc8a0
commit 5ce560ce97
19 changed files with 946 additions and 613 deletions
@@ -214,84 +214,126 @@ export class SharedSaleInvoiceCreateService {
data: SharedCreateSalesInvoiceDto,
businessId: string,
) {
if (data.customer_id) {
return data.customer_id
const { customer_id, customer_type, customer } = data
if (customer_id) {
return customer_id
}
if (
data.customer_type === CustomerType.INDIVIDUAL &&
data.customer?.customer_individual
) {
const { national_id, mobile_number, ...rest } = data.customer.customer_individual
if (customer_type === CustomerType.INDIVIDUAL && customer?.customer_individual) {
const { national_id, postal_code, economic_code, ...rest } =
customer.customer_individual
const customerIndividual = await tx.customerIndividual.upsert({
const foundedCustomer = await tx.customerIndividual.findFirst({
where: {
business_activity_id_national_id: {
business_activity_id: businessId,
national_id: data.customer.customer_individual.national_id,
},
},
create: {
...data.customer.customer_individual,
customer: {
create: {
type: CustomerType.INDIVIDUAL,
business_activity_id: businessId,
OR: [
{
economic_code,
},
},
business_activity: {
connect: {
id: businessId,
{
postal_code,
national_id,
},
},
},
update: { ...rest },
select: {
customer_id: true,
},
})
if (!customerIndividual) {
throw new BadRequestException('متاسفانه مشکلی در اطلاعات مشتری وجود دارد.')
}
return customerIndividual.customer_id
}
if (data.customer_type === CustomerType.LEGAL && data.customer?.customer_legal) {
const { registration_number, ...rest } = data.customer.customer_legal
const customerLegal = await tx.customerLegal.upsert({
where: {
business_activity_id_economic_code: {
business_activity_id: businessId,
economic_code: data.customer.customer_legal.economic_code,
},
},
create: {
...data.customer.customer_legal,
customer: {
create: {
type: CustomerType.LEGAL,
},
},
business_activity: {
connect: {
id: businessId,
},
},
},
update: {
...rest,
],
},
select: {
customer_id: true,
},
})
let customerIndividualId: string | undefined = foundedCustomer?.customer_id
if (!customerLegal) {
if (foundedCustomer) {
await tx.customerIndividual.update({
where: {
customer_id: foundedCustomer.customer_id,
},
data: {
...customer.customer_individual,
},
})
} else {
const createdCustomer = await tx.customerIndividual.create({
data: {
...customer.customer_individual,
customer: {
create: {
type: CustomerType.INDIVIDUAL,
},
},
business_activity: {
connect: {
id: businessId,
},
},
},
})
customerIndividualId = createdCustomer.customer_id
}
if (!customerIndividualId) {
throw new BadRequestException('متاسفانه مشکلی در اطلاعات مشتری وجود دارد.')
}
return customerLegal.customer_id
return customerIndividualId
}
if (customer_type === CustomerType.LEGAL && customer?.customer_legal) {
const { registration_number, economic_code, postal_code } = customer.customer_legal
const foundedCustomer = await tx.customerLegal.findFirst({
where: {
business_activity_id: businessId,
OR: [
{
economic_code,
},
{
postal_code,
registration_number,
},
],
},
select: {
customer_id: true,
},
})
let customerLegalId: string | undefined = foundedCustomer?.customer_id
if (foundedCustomer) {
await tx.customerLegal.update({
where: {
customer_id: foundedCustomer.customer_id,
},
data: {
...customer.customer_legal,
},
})
} else {
const createdCustomer = await tx.customerLegal.create({
data: {
...customer.customer_legal,
customer: {
create: {
type: CustomerType.LEGAL,
},
},
business_activity: {
connect: {
id: businessId,
},
},
},
})
customerLegalId = createdCustomer.customer_id
}
if (!customerLegalId) {
throw new BadRequestException('متاسفانه مشکلی در اطلاعات مشتری وجود دارد.')
}
return customerLegalId
}
return null