146 lines
4.1 KiB
TypeScript
146 lines
4.1 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import CustomersService from "@Services/admin/CustomersService/CustomersService";
|
|
import { Service } from "typedi";
|
|
import { Customer } from "le-coffre-resources/dist/Admin";
|
|
import { validateOrReject } from "class-validator";
|
|
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/admin/customers", [authHandler, ruleHandler])
|
|
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 Create a new customer
|
|
*/
|
|
@Post("/api/v1/admin/customers", [authHandler, ruleHandler])
|
|
protected async post(req: Request, response: Response) {
|
|
try {
|
|
//init IUser resource with request body values
|
|
const customerEntity = Customer.hydrate<Customer>(req.body);
|
|
//validate user
|
|
await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false });
|
|
|
|
//call service to get prisma entity
|
|
const customerEntityCreated = await this.customersService.create(customerEntity);
|
|
//Hydrate ressource with prisma entity
|
|
const customer = Customer.hydrate<Customer>(customerEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpCreated(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific customer by uid
|
|
*/
|
|
@Put("/api/v1/admin/customers/:uid", [authHandler, ruleHandler])
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const userFound = await this.customersService.getByUid(uid);
|
|
|
|
if (!userFound) {
|
|
this.httpNotFoundRequest(response, "user not found");
|
|
return;
|
|
}
|
|
|
|
//init IUser resource with request body values
|
|
const customerEntity = Customer.hydrate<Customer>(req.body);
|
|
|
|
//validate user
|
|
await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false });
|
|
|
|
//call service to get prisma entity
|
|
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customer = Customer.hydrate<Customer>(customerEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific customer by uid
|
|
*/
|
|
@Get("/api/v1/admin/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;
|
|
}
|
|
//get query
|
|
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;
|
|
}
|
|
}
|
|
}
|