139 lines
4.4 KiB
TypeScript
139 lines
4.4 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import { Service } from "typedi";
|
|
import UserNotification from "le-coffre-resources/dist/Notary/UserNotification";
|
|
import UserNotificationService from "@Services/common/UserNotificationService/UserNotificationService";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import { Prisma } from "@prisma/client";
|
|
import roleHandler from "@App/middlewares/RolesHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class UserNotificationController extends ApiController {
|
|
constructor(private userNotificationService: UserNotificationService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all customers
|
|
*/
|
|
@Get("/api/v1/notary/notifications", [authHandler, roleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.UserNotificationsFindManyArgs = {};
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
if (query.where?.uid) {
|
|
this.httpBadRequest(response, "You can't filter by uid");
|
|
return;
|
|
}
|
|
}
|
|
const userId: string = req.body.user.userId;
|
|
if(query.where?.user_uid) delete query.where.user_uid;
|
|
if(query.where?.user?.uid) delete query.where.user.uid;
|
|
query.orderBy = { notification : { created_at: "asc" }};
|
|
const notificationWhereInput: Prisma.UserNotificationsWhereInput = { ...query.where, user_uid: userId };
|
|
query.where = notificationWhereInput;
|
|
|
|
query.include = { notification: true };
|
|
//call service to get prisma entity
|
|
const userNotificationEntities = await this.userNotificationService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const userNotifications = UserNotification.hydrateArray<UserNotification>(userNotificationEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, userNotifications);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific customer by uid
|
|
*/
|
|
@Put("/api/v1/notary/notifications/:uid", [authHandler, roleHandler])
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const userNotificationFound = await this.userNotificationService.getByUid(uid);
|
|
|
|
if (!userNotificationFound) {
|
|
this.httpNotFoundRequest(response, "user notification not found");
|
|
return;
|
|
}
|
|
|
|
if(userNotificationFound.user_uid !== req.body.user.userId) {
|
|
this.httpForbidden(response, "You are not allowed to update this user notification");
|
|
return;
|
|
}
|
|
|
|
//init IUser resource with request body values
|
|
const userNotificationEntity = UserNotification.hydrate<UserNotification>(req.body);
|
|
|
|
//call service to get prisma entity
|
|
const userNotificationEntityUpdated = await this.userNotificationService.update(uid, userNotificationEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customer = UserNotification.hydrate<UserNotification>(userNotificationEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific customer by uid
|
|
*/
|
|
@Get("/api/v1/notary/notifications/:uid", [authHandler, roleHandler])
|
|
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 userNotificationEntity = await this.userNotificationService.getByUid(uid, query);
|
|
|
|
if (!userNotificationEntity) {
|
|
this.httpNotFoundRequest(response, "user notification not found");
|
|
return;
|
|
}
|
|
|
|
if(userNotificationEntity.user_uid !== req.body.userId) {
|
|
this.httpForbidden(response, "You are allowed to get this user notification");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const userNotification = UserNotification.hydrate<UserNotification>(userNotificationEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, userNotification);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|