diff --git a/src/app/api/admin/CustomersController.ts b/src/app/api/admin/CustomersController.ts index 90e429cf..0094a678 100644 --- a/src/app/api/admin/CustomersController.ts +++ b/src/app/api/admin/CustomersController.ts @@ -8,6 +8,7 @@ import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import roleHandler from "@App/middlewares/RolesHandler"; +import { Prisma } from "@prisma/client"; @Controller() @Service() @@ -28,6 +29,12 @@ export default class CustomersController extends ApiController { query = JSON.parse(req.query["q"] as string); } + const officeId: string = req.body.user.office_Id; + if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid; + if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid; + const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }}; + query.where = customerWhereInput; + //call service to get prisma entity const customersEntities = await this.customersService.get(query); diff --git a/src/app/api/customer/CustomersController.ts b/src/app/api/customer/CustomersController.ts deleted file mode 100644 index f958ab7e..00000000 --- a/src/app/api/customer/CustomersController.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Response, Request } from "express"; -import { Controller, Get } from "@ControllerPattern/index"; -import ApiController from "@Common/system/controller-pattern/ApiController"; -import CustomersService from "@Services/customer/CustomersService/CustomersService"; -import { Service } from "typedi"; -import Customer from "le-coffre-resources/dist/Customer"; -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/customer/customers") - protected async get(req: Request, response: Response) { - try { - //get query - let query; - if (req.query["q"]) { - query = JSON.parse(req.query["q"] as string); - } - - - //call service to get prisma entity - const 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 Get a specific customer by uid - */ - @Get("/api/v1/customer/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 query; - if (req.query["q"]) { - query = JSON.parse(req.query["q"] as string); - } - - const customerEntity = await this.customersService.getByUid(uid, query); - - 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/customer/OfficeFoldersController.ts b/src/app/api/customer/OfficeFoldersController.ts index e524b445..5905f1e9 100644 --- a/src/app/api/customer/OfficeFoldersController.ts +++ b/src/app/api/customer/OfficeFoldersController.ts @@ -5,6 +5,8 @@ import OfficeFoldersService from "@Services/customer/OfficeFoldersService/Office import { Service } from "typedi"; import { OfficeFolders, Prisma } from "@prisma/client"; import { OfficeFolder } from "le-coffre-resources/dist/Customer"; +import officeFolderHandler from "@App/middlewares/CustomerHandler/FolderHandler"; +import authHandler from "@App/middlewares/AuthHandler"; // import authHandler from "@App/middlewares/AuthHandler"; // import ruleHandler from "@App/middlewares/RulesHandler"; // import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; @@ -19,7 +21,7 @@ export default class OfficeFoldersController extends ApiController { /** * @description Get all folders */ - @Get("/api/v1/customer/folders") + @Get("/api/v1/customer/folders", [authHandler]) protected async get(req: Request, response: Response) { try { //get query @@ -28,37 +30,14 @@ export default class OfficeFoldersController extends ApiController { query = JSON.parse(req.query["q"] as string); } - - if (req.query["search"] && typeof req.query["search"] === "string") { - const filter = req.query["search"]; - query = { - where: { - OR: [ - { - name: { contains: filter, mode: "insensitive" }, - }, - { - folder_number: { contains: filter, mode: "insensitive" }, - }, - { - customers: { - some: { - contact: { - OR: [ - { first_name: { contains: filter, mode: "insensitive" } }, - { last_name: { contains: filter, mode: "insensitive" } }, - ], - }, - }, - }, - }, - ], - }, - }; + const customerId: string = req.body.user.customerId; + if(!customerId) { + this.httpBadRequest(response, "No customerId provided"); + return; } - const officeWhereInput: Prisma.OfficesWhereInput = {}; - if (!query.where) query.where = { office: officeWhereInput }; - query.where.office = officeWhereInput; + if(query.where?.customers) delete query.where.customers; + const officeFolderWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, customers: { some: { uid: customerId } }}; + query.where = officeFolderWhereInput; //call service to get prisma entity const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); @@ -79,7 +58,7 @@ export default class OfficeFoldersController extends ApiController { * @description Get a specific folder by uid * @returns IFolder */ - @Get("/api/v1/customer/folders/:uid") + @Get("/api/v1/customer/folders/:uid", [authHandler, officeFolderHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/api/notary/CustomersController.ts b/src/app/api/notary/CustomersController.ts index 59d070c7..af756207 100644 --- a/src/app/api/notary/CustomersController.ts +++ b/src/app/api/notary/CustomersController.ts @@ -7,6 +7,7 @@ import { Customer } from "le-coffre-resources/dist/Notary"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; +import { Prisma } from "@prisma/client"; @Controller() @Service() @@ -22,11 +23,17 @@ export default class CustomersController extends ApiController { protected async get(req: Request, response: Response) { try { //get query - let query; + let query: Prisma.CustomersFindManyArgs = {}; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } + const officeId: string = req.body.user.office_Id; + if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid; + if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid; + const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }}; + query.where = customerWhereInput; + //call service to get prisma entity const customersEntities = await this.customersService.get(query); diff --git a/src/app/api/super-admin/CustomersController.ts b/src/app/api/super-admin/CustomersController.ts index d1a15fd2..62a82548 100644 --- a/src/app/api/super-admin/CustomersController.ts +++ b/src/app/api/super-admin/CustomersController.ts @@ -8,6 +8,7 @@ import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import roleHandler from "@App/middlewares/RolesHandler"; +import { Prisma } from "@prisma/client"; @Controller() @Service() @@ -28,6 +29,12 @@ export default class CustomersController extends ApiController { query = JSON.parse(req.query["q"] as string); } + const officeId: string = req.body.user.office_Id; + if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid; + if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid; + const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }}; + query.where = customerWhereInput; + //call service to get prisma entity const customersEntities = await this.customersService.get(query); diff --git a/src/app/index.ts b/src/app/index.ts index fd205cf5..92065837 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -42,7 +42,6 @@ import FilesControllerCustomer from "./api/customer/FilesController"; import DocumentsControllerCustomer from "./api/customer/DocumentsController"; import OfficeFoldersController from "./api/customer/OfficeFoldersController"; import OfficeFolderAnchorsController from "./api/notary/OfficeFolderAnchorsController"; -import CustomersController from "./api/customer/CustomersController"; import AppointmentsController from "./api/super-admin/AppointmentsController"; import VotesController from "./api/super-admin/VotesController"; import LiveVoteController from "./api/super-admin/LiveVoteController"; @@ -105,7 +104,6 @@ export default { Container.get(DocumentsControllerCustomer); Container.get(OfficeFoldersController); Container.get(OfficeFolderAnchorsController); - Container.get(CustomersController); Container.get(UserNotificationController); Container.get(DocumentControllerId360); Container.get(CustomerControllerId360); diff --git a/src/app/middlewares/CustomerHandler/FolderHandler.ts b/src/app/middlewares/CustomerHandler/FolderHandler.ts new file mode 100644 index 00000000..aaf6ad54 --- /dev/null +++ b/src/app/middlewares/CustomerHandler/FolderHandler.ts @@ -0,0 +1,24 @@ +import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; +import OfficeFoldersService from "@Services/customer/OfficeFoldersService/OfficeFoldersService"; +import { NextFunction, Request, Response } from "express"; +import Container from "typedi"; + +export default async function officeFolderHandler(req: Request, response: Response, next: NextFunction) { + const customerId = req.body.user.customerId; + const uid = req.path && req.path.split("/")[5]; + + if (uid) { + const officeFolderService = Container.get(OfficeFoldersService); + const officeFolder = await officeFolderService.getByUidWithCustomers(uid); + if (!officeFolder) { + response.status(HttpCodes.NOT_FOUND).send("Folder not found"); + return; + } + if (!officeFolder.customers.find((customer) => customer.uid == customerId)) { + response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor"); + return; + } + } + + next(); +} diff --git a/src/common/repositories/OfficeFoldersRepository.ts b/src/common/repositories/OfficeFoldersRepository.ts index d391ef92..8eb5c7c9 100644 --- a/src/common/repositories/OfficeFoldersRepository.ts +++ b/src/common/repositories/OfficeFoldersRepository.ts @@ -109,6 +109,20 @@ export default class OfficeFoldersRepository extends BaseRepository { }); } + /** + * @description : Find one office folder + */ + public async findOneByUidWithCustomers(uid: string) { + return this.model.findUnique({ + where: { + uid: uid, + }, + include: { + customers: true, + } + }); + } + /** * @description : Find one office folder */ diff --git a/src/services/common/ContactService.ts b/src/services/common/ContactService.ts deleted file mode 100644 index e1cc6c19..00000000 --- a/src/services/common/ContactService.ts +++ /dev/null @@ -1,19 +0,0 @@ -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/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts index 68c9888e..8ec5f672 100644 --- a/src/services/customer/CustomersService/CustomersService.ts +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -1,12 +1,11 @@ import { Customers, Prisma } from "@prisma/client"; import CustomersRepository from "@Repositories/CustomersRepository"; -import ContactRepository from "@Repositories/ContactRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; @Service() export default class CustomersService extends BaseService { - constructor(private customerRepository: CustomersRepository, private contactRepository: ContactRepository) { + constructor(private customerRepository: CustomersRepository) { super(); } @@ -17,30 +16,4 @@ export default class CustomersService extends BaseService { public async get(query: Prisma.CustomersFindManyArgs): Promise { return this.customerRepository.findMany(query); } - - /** - * @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); - } - - /** - * @description : Get a customer by contact uid - * @throws {Error} If customer cannot be get by contact uid - */ - public async getByEmail(contactUid: string) { - return this.contactRepository.findOneByEmail(contactUid); - } - - -} +} \ No newline at end of file diff --git a/src/services/customer/OfficeFoldersService/OfficeFoldersService.ts b/src/services/customer/OfficeFoldersService/OfficeFoldersService.ts index 6afdede7..5095585a 100644 --- a/src/services/customer/OfficeFoldersService/OfficeFoldersService.ts +++ b/src/services/customer/OfficeFoldersService/OfficeFoldersService.ts @@ -26,4 +26,14 @@ export default class OfficeFoldersService extends BaseService { 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 getByUidWithCustomers(uid: string) { + return this.officeFoldersRepository.findOneByUidWithCustomers(uid); + } + + } diff --git a/src/services/notary/CustomersService/CustomersService.ts b/src/services/notary/CustomersService/CustomersService.ts index 72e6923d..3202d143 100644 --- a/src/services/notary/CustomersService/CustomersService.ts +++ b/src/services/notary/CustomersService/CustomersService.ts @@ -23,6 +23,14 @@ export default class CustomersService extends BaseService { * @throws {Error} If customer cannot be created */ public async create(customerEntity: Customer): Promise { + const customers = await this.get({ + where: { + contact: { + OR: [{ email: customerEntity.contact?.email }, { cell_phone_number: customerEntity.contact?.cell_phone_number }], + }, + }, + }); + if(customers[0]) return customers[0]; return this.customerRepository.create(customerEntity); }