User notification controller

This commit is contained in:
Vins 2023-09-22 11:20:18 +02:00
parent d45fee73e5
commit fd8ff9ec78
4 changed files with 208 additions and 1 deletions

View File

@ -0,0 +1,114 @@
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";
@Controller()
@Service()
export default class UserNotificationController extends ApiController {
constructor(private userNotificationService: UserNotificationService) {
super();
}
/**
* @description Get all customers
*/
@Get("/api/v1/notifications")
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 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/notifications/:uid")
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;
}
//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/notifications/:uid")
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;
}
//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;
}
}
}

View File

@ -45,6 +45,7 @@ import CustomersController from "./api/customer/CustomersController";
import AppointmentsController from "./api/super-admin/AppointmentsController";
import VotesController from "./api/super-admin/VotesController";
import LiveVoteController from "./api/super-admin/LiveVoteController";
import UserNotificationController from "./api/common/UserNotificationController";
/**
@ -98,6 +99,7 @@ export default {
Container.get(FilesControllerCustomer);
Container.get(DocumentsControllerCustomer);
Container.get(OfficeFoldersController);
Container.get(CustomersController)
Container.get(CustomersController);
Container.get(UserNotificationController);
},
};

View File

@ -0,0 +1,53 @@
import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { Prisma, UserNotifications } from "prisma/prisma-client";
import UserNotification from "le-coffre-resources/dist/Notary/UserNotification";
@Service()
export default class UserNotificationRepository extends BaseRepository {
constructor(private database: Database) {
super();
}
protected get model() {
return this.database.getClient().userNotifications;
}
protected get instanceDb() {
return this.database.getClient();
}
/**
* @description : Find many user notifications
*/
public async findMany(query: Prisma.UserNotificationsFindManyArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
return this.model.findMany(query);
}
/**
* @description : find unique user notification
*/
public async findOneByUid(uid: string) {
return this.model.findUnique({
where: {
uid: uid,
},
});
}
/**
* @description : UUpdate a user notification
*/
public async update(uid: string, userNotification: UserNotification): Promise<UserNotifications> {
const updateArgs: Prisma.UserNotificationsUpdateArgs = {
where: {
uid: uid,
},
data: {
read: userNotification.read,
},
};
return this.model.update({ ...updateArgs });
}
}

View File

@ -0,0 +1,38 @@
import UserNotificationRepository from "@Repositories/UserNotificationRepository";
import BaseService from "@Services/BaseService";
import { UserNotifications } from "@prisma/client";
import UserNotification from "le-coffre-resources/dist/Notary/UserNotification";
import { Service } from "typedi";
@Service()
export default class UserNotificationService extends BaseService {
constructor(private userNotificationRepository: UserNotificationRepository) {
super();
}
/**
* @description : Get all user notifications
* @returns : T
* @throws {Error} If user notifications cannot be get
*/
public async get(query: any): Promise<UserNotifications[]> {
return this.userNotificationRepository.findMany(query);
}
/**
* @description : Update data of a deed with document types
* @throws {Error} If deeds cannot be updated with document types or one of them
*/
public async update(uid: string, userNotification: UserNotification): Promise<UserNotifications> {
return this.userNotificationRepository.update(uid, userNotification);
}
/**
* @description : Get a user notification by uid
* @returns : T
* @throws {Error} If user notification cannot found
*/
public async getByUid(uid: string, query?: any): Promise<UserNotifications | null> {
return this.userNotificationRepository.findOneByUid(uid);
}
}