From fd8ff9ec78f4b32bb16a65bb6ba2f242e5dd3bf0 Mon Sep 17 00:00:00 2001 From: Vins Date: Fri, 22 Sep 2023 11:20:18 +0200 Subject: [PATCH] User notification controller --- .../api/common/UserNotificationController.ts | 114 ++++++++++++++++++ src/app/index.ts | 4 +- .../UserNotificationRepository.ts | 53 ++++++++ .../UserNotificationService.ts | 38 ++++++ 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/app/api/common/UserNotificationController.ts create mode 100644 src/common/repositories/UserNotificationRepository.ts create mode 100644 src/services/common/UserNotificationService/UserNotificationService.ts diff --git a/src/app/api/common/UserNotificationController.ts b/src/app/api/common/UserNotificationController.ts new file mode 100644 index 00000000..39709b82 --- /dev/null +++ b/src/app/api/common/UserNotificationController.ts @@ -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(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(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(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(userNotificationEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, userNotification); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/index.ts b/src/app/index.ts index dc98b2e4..68208213 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -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); }, }; diff --git a/src/common/repositories/UserNotificationRepository.ts b/src/common/repositories/UserNotificationRepository.ts new file mode 100644 index 00000000..67826d94 --- /dev/null +++ b/src/common/repositories/UserNotificationRepository.ts @@ -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 { + const updateArgs: Prisma.UserNotificationsUpdateArgs = { + where: { + uid: uid, + }, + data: { + read: userNotification.read, + }, + }; + + return this.model.update({ ...updateArgs }); + } +} diff --git a/src/services/common/UserNotificationService/UserNotificationService.ts b/src/services/common/UserNotificationService/UserNotificationService.ts new file mode 100644 index 00000000..f5feb8b7 --- /dev/null +++ b/src/services/common/UserNotificationService/UserNotificationService.ts @@ -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 { + 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 { + 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 { + return this.userNotificationRepository.findOneByUid(uid); + } +}