2026-01-08 21:43:22 +00:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
HttpException,
|
|
|
|
|
HttpStatus,
|
|
|
|
|
Param,
|
|
|
|
|
Post,
|
|
|
|
|
Query,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
2025-01-27 20:44:27 +00:00
|
|
|
import { error } from 'console';
|
|
|
|
|
import { Customer } from 'src/domain/models/customer.model';
|
2026-01-08 21:43:22 +00:00
|
|
|
import { ResultModel } from '../../domain/models/result.model';
|
|
|
|
|
import { CustomerService } from './customer.service';
|
2025-01-27 20:44:27 +00:00
|
|
|
|
|
|
|
|
@ApiTags('Customer')
|
|
|
|
|
@Controller('api/v1/customer')
|
2026-01-08 21:43:22 +00:00
|
|
|
export class CustomerController {
|
|
|
|
|
constructor(private readonly customerService: CustomerService) {}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Get(':name')
|
|
|
|
|
async getCustomerByName(@Param('name') name: string) {
|
|
|
|
|
try {
|
|
|
|
|
const customers = await this.customerService.findCustomerByName(name);
|
|
|
|
|
return new ResultModel(true, null, customers, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(
|
|
|
|
|
false,
|
|
|
|
|
'Não foi possível consultar o cadastro de clientes.',
|
|
|
|
|
{},
|
|
|
|
|
error,
|
|
|
|
|
),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Get('categories/fechAll')
|
|
|
|
|
async getCategories() {
|
|
|
|
|
try {
|
|
|
|
|
const categories = await this.customerService.getCategory();
|
|
|
|
|
return categories;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(false, err.message, {}, error),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Get('subcategories/fechAll')
|
|
|
|
|
async getSubCategories() {
|
|
|
|
|
try {
|
|
|
|
|
const subCategories = await this.customerService.getSubCategory();
|
|
|
|
|
return subCategories;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(false, err.message, {}, error),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Get()
|
|
|
|
|
async getCustomer(@Query() query) {
|
|
|
|
|
try {
|
|
|
|
|
const field = query['field'];
|
|
|
|
|
const textSearch = query['textsearch'];
|
|
|
|
|
const customers = await this.customerService.findCustomerByQuery(
|
|
|
|
|
field,
|
|
|
|
|
textSearch,
|
|
|
|
|
);
|
|
|
|
|
return new ResultModel(true, null, customers, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// 'Não foi possível consultar o cadastro de clientes.'
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(false, err.message, {}, error),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Get(':id')
|
|
|
|
|
async getCustomerById(@Param('id') id: number) {
|
|
|
|
|
try {
|
|
|
|
|
const customers = await this.customerService.findCustomerById(id);
|
|
|
|
|
return new ResultModel(true, null, customers, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(
|
|
|
|
|
false,
|
|
|
|
|
'Não foi possível consultar o cadastro de clientes.',
|
|
|
|
|
{},
|
|
|
|
|
error,
|
|
|
|
|
),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get('cpf/:cpf')
|
|
|
|
|
async getCustomerByCpf(@Param('cpf') cpf: string) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('pesquisando por cpf');
|
|
|
|
|
const customer = await this.customerService.findCustomerByCpf(cpf);
|
|
|
|
|
if (!customer)
|
|
|
|
|
return new ResultModel(false, 'Cliente não cadastrado', null, null);
|
|
|
|
|
return new ResultModel(true, null, customer, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(
|
|
|
|
|
false,
|
|
|
|
|
'Não foi possível consultar o cadastro de clientes.',
|
|
|
|
|
{},
|
|
|
|
|
error,
|
|
|
|
|
),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
|
|
|
|
@Get('create/proxnumcli')
|
|
|
|
|
async IdCustomer() {
|
|
|
|
|
try {
|
|
|
|
|
console.log('proxnumcli');
|
|
|
|
|
const id = await this.customerService.generateIdCustomer();
|
|
|
|
|
return new ResultModel(true, null, id, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
2025-01-27 20:44:27 +00:00
|
|
|
|
2026-01-08 21:43:22 +00:00
|
|
|
@Post('create')
|
|
|
|
|
async createCustomer(@Body() customer: Customer) {
|
|
|
|
|
try {
|
|
|
|
|
console.log(customer);
|
|
|
|
|
const result = await this.customerService.createCustomer(customer);
|
|
|
|
|
return new ResultModel(true, null, result, null);
|
|
|
|
|
//return new ResultModel(true, null, id, null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
new ResultModel(false, 'Erro ao cadastrar cliente.', {}, err),
|
|
|
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
|
|
|
);
|
2025-01-27 20:44:27 +00:00
|
|
|
}
|
2026-01-08 21:43:22 +00:00
|
|
|
}
|
|
|
|
|
}
|