2026-04-08 18:15:44 +03:30
|
|
|
import { CustomerSelect, CustomerWhereInput } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import {
|
|
|
|
|
UpdateCustomerIndividualDto,
|
|
|
|
|
UpdateCustomerLegalDto,
|
|
|
|
|
} from './dto/create-customers.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class consumerCustomersService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
defaultSelect: CustomerSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
type: true,
|
|
|
|
|
is_favorite: true,
|
|
|
|
|
created_at: true,
|
2026-04-27 10:45:39 +03:30
|
|
|
individual: {
|
2026-04-08 18:15:44 +03:30
|
|
|
select: {
|
|
|
|
|
first_name: true,
|
|
|
|
|
last_name: true,
|
|
|
|
|
national_id: true,
|
|
|
|
|
postal_code: true,
|
|
|
|
|
economic_code: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-27 10:45:39 +03:30
|
|
|
legal: {
|
2026-04-08 18:15:44 +03:30
|
|
|
select: {
|
|
|
|
|
company_name: true,
|
|
|
|
|
registration_number: true,
|
|
|
|
|
postal_code: true,
|
|
|
|
|
economic_code: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
private defaultWhere(consumer_id: string): CustomerWhereInput {
|
2026-04-08 18:15:44 +03:30
|
|
|
return {
|
|
|
|
|
OR: [
|
|
|
|
|
{
|
2026-04-27 10:45:39 +03:30
|
|
|
individual: {
|
|
|
|
|
business_activity: {
|
|
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-04-27 10:45:39 +03:30
|
|
|
legal: {
|
|
|
|
|
business_activity: {
|
|
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
async findAll(consumer_id: string, page = 1, perPage = 10) {
|
|
|
|
|
const [customers, total] = await this.prisma.$transaction(async tx => [
|
2026-04-08 18:15:44 +03:30
|
|
|
await tx.customer.findMany({
|
2026-04-11 14:47:05 +03:30
|
|
|
where: this.defaultWhere(consumer_id),
|
2026-04-08 18:15:44 +03:30
|
|
|
select: this.defaultSelect,
|
2026-04-26 09:54:48 +03:30
|
|
|
skip: (page - 1) * perPage,
|
2026-04-08 18:15:44 +03:30
|
|
|
take: 10,
|
|
|
|
|
}),
|
|
|
|
|
await tx.customer.count({
|
2026-04-11 14:47:05 +03:30
|
|
|
where: this.defaultWhere(consumer_id),
|
2026-04-08 18:15:44 +03:30
|
|
|
}),
|
|
|
|
|
])
|
2026-04-26 09:54:48 +03:30
|
|
|
return ResponseMapper.paginate(customers, { total, page, perPage })
|
2026-04-08 18:15:44 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findOne(consumer_id: string, customer_id: string) {
|
2026-04-08 18:15:44 +03:30
|
|
|
const customer = await this.prisma.customer.findUniqueOrThrow({
|
|
|
|
|
where: {
|
2026-04-11 14:47:05 +03:30
|
|
|
...this.defaultWhere(consumer_id),
|
2026-04-08 18:15:44 +03:30
|
|
|
id: customer_id,
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single(customer)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async updateLegal(
|
|
|
|
|
consumer_id: string,
|
|
|
|
|
customer_id: string,
|
|
|
|
|
data: UpdateCustomerLegalDto,
|
|
|
|
|
) {
|
2026-04-08 18:15:44 +03:30
|
|
|
const customer = await this.prisma.customer.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: customer_id,
|
2026-04-27 10:45:39 +03:30
|
|
|
legal: {
|
|
|
|
|
business_activity: {
|
|
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
is_favorite: data.is_favorite,
|
2026-04-27 10:45:39 +03:30
|
|
|
legal: {
|
2026-04-08 18:15:44 +03:30
|
|
|
update: {
|
|
|
|
|
...data,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.update(customer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateIndividual(
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id: string,
|
2026-04-08 18:15:44 +03:30
|
|
|
customer_id: string,
|
|
|
|
|
data: UpdateCustomerIndividualDto,
|
|
|
|
|
) {
|
|
|
|
|
const customer = await this.prisma.customer.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: customer_id,
|
2026-04-27 10:45:39 +03:30
|
|
|
individual: {
|
|
|
|
|
business_activity: {
|
|
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
is_favorite: data.is_favorite,
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
individual: {
|
2026-04-08 18:15:44 +03:30
|
|
|
update: {
|
|
|
|
|
...data,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.update(customer)
|
|
|
|
|
}
|
|
|
|
|
}
|