45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
|
|
import DeedsService from "@Services/super-admin/DeedsService/DeedsService";
|
|
import { DocumentType } from "le-coffre-resources/dist/SuperAdmin";
|
|
import { NextFunction, Request, Response } from "express";
|
|
import Container from "typedi";
|
|
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
|
|
|
|
export default async function deedHandler(req: Request, response: Response, next: NextFunction) {
|
|
const officeId = req.body.user.office_Id;
|
|
const uid = req.path && req.path.split("/")[5];
|
|
const documentTypes: DocumentType[] = req.body.document_types;
|
|
|
|
if (uid) {
|
|
const deedService = Container.get(DeedsService);
|
|
const deed = await deedService.getByUidWithOffice(uid);
|
|
|
|
if (!deed) {
|
|
response.status(HttpCodes.NOT_FOUND).send("Deed not found");
|
|
return;
|
|
}
|
|
|
|
if (deed.deed_type.office.uid != officeId) {
|
|
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (documentTypes) {
|
|
const documentTypeService = Container.get(DocumentTypesService);
|
|
documentTypes.forEach(async (documentType) => {
|
|
const deedTypeWithOffice = await documentTypeService.getByUidWithOffice(documentType.uid!);
|
|
if (!deedTypeWithOffice) {
|
|
response.status(HttpCodes.NOT_FOUND).send("Deed type not found");
|
|
return;
|
|
}
|
|
if (deedTypeWithOffice.office?.uid != officeId) {
|
|
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
next();
|
|
}
|