141 lines
3.2 KiB
TypeScript
141 lines
3.2 KiB
TypeScript
import { IPosPayload } from '@/common/models'
|
|
import { ResponseMapper } from '@/common/response/response-mapper'
|
|
import { CustomerType } from '@/generated/prisma/enums'
|
|
import { CustomerSelect, CustomerWhereInput } from '@/generated/prisma/models'
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
import { Injectable } from '@nestjs/common'
|
|
import { PosCustomerFilterDto } from './dto/customer-filter.dto'
|
|
|
|
@Injectable()
|
|
export class CustomersService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
async findAll(posInfo: IPosPayload, query: PosCustomerFilterDto) {
|
|
const { type, q } = query
|
|
const where: CustomerWhereInput = {
|
|
type,
|
|
}
|
|
const select: CustomerSelect = {
|
|
id: true,
|
|
}
|
|
|
|
console.log('type', type)
|
|
console.log('type === CustomerType.LEGAL', type === CustomerType.LEGAL)
|
|
console.log('type === CustomerType.INDIVIDUAL', type === CustomerType.INDIVIDUAL)
|
|
|
|
if (type === CustomerType.LEGAL) {
|
|
where.legal = {
|
|
business_activity_id: posInfo.business_id,
|
|
}
|
|
select.legal = {
|
|
select: {
|
|
name: true,
|
|
economic_code: true,
|
|
postal_code: true,
|
|
registration_number: true,
|
|
},
|
|
}
|
|
if (q) {
|
|
where.legal = {
|
|
...where.legal,
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
economic_code: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
postal_code: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
registration_number: {
|
|
contains: q,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
}
|
|
} else if (type === CustomerType.INDIVIDUAL) {
|
|
where.individual = {
|
|
business_activity_id: posInfo.business_id,
|
|
}
|
|
select.individual = {
|
|
select: {
|
|
first_name: true,
|
|
last_name: true,
|
|
economic_code: true,
|
|
postal_code: true,
|
|
mobile_number: true,
|
|
national_id: true,
|
|
},
|
|
}
|
|
if (q) {
|
|
where.individual = {
|
|
...where.individual,
|
|
OR: [
|
|
{
|
|
first_name: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
last_name: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
economic_code: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
postal_code: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
mobile_number: {
|
|
contains: q,
|
|
},
|
|
},
|
|
{
|
|
national_id: {
|
|
contains: q,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
}
|
|
}
|
|
const customers = await this.prisma.customer.findMany({
|
|
where,
|
|
take: 20,
|
|
orderBy: {
|
|
created_at: 'desc',
|
|
},
|
|
select: {
|
|
id: true,
|
|
...select,
|
|
},
|
|
})
|
|
return ResponseMapper.list(customers)
|
|
}
|
|
|
|
findOne(id: number) {
|
|
// TODO: Implement fetching a single customer
|
|
return {}
|
|
}
|
|
|
|
create(data: any) {
|
|
// TODO: Implement customer creation
|
|
return data
|
|
}
|
|
}
|