80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import CustomersService from "@Services/customer/CustomersService/CustomersService";
|
|
import { Service } from "typedi";
|
|
import Customer from "le-coffre-resources/dist/Customer";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class CustomersController extends ApiController {
|
|
constructor(private customersService: CustomersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all customers
|
|
*/
|
|
@Get("/api/v1/customer/customers")
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
|
|
//call service to get prisma entity
|
|
const customersEntities = await this.customersService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customers = Customer.hydrateArray<Customer>(customersEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, customers);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @description Get a specific customer by uid
|
|
*/
|
|
@Get("/api/v1/customer/customers/:uid", [authHandler, ruleHandler])
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
const customerEntity = await this.customersService.getByUid(uid, query);
|
|
|
|
if (!customerEntity) {
|
|
this.httpNotFoundRequest(response, "customer not found");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customer = Customer.hydrate<Customer>(customerEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|