fix document & file middleware for customers

This commit is contained in:
OxSaitama 2023-10-23 17:55:50 +02:00
parent aaa0d9ba21
commit 78c7364a81
2 changed files with 11 additions and 6 deletions

View File

@ -31,10 +31,15 @@ export default class FilesController extends ApiController {
if (req.query["q"]) { if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string); query = JSON.parse(req.query["q"] as string);
} }
const customerId: string = req.body.user.customerId; const email: string = req.body.user.email;
const customerWhereInput: Prisma.FilesWhereInput = { document: { depositor: { uid: customerId } } }; if (!email) {
this.httpBadRequest(response, "Missing customer email");
return;
}
if (query.where?.document?.depositor) delete query.where.document.depositor;
const customerWhereInput: Prisma.FilesWhereInput = { ...query.where, document: {depositor: { contact: { email: email } } }};
query.where = customerWhereInput; query.where = customerWhereInput;
if(query.include?.document) delete query.include.document; if (query.include?.document) delete query.include.document;
//call service to get prisma entity //call service to get prisma entity
const fileEntities = await this.filesService.get(query); const fileEntities = await this.filesService.get(query);

View File

@ -3,9 +3,9 @@ import DocumentsService from "@Services/customer/DocumentsService/DocumentsServi
import Document from "le-coffre-resources/dist/SuperAdmin/Document"; import Document from "le-coffre-resources/dist/SuperAdmin/Document";
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import Container from "typedi"; import Container from "typedi";
import ContactsService from "@Services/common/ContactService/ContactService";
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService"; import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import CustomersService from "@Services/super-admin/CustomersService/CustomersService";
export default async function documentHandler(req: Request, response: Response, next: NextFunction) { export default async function documentHandler(req: Request, response: Response, next: NextFunction) {
try { try {
@ -23,8 +23,8 @@ export default async function documentHandler(req: Request, response: Response,
} }
if (document?.depositor_uid != customerId) { if (document?.depositor_uid != customerId) {
const contactService = Container.get(ContactsService); const customerService = Container.get(CustomersService);
const customers = await contactService.getByEmail(customerEmail); const customers = await customerService.get({where: {contact: { email: customerEmail}}});
if (customers && !customers.find((customer) => customer.uid === document?.depositor_uid)) { if (customers && !customers.find((customer) => customer.uid === document?.depositor_uid)) {
response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor"); response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor");
return; return;