lecoffre-back/src/app/api/super-admin/AppointmentsController.ts
2023-07-26 17:11:32 +02:00

77 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 AppointmentsService from "@Services/super-admin/AppointmentsService/AppointmentsService";
import { Service } from "typedi";
import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
import authHandler from "@App/middlewares/AuthHandler";
@Controller()
@Service()
export default class AppointmentsController extends ApiController {
constructor(private appointmentsService: AppointmentsService) {
super();
}
/**
* @description Get all appointments
*/
@Get("/api/v1/super-admin/appointments", [authHandler])
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 appointmentsEntities = await this.appointmentsService.get(query);
//Hydrate ressource with prisma entity
const appointments = Appointment.hydrateArray<Appointment>(appointmentsEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, appointments);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific appointment by uid
*/
@Get("/api/v1/super-admin/appointments/:uid", [authHandler])
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 appointmentEntity = await this.appointmentsService.getByUid(uid, query);
if (!appointmentEntity) {
this.httpNotFoundRequest(response, "appointment not found");
return;
}
//Hydrate ressource with prisma entity
const appointment = Appointment.hydrate<Appointment>(appointmentEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, appointment);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}