lecoffre-back/src/app/api/super-admin/CustomersController.ts
2023-04-20 09:52:26 +02:00

131 lines
3.8 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/super-admin/CustomersService/CustomersService";
import { Service } from "typedi";
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
import { Customer } from "le-coffre-resources/dist/SuperAdmin";
import { Customers } from "@prisma/client";
import { validateOrReject } from "class-validator";
@Controller()
@Service()
export default class CustomersController extends ApiController {
constructor(private customersService: CustomersService) {
super();
}
/**
* @description Get all customers
* @returns ICustomer[] list of customers
*/
@Get("/api/v1/super-admin/customers")
protected async get(req: Request, response: Response) {
try {
//get query
const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity
const customersEntity: Customers[] = await this.customersService.get(query);
//Hydrate ressource with prisma entity
const customers = ObjectHydrate.map<Customer>(Customer, customersEntity, { strategy: "exposeAll" });
//success
this.httpSuccess(response, customers);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Create a new customer
* @returns ICustomer created
*/
@Post("/api/v1/super-admin/customers")
protected async post(req: Request, response: Response) {
try {
//init IUser resource with request body values
const customerEntity = new Customer();
ObjectHydrate.hydrate(customerEntity, req.body);
//validate user
await validateOrReject(customerEntity, { groups: ["create"] });
//call service to get prisma entity
const prismaEntityCreated = await this.customersService.create(customerEntity);
//Hydrate ressource with prisma entity
const customerEntityCreated = ObjectHydrate.hydrate<Customer>(new Customer(), prismaEntityCreated, {
strategy: "exposeAll",
});
//success
this.httpSuccess(response, customerEntityCreated);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Modify a specific customer by uid
* @returns ICustomer modified
*/
@Put("/api/v1/super-admin/customers/:uid")
protected async put(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
//init IUser resource with request body values
const customerEntity = new Customer();
ObjectHydrate.hydrate(customerEntity, req.body);
//validate user
await validateOrReject(customerEntity, { groups: ["update"] });
//call service to get prisma entity
const prismaEntityUpdated = await this.customersService.update(uid, customerEntity);
//Hydrate ressource with prisma entity
const customerEntityUpdated = ObjectHydrate.hydrate<Customer>(new Customer(), prismaEntityUpdated, {
strategy: "exposeAll",
});
//success
this.httpSuccess(response, customerEntityUpdated);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Get a specific customer by uid
* @returns ICustomer
*/
@Get("/api/v1/super-admin/customers/:uid")
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uuid provided");
}
//call service to get prisma entity
const customerEntity: Customers = await this.customersService.getByUid(uid);
//Hydrate ressource with prisma entity
const customer = ObjectHydrate.hydrate<Customer>(new Customer(), customerEntity, { strategy: "exposeAll" });
//success
this.httpSuccess(response, customer);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
}