diff --git a/package.json b/package.json index d2d12d63..02876930 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "cors": "^2.8.5", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.54", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.55", "module-alias": "^2.2.2", "multer": "^1.4.5-lts.1", "next": "^13.1.5", diff --git a/src/app/api/admin/CustomersController.ts b/src/app/api/admin/CustomersController.ts new file mode 100644 index 00000000..01097a90 --- /dev/null +++ b/src/app/api/admin/CustomersController.ts @@ -0,0 +1,146 @@ +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/admin/CustomersService/CustomersService"; +import { Service } from "typedi"; +import { Customer } from "le-coffre-resources/dist/Admin"; +import { Customers } from "@prisma/client"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class CustomersController extends ApiController { + constructor(private customersService: CustomersService) { + super(); + } + + /** + * @description Get all customers + */ + @Get("/api/v1/admin/customers", [authHandler, ruleHandler]) + 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 customersEntities = await this.customersService.get(query); + + //Hydrate ressource with prisma entity + const customers = Customer.hydrateArray(customersEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customers); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new customer + */ + @Post("/api/v1/admin/customers", [authHandler, ruleHandler]) + protected async post(req: Request, response: Response) { + try { + //init IUser resource with request body values + const customerEntity = Customer.hydrate(req.body); + //validate user + await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + + //call service to get prisma entity + const customerEntityCreated = await this.customersService.create(customerEntity); + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific customer by uid + */ + @Put("/api/v1/admin/customers/:uid", [authHandler, ruleHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const userFound = await this.customersService.getByUid(uid); + + if (!userFound) { + this.httpNotFoundRequest(response, "user not found"); + return; + } + + //init IUser resource with request body values + const customerEntity = Customer.hydrate(req.body); + + //validate user + await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false }); + + //call service to get prisma entity + const customerEntityUpdated = await this.customersService.update(uid, customerEntity); + + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific customer by uid + */ + @Get("/api/v1/admin/customers/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let customerEntity: Customers | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + customerEntity = await this.customersService.getByUid(uid, query); + } else { + //call service to get prisma entity + customerEntity = await this.customersService.getByUid(uid); + } + + if (!customerEntity) { + this.httpNotFoundRequest(response, "customer not found"); + return; + } + + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/DeedTypesController.ts b/src/app/api/admin/DeedTypesController.ts new file mode 100644 index 00000000..247dca8f --- /dev/null +++ b/src/app/api/admin/DeedTypesController.ts @@ -0,0 +1,156 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DeedTypesService from "@Services/admin/DeedTypesService/DeedTypesService"; +import { DeedTypes, Prisma } from "@prisma/client"; +import { DeedType } from "le-coffre-resources/dist/Admin"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import deedTypeHandler from "@App/middlewares/OfficeMembershipHandlers/DeedTypeHandler"; + +@Controller() +@Service() +export default class DeedTypesController extends ApiController { + constructor(private deedTypesService: DeedTypesService) { + super(); + } + + /** + * @description Get all deedtypes + * @returns Deedtype[] list of deedtypes + */ + @Get("/api/v1/admin/deed-types", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DeedTypesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const deedTypeEntities: DeedTypes[] = await this.deedTypesService.get(query); + + //Hydrate ressource with prisma entity + const DeedTypes = DeedType.hydrateArray(deedTypeEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, DeedTypes); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new deedtype + * @returns Deedtype created + */ + @Post("/api/v1/admin/deed-types", [authHandler, ruleHandler, deedTypeHandler]) + protected async post(req: Request, response: Response) { + try { + //init DeedType resource with request body values + const deedTypeEntity = DeedType.hydrate(req.body); + + //validate deed type + await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); + + //call service to get prisma entity + const deedTypeEntityCreated = await this.deedTypesService.create(deedTypeEntity); + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific deedtype by uid + * @returns Deedtype modified + */ + @Put("/api/v1/admin/deed-types/:uid", [authHandler, ruleHandler, deedTypeHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const deedTypeFound = await this.deedTypesService.getByUid(uid); + + if (!deedTypeFound) { + this.httpNotFoundRequest(response, "deed type not found"); + return; + } + + //init DeedType resource with request body values + const deedTypeEntity = DeedType.hydrate(req.body); + + //validate deed type + await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] }); + + //call service to get prisma entity + const deedTypeEntityUpdated = await this.deedTypesService.update(uid, deedTypeEntity); + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific deedtype by uid + * @returns IDeedtype + */ + @Get("/api/v1/admin/deed-types/:uid", [authHandler, ruleHandler, deedTypeHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let deedTypeEntity: DeedTypes | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + deedTypeEntity = await this.deedTypesService.getByUid(uid, query); + } else { + //call service to get prisma entity + deedTypeEntity = await this.deedTypesService.getByUid(uid); + } + + if (!deedTypeEntity) { + this.httpNotFoundRequest(response, "deed type not found"); + return; + } + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/DeedsController.ts b/src/app/api/admin/DeedsController.ts new file mode 100644 index 00000000..c811f309 --- /dev/null +++ b/src/app/api/admin/DeedsController.ts @@ -0,0 +1,126 @@ +import { Response, Request } from "express"; +import { Controller, Get, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import DeedsService from "@Services/admin/DeedsService/DeedsService"; +import { Service } from "typedi"; +import { Deeds, Prisma } from "@prisma/client"; +import { Deed } from "le-coffre-resources/dist/Admin"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import deedHandler from "@App/middlewares/OfficeMembershipHandlers/DeedHandler"; + +@Controller() +@Service() +export default class DeedsController extends ApiController { + constructor(private deedsService: DeedsService) { + super(); + } + + /** + * @description Get all deeds + * @returns Deed[] list of deeds + */ + @Get("/api/v1/admin/deeds", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DeedsWhereInput = { deed_type: { office: { uid: officeId } } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const deedEntities: Deeds[] = await this.deedsService.get(query); + + //Hydrate ressource with prisma entity + const deeds = Deed.hydrateArray(deedEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deeds); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific deed by uid + * @returns Deed + */ + @Get("/api/v1/admin/deeds/:uid", [authHandler, ruleHandler, deedHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let deedEntity: Deeds | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + deedEntity = await this.deedsService.getByUid(uid, query); + } else { + //call service to get prisma entity + deedEntity = await this.deedsService.getByUid(uid); + } + + if (!deedEntity) { + this.httpNotFoundRequest(response, "deed not found"); + return; + } + + //Hydrate ressource with prisma entity + const deed = Deed.hydrate(deedEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deed); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific deed by uid + */ + @Put("/api/v1/admin/deeds/:uid", [authHandler, ruleHandler, deedHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const deedFound = await this.deedsService.getByUid(uid); + + if (!deedFound) { + this.httpNotFoundRequest(response, "deed not found"); + return; + } + + //init OfficeFolder resource with request body values + const deedEntity = Deed.hydrate(req.body); + + //validate folder + await validateOrReject(deedEntity, { groups: ["updateDeed"], forbidUnknownValues: false }); + + //call service to get prisma entity + const deedEntityUpdated = await this.deedsService.update(uid, deedEntity); + + //Hydrate ressource with prisma entity + const deed = Deed.hydrate(deedEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, deed); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/DocumentTypesController.ts b/src/app/api/admin/DocumentTypesController.ts new file mode 100644 index 00000000..1bb2e009 --- /dev/null +++ b/src/app/api/admin/DocumentTypesController.ts @@ -0,0 +1,145 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DocumentTypesService from "@Services/admin/DocumentTypesService/DocumentTypesService"; +import { DocumentTypes, Prisma } from "@prisma/client"; +import ObjectHydrate from "@Common/helpers/ObjectHydrate"; +import { DocumentType } from "le-coffre-resources/dist/Admin"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import documentTypeHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentTypeHandler"; + +@Controller() +@Service() +export default class DocumentTypesController extends ApiController { + constructor(private documentTypesService: DocumentTypesService) { + super(); + } + + /** + * @description Get all document-types + */ + @Get("/api/v1/admin/document-types", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DocumentTypesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const documentTypeEntities: DocumentTypes[] = await this.documentTypesService.get(query); + + //Hydrate ressource with prisma entity + const documentTypes = DocumentType.hydrateArray(documentTypeEntities, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, documentTypes); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new documentType + */ + @Post("/api/v1/admin/document-types", [authHandler, ruleHandler, documentTypeHandler]) + protected async post(req: Request, response: Response) { + try { + //init DocumentType resource with request body values + const documentTypeEntity = DocumentType.hydrate(req.body); + //validate user + await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false }); + //call service to get prisma entity + const documentTypeEntityCreated = await this.documentTypesService.create(documentTypeEntity); + //Hydrate ressource with prisma entity + const userEntityCreated = DocumentType.hydrate(documentTypeEntityCreated, { + strategy: "excludeAll", + }); + //success + this.httpCreated(response, userEntityCreated); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific documentType by uid + */ + @Put("/api/v1/admin/document-types/:uid", [authHandler, ruleHandler, documentTypeHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentTypeFound = await this.documentTypesService.getByUid(uid); + + if (!documentTypeFound) { + this.httpNotFoundRequest(response, "document type not found"); + return; + } + //init DocumentType resource with request body values + const documentTypeEntity = DocumentType.hydrate(req.body); + + //validate user + await validateOrReject(documentTypeEntity, { groups: ["updateDocumentType"] }); + + //call service to get prisma entity + const documentTypeEntityUpdated = await this.documentTypesService.update(uid, documentTypeEntity); + + //Hydrate ressource with prisma entity + const documentType = DocumentType.hydrate(documentTypeEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, documentType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific documentType by uid + */ + @Get("/api/v1/admin/document-types/:uid", [authHandler, ruleHandler, documentTypeHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let documentTypeEntity: DocumentTypes | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + documentTypeEntity = await this.documentTypesService.getByUid(uid, query); + } else { + //call service to get prisma entity + documentTypeEntity = await this.documentTypesService.getByUid(uid); + } + + //Hydrate ressource with prisma entity + const user = ObjectHydrate.hydrate(new DocumentType(), documentTypeEntity!, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, user); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/DocumentsController.ts b/src/app/api/admin/DocumentsController.ts new file mode 100644 index 00000000..854d6ba5 --- /dev/null +++ b/src/app/api/admin/DocumentsController.ts @@ -0,0 +1,185 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DocumentsService from "@Services/admin/DocumentsService/DocumentsService"; +import { Documents, Prisma } from "@prisma/client"; +import { Document } from "le-coffre-resources/dist/Admin"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import documentHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentHandler"; + +@Controller() +@Service() +export default class DocumentsController extends ApiController { + constructor(private documentsService: DocumentsService) { + super(); + } + + /** + * @description Get all documents + * @returns IDocument[] list of documents + */ + @Get("/api/v1/admin/documents", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DocumentsWhereInput = { document_type: { office: { uid: officeId } } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const documentEntities = await this.documentsService.get(query); + + //Hydrate ressource with prisma entity + const documents = Document.hydrateArray(documentEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, documents); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new document + * @returns IDocument created + */ + @Post("/api/v1/admin/documents", [authHandler, ruleHandler, documentHandler]) + protected async post(req: Request, response: Response) { + try { + //init Document resource with request body values + const documentEntity = Document.hydrate(req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); + + //call service to get prisma entity + const documentEntityCreated = await this.documentsService.create(documentEntity); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/admin/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async update(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //init Document resource with request body values + const documentEntity = Document.hydrate(req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["updateDocument"] }); + + //call service to get prisma entity + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Delete a specific document + */ + @Delete("/api/v1/admin/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //call service to get prisma entity + const documentEntity: Documents = await this.documentsService.delete(uid); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific document by uid + */ + @Get("/api/v1/admin/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let documentEntity: Documents | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + documentEntity = await this.documentsService.getByUid(uid, query); + } else { + //call service to get prisma entity + documentEntity = await this.documentsService.getByUid(uid); + } + + if (!documentEntity) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/FilesController.ts b/src/app/api/admin/FilesController.ts new file mode 100644 index 00000000..b6fe9050 --- /dev/null +++ b/src/app/api/admin/FilesController.ts @@ -0,0 +1,140 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import FilesService from "@Services/common/FilesService/FilesService"; +import { Prisma } from "@prisma/client"; +import { File } from "le-coffre-resources/dist/Admin"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import fileHandler from "@App/middlewares/OfficeMembershipHandlers/FileHandler"; + +@Controller() +@Service() +export default class FilesController extends ApiController { + constructor(private filesService: FilesService) { + super(); + } + + /** + * @description Get all Files + * @returns File[] list of Files + */ + @Get("/api/v1/admin/files", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.FilesWhereInput = { document: { folder: { office: { uid: officeId } } } }; + query.where = officeWhereInput; + //call service to get prisma entity + const fileEntities = await this.filesService.get(query); + + //Hydrate ressource with prisma entity + const files = File.hydrateArray(fileEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, files); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/admin/files/download/:uid", [authHandler, ruleHandler, fileHandler]) + protected async download(req: Request, response: Response) { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "uid not found"); + return; + } + try { + const fileInfo = await this.filesService.download(uid); + + if (!fileInfo) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + response.setHeader("Content-Type", fileInfo.file.mimetype); + response.setHeader("Content-Disposition", `inline; filename=${encodeURIComponent(fileInfo.file.file_name)}`); + + this.httpSuccess(response, fileInfo.buffer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Delete a specific File + */ + @Delete("/api/v1/admin/files/:uid", [authHandler, ruleHandler, fileHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileFound = await this.filesService.getByUid(uid); + + if (!fileFound) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //call service to get prisma entity + const fileEntity = await this.filesService.deleteKeyAndArchive(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/admin/files/:uid", [authHandler, ruleHandler, fileHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileEntity = await this.filesService.getByUid(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/OfficeFoldersController.ts b/src/app/api/admin/OfficeFoldersController.ts new file mode 100644 index 00000000..95c79f2e --- /dev/null +++ b/src/app/api/admin/OfficeFoldersController.ts @@ -0,0 +1,183 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficeFoldersService from "@Services/admin/OfficeFoldersService/OfficeFoldersService"; +import { Service } from "typedi"; +import { OfficeFolders, Prisma } from "@prisma/client"; +import { OfficeFolder } from "le-coffre-resources/dist/Admin"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; + +@Controller() +@Service() +export default class OfficeFoldersController extends ApiController { + constructor(private officeFoldersService: OfficeFoldersService) { + super(); + } + + /** + * @description Get all folders + */ + @Get("/api/v1/admin/folders", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.OfficeFoldersWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + //call service to get prisma entity + const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrateArray(officeFolderEntities, { + strategy: "excludeAll", + }); + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new folder + */ + @Post("/api/v1/admin/folders", [authHandler, ruleHandler, folderHandler]) + protected async post(req: Request, response: Response) { + try { + //init OfficeFolder resource with request body values + const officeFolderRessource = OfficeFolder.hydrate(req.body); + await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); + + //call service to get prisma entity + const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntity, { + strategy: "excludeAll", + }); + //success + this.httpCreated(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific folder by uid + */ + @Put("/api/v1/admin/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //init OfficeFolder resource with request body values + const officeFolderEntity = OfficeFolder.hydrate(req.body); + + //validate folder + await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); + + //call service to get prisma entity + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific folder by uid + * @returns IFolder + */ + @Get("/api/v1/admin/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let officeFolderEntity: OfficeFolders | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeFolderEntity = await this.officeFoldersService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeFolderEntity = await this.officeFoldersService.getByUid(uid); + } + + if (!officeFolderEntity) { + this.httpNotFoundRequest(response, "folder not found"); + return; + } + + //Hydrate ressource with prisma entity + const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeFolder); + } catch (error) { + this.httpInternalError(response, error); + return; + } + this.httpSuccess(response, await this.officeFoldersService.getByUid("uid")); + } + + /** + * @description Delete a specific folder + */ + @Delete("/api/v1/admin/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //call service to get prisma entity + const officeFoldertEntity: OfficeFolders = await this.officeFoldersService.delete(uid); + + //Hydrate ressource with prisma entity + const officeFolder = OfficeFolder.hydrate(officeFoldertEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeFolder); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/OfficeRolesController.ts b/src/app/api/admin/OfficeRolesController.ts new file mode 100644 index 00000000..afe5e15d --- /dev/null +++ b/src/app/api/admin/OfficeRolesController.ts @@ -0,0 +1,151 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficeRolesService from "@Services/admin/OfficeRolesService/OfficeRolesService"; +import { Service } from "typedi"; +import { validateOrReject } from "class-validator"; +import { OfficeRole } from "le-coffre-resources/dist/Admin"; +import { OfficeRoles, Prisma } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import officeRoleHandler from "@App/middlewares/OfficeMembershipHandlers/OfficeRoleHandler"; + +@Controller() +@Service() +export default class OfficeRolesController extends ApiController { + constructor(private officeRolesService: OfficeRolesService) { + super(); + } + + /** + * @description Get all officeRoles + */ + @Get("/api/v1/admin/officeRoles", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.OfficeRolesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const officeRolesEntities = await this.officeRolesService.get(query); + + //Hydrate ressource with prisma entity + const officeRoles = OfficeRole.hydrateArray(officeRolesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeRoles); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new officeRole + */ + @Post("/api/v1/admin/office-roles", [authHandler, ruleHandler, officeRoleHandler]) + protected async getAddresses(req: Request, response: Response) { + try { + //init IOfficeRole resource with request body values + const officeRoleEntity = OfficeRole.hydrate(req.body); + + //validate officeRole + await validateOrReject(officeRoleEntity, { groups: ["createOfficeRole"] }); + + //call service to get prisma entity + const officeRoleEntityCreated = await this.officeRolesService.create(officeRoleEntity); + + //Hydrate ressource with prisma entity + const officeRole = OfficeRole.hydrate(officeRoleEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, officeRole); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific officeRole by uid + */ + @Put("/api/v1/admin/office-roles/:uid", [authHandler, ruleHandler, officeRoleHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeRoleFound = await this.officeRolesService.getByUid(uid); + + if (!officeRoleFound) { + this.httpNotFoundRequest(response, "officeRole not found"); + return; + } + + //init IOfficeRole resource with request body values + const officeRoleEntity = OfficeRole.hydrate(req.body); + + //validate officeRole + await validateOrReject(officeRoleEntity, { groups: ["updateOfficeRole"] }); + + //call service to get prisma entity + const officeRoleEntityUpdated = await this.officeRolesService.update(officeRoleEntity); + + //Hydrate ressource with prisma entity + const officeRole = OfficeRole.hydrate(officeRoleEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, officeRole); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific officeRole by uid + */ + @Get("/api/v1/admin/office-roles/:uid", [authHandler, ruleHandler, officeRoleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let officeRoleEntity: OfficeRoles | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeRoleEntity = await this.officeRolesService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeRoleEntity = await this.officeRolesService.getByUid(uid); + } + + if (!officeRoleEntity) { + this.httpNotFoundRequest(response, "officeRole not found"); + return; + } + + //Hydrate ressource with prisma entity + const officeRole = OfficeRole.hydrate(officeRoleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeRole); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/OfficesController.ts b/src/app/api/admin/OfficesController.ts new file mode 100644 index 00000000..76c98668 --- /dev/null +++ b/src/app/api/admin/OfficesController.ts @@ -0,0 +1,72 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficesService from "@Services/admin/OfficesService/OfficesService"; +import { Service } from "typedi"; +import { Offices } from "@prisma/client"; +import { Office as OfficeResource } from "le-coffre-resources/dist/Admin"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import authHandler from "@App/middlewares/AuthHandler"; + +@Controller() +@Service() +export default class OfficesController extends ApiController { + constructor(private officesService: OfficesService) { + super(); + } + /** + * @description Get all offices + */ + @Get("/api/v1/admin/offices", [authHandler, ruleHandler]) + 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 officesEntities: Offices[] = await this.officesService.get(query); + //Hydrate ressource with prisma entity + const offices = OfficeResource.hydrateArray(officesEntities, { strategy: "excludeAll" }); + //success + this.httpSuccess(response, offices); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific office by uid + */ + @Get("/api/v1/admin/offices/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let officeEntity: Offices | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeEntity = await this.officesService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeEntity = await this.officesService.getByUid(uid); + } + + if (!officeEntity) { + this.httpNotFoundRequest(response, "office not found"); + return; + } + + //Hydrate ressource with prisma entity + const office = OfficeResource.hydrate(officeEntity, { strategy: "excludeAll" }); + //success + this.httpSuccess(response, office); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/RolesController.ts b/src/app/api/admin/RolesController.ts new file mode 100644 index 00000000..b587ce3d --- /dev/null +++ b/src/app/api/admin/RolesController.ts @@ -0,0 +1,77 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RolesService from "@Services/admin/RolesService/RolesService"; +import { Service } from "typedi"; +import { Role } from "le-coffre-resources/dist/Admin"; +import { Roles } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class RolesController extends ApiController { + constructor(private rolesService: RolesService) { + super(); + } + + /** + * @description Get all roles + */ + @Get("/api/v1/admin/roles", [authHandler, ruleHandler]) + 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 rolesEntities = await this.rolesService.get(query); + + //Hydrate ressource with prisma entity + const roles = Role.hydrateArray(rolesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, roles); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific role by uid + */ + @Get("/api/v1/admin/roles/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let roleEntity: Roles | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + roleEntity = await this.rolesService.getByUid(uid, query); + } else { + //call service to get prisma entity + roleEntity = await this.rolesService.getByUid(uid); + } + + if (!roleEntity) { + this.httpNotFoundRequest(response, "role not found"); + return; + } + + //Hydrate ressource with prisma entity + const role = Role.hydrate(roleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, role); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/RulesController.ts b/src/app/api/admin/RulesController.ts new file mode 100644 index 00000000..49641e09 --- /dev/null +++ b/src/app/api/admin/RulesController.ts @@ -0,0 +1,77 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RulesService from "@Services/admin/RulesService/RulesService"; +import { Service } from "typedi"; +import { Rule } from "le-coffre-resources/dist/Admin"; +import { Rules } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class RulesController extends ApiController { + constructor(private rulesService: RulesService) { + super(); + } + + /** + * @description Get all rules + */ + @Get("/api/v1/admin/rules", [authHandler, ruleHandler]) + 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 rulesEntities = await this.rulesService.get(query); + + //Hydrate ressource with prisma entity + const rules = Rule.hydrateArray(rulesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rules); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific rule by uid + */ + @Get("/api/v1/admin/rules/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let ruleEntity: Rules | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + ruleEntity = await this.rulesService.getByUid(uid, query); + } else { + //call service to get prisma entity + ruleEntity = await this.rulesService.getByUid(uid); + } + + if (!ruleEntity) { + this.httpNotFoundRequest(response, "rule not found"); + return; + } + + //Hydrate ressource with prisma entity + const rule = Rule.hydrate(ruleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rule); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/admin/UsersController.ts b/src/app/api/admin/UsersController.ts new file mode 100644 index 00000000..941dc67f --- /dev/null +++ b/src/app/api/admin/UsersController.ts @@ -0,0 +1,81 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import UsersService from "@Services/admin/UsersService/UsersService"; +import { Service } from "typedi"; +import User from "le-coffre-resources/dist/Admin"; +import { Prisma, Users } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import userHandler from "@App/middlewares/OfficeMembershipHandlers/UserHandler"; + +@Controller() +@Service() +export default class UsersController extends ApiController { + constructor(private usersService: UsersService) { + super(); + } + + /** + * @description Get all users + */ + @Get("/api/v1/admin/users", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.UsersWhereInput = { office_membership: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const usersEntities = await this.usersService.get(query); + + //Hydrate ressource with prisma entity + const users = User.hydrateArray(usersEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, users); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific user by uid + */ + @Get("/api/v1/admin/users/:uid", [authHandler, ruleHandler, userHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let userEntity: Users | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + userEntity = await this.usersService.getByUid(uid, query); + } else { + //call service to get prisma entity + userEntity = await this.usersService.getByUid(uid); + } + + if (!userEntity) { + this.httpNotFoundRequest(response, "user not found"); + return; + } + + //Hydrate ressource with prisma entity + const user = User.hydrate(userEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, user); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 8be29e46..eca88301 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -1,11 +1,12 @@ import { Response, Request } from "express"; -import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import { Controller, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; -import { Documents } from "@prisma/client"; +import { Documents, Prisma } from "@prisma/client"; import { Document } from "le-coffre-resources/dist/Customer"; -import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import documentHandler from "@App/middlewares/CustomerHandler/DocumentHandler"; @Controller() @Service() @@ -18,11 +19,14 @@ export default class DocumentsController extends ApiController { * @description Get all documents * @returns IDocument[] list of documents */ - @Get("/api/v1/customer/documents") + @Get("/api/v1/customer/documents", [authHandler]) protected async get(req: Request, response: Response) { try { //get query const query = JSON.parse(req.query["q"] as string); + const customerId: string = req.body.user.customerId; + const customerWhereInput: Prisma.DocumentsWhereInput ={ depositor: { uid: customerId } }; + query.where = customerWhereInput; //call service to get prisma entity const documentEntities: Documents[] = await this.documentsService.get(query); @@ -38,112 +42,10 @@ export default class DocumentsController extends ApiController { } } - /** - * @description Create a new document - * @returns IDocument created - */ - @Post("/api/v1/customer/documents") - protected async post(req: Request, response: Response) { - try { - //init Document resource with request body values - const documentEntity = Document.hydrate(req.body); - - //validate document - await validateOrReject(documentEntity, { groups: ["createDocument"] }); - - //call service to get prisma entity - const documentEntityCreated = await this.documentsService.create(documentEntity); - - //Hydrate ressource with prisma entity - const document = Document.hydrate(documentEntityCreated, { - strategy: "excludeAll", - }); - - //success - this.httpSuccess(response, document); - } catch (error) { - this.httpInternalError(response); - return; - } - } - - /** - * @description Update a specific document - */ - @Put("/api/v1/customer/documents/:uid") - protected async update(req: Request, response: Response) { - try { - const uid = req.params["uid"]; - if (!uid) { - this.httpBadRequest(response, "No uid provided"); - return; - } - - const documentFound = await this.documentsService.getByUid(uid); - - if (!documentFound) { - this.httpNotFoundRequest(response, "document not found"); - return; - } - - //init Document resource with request body values - const documentEntity = new Document(); - Document.hydrate(documentEntity, req.body); - - //validate document - await validateOrReject(documentEntity, { groups: ["createDocument"] }); - - //call service to get prisma entity - const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); - - //Hydrate ressource with prisma entity - const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); - - //success - this.httpSuccess(response, document); - } catch (error) { - this.httpInternalError(response); - return; - } - } - - /** - * @description Delete a specific document - */ - @Delete("/api/v1/customer/documents/:uid") - protected async delete(req: Request, response: Response) { - try { - const uid = req.params["uid"]; - if (!uid) { - this.httpBadRequest(response, "No uid provided"); - return; - } - - const documentEntity = await this.documentsService.getByUid(uid); - - if (!documentEntity) { - this.httpNotFoundRequest(response, "document not found"); - return; - } - - //call service to get prisma entity - const documentEntityDeleted: Documents = await this.documentsService.delete(uid); - - //Hydrate ressource with prisma entity - const document = Document.hydrate(documentEntityDeleted, { strategy: "excludeAll" }); - - //success - this.httpSuccess(response, document); - } catch (error) { - this.httpInternalError(response); - return; - } - } - /** * @description Get a specific document by uid */ - @Get("/api/v1/customer/documents/:uid") + @Get("/api/v1/customer/documents/:uid",[authHandler,documentHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/api/customer/FilesController.ts b/src/app/api/customer/FilesController.ts new file mode 100644 index 00000000..ca3ee229 --- /dev/null +++ b/src/app/api/customer/FilesController.ts @@ -0,0 +1,219 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import FilesService from "@Services/common/FilesService/FilesService"; +import { Files, Prisma } from "@prisma/client"; +import { File } from "le-coffre-resources/dist/Customer"; +import { Document } from "le-coffre-resources/dist/Customer"; +import { validateOrReject } from "class-validator"; +import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; +import authHandler from "@App/middlewares/AuthHandler"; +import fileHandler from "@App/middlewares/CustomerHandler/FileHandler"; + +@Controller() +@Service() +export default class FilesController extends ApiController { + constructor(private filesService: FilesService, private documentService: DocumentsService) { + super(); + } + + /** + * @description Get all Files + * @returns File[] list of Files + */ + @Get("/api/v1/customer/files", [authHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const customerId: string = req.body.user.customerId; + const customerWhereInput: Prisma.FilesWhereInput = { document: { depositor: { uid: customerId } } }; + query.where = customerWhereInput; + //call service to get prisma entity + const fileEntities = await this.filesService.get(query); + + //Hydrate ressource with prisma entity + const files = File.hydrateArray(fileEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, files); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/customer/files/download/:uid", [authHandler, fileHandler]) + protected async download(req: Request, response: Response) { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "uid not found"); + return; + } + try { + const fileInfo = await this.filesService.download(uid); + + if (!fileInfo) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + response.setHeader("Content-Type", fileInfo.file.mimetype); + response.setHeader("Content-Disposition", `inline; filename=${encodeURIComponent(fileInfo.file.file_name)}`); + + this.httpSuccess(response, fileInfo.buffer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new File + * @returns File created + */ + @Post("/api/v1/customer/files", [authHandler, fileHandler]) + protected async post(req: Request, response: Response) { + try { + //get file + if (!req.file) throw new Error("No file provided"); + + //init File resource with request body values + const fileEntity = File.hydrate(JSON.parse(req.body["q"])); + + //validate File + await validateOrReject(fileEntity, { groups: ["createFile"] }); + + //call service to get prisma entity + const fileEntityCreated = await this.filesService.create(fileEntity, req.file); + + const document = await this.documentService.getByUid(fileEntity.document!.uid!); + + const documentToUpdate = Document.hydrate(document!); + + documentToUpdate!.document_status = "DEPOSITED"; + await this.documentService.update(document!.uid!, documentToUpdate); + + //Hydrate ressource with prisma entity + const fileEntityHydrated = File.hydrate(fileEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, fileEntityHydrated); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Update a specific file + */ + @Put("/api/v1/customer/files/:uid", [authHandler, fileHandler]) + protected async update(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + throw new Error("No uid provided"); + } + + const fileFound = await this.filesService.getByUid(uid); + + if (!fileFound) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //init File resource with request body values + const fileEntity = File.hydrate(req.body); + + //validate file + await validateOrReject(fileEntity, { groups: ["updateFile"] }); + + //call service to get prisma entity + const fileEntityUpdated: Files = await this.filesService.update(uid, fileEntity); + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Delete a specific File + */ + @Delete("/api/v1/customer/files/:uid", [authHandler, fileHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileFound = await this.filesService.getByUid(uid); + + if (!fileFound) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //call service to get prisma entity + const fileEntity = await this.filesService.deleteKeyAndArchive(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/customer/files/:uid", [authHandler, fileHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileEntity = await this.filesService.getByUid(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/franceConnect/CustomerController.ts b/src/app/api/franceConnect/CustomerController.ts new file mode 100644 index 00000000..54e19b12 --- /dev/null +++ b/src/app/api/franceConnect/CustomerController.ts @@ -0,0 +1,64 @@ +import { Response, Request } from "express"; +import { Controller, Post } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import AuthService from "@Services/common/AuthService/AuthService"; +import { JwtPayload } from "jsonwebtoken"; + +@Controller() +@Service() +export default class CustomerController extends ApiController { + constructor(private authService: AuthService) { + super(); + } + + @Post("/api/v1/france-connect/customer/login/:email") + protected async login(req: Request, response: Response) { + try { + const email = req.params["email"]; + if (!email) throw new Error("email is required"); + + const payload = await this.authService.getCustomerJwtPayload(email); + const accessToken = this.authService.generateAccessToken(payload); + const refreshToken = this.authService.generateRefreshToken(payload); + + //success + this.httpSuccess(response, { accessToken, refreshToken }); + } catch (error) { + this.httpInternalError(response); + return; + } + } + + @Post("/api/v1/france-connect/customer/refresh-token") + protected async refreshToken(req: Request, response: Response) { + try { + const authHeader = req.headers["authorization"]; + const token = authHeader && authHeader.split(" ")[1]; + + if (!token) { + this.httpBadRequest(response); + return; + } + + let accessToken; + this.authService.verifyRefreshToken(token, (err, customerPayload) => { + if (err) { + this.httpUnauthorized(response); + return; + } + + const customer = customerPayload as JwtPayload; + delete customer.iat; + delete customer!.exp; + accessToken = this.authService.generateAccessToken(customer); + }); + + //success + this.httpSuccess(response, accessToken); + } catch (error) { + this.httpInternalError(response); + return; + } + } +} diff --git a/src/app/api/notary/CustomersController.ts b/src/app/api/notary/CustomersController.ts new file mode 100644 index 00000000..f1372d8b --- /dev/null +++ b/src/app/api/notary/CustomersController.ts @@ -0,0 +1,146 @@ +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/notary/CustomersService/CustomersService"; +import { Service } from "typedi"; +import { Customer } from "le-coffre-resources/dist/Notary"; +import { Customers } from "@prisma/client"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class CustomersController extends ApiController { + constructor(private customersService: CustomersService) { + super(); + } + + /** + * @description Get all customers + */ + @Get("/api/v1/notary/customers", [authHandler, ruleHandler]) + 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 customersEntities = await this.customersService.get(query); + + //Hydrate ressource with prisma entity + const customers = Customer.hydrateArray(customersEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customers); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new customer + */ + @Post("/api/v1/notary/customers", [authHandler, ruleHandler]) + protected async post(req: Request, response: Response) { + try { + //init IUser resource with request body values + const customerEntity = Customer.hydrate(req.body); + //validate user + await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + + //call service to get prisma entity + const customerEntityCreated = await this.customersService.create(customerEntity); + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific customer by uid + */ + @Put("/api/v1/notary/customers/:uid", [authHandler, ruleHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const userFound = await this.customersService.getByUid(uid); + + if (!userFound) { + this.httpNotFoundRequest(response, "user not found"); + return; + } + + //init IUser resource with request body values + const customerEntity = Customer.hydrate(req.body); + + //validate user + await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false }); + + //call service to get prisma entity + const customerEntityUpdated = await this.customersService.update(uid, customerEntity); + + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntityUpdated, { + 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/customers/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let customerEntity: Customers | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + customerEntity = await this.customersService.getByUid(uid, query); + } else { + //call service to get prisma entity + customerEntity = await this.customersService.getByUid(uid); + } + + if (!customerEntity) { + this.httpNotFoundRequest(response, "customer not found"); + return; + } + + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/DeedTypesController.ts b/src/app/api/notary/DeedTypesController.ts new file mode 100644 index 00000000..0ea44863 --- /dev/null +++ b/src/app/api/notary/DeedTypesController.ts @@ -0,0 +1,156 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DeedTypesService from "@Services/notary/DeedTypesService/DeedTypesService"; +import { DeedTypes, Prisma } from "@prisma/client"; +import { DeedType } from "le-coffre-resources/dist/Notary"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import deedTypeHandler from "@App/middlewares/OfficeMembershipHandlers/DeedTypeHandler"; + +@Controller() +@Service() +export default class DeedTypesController extends ApiController { + constructor(private deedTypesService: DeedTypesService) { + super(); + } + + /** + * @description Get all deedtypes + * @returns Deedtype[] list of deedtypes + */ + @Get("/api/v1/notary/deed-types", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DeedTypesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const deedTypeEntities: DeedTypes[] = await this.deedTypesService.get(query); + + //Hydrate ressource with prisma entity + const DeedTypes = DeedType.hydrateArray(deedTypeEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, DeedTypes); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new deedtype + * @returns Deedtype created + */ + @Post("/api/v1/notary/deed-types", [authHandler, ruleHandler, deedTypeHandler]) + protected async post(req: Request, response: Response) { + try { + //init DeedType resource with request body values + const deedTypeEntity = DeedType.hydrate(req.body); + + //validate deed type + await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); + + //call service to get prisma entity + const deedTypeEntityCreated = await this.deedTypesService.create(deedTypeEntity); + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific deedtype by uid + * @returns Deedtype modified + */ + @Put("/api/v1/notary/deed-types/:uid", [authHandler, ruleHandler, deedTypeHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const deedTypeFound = await this.deedTypesService.getByUid(uid); + + if (!deedTypeFound) { + this.httpNotFoundRequest(response, "deed type not found"); + return; + } + + //init DeedType resource with request body values + const deedTypeEntity = DeedType.hydrate(req.body); + + //validate deed type + await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] }); + + //call service to get prisma entity + const deedTypeEntityUpdated = await this.deedTypesService.update(uid, deedTypeEntity); + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific deedtype by uid + * @returns IDeedtype + */ + @Get("/api/v1/notary/deed-types/:uid", [authHandler, ruleHandler, deedTypeHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let deedTypeEntity: DeedTypes | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + deedTypeEntity = await this.deedTypesService.getByUid(uid, query); + } else { + //call service to get prisma entity + deedTypeEntity = await this.deedTypesService.getByUid(uid); + } + + if (!deedTypeEntity) { + this.httpNotFoundRequest(response, "deed type not found"); + return; + } + + //Hydrate ressource with prisma entity + const deedType = DeedType.hydrate(deedTypeEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deedType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/DeedsController.ts b/src/app/api/notary/DeedsController.ts new file mode 100644 index 00000000..8ac0d98b --- /dev/null +++ b/src/app/api/notary/DeedsController.ts @@ -0,0 +1,126 @@ +import { Response, Request } from "express"; +import { Controller, Get, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import DeedsService from "@Services/notary/DeedsService/DeedsService"; +import { Service } from "typedi"; +import { Deeds, Prisma } from "@prisma/client"; +import { Deed } from "le-coffre-resources/dist/Notary"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import deedHandler from "@App/middlewares/OfficeMembershipHandlers/DeedHandler"; + +@Controller() +@Service() +export default class DeedsController extends ApiController { + constructor(private deedsService: DeedsService) { + super(); + } + + /** + * @description Get all deeds + * @returns Deed[] list of deeds + */ + @Get("/api/v1/notary/deeds", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DeedsWhereInput = { deed_type: { office: { uid: officeId } } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const deedEntities: Deeds[] = await this.deedsService.get(query); + + //Hydrate ressource with prisma entity + const deeds = Deed.hydrateArray(deedEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deeds); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific deed by uid + * @returns Deed + */ + @Get("/api/v1/notary/deeds/:uid", [authHandler, ruleHandler, deedHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let deedEntity: Deeds | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + deedEntity = await this.deedsService.getByUid(uid, query); + } else { + //call service to get prisma entity + deedEntity = await this.deedsService.getByUid(uid); + } + + if (!deedEntity) { + this.httpNotFoundRequest(response, "deed not found"); + return; + } + + //Hydrate ressource with prisma entity + const deed = Deed.hydrate(deedEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, deed); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific deed by uid + */ + @Put("/api/v1/notary/deeds/:uid", [authHandler, ruleHandler, deedHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const deedFound = await this.deedsService.getByUid(uid); + + if (!deedFound) { + this.httpNotFoundRequest(response, "deed not found"); + return; + } + + //init OfficeFolder resource with request body values + const deedEntity = Deed.hydrate(req.body); + + //validate folder + await validateOrReject(deedEntity, { groups: ["updateDeed"], forbidUnknownValues: false }); + + //call service to get prisma entity + const deedEntityUpdated = await this.deedsService.update(uid, deedEntity); + + //Hydrate ressource with prisma entity + const deed = Deed.hydrate(deedEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, deed); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/DocumentTypesController.ts b/src/app/api/notary/DocumentTypesController.ts new file mode 100644 index 00000000..4cc25b5a --- /dev/null +++ b/src/app/api/notary/DocumentTypesController.ts @@ -0,0 +1,145 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DocumentTypesService from "@Services/notary/DocumentTypesService/DocumentTypesService"; +import { DocumentTypes, Prisma } from "@prisma/client"; +import ObjectHydrate from "@Common/helpers/ObjectHydrate"; +import { DocumentType } from "le-coffre-resources/dist/Notary"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import documentTypeHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentTypeHandler"; + +@Controller() +@Service() +export default class DocumentTypesController extends ApiController { + constructor(private documentTypesService: DocumentTypesService) { + super(); + } + + /** + * @description Get all document-types + */ + @Get("/api/v1/notary/document-types", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DocumentTypesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const documentTypeEntities: DocumentTypes[] = await this.documentTypesService.get(query); + + //Hydrate ressource with prisma entity + const documentTypes = DocumentType.hydrateArray(documentTypeEntities, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, documentTypes); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new documentType + */ + @Post("/api/v1/notary/document-types", [authHandler, ruleHandler, documentTypeHandler]) + protected async post(req: Request, response: Response) { + try { + //init DocumentType resource with request body values + const documentTypeEntity = DocumentType.hydrate(req.body); + //validate user + await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false }); + //call service to get prisma entity + const documentTypeEntityCreated = await this.documentTypesService.create(documentTypeEntity); + //Hydrate ressource with prisma entity + const userEntityCreated = DocumentType.hydrate(documentTypeEntityCreated, { + strategy: "excludeAll", + }); + //success + this.httpCreated(response, userEntityCreated); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific documentType by uid + */ + @Put("/api/v1/notary/document-types/:uid", [authHandler, ruleHandler, documentTypeHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentTypeFound = await this.documentTypesService.getByUid(uid); + + if (!documentTypeFound) { + this.httpNotFoundRequest(response, "document type not found"); + return; + } + //init DocumentType resource with request body values + const documentTypeEntity = DocumentType.hydrate(req.body); + + //validate user + await validateOrReject(documentTypeEntity, { groups: ["updateDocumentType"] }); + + //call service to get prisma entity + const documentTypeEntityUpdated = await this.documentTypesService.update(uid, documentTypeEntity); + + //Hydrate ressource with prisma entity + const documentType = DocumentType.hydrate(documentTypeEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, documentType); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific documentType by uid + */ + @Get("/api/v1/notary/document-types/:uid", [authHandler, ruleHandler, documentTypeHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let documentTypeEntity: DocumentTypes | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + documentTypeEntity = await this.documentTypesService.getByUid(uid, query); + } else { + //call service to get prisma entity + documentTypeEntity = await this.documentTypesService.getByUid(uid); + } + + //Hydrate ressource with prisma entity + const user = ObjectHydrate.hydrate(new DocumentType(), documentTypeEntity!, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, user); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/DocumentsController.ts b/src/app/api/notary/DocumentsController.ts new file mode 100644 index 00000000..b6b35314 --- /dev/null +++ b/src/app/api/notary/DocumentsController.ts @@ -0,0 +1,185 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DocumentsService from "@Services/notary/DocumentsService/DocumentsService"; +import { Documents, Prisma } from "@prisma/client"; +import { Document } from "le-coffre-resources/dist/Notary"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import documentHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentHandler"; + +@Controller() +@Service() +export default class DocumentsController extends ApiController { + constructor(private documentsService: DocumentsService) { + super(); + } + + /** + * @description Get all documents + * @returns IDocument[] list of documents + */ + @Get("/api/v1/notary/documents", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.DocumentsWhereInput = { document_type: { office: { uid: officeId } } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const documentEntities = await this.documentsService.get(query); + + //Hydrate ressource with prisma entity + const documents = Document.hydrateArray(documentEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, documents); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new document + * @returns IDocument created + */ + @Post("/api/v1/notary/documents", [authHandler, ruleHandler, documentHandler]) + protected async post(req: Request, response: Response) { + try { + //init Document resource with request body values + const documentEntity = Document.hydrate(req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); + + //call service to get prisma entity + const documentEntityCreated = await this.documentsService.create(documentEntity); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async update(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //init Document resource with request body values + const documentEntity = Document.hydrate(req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["updateDocument"] }); + + //call service to get prisma entity + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Delete a specific document + */ + @Delete("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //call service to get prisma entity + const documentEntity: Documents = await this.documentsService.delete(uid); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific document by uid + */ + @Get("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, documentHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let documentEntity: Documents | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + documentEntity = await this.documentsService.getByUid(uid, query); + } else { + //call service to get prisma entity + documentEntity = await this.documentsService.getByUid(uid); + } + + if (!documentEntity) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/FilesController.ts b/src/app/api/notary/FilesController.ts new file mode 100644 index 00000000..5e32a319 --- /dev/null +++ b/src/app/api/notary/FilesController.ts @@ -0,0 +1,140 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import FilesService from "@Services/common/FilesService/FilesService"; +import { Prisma } from "@prisma/client"; +import { File } from "le-coffre-resources/dist/Notary"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import fileHandler from "@App/middlewares/OfficeMembershipHandlers/FileHandler"; + +@Controller() +@Service() +export default class FilesController extends ApiController { + constructor(private filesService: FilesService) { + super(); + } + + /** + * @description Get all Files + * @returns File[] list of Files + */ + @Get("/api/v1/notary/files", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.FilesWhereInput = { document: { folder: { office: { uid: officeId } } } }; + query.where = officeWhereInput; + //call service to get prisma entity + const fileEntities = await this.filesService.get(query); + + //Hydrate ressource with prisma entity + const files = File.hydrateArray(fileEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, files); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/notary/files/download/:uid", [authHandler, ruleHandler, fileHandler]) + protected async download(req: Request, response: Response) { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "uid not found"); + return; + } + try { + const fileInfo = await this.filesService.download(uid); + + if (!fileInfo) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + response.setHeader("Content-Type", fileInfo.file.mimetype); + response.setHeader("Content-Disposition", `inline; filename=${encodeURIComponent(fileInfo.file.file_name)}`); + + this.httpSuccess(response, fileInfo.buffer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Delete a specific File + */ + @Delete("/api/v1/notary/files/:uid", [authHandler, ruleHandler, fileHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileFound = await this.filesService.getByUid(uid); + + if (!fileFound) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //call service to get prisma entity + const fileEntity = await this.filesService.deleteKeyAndArchive(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific File by uid + */ + @Get("/api/v1/notary/files/:uid", [authHandler, ruleHandler, fileHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const fileEntity = await this.filesService.getByUid(uid); + + if (!fileEntity) { + this.httpNotFoundRequest(response, "file not found"); + return; + } + + //Hydrate ressource with prisma entity + const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, file); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/OfficeFoldersController.ts b/src/app/api/notary/OfficeFoldersController.ts new file mode 100644 index 00000000..2468dcb8 --- /dev/null +++ b/src/app/api/notary/OfficeFoldersController.ts @@ -0,0 +1,183 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; +import { Service } from "typedi"; +import { OfficeFolders, Prisma } from "@prisma/client"; +import { OfficeFolder } from "le-coffre-resources/dist/Notary"; +import { validateOrReject } from "class-validator"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; + +@Controller() +@Service() +export default class OfficeFoldersController extends ApiController { + constructor(private officeFoldersService: OfficeFoldersService) { + super(); + } + + /** + * @description Get all folders + */ + @Get("/api/v1/notary/folders", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.OfficeFoldersWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + //call service to get prisma entity + const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrateArray(officeFolderEntities, { + strategy: "excludeAll", + }); + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new folder + */ + @Post("/api/v1/notary/folders", [authHandler, ruleHandler, folderHandler]) + protected async post(req: Request, response: Response) { + try { + //init OfficeFolder resource with request body values + const officeFolderRessource = OfficeFolder.hydrate(req.body); + await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); + + //call service to get prisma entity + const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntity, { + strategy: "excludeAll", + }); + //success + this.httpCreated(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific folder by uid + */ + @Put("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //init OfficeFolder resource with request body values + const officeFolderEntity = OfficeFolder.hydrate(req.body); + + //validate folder + await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); + + //call service to get prisma entity + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific folder by uid + * @returns IFolder + */ + @Get("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let officeFolderEntity: OfficeFolders | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeFolderEntity = await this.officeFoldersService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeFolderEntity = await this.officeFoldersService.getByUid(uid); + } + + if (!officeFolderEntity) { + this.httpNotFoundRequest(response, "folder not found"); + return; + } + + //Hydrate ressource with prisma entity + const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeFolder); + } catch (error) { + this.httpInternalError(response, error); + return; + } + this.httpSuccess(response, await this.officeFoldersService.getByUid("uid")); + } + + /** + * @description Delete a specific folder + */ + @Delete("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //call service to get prisma entity + const officeFoldertEntity: OfficeFolders = await this.officeFoldersService.delete(uid); + + //Hydrate ressource with prisma entity + const officeFolder = OfficeFolder.hydrate(officeFoldertEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeFolder); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/OfficeRolesController.ts b/src/app/api/notary/OfficeRolesController.ts new file mode 100644 index 00000000..c351741b --- /dev/null +++ b/src/app/api/notary/OfficeRolesController.ts @@ -0,0 +1,81 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficeRolesService from "@Services/notary/OfficeRolesService/OfficeRolesService"; +import { Service } from "typedi"; +import { OfficeRole } from "le-coffre-resources/dist/Notary"; +import { OfficeRoles, Prisma } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import officeRoleHandler from "@App/middlewares/OfficeMembershipHandlers/OfficeRoleHandler"; + +@Controller() +@Service() +export default class OfficeRolesController extends ApiController { + constructor(private officeRolesService: OfficeRolesService) { + super(); + } + + /** + * @description Get all officeRoles + */ + @Get("/api/v1/notary/officeRoles", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.OfficeRolesWhereInput = { office: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const officeRolesEntities = await this.officeRolesService.get(query); + + //Hydrate ressource with prisma entity + const officeRoles = OfficeRole.hydrateArray(officeRolesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeRoles); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific officeRole by uid + */ + @Get("/api/v1/notary/office-roles/:uid", [authHandler, ruleHandler, officeRoleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let officeRoleEntity: OfficeRoles | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeRoleEntity = await this.officeRolesService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeRoleEntity = await this.officeRolesService.getByUid(uid); + } + + if (!officeRoleEntity) { + this.httpNotFoundRequest(response, "officeRole not found"); + return; + } + + //Hydrate ressource with prisma entity + const officeRole = OfficeRole.hydrate(officeRoleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, officeRole); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/OfficesController.ts b/src/app/api/notary/OfficesController.ts new file mode 100644 index 00000000..eef0066a --- /dev/null +++ b/src/app/api/notary/OfficesController.ts @@ -0,0 +1,72 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import OfficesService from "@Services/notary/OfficesService/OfficesService"; +import { Service } from "typedi"; +import { Offices } from "@prisma/client"; +import { Office as OfficeResource } from "le-coffre-resources/dist/Notary"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import authHandler from "@App/middlewares/AuthHandler"; + +@Controller() +@Service() +export default class OfficesController extends ApiController { + constructor(private officesService: OfficesService) { + super(); + } + /** + * @description Get all offices + */ + @Get("/api/v1/notary/offices", [authHandler, ruleHandler]) + 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 officesEntities: Offices[] = await this.officesService.get(query); + //Hydrate ressource with prisma entity + const offices = OfficeResource.hydrateArray(officesEntities, { strategy: "excludeAll" }); + //success + this.httpSuccess(response, offices); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific office by uid + */ + @Get("/api/v1/notary/offices/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let officeEntity: Offices | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + officeEntity = await this.officesService.getByUid(uid, query); + } else { + //call service to get prisma entity + officeEntity = await this.officesService.getByUid(uid); + } + + if (!officeEntity) { + this.httpNotFoundRequest(response, "office not found"); + return; + } + + //Hydrate ressource with prisma entity + const office = OfficeResource.hydrate(officeEntity, { strategy: "excludeAll" }); + //success + this.httpSuccess(response, office); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/RolesController.ts b/src/app/api/notary/RolesController.ts new file mode 100644 index 00000000..15d3711c --- /dev/null +++ b/src/app/api/notary/RolesController.ts @@ -0,0 +1,77 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RolesService from "@Services/notary/RolesService/RolesService"; +import { Service } from "typedi"; +import { Role } from "le-coffre-resources/dist/Notary"; +import { Roles } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class RolesController extends ApiController { + constructor(private rolesService: RolesService) { + super(); + } + + /** + * @description Get all roles + */ + @Get("/api/v1/notary/roles", [authHandler, ruleHandler]) + 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 rolesEntities = await this.rolesService.get(query); + + //Hydrate ressource with prisma entity + const roles = Role.hydrateArray(rolesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, roles); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific role by uid + */ + @Get("/api/v1/notary/roles/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let roleEntity: Roles | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + roleEntity = await this.rolesService.getByUid(uid, query); + } else { + //call service to get prisma entity + roleEntity = await this.rolesService.getByUid(uid); + } + + if (!roleEntity) { + this.httpNotFoundRequest(response, "role not found"); + return; + } + + //Hydrate ressource with prisma entity + const role = Role.hydrate(roleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, role); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/RulesController.ts b/src/app/api/notary/RulesController.ts new file mode 100644 index 00000000..773ba05a --- /dev/null +++ b/src/app/api/notary/RulesController.ts @@ -0,0 +1,77 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RulesService from "@Services/notary/RulesService/RulesService"; +import { Service } from "typedi"; +import { Rule } from "le-coffre-resources/dist/Notary"; +import { Rules } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class RulesController extends ApiController { + constructor(private rulesService: RulesService) { + super(); + } + + /** + * @description Get all rules + */ + @Get("/api/v1/notary/rules", [authHandler, ruleHandler]) + 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 rulesEntities = await this.rulesService.get(query); + + //Hydrate ressource with prisma entity + const rules = Rule.hydrateArray(rulesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rules); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific rule by uid + */ + @Get("/api/v1/notary/rules/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let ruleEntity: Rules | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + ruleEntity = await this.rulesService.getByUid(uid, query); + } else { + //call service to get prisma entity + ruleEntity = await this.rulesService.getByUid(uid); + } + + if (!ruleEntity) { + this.httpNotFoundRequest(response, "rule not found"); + return; + } + + //Hydrate ressource with prisma entity + const rule = Rule.hydrate(ruleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rule); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/UsersController.ts b/src/app/api/notary/UsersController.ts new file mode 100644 index 00000000..c2836a60 --- /dev/null +++ b/src/app/api/notary/UsersController.ts @@ -0,0 +1,81 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import UsersService from "@Services/notary/UsersService/UsersService"; +import { Service } from "typedi"; +import User from "le-coffre-resources/dist/Notary"; +import { Prisma, Users } from "@prisma/client"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; +import userHandler from "@App/middlewares/OfficeMembershipHandlers/UserHandler"; + +@Controller() +@Service() +export default class UsersController extends ApiController { + constructor(private usersService: UsersService) { + super(); + } + + /** + * @description Get all users + */ + @Get("/api/v1/notary/users", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + const officeId: string = req.body.user.office_Id; + const officeWhereInput: Prisma.UsersWhereInput = { office_membership: { uid: officeId } }; + query.where = officeWhereInput; + + //call service to get prisma entity + const usersEntities = await this.usersService.get(query); + + //Hydrate ressource with prisma entity + const users = User.hydrateArray(usersEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, users); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific user by uid + */ + @Get("/api/v1/notary/users/:uid", [authHandler, ruleHandler, userHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let userEntity: Users | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + userEntity = await this.usersService.getByUid(uid, query); + } else { + //call service to get prisma entity + userEntity = await this.usersService.getByUid(uid); + } + + if (!userEntity) { + this.httpNotFoundRequest(response, "user not found"); + return; + } + + //Hydrate ressource with prisma entity + const user = User.hydrate(userEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, user); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/super-admin/FilesController.ts b/src/app/api/super-admin/FilesController.ts index 33611649..78b99b4c 100644 --- a/src/app/api/super-admin/FilesController.ts +++ b/src/app/api/super-admin/FilesController.ts @@ -1,12 +1,10 @@ import { Response, Request } from "express"; -import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import { Controller, Delete, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import FilesService from "@Services/common/FilesService/FilesService"; -import { Files, Prisma } from "@prisma/client"; +import { Prisma } from "@prisma/client"; import { File } from "le-coffre-resources/dist/SuperAdmin"; -import { validateOrReject } from "class-validator"; -import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import fileHandler from "@App/middlewares/OfficeMembershipHandlers/FileHandler"; @@ -14,7 +12,7 @@ import fileHandler from "@App/middlewares/OfficeMembershipHandlers/FileHandler"; @Controller() @Service() export default class FilesController extends ApiController { - constructor(private filesService: FilesService, private documentService: DocumentsService) { + constructor(private filesService: FilesService) { super(); } @@ -72,81 +70,6 @@ export default class FilesController extends ApiController { } } - /** - * @description Create a new File - * @returns File created - */ - @Post("/api/v1/super-admin/files", [authHandler, ruleHandler, fileHandler]) - protected async post(req: Request, response: Response) { - try { - //get file - if (!req.file) throw new Error("No file provided"); - - //init File resource with request body values - const fileEntity = File.hydrate(JSON.parse(req.body["q"])); - - //validate File - await validateOrReject(fileEntity, { groups: ["createFile"] }); - - //call service to get prisma entity - const fileEntityCreated = await this.filesService.create(fileEntity, req.file); - - const document = await this.documentService.getByUid(fileEntity.document!.uid!); - - document!.document_status = "DEPOSITED"; - await this.documentService.update(document!.uid!, document!); - - //Hydrate ressource with prisma entity - const fileEntityHydrated = File.hydrate(fileEntityCreated, { - strategy: "excludeAll", - }); - - //success - this.httpCreated(response, fileEntityHydrated); - } catch (error) { - this.httpBadRequest(response, error); - return; - } - } - - /** - * @description Update a specific file - */ - @Put("/api/v1/super-admin/files/:uid", [authHandler, ruleHandler, fileHandler]) - protected async update(req: Request, response: Response) { - try { - const uid = req.params["uid"]; - if (!uid) { - throw new Error("No uid provided"); - } - - const fileFound = await this.filesService.getByUid(uid); - - if (!fileFound) { - this.httpNotFoundRequest(response, "file not found"); - return; - } - - //init File resource with request body values - const fileEntity = File.hydrate(req.body); - - //validate file - await validateOrReject(fileEntity, { groups: ["updateFile"] }); - - //call service to get prisma entity - const fileEntityUpdated: Files = await this.filesService.update(uid, fileEntity); - - //Hydrate ressource with prisma entity - const file = File.hydrate(fileEntityUpdated, { strategy: "excludeAll" }); - - //success - this.httpSuccess(response, file); - } catch (error) { - this.httpBadRequest(response, error); - return; - } - } - /** * @description Delete a specific File */ diff --git a/src/app/api/super-admin/OfficeRolesController.ts b/src/app/api/super-admin/OfficeRolesController.ts index 7adff4a8..ae2702f7 100644 --- a/src/app/api/super-admin/OfficeRolesController.ts +++ b/src/app/api/super-admin/OfficeRolesController.ts @@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficeRolesService from "@Services/super-admin/OfficeRolesService/OfficeRolesService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; -import { OfficeRole } from "le-coffre-resources/dist/Notary"; +import { OfficeRole } from "le-coffre-resources/dist/SuperAdmin"; import { OfficeRoles, Prisma } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; diff --git a/src/app/api/super-admin/RolesController.ts b/src/app/api/super-admin/RolesController.ts index a8afcf27..e5c774e5 100644 --- a/src/app/api/super-admin/RolesController.ts +++ b/src/app/api/super-admin/RolesController.ts @@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import RolesService from "@Services/super-admin/RolesService/RolesService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; -import { Role } from "le-coffre-resources/dist/Notary"; +import { Role } from "le-coffre-resources/dist/SuperAdmin"; import { Roles } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; diff --git a/src/app/api/super-admin/RulesController.ts b/src/app/api/super-admin/RulesController.ts index 6dd93840..9555cef8 100644 --- a/src/app/api/super-admin/RulesController.ts +++ b/src/app/api/super-admin/RulesController.ts @@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import RulesService from "@Services/super-admin/RulesService/RulesService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; -import { Rule } from "le-coffre-resources/dist/Notary"; +import { Rule } from "le-coffre-resources/dist/SuperAdmin"; import { Rules } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index ca058591..3a4e620e 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import UsersService from "@Services/super-admin/UsersService/UsersService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; -import User from "le-coffre-resources/dist/Notary"; +import User from "le-coffre-resources/dist/SuperAdmin"; import { Users } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; diff --git a/src/app/middlewares/CustomerHandler/DocumentHandler.ts b/src/app/middlewares/CustomerHandler/DocumentHandler.ts new file mode 100644 index 00000000..0f82fd35 --- /dev/null +++ b/src/app/middlewares/CustomerHandler/DocumentHandler.ts @@ -0,0 +1,23 @@ +import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; +import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; +import { NextFunction, Request, Response } from "express"; +import Container from "typedi"; + +export default async function documentHandler(req: Request, response: Response, next: NextFunction) { + const customerId = req.body.user.customerId; + const uid = req.path && req.path.split("/")[5]; + + if(!uid) { + response.sendStatus(HttpCodes.BAD_REQUEST); + return; + } + + const documentService = Container.get(DocumentsService); + const document = await documentService.getByUid(uid); + + if(document?.depositor_uid != customerId) { + response.sendStatus(HttpCodes.UNAUTHORIZED); + return; + } + +} diff --git a/src/app/middlewares/CustomerHandler/FileHandler.ts b/src/app/middlewares/CustomerHandler/FileHandler.ts new file mode 100644 index 00000000..aa245d70 --- /dev/null +++ b/src/app/middlewares/CustomerHandler/FileHandler.ts @@ -0,0 +1,37 @@ +import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; +import FilesService from "@Services/common/FilesService/FilesService"; +import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; +import { NextFunction, Request, Response } from "express"; +import Container from "typedi"; + +export default async function fileHandler(req: Request, response: Response, next: NextFunction) { + const customerId = req.body.user.customerId; + const uid = req.path && req.path.split("/")[5]; + const document = req.body.document; + + if (uid) { + const fileService = Container.get(FilesService); + const file = await fileService.getByUidWithDocument(uid); + if (!file) { + response.sendStatus(HttpCodes.BAD_REQUEST); + return; + } + if (file.document.depositor_uid != customerId) { + response.sendStatus(HttpCodes.UNAUTHORIZED); + return; + } + } + + if (document) { + const documentService = Container.get(DocumentsService); + const documentFound = await documentService.getByUid(document.uid!); + if(!documentFound) { + response.sendStatus(HttpCodes.BAD_REQUEST); + return; + } + if (documentFound.depositor_uid != customerId) { + response.sendStatus(HttpCodes.UNAUTHORIZED); + return; + } + } +} diff --git a/src/common/repositories/ContactRepository.ts b/src/common/repositories/ContactRepository.ts new file mode 100644 index 00000000..8a3861ee --- /dev/null +++ b/src/common/repositories/ContactRepository.ts @@ -0,0 +1,29 @@ +import Database from "@Common/databases/database"; +import BaseRepository from "@Repositories/BaseRepository"; +import { Service } from "typedi"; +import { Contacts, Customers } from "@prisma/client"; + +@Service() +export default class ContactRepository extends BaseRepository { + constructor(private database: Database) { + super(); + } + protected get model() { + return this.database.getClient().contacts; + } + protected get instanceDb() { + return this.database.getClient(); + } + + /** + * @description : Find unique customer by email + */ + public async findOneByEmail(email: string): Promise<(Contacts & {customers: Customers | null}) | null> { + return this.model.findUnique({ + where: { + email: email, + }, + include: { customers: true } + }); + } +} diff --git a/src/common/repositories/CustomersRepository.ts b/src/common/repositories/CustomersRepository.ts index a4ebfe4a..8a27293a 100644 --- a/src/common/repositories/CustomersRepository.ts +++ b/src/common/repositories/CustomersRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { Contacts, Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client"; +import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client"; import { Customer } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -28,7 +28,7 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Create a customer */ - public async create(customer: Customer): Promise { + public async create(customer: Customer): Promise { const createArgs: Prisma.CustomersCreateArgs = { data: { status: ECustomerStatus.PENDING, @@ -61,7 +61,7 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Update data from a customer */ - public async update(uid: string, customer: Customer): Promise { + public async update(uid: string, customer: Customer): Promise { const updateArgs: Prisma.CustomersUpdateArgs = { where: { uid: uid, @@ -102,4 +102,15 @@ export default class CustomersRepository extends BaseRepository { include: query, }); } + + /** + * @description : Find unique customer + */ + public async findOneByContact(contactUid: string) { + return this.model.findUnique({ + where: { + contact_uid: contactUid, + }, + }); + } } diff --git a/src/common/repositories/DeedTypesRepository.ts b/src/common/repositories/DeedTypesRepository.ts index 62059131..be7b4194 100644 --- a/src/common/repositories/DeedTypesRepository.ts +++ b/src/common/repositories/DeedTypesRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { DeedTypes, DocumentTypes, Prisma } from "@prisma/client"; +import { DeedTypes, Prisma } from "@prisma/client"; import { DeedType } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -52,7 +52,7 @@ export default class DeedTypesRepository extends BaseRepository { /** * @description : Update data of a deed type */ - public async update(uid: string, deedType: DeedType): Promise { + public async update(uid: string, deedType: DeedType): Promise { const updateArgs: Prisma.DeedTypesUpdateArgs = { where: { uid: uid, diff --git a/src/common/repositories/DeedsRepository.ts b/src/common/repositories/DeedsRepository.ts index becc4d55..e9501210 100644 --- a/src/common/repositories/DeedsRepository.ts +++ b/src/common/repositories/DeedsRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { Deeds, DocumentTypes, Prisma } from "@prisma/client"; +import { Deeds, Prisma } from "@prisma/client"; import { Deed } from "le-coffre-resources/dist/Notary"; @Service() @@ -27,7 +27,7 @@ export default class DeedsRepository extends BaseRepository { /** * @description : Create a deed based on a deed type */ - public async create(deed: Deed): Promise { + public async create(deed: Deed): Promise { const createArgs: Prisma.DeedsCreateArgs = { data: { deed_type: { @@ -59,7 +59,7 @@ export default class DeedsRepository extends BaseRepository { /** * @description : Update data of a deed type */ - public async update(uid: string, deed: Deed): Promise { + public async update(uid: string, deed: Deed): Promise { const updateArgs: Prisma.DeedsUpdateArgs = { where: { uid: uid, diff --git a/src/common/repositories/DocumentsRepository.ts b/src/common/repositories/DocumentsRepository.ts index 5a81d7ca..98b66e1b 100644 --- a/src/common/repositories/DocumentsRepository.ts +++ b/src/common/repositories/DocumentsRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { DocumentTypes, Documents, EDocumentStatus, Prisma } from "@prisma/client"; +import { Documents, EDocumentStatus, Prisma } from "@prisma/client"; import { Document } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -27,7 +27,7 @@ export default class DocumentsRepository extends BaseRepository { /** * @description : Create a document */ - public async create(document: Document): Promise { + public async create(document: Document): Promise { const createArgs: Prisma.DocumentsCreateArgs = { data: { folder: { @@ -125,7 +125,7 @@ export default class DocumentsRepository extends BaseRepository { /** * @description : Find unique document */ - public async findOneByUid(uid: string, query?: Prisma.DocumentsInclude) { + public async findOneByUid(uid: string, query?: Prisma.DocumentsInclude): Promise { return this.model.findUnique({ where: { uid: uid, diff --git a/src/common/repositories/FilesRepository.ts b/src/common/repositories/FilesRepository.ts index 100e10a2..02776bdf 100644 --- a/src/common/repositories/FilesRepository.ts +++ b/src/common/repositories/FilesRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { Documents, Files, Prisma } from "@prisma/client"; +import { Files, Prisma } from "@prisma/client"; import { File } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -27,7 +27,7 @@ export default class FilesRepository extends BaseRepository { /** * @description : Create a file linked to a document */ - public async create(file: File, key: string): Promise { + public async create(file: File, key: string): Promise { const createArgs: Prisma.FilesCreateArgs = { data: { document: { @@ -48,7 +48,7 @@ export default class FilesRepository extends BaseRepository { /** * @description : Update data of a file */ - public async update(uid: string, file: File, key: string): Promise { + public async update(uid: string, file: File, key: string): Promise { const updateArgs: Prisma.FilesUpdateArgs = { where: { uid: uid, @@ -67,7 +67,7 @@ export default class FilesRepository extends BaseRepository { /** * @description : Delete a file key and archive */ - public async deleteKeyAndArchive(uid: string): Promise { + public async deleteKeyAndArchive(uid: string): Promise { const updateArgs: Prisma.FilesUpdateArgs = { where: { uid: uid, @@ -103,4 +103,16 @@ export default class FilesRepository extends BaseRepository { include: { document: { include: { folder: { include: { office: true } } } } }, }); } + + /** + * @description : Find unique file with document + */ + public async findOneByUidWithDocument(uid: string) { + return this.model.findUnique({ + where: { + uid: uid, + }, + include: { document: true }, + }); + } } diff --git a/src/common/repositories/OfficeFoldersRepository.ts b/src/common/repositories/OfficeFoldersRepository.ts index e36d2b36..43011a95 100644 --- a/src/common/repositories/OfficeFoldersRepository.ts +++ b/src/common/repositories/OfficeFoldersRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { Customers, Documents, EFolderStatus, OfficeFolders, Prisma, Users } from "@prisma/client"; +import { EFolderStatus, OfficeFolders, Prisma } from "@prisma/client"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -27,7 +27,7 @@ export default class OfficeFoldersRepository extends BaseRepository { /** * @description : Create new office folder with stakeholders */ - public async create(officeFolder: OfficeFolder): Promise { + public async create(officeFolder: OfficeFolder): Promise { const createArgs: Prisma.OfficeFoldersCreateArgs = { data: { folder_number: officeFolder.folder_number, @@ -61,7 +61,7 @@ export default class OfficeFoldersRepository extends BaseRepository { public async update( officeFolderuid: string, officeFolder: OfficeFolder, - ): Promise { + ): Promise { const updateArgs: Prisma.OfficeFoldersUpdateArgs = { where: { uid: officeFolderuid, diff --git a/src/common/repositories/OfficeRolesRepository.ts b/src/common/repositories/OfficeRolesRepository.ts index 940c9bc4..f96a9926 100644 --- a/src/common/repositories/OfficeRolesRepository.ts +++ b/src/common/repositories/OfficeRolesRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { OfficeRoles, Prisma, Rules } from "@prisma/client"; +import { OfficeRoles, Prisma } from "@prisma/client"; import { OfficeRole } from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -27,7 +27,7 @@ export default class OfficeRolesRepository extends BaseRepository { /** * @description : Create new officeRole with rules */ - public async create(officeRole: OfficeRole): Promise { + public async create(officeRole: OfficeRole): Promise { const createArgs: Prisma.OfficeRolesCreateArgs = { data: { name: officeRole.name, @@ -50,7 +50,7 @@ export default class OfficeRolesRepository extends BaseRepository { /** * @description : Update data of a officeRole with rules */ - public async update(officeRole: OfficeRole): Promise { + public async update(officeRole: OfficeRole): Promise { const updateArgs: Prisma.OfficeRolesUpdateArgs = { where: { uid: officeRole.uid, diff --git a/src/common/repositories/UsersRepository.ts b/src/common/repositories/UsersRepository.ts index 7f98728b..b43d0a8f 100644 --- a/src/common/repositories/UsersRepository.ts +++ b/src/common/repositories/UsersRepository.ts @@ -1,7 +1,7 @@ import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; -import { Addresses, Contacts, ECivility, Offices, Prisma, Users } from "@prisma/client"; +import { ECivility, Prisma, Users } from "@prisma/client"; import User from "le-coffre-resources/dist/SuperAdmin"; @Service() @@ -87,17 +87,7 @@ export default class UsersRepository extends BaseRepository { /** * @description : Update data from a user */ - public async update( - uid: string, - user: User, - ): Promise< - Users & { - contact: Contacts; - office_membership: Offices & { - address: Addresses; - }; - } - > { + public async update(uid: string, user: User): Promise { const updateArgs: Prisma.UsersUpdateArgs = { where: { uid: uid, diff --git a/src/services/admin/CustomersService/CustomersService.ts b/src/services/admin/CustomersService/CustomersService.ts new file mode 100644 index 00000000..7656bed2 --- /dev/null +++ b/src/services/admin/CustomersService/CustomersService.ts @@ -0,0 +1,52 @@ +import { Customers, Prisma } from "@prisma/client"; +import CustomersRepository from "@Repositories/CustomersRepository"; +import BaseService from "@Services/BaseService"; +import { Customer } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Service() +export default class CustomersService extends BaseService { + constructor(private customerRepository: CustomersRepository) { + super(); + } + + /** + * @description : Get all Customers + * @throws {Error} If Customers cannot be get + */ + public async get(query: Prisma.CustomersFindManyArgs): Promise { + return this.customerRepository.findMany(query); + } + + /** + * @description : Create a new customer + * @throws {Error} If customer cannot be created + */ + public async create(customerEntity: Customer): Promise { + return this.customerRepository.create(customerEntity); + } + + /** + * @description : Modify a customer + * @throws {Error} If customer cannot be modified + */ + public async update(uid: string, customerEntity: Customer): Promise { + return this.customerRepository.update(uid, customerEntity); + } + + /** + * @description : Get a customer by uid + * @throws {Error} If customer cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.CustomersInclude): Promise { + return this.customerRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a customer by contact uid + * @throws {Error} If customer cannot be get by contact uid + */ + public async getByContact(contactUid: string): Promise { + return this.customerRepository.findOneByContact(contactUid); + } +} diff --git a/src/services/admin/DeedTypesService/DeedTypesService.ts b/src/services/admin/DeedTypesService/DeedTypesService.ts new file mode 100644 index 00000000..35daf668 --- /dev/null +++ b/src/services/admin/DeedTypesService/DeedTypesService.ts @@ -0,0 +1,52 @@ +import { DeedTypes, Prisma } from "@prisma/client"; +import DeedTypesRepository from "@Repositories/DeedTypesRepository"; +import BaseService from "@Services/BaseService"; +import { DeedType } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Service() +export default class DeedTypesService extends BaseService { + constructor(private deedTypeRepository: DeedTypesRepository) { + super(); + } + + /** + * @description : Get all deed-types + * @throws {Error} If deed-types cannot be get + */ + public async get(query: Prisma.DeedTypesFindManyArgs) { + return this.deedTypeRepository.findMany(query); + } + + /** + * @description : Create a new deed-type + * @throws {Error} If deed-type cannot be created + */ + public async create(deedTypeEntity: DeedType): Promise { + return this.deedTypeRepository.create(deedTypeEntity); + } + + /** + * @description : Modify a deed-type + * @throws {Error} If deed-type cannot be modifified + */ + public async update(uid: string, deedTypeEntity: DeedType): Promise { + return this.deedTypeRepository.update(uid, deedTypeEntity); + } + + /** + * @description : Get a deedtype by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DeedTypesInclude): Promise { + return this.deedTypeRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a deedtype by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.deedTypeRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/admin/DeedsService/DeedsService.ts b/src/services/admin/DeedsService/DeedsService.ts new file mode 100644 index 00000000..56d88427 --- /dev/null +++ b/src/services/admin/DeedsService/DeedsService.ts @@ -0,0 +1,48 @@ +import { Deeds, Prisma } from "@prisma/client"; +import DeedsRepository from "@Repositories/DeedsRepository"; +import BaseService from "@Services/BaseService"; +import { Deed } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Service() +export default class DeedsService extends BaseService { + constructor(private deedRepository: DeedsRepository) { + super(); + } + + /** + * @description : Get all deeds + * @throws {Error} If deeds cannot be get + */ + public async get(query: Prisma.DeedsFindManyArgs) { + return this.deedRepository.findMany(query); + } + + /** + * @description : Create a new deed with document types + * @throws {Error} If deeds cannot be created + */ + public async create(deed: Deed): Promise { + return this.deedRepository.create(deed); + } + + /** + * @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, deed: Deed): Promise { + return this.deedRepository.update(uid, deed); + } + + /** + * @description : Get a deed by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DeedsInclude) { + return this.deedRepository.findOneByUid(uid, query); + } + + public async getOneByUidWithOffice(uid: string) { + return this.deedRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/admin/DocumentTypesService/DocumentTypesService.ts b/src/services/admin/DocumentTypesService/DocumentTypesService.ts new file mode 100644 index 00000000..30d56724 --- /dev/null +++ b/src/services/admin/DocumentTypesService/DocumentTypesService.ts @@ -0,0 +1,52 @@ +import { DocumentTypes, Prisma } from "@prisma/client"; +import DocumentTypesRepository from "@Repositories/DocumentTypesRepository"; +import BaseService from "@Services/BaseService"; +import { DocumentType } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; + +@Service() +export default class DocumentTypesService extends BaseService { + constructor(private documentTypeRepository: DocumentTypesRepository) { + super(); + } + + /** + * @description : Get all document-types + * @throws {Error} If document-types cannot be get + */ + public async get(query: Prisma.DocumentTypesFindManyArgs) { + return this.documentTypeRepository.findMany(query); + } + + /** + * @description : Create a new document-type + * @throws {Error} If document-types cannot be created + */ + public async create(documentTypeEntity: DocumentType): Promise { + return this.documentTypeRepository.create(documentTypeEntity); + } + + /** + * @description : Modify a document-type + * @throws {Error} If document-type cannot be modified + */ + public async update(uid: string, documentTypeEntity: DocumentType): Promise { + return this.documentTypeRepository.update(uid, documentTypeEntity); + } + + /** + * @description : Get a document-type by uid + * @throws {Error} If document-type is not found + */ + public async getByUid(uid: string, query?: Prisma.DocumentTypesInclude) { + return this.documentTypeRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a document-type by uid with relations + * @throws {Error} If document-type is not found + */ + public async getByUidWithOffice(uid: string) { + return this.documentTypeRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/admin/DocumentsService/DocumentsService.ts b/src/services/admin/DocumentsService/DocumentsService.ts new file mode 100644 index 00000000..c8e7aa0b --- /dev/null +++ b/src/services/admin/DocumentsService/DocumentsService.ts @@ -0,0 +1,75 @@ +import { Documents, Prisma } from "@prisma/client"; +import { Document } from "le-coffre-resources/dist/Admin"; +import DocumentsRepository from "@Repositories/DocumentsRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class DocumentsService extends BaseService { + constructor(private documentsRepository: DocumentsRepository) { + super(); + } + + /** + * @description : Get all documents + * @throws {Error} If documents cannot be get + */ + public async get(query: Prisma.DocumentsFindManyArgs) { + return this.documentsRepository.findMany(query); + } + + /** + * @description : Create a new document + * @throws {Error} If document cannot be created + */ + public async create(document: Document): Promise { + return this.documentsRepository.create(document); + } + + /** + * @description : Create new documents + * @throws {Error} If documents or one of them cannot be created + */ + public async createMany(documents: Document[]): Promise { + return this.documentsRepository.createMany(documents); + } + + /** + * @description : Modify a document + * @throws {Error} If document cannot be modified + */ + public async update(uid: string, document: Partial, refused_reason?: string): Promise { + return this.documentsRepository.update(uid, document, refused_reason); + } + + /** + * @description : Delete a document + * @throws {Error} If document cannot be deleted + */ + public async delete(uid: string): Promise { + const documentEntity = await this.documentsRepository.findOneByUid(uid, { files: true }); + if (!documentEntity) throw new Error("document not found"); + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + if (document.files && document.files.length !== 0) { + throw new Error("Can't delete a document with file"); + } + return this.documentsRepository.delete(uid); + } + + /** + * @description : Get a document by uid + * @throws {Error} If document cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DocumentsInclude): Promise { + return this.documentsRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a document by uid + * @throws {Error} If document cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.documentsRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/admin/OfficeFoldersService/OfficeFoldersService.ts b/src/services/admin/OfficeFoldersService/OfficeFoldersService.ts new file mode 100644 index 00000000..8d474aef --- /dev/null +++ b/src/services/admin/OfficeFoldersService/OfficeFoldersService.ts @@ -0,0 +1,79 @@ +import { OfficeFolders } from ".prisma/client"; +import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository"; +import BaseService from "@Services/BaseService"; +import { OfficeFolder } from "le-coffre-resources/dist/Admin"; +import { Service } from "typedi"; +import DeedTypesService from "../DeedTypesService/DeedTypesService"; +import DeedsRepository from "@Repositories/DeedsRepository"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class OfficeFoldersService extends BaseService { + constructor( + private officeFoldersRepository: OfficeFoldersRepository, + private deedTypeService: DeedTypesService, + private deedRepository: DeedsRepository, + ) { + super(); + } + + /** + * @description : Get all folders + * @throws {Error} If folders cannot be get + */ + public async get(query: Prisma.OfficeFoldersFindManyArgs) { + return this.officeFoldersRepository.findMany(query); + } + + /** + * @description : Create a new folder + * @throws {Error} If folder cannot be created + */ + public async create(officeFolderEntity: OfficeFolder): Promise { + const deedType = await this.deedTypeService.getByUid(officeFolderEntity.deed!.deed_type!.uid!); + if (!deedType) throw new Error("deed type not found"); + if (deedType.archived_at) throw new Error("deed type is archived"); + const deed = await this.deedRepository.create(officeFolderEntity.deed!); + officeFolderEntity.deed!.uid = deed.uid; + return this.officeFoldersRepository.create(officeFolderEntity); + } + + /** + * @description : Modify a folder + * @throws {Error} If folder cannot be modified + */ + public async update(officeFolderuid: string, officeFolderEntity: OfficeFolder): Promise { + return this.officeFoldersRepository.update(officeFolderuid, officeFolderEntity); + } + + /** + * @description : Get a folder by uid + * @throws {Error} If folder cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.OfficeFoldersInclude) { + return this.officeFoldersRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a folder by uid + * @throws {Error} If folder cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.officeFoldersRepository.findOneByUidWithOffice(uid); + } + + /** + * @description : Delete a folder + * @throws {Error} If document cannot be deleted + */ + public async delete(uid: string): Promise { + const officeFolderEntity = await this.officeFoldersRepository.findOneByUid(uid, { customers: true }); + if (!officeFolderEntity) throw new Error("office folder not found"); + const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); + + if (officeFolder.customers?.length) { + throw new Error("This folder is used by customers"); + } + return this.officeFoldersRepository.delete(uid); + } +} diff --git a/src/services/admin/OfficeRolesService/OfficeRolesService.ts b/src/services/admin/OfficeRolesService/OfficeRolesService.ts new file mode 100644 index 00000000..2fb82f48 --- /dev/null +++ b/src/services/admin/OfficeRolesService/OfficeRolesService.ts @@ -0,0 +1,52 @@ +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; +import OfficeRolesRepository from "@Repositories/OfficeRolesRepository"; +import { OfficeRole } from "le-coffre-resources/dist/Admin"; +import { Prisma, OfficeRoles } from "@prisma/client"; + +@Service() +export default class OfficeRolesService extends BaseService { + constructor(private officeRoleRepository: OfficeRolesRepository) { + super(); + } + + /** + * @description : Get all officeRoles + * @throws {Error} If officeRoles cannot be get + */ + public get(query: Prisma.OfficeRolesFindManyArgs) { + return this.officeRoleRepository.findMany(query); + } + + /** + * @description : Create a officeRole + * @throws {Error} If officeRole couldn't be created + */ + public create(officeRoleEntity: OfficeRole): Promise { + return this.officeRoleRepository.create(officeRoleEntity); + } + + /** + * @description : Modify a officeRole + * @throws {Error} If officeRole modification failed + */ + public update(officeRoleEntity: OfficeRole): Promise { + return this.officeRoleRepository.update(officeRoleEntity); + } + + /** + * @description : Get a officeRole by uid + * @throws {Error} If officeRole cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.OfficeRolesInclude) { + return this.officeRoleRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a officeRole by uid + * @throws {Error} If officeRole cannot be get by uid + */ + public getByUidWithOffice(uid: string) { + return this.officeRoleRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/admin/OfficesService/OfficesService.ts b/src/services/admin/OfficesService/OfficesService.ts new file mode 100644 index 00000000..af25083b --- /dev/null +++ b/src/services/admin/OfficesService/OfficesService.ts @@ -0,0 +1,27 @@ +import { Prisma } from "@prisma/client"; +import OfficesRepository from "@Repositories/OfficesRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class OfficesService extends BaseService { + constructor(private officeRepository: OfficesRepository) { + super(); + } + + /** + * @description : Get all offices + * @throws {Error} If offices cannot be get + */ + public async get(query: Prisma.OfficesFindManyArgs) { + return this.officeRepository.findMany(query); + } + + /** + * @description : Get a office by uid + * @throws {Error} If office cannot be get + */ + public async getByUid(uid: string, query?: Prisma.OfficesInclude) { + return this.officeRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/admin/RolesService/RolesService.ts b/src/services/admin/RolesService/RolesService.ts new file mode 100644 index 00000000..2e4032d0 --- /dev/null +++ b/src/services/admin/RolesService/RolesService.ts @@ -0,0 +1,45 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RolesRepository from "@Repositories/RolesRepository"; +import { Role } from "le-coffre-resources/dist/Admin"; +import { Prisma, Roles } from "@prisma/client"; + +@Service() +export default class RolesService extends BaseService { + constructor(private roleRepository: RolesRepository) { + super(); + } + + /** + * @description : Get all roles + * @throws {Error} If roles cannot be get + */ + public get(query: Prisma.RolesFindManyArgs) { + return this.roleRepository.findMany(query); + } + + /** + * @description : Create a role + * @throws {Error} If role couldn't be created + */ + public create(roleEntity: Role): Promise { + return this.roleRepository.create(roleEntity); + } + + /** + * @description : Modify a role + * @throws {Error} If role modification failed + */ + public update(roleEntity: Role): Promise { + return this.roleRepository.update(roleEntity); + } + + /** + * @description : Get a role by uid + * @throws {Error} If role cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.RolesInclude) { + return this.roleRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/admin/RulesService/RulesService.ts b/src/services/admin/RulesService/RulesService.ts new file mode 100644 index 00000000..28c60784 --- /dev/null +++ b/src/services/admin/RulesService/RulesService.ts @@ -0,0 +1,45 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RulesRepository from "@Repositories/RulesRepository"; +import { Rule } from "le-coffre-resources/dist/Admin"; +import { Prisma, Rules } from "@prisma/client"; + +@Service() +export default class RulesService extends BaseService { + constructor(private ruleRepository: RulesRepository) { + super(); + } + + /** + * @description : Get all rules + * @throws {Error} If rules cannot be get + */ + public get(query: Prisma.RulesFindManyArgs) { + return this.ruleRepository.findMany(query); + } + + /** + * @description : Create a rule + * @throws {Error} If rule couldn't be created + */ + public create(ruleEntity: Rule): Promise { + return this.ruleRepository.create(ruleEntity); + } + + /** + * @description : Modify a rule + * @throws {Error} If rule modification failed + */ + public update(ruleEntity: Rule): Promise { + return this.ruleRepository.update(ruleEntity); + } + + /** + * @description : Get a rule by uid + * @throws {Error} If rule cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.RulesInclude) { + return this.ruleRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/admin/UsersService/UsersService.ts b/src/services/admin/UsersService/UsersService.ts new file mode 100644 index 00000000..d2216c3b --- /dev/null +++ b/src/services/admin/UsersService/UsersService.ts @@ -0,0 +1,61 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import UsersRepository from "@Repositories/UsersRepository"; +import User from "le-coffre-resources/dist/Admin"; +import { Prisma, Users } from "@prisma/client"; + +@Service() +export default class UsersService extends BaseService { + constructor(private userRepository: UsersRepository) { + super(); + } + + /** + * @description : Get all users + * @throws {Error} If users cannot be get + */ + public get(query: Prisma.UsersFindManyArgs) { + return this.userRepository.findMany(query); + } + + /** + * @description : Create a user + * @throws {Error} If user couldn't be created + */ + public create(userEntity: User): Promise { + return this.userRepository.create(userEntity); + } + + /** + * @description : Modify a user + * @throws {Error} If user modification failed + */ + public update(uid: string, userEntity: User): Promise { + return this.userRepository.update(uid, userEntity); + } + + /** + * @description : Get a user by uid + * @throws {Error} If user cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.UsersInclude) { + return this.userRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a user by uid with office + * @throws {Error} If user cannot be get by uid + */ + public getByUidWithOffice(uid: string) { + return this.userRepository.findOneByUidWithOffice(uid); + } + + /** + * @description : Get a user by uid + * @throws {Error} If user cannot be get by uid + */ + public getByProvider(providerName: string, id: string) { + return this.userRepository.findOneByProvider(providerName, id); + } +} diff --git a/src/services/common/AuthService/AuthService.ts b/src/services/common/AuthService/AuthService.ts index 4c496cf0..278255fe 100644 --- a/src/services/common/AuthService/AuthService.ts +++ b/src/services/common/AuthService/AuthService.ts @@ -3,12 +3,21 @@ import BaseService from "@Services/BaseService"; import { BackendVariables } from "@Common/config/variables/Variables"; import { Service } from "typedi"; import UsersService from "@Services/super-admin/UsersService/UsersService"; +import CustomersService from "@Services/super-admin/CustomersService/CustomersService"; +import ContactService from "../ContactService"; +import { ECustomerStatus } from "@prisma/client"; enum PROVIDER_OPENID { idNot = "idNot", } -interface IJwtPayload { +interface ICustomerJwtPayload { + customerId: string; + email: string; +} + + +interface IUserJwtPayload { userId: string; openId: { providerName: PROVIDER_OPENID; @@ -21,11 +30,27 @@ interface IJwtPayload { @Service() export default class AuthService extends BaseService { - constructor(protected variables: BackendVariables, private userService: UsersService) { + constructor(protected variables: BackendVariables, private userService: UsersService, private customerService: CustomersService, private contactService: ContactService) { super(); } - public async getUserJwtPayload(id: string, providerName: PROVIDER_OPENID = PROVIDER_OPENID.idNot): Promise { + public async getCustomerJwtPayload(email:string): Promise { + const contact = await this.contactService.getByEmail(email); + + if (!contact) return null; + + if(contact.customers?.status === ECustomerStatus["PENDING"]) { + contact.customers.status = ECustomerStatus["VALIDATED"]; + this.customerService.update(contact.customers.uid, contact.customers); + } + + return { + customerId: contact.customers!.uid, + email: contact.email, + }; + } + + public async getUserJwtPayload(id: string, providerName: PROVIDER_OPENID = PROVIDER_OPENID.idNot): Promise { const user = await this.userService.getByProvider(providerName, id); if (!user) return null; diff --git a/src/services/common/ContactService.ts b/src/services/common/ContactService.ts new file mode 100644 index 00000000..e1cc6c19 --- /dev/null +++ b/src/services/common/ContactService.ts @@ -0,0 +1,19 @@ +import { Contacts, Customers } from "@prisma/client"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; +import ContactRepository from "@Repositories/ContactRepository"; + +@Service() +export default class DocumentsService extends BaseService { + constructor(private contactRepository: ContactRepository) { + super(); + } + + /** + * @description : Get a contact by email + * @throws {Error} If contact cannot be get by email + */ + public async getByEmail(email: string): Promise<(Contacts & {customers: Customers | null}) | null> { + return this.contactRepository.findOneByEmail(email); + } +} diff --git a/src/services/common/FilesService/FilesService.ts b/src/services/common/FilesService/FilesService.ts index 0f13bc9d..2ec4e6b5 100644 --- a/src/services/common/FilesService/FilesService.ts +++ b/src/services/common/FilesService/FilesService.ts @@ -45,6 +45,14 @@ export default class FilesService extends BaseService { return this.filesRepository.findOneByUidWithOffice(uid); } + /** + * @description : Get a file by uid with document + * @throws {Error} If project cannot be created + */ + public async getByUidWithDocument(uid: string) { + return this.filesRepository.findOneByUidWithDocument(uid); + } + /** * @description : view a file * @throws {Error} If file cannot be deleted diff --git a/src/services/notary/CustomersService/CustomersService.ts b/src/services/notary/CustomersService/CustomersService.ts new file mode 100644 index 00000000..72e6923d --- /dev/null +++ b/src/services/notary/CustomersService/CustomersService.ts @@ -0,0 +1,52 @@ +import { Customers, Prisma } from "@prisma/client"; +import CustomersRepository from "@Repositories/CustomersRepository"; +import BaseService from "@Services/BaseService"; +import { Customer } from "le-coffre-resources/dist/Notary"; +import { Service } from "typedi"; + +@Service() +export default class CustomersService extends BaseService { + constructor(private customerRepository: CustomersRepository) { + super(); + } + + /** + * @description : Get all Customers + * @throws {Error} If Customers cannot be get + */ + public async get(query: Prisma.CustomersFindManyArgs): Promise { + return this.customerRepository.findMany(query); + } + + /** + * @description : Create a new customer + * @throws {Error} If customer cannot be created + */ + public async create(customerEntity: Customer): Promise { + return this.customerRepository.create(customerEntity); + } + + /** + * @description : Modify a customer + * @throws {Error} If customer cannot be modified + */ + public async update(uid: string, customerEntity: Customer): Promise { + return this.customerRepository.update(uid, customerEntity); + } + + /** + * @description : Get a customer by uid + * @throws {Error} If customer cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.CustomersInclude): Promise { + return this.customerRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a customer by contact uid + * @throws {Error} If customer cannot be get by contact uid + */ + public async getByContact(contactUid: string): Promise { + return this.customerRepository.findOneByContact(contactUid); + } +} diff --git a/src/services/notary/DeedTypesService/DeedTypesService.ts b/src/services/notary/DeedTypesService/DeedTypesService.ts new file mode 100644 index 00000000..fa4a5551 --- /dev/null +++ b/src/services/notary/DeedTypesService/DeedTypesService.ts @@ -0,0 +1,52 @@ +import { DeedTypes, Prisma } from "@prisma/client"; +import DeedTypesRepository from "@Repositories/DeedTypesRepository"; +import BaseService from "@Services/BaseService"; +import { DeedType } from "le-coffre-resources/dist/Notary"; +import { Service } from "typedi"; + +@Service() +export default class DeedTypesService extends BaseService { + constructor(private deedTypeRepository: DeedTypesRepository) { + super(); + } + + /** + * @description : Get all deed-types + * @throws {Error} If deed-types cannot be get + */ + public async get(query: Prisma.DeedTypesFindManyArgs) { + return this.deedTypeRepository.findMany(query); + } + + /** + * @description : Create a new deed-type + * @throws {Error} If deed-type cannot be created + */ + public async create(deedTypeEntity: DeedType): Promise { + return this.deedTypeRepository.create(deedTypeEntity); + } + + /** + * @description : Modify a deed-type + * @throws {Error} If deed-type cannot be modifified + */ + public async update(uid: string, deedTypeEntity: DeedType): Promise { + return this.deedTypeRepository.update(uid, deedTypeEntity); + } + + /** + * @description : Get a deedtype by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DeedTypesInclude): Promise { + return this.deedTypeRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a deedtype by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.deedTypeRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/notary/DeedsService/DeedsService.ts b/src/services/notary/DeedsService/DeedsService.ts new file mode 100644 index 00000000..47f18cdd --- /dev/null +++ b/src/services/notary/DeedsService/DeedsService.ts @@ -0,0 +1,48 @@ +import { Deeds, Prisma } from "@prisma/client"; +import DeedsRepository from "@Repositories/DeedsRepository"; +import BaseService from "@Services/BaseService"; +import { Deed } from "le-coffre-resources/dist/Notary"; +import { Service } from "typedi"; + +@Service() +export default class DeedsService extends BaseService { + constructor(private deedRepository: DeedsRepository) { + super(); + } + + /** + * @description : Get all deeds + * @throws {Error} If deeds cannot be get + */ + public async get(query: Prisma.DeedsFindManyArgs) { + return this.deedRepository.findMany(query); + } + + /** + * @description : Create a new deed with document types + * @throws {Error} If deeds cannot be created + */ + public async create(deed: Deed): Promise { + return this.deedRepository.create(deed); + } + + /** + * @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, deed: Deed): Promise { + return this.deedRepository.update(uid, deed); + } + + /** + * @description : Get a deed by uid + * @throws {Error} If deed-type cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DeedsInclude) { + return this.deedRepository.findOneByUid(uid, query); + } + + public async getOneByUidWithOffice(uid: string) { + return this.deedRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/notary/DocumentTypesService/DocumentTypesService.ts b/src/services/notary/DocumentTypesService/DocumentTypesService.ts new file mode 100644 index 00000000..fb55f818 --- /dev/null +++ b/src/services/notary/DocumentTypesService/DocumentTypesService.ts @@ -0,0 +1,52 @@ +import { DocumentTypes, Prisma } from "@prisma/client"; +import DocumentTypesRepository from "@Repositories/DocumentTypesRepository"; +import BaseService from "@Services/BaseService"; +import { DocumentType } from "le-coffre-resources/dist/Notary"; +import { Service } from "typedi"; + +@Service() +export default class DocumentTypesService extends BaseService { + constructor(private documentTypeRepository: DocumentTypesRepository) { + super(); + } + + /** + * @description : Get all document-types + * @throws {Error} If document-types cannot be get + */ + public async get(query: Prisma.DocumentTypesFindManyArgs) { + return this.documentTypeRepository.findMany(query); + } + + /** + * @description : Create a new document-type + * @throws {Error} If document-types cannot be created + */ + public async create(documentTypeEntity: DocumentType): Promise { + return this.documentTypeRepository.create(documentTypeEntity); + } + + /** + * @description : Modify a document-type + * @throws {Error} If document-type cannot be modified + */ + public async update(uid: string, documentTypeEntity: DocumentType): Promise { + return this.documentTypeRepository.update(uid, documentTypeEntity); + } + + /** + * @description : Get a document-type by uid + * @throws {Error} If document-type is not found + */ + public async getByUid(uid: string, query?: Prisma.DocumentTypesInclude) { + return this.documentTypeRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a document-type by uid with relations + * @throws {Error} If document-type is not found + */ + public async getByUidWithOffice(uid: string) { + return this.documentTypeRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/notary/DocumentsService/DocumentsService.ts b/src/services/notary/DocumentsService/DocumentsService.ts new file mode 100644 index 00000000..649ad2bf --- /dev/null +++ b/src/services/notary/DocumentsService/DocumentsService.ts @@ -0,0 +1,75 @@ +import { Documents, Prisma } from "@prisma/client"; +import { Document } from "le-coffre-resources/dist/Notary"; +import DocumentsRepository from "@Repositories/DocumentsRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class DocumentsService extends BaseService { + constructor(private documentsRepository: DocumentsRepository) { + super(); + } + + /** + * @description : Get all documents + * @throws {Error} If documents cannot be get + */ + public async get(query: Prisma.DocumentsFindManyArgs) { + return this.documentsRepository.findMany(query); + } + + /** + * @description : Create a new document + * @throws {Error} If document cannot be created + */ + public async create(document: Document): Promise { + return this.documentsRepository.create(document); + } + + /** + * @description : Create new documents + * @throws {Error} If documents or one of them cannot be created + */ + public async createMany(documents: Document[]): Promise { + return this.documentsRepository.createMany(documents); + } + + /** + * @description : Modify a document + * @throws {Error} If document cannot be modified + */ + public async update(uid: string, document: Partial, refused_reason?: string): Promise { + return this.documentsRepository.update(uid, document, refused_reason); + } + + /** + * @description : Delete a document + * @throws {Error} If document cannot be deleted + */ + public async delete(uid: string): Promise { + const documentEntity = await this.documentsRepository.findOneByUid(uid, { files: true }); + if (!documentEntity) throw new Error("document not found"); + const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + + if (document.files && document.files.length !== 0) { + throw new Error("Can't delete a document with file"); + } + return this.documentsRepository.delete(uid); + } + + /** + * @description : Get a document by uid + * @throws {Error} If document cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.DocumentsInclude): Promise { + return this.documentsRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a document by uid + * @throws {Error} If document cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.documentsRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/notary/OfficeFoldersService/OfficeFoldersService.ts b/src/services/notary/OfficeFoldersService/OfficeFoldersService.ts new file mode 100644 index 00000000..97fa5680 --- /dev/null +++ b/src/services/notary/OfficeFoldersService/OfficeFoldersService.ts @@ -0,0 +1,79 @@ +import { OfficeFolders } from ".prisma/client"; +import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository"; +import BaseService from "@Services/BaseService"; +import { OfficeFolder } from "le-coffre-resources/dist/Notary"; +import { Service } from "typedi"; +import DeedTypesService from "../DeedTypesService/DeedTypesService"; +import DeedsRepository from "@Repositories/DeedsRepository"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class OfficeFoldersService extends BaseService { + constructor( + private officeFoldersRepository: OfficeFoldersRepository, + private deedTypeService: DeedTypesService, + private deedRepository: DeedsRepository, + ) { + super(); + } + + /** + * @description : Get all folders + * @throws {Error} If folders cannot be get + */ + public async get(query: Prisma.OfficeFoldersFindManyArgs) { + return this.officeFoldersRepository.findMany(query); + } + + /** + * @description : Create a new folder + * @throws {Error} If folder cannot be created + */ + public async create(officeFolderEntity: OfficeFolder): Promise { + const deedType = await this.deedTypeService.getByUid(officeFolderEntity.deed!.deed_type!.uid!); + if (!deedType) throw new Error("deed type not found"); + if (deedType.archived_at) throw new Error("deed type is archived"); + const deed = await this.deedRepository.create(officeFolderEntity.deed!); + officeFolderEntity.deed!.uid = deed.uid; + return this.officeFoldersRepository.create(officeFolderEntity); + } + + /** + * @description : Modify a folder + * @throws {Error} If folder cannot be modified + */ + public async update(officeFolderuid: string, officeFolderEntity: OfficeFolder): Promise { + return this.officeFoldersRepository.update(officeFolderuid, officeFolderEntity); + } + + /** + * @description : Get a folder by uid + * @throws {Error} If folder cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.OfficeFoldersInclude) { + return this.officeFoldersRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a folder by uid + * @throws {Error} If folder cannot be get by uid + */ + public async getByUidWithOffice(uid: string) { + return this.officeFoldersRepository.findOneByUidWithOffice(uid); + } + + /** + * @description : Delete a folder + * @throws {Error} If document cannot be deleted + */ + public async delete(uid: string): Promise { + const officeFolderEntity = await this.officeFoldersRepository.findOneByUid(uid, { customers: true }); + if (!officeFolderEntity) throw new Error("office folder not found"); + const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); + + if (officeFolder.customers?.length) { + throw new Error("This folder is used by customers"); + } + return this.officeFoldersRepository.delete(uid); + } +} diff --git a/src/services/notary/OfficeRolesService/OfficeRolesService.ts b/src/services/notary/OfficeRolesService/OfficeRolesService.ts new file mode 100644 index 00000000..2c8f9f9e --- /dev/null +++ b/src/services/notary/OfficeRolesService/OfficeRolesService.ts @@ -0,0 +1,35 @@ +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; +import OfficeRolesRepository from "@Repositories/OfficeRolesRepository"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class OfficeRolesService extends BaseService { + constructor(private officeRoleRepository: OfficeRolesRepository) { + super(); + } + + /** + * @description : Get all officeRoles + * @throws {Error} If officeRoles cannot be get + */ + public get(query: Prisma.OfficeRolesFindManyArgs) { + return this.officeRoleRepository.findMany(query); + } + + /** + * @description : Get a officeRole by uid + * @throws {Error} If officeRole cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.OfficeRolesInclude) { + return this.officeRoleRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a officeRole by uid + * @throws {Error} If officeRole cannot be get by uid + */ + public getByUidWithOffice(uid: string) { + return this.officeRoleRepository.findOneByUidWithOffice(uid); + } +} diff --git a/src/services/notary/OfficesService/OfficesService.ts b/src/services/notary/OfficesService/OfficesService.ts new file mode 100644 index 00000000..af25083b --- /dev/null +++ b/src/services/notary/OfficesService/OfficesService.ts @@ -0,0 +1,27 @@ +import { Prisma } from "@prisma/client"; +import OfficesRepository from "@Repositories/OfficesRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class OfficesService extends BaseService { + constructor(private officeRepository: OfficesRepository) { + super(); + } + + /** + * @description : Get all offices + * @throws {Error} If offices cannot be get + */ + public async get(query: Prisma.OfficesFindManyArgs) { + return this.officeRepository.findMany(query); + } + + /** + * @description : Get a office by uid + * @throws {Error} If office cannot be get + */ + public async getByUid(uid: string, query?: Prisma.OfficesInclude) { + return this.officeRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/notary/RolesService/RolesService.ts b/src/services/notary/RolesService/RolesService.ts new file mode 100644 index 00000000..f59c26b1 --- /dev/null +++ b/src/services/notary/RolesService/RolesService.ts @@ -0,0 +1,28 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RolesRepository from "@Repositories/RolesRepository"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class RolesService extends BaseService { + constructor(private roleRepository: RolesRepository) { + super(); + } + + /** + * @description : Get all roles + * @throws {Error} If roles cannot be get + */ + public get(query: Prisma.RolesFindManyArgs) { + return this.roleRepository.findMany(query); + } + + /** + * @description : Get a role by uid + * @throws {Error} If role cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.RolesInclude) { + return this.roleRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/notary/RulesService/RulesService.ts b/src/services/notary/RulesService/RulesService.ts new file mode 100644 index 00000000..e52ad9bf --- /dev/null +++ b/src/services/notary/RulesService/RulesService.ts @@ -0,0 +1,28 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RulesRepository from "@Repositories/RulesRepository"; +import { Prisma } from "@prisma/client"; + +@Service() +export default class RulesService extends BaseService { + constructor(private ruleRepository: RulesRepository) { + super(); + } + + /** + * @description : Get all rules + * @throws {Error} If rules cannot be get + */ + public get(query: Prisma.RulesFindManyArgs) { + return this.ruleRepository.findMany(query); + } + + /** + * @description : Get a rule by uid + * @throws {Error} If rule cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.RulesInclude) { + return this.ruleRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/notary/UsersService/UsersService.ts b/src/services/notary/UsersService/UsersService.ts new file mode 100644 index 00000000..f6927c87 --- /dev/null +++ b/src/services/notary/UsersService/UsersService.ts @@ -0,0 +1,61 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import UsersRepository from "@Repositories/UsersRepository"; +import User from "le-coffre-resources/dist/Notary"; +import { Prisma, Users } from "@prisma/client"; + +@Service() +export default class UsersService extends BaseService { + constructor(private userRepository: UsersRepository) { + super(); + } + + /** + * @description : Get all users + * @throws {Error} If users cannot be get + */ + public get(query: Prisma.UsersFindManyArgs) { + return this.userRepository.findMany(query); + } + + /** + * @description : Create a user + * @throws {Error} If user couldn't be created + */ + public create(userEntity: User): Promise { + return this.userRepository.create(userEntity); + } + + /** + * @description : Modify a user + * @throws {Error} If user modification failed + */ + public update(uid: string, userEntity: User): Promise { + return this.userRepository.update(uid, userEntity); + } + + /** + * @description : Get a user by uid + * @throws {Error} If user cannot be get by uid + */ + public getByUid(uid: string, query?: Prisma.UsersInclude) { + return this.userRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a user by uid with office + * @throws {Error} If user cannot be get by uid + */ + public getByUidWithOffice(uid: string) { + return this.userRepository.findOneByUidWithOffice(uid); + } + + /** + * @description : Get a user by uid + * @throws {Error} If user cannot be get by uid + */ + public getByProvider(providerName: string, id: string) { + return this.userRepository.findOneByProvider(providerName, id); + } +} diff --git a/src/services/super-admin/CustomersService/CustomersService.ts b/src/services/super-admin/CustomersService/CustomersService.ts index 95c702ef..5501e1cb 100644 --- a/src/services/super-admin/CustomersService/CustomersService.ts +++ b/src/services/super-admin/CustomersService/CustomersService.ts @@ -38,7 +38,15 @@ export default class CustomersService extends BaseService { * @description : Get a customer by uid * @throws {Error} If customer cannot be get by uid */ - public async getByUid(uid: string, query?: any): Promise { + public async getByUid(uid: string, query?: Prisma.CustomersInclude): Promise { return this.customerRepository.findOneByUid(uid, query); } + + /** + * @description : Get a customer by contact uid + * @throws {Error} If customer cannot be get by contact uid + */ + public async getByContact(contactUid: string): Promise { + return this.customerRepository.findOneByContact(contactUid); + } } diff --git a/src/services/super-admin/DocumentsService/DocumentsService.ts b/src/services/super-admin/DocumentsService/DocumentsService.ts index c86fcb0f..e488b1be 100644 --- a/src/services/super-admin/DocumentsService/DocumentsService.ts +++ b/src/services/super-admin/DocumentsService/DocumentsService.ts @@ -61,7 +61,7 @@ export default class DocumentsService extends BaseService { * @description : Get a document by uid * @throws {Error} If document cannot be get by uid */ - public async getByUid(uid: string, query?: Prisma.DocumentsInclude) { + public async getByUid(uid: string, query?: Prisma.DocumentsInclude): Promise { return this.documentsRepository.findOneByUid(uid, query); }