add validation errors

This commit is contained in:
OxSaitama 2023-10-12 19:45:26 +02:00
parent 13139cf167
commit 819b554b64
14 changed files with 139 additions and 115 deletions

View File

@ -53,7 +53,7 @@
"express": "^4.18.2", "express": "^4.18.2",
"fp-ts": "^2.16.1", "fp-ts": "^2.16.1",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.90", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.92",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"monocle-ts": "^2.3.13", "monocle-ts": "^2.3.13",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",

View File

@ -74,11 +74,6 @@ export default class DeedTypesController extends ApiController {
//validate deed type //validate deed type
await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false });
const doesExist = await this.deedTypesService.get({ where: { name: deedTypeEntity.name } });
if (doesExist.length > 0) {
this.httpBadRequest(response, "Deed type name already used");
return;
}
//call service to get prisma entity //call service to get prisma entity
const deedTypeEntityCreated = await this.deedTypesService.create(deedTypeEntity); const deedTypeEntityCreated = await this.deedTypesService.create(deedTypeEntity);

View File

@ -89,6 +89,7 @@ export default class OfficeFoldersController extends ApiController {
await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false });
//call service to get prisma entity //call service to get prisma entity
try {
const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, {
@ -96,6 +97,10 @@ export default class OfficeFoldersController extends ApiController {
}); });
//success //success
this.httpCreated(response, officeFolders); this.httpCreated(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;
@ -128,6 +133,7 @@ export default class OfficeFoldersController extends ApiController {
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
//call service to get prisma entity //call service to get prisma entity
try {
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
@ -137,6 +143,10 @@ export default class OfficeFoldersController extends ApiController {
//success //success
this.httpSuccess(response, officeFolders); this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;

View File

@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import DocumentTypesService from "@Services/notary/DocumentTypesService/DocumentTypesService"; import DocumentTypesService from "@Services/notary/DocumentTypesService/DocumentTypesService";
import { DocumentTypes, Prisma } from "@prisma/client"; import { DocumentTypes, Prisma } from "@prisma/client";
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
import { DocumentType } from "le-coffre-resources/dist/Notary"; import { DocumentType } from "le-coffre-resources/dist/Notary";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
@ -133,11 +132,15 @@ export default class DocumentTypesController extends ApiController {
const documentTypeEntity = await this.documentTypesService.getByUid(uid, query); const documentTypeEntity = await this.documentTypesService.getByUid(uid, query);
//Hydrate ressource with prisma entity if (!documentTypeEntity) {
const user = ObjectHydrate.hydrate<DocumentType>(new DocumentType(), documentTypeEntity!, { strategy: "excludeAll" }); this.httpNotFoundRequest(response, "document type not found");
return;
}
const documentType = DocumentType.hydrate<DocumentType>(documentTypeEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, user); this.httpSuccess(response, documentType);
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;

View File

@ -86,6 +86,7 @@ export default class OfficeFoldersController extends ApiController {
await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false });
//call service to get prisma entity //call service to get prisma entity
try {
const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, {
@ -93,6 +94,10 @@ export default class OfficeFoldersController extends ApiController {
}); });
//success //success
this.httpCreated(response, officeFolders); this.httpCreated(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;
@ -111,9 +116,7 @@ export default class OfficeFoldersController extends ApiController {
return; return;
} }
const officeFolderFound = await this.officeFoldersService.getByUid(uid, { const officeFolderFound = await this.officeFoldersService.getByUid(uid);
folder_anchor: true,
});
if (!officeFolderFound) { if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found"); this.httpNotFoundRequest(response, "office folder not found");
@ -121,17 +124,14 @@ export default class OfficeFoldersController extends ApiController {
} }
//init OfficeFolder resource with request body values //init OfficeFolder resource with request body values
const officefolderToUpdate = OfficeFolder.hydrate<OfficeFolder>(req.body); const officeFolderEntity = OfficeFolder.hydrate<OfficeFolder>(req.body);
const officeFolderFoundEntity = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound);
//validate folder //validate folder
await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false }); await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
if (officeFolderFoundEntity.folder_anchor?.status === "VERIFIED_ON_CHAIN") {
this.httpBadRequest(response, "Cannot update a verified folder");
return;
}
//call service to get prisma entity //call service to get prisma entity
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate); try {
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
@ -140,6 +140,10 @@ export default class OfficeFoldersController extends ApiController {
//success //success
this.httpSuccess(response, officeFolders); this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;

View File

@ -87,6 +87,7 @@ export default class OfficeFoldersController extends ApiController {
await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false });
//call service to get prisma entity //call service to get prisma entity
try {
const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, {
@ -94,6 +95,10 @@ export default class OfficeFoldersController extends ApiController {
}); });
//success //success
this.httpCreated(response, officeFolders); this.httpCreated(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;
@ -126,6 +131,7 @@ export default class OfficeFoldersController extends ApiController {
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
//call service to get prisma entity //call service to get prisma entity
try {
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
@ -135,6 +141,10 @@ export default class OfficeFoldersController extends ApiController {
//success //success
this.httpSuccess(response, officeFolders); this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;

View File

@ -11,12 +11,24 @@ export default async function deedTypeHandler(req: Request, response: Response,
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const documentTypes: DocumentType[] = req.body.document_types; const documentTypes: DocumentType[] = req.body.document_types;
const office = req.body.office; const office = req.body.office;
const name = req.body.name;
if (office && office.uid != officeId) { if (office && office.uid != officeId) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office"); response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
return; return;
} }
if (name) {
const deedTypeService = Container.get(DeedTypesService);
const deedType = await deedTypeService.get({
where: { AND: [{ name: { equals: name, mode: "insensitive" } }, { office: { uid: officeId } }] },
});
if (deedType[0] && (!uid || deedType[0].uid != uid)) {
response.status(HttpCodes.VALIDATION_ERROR).send([{ property: "name", constraints: { name: "Nom d'acte déjà utilisé" } }]);
return;
}
}
if (uid) { if (uid) {
const deedTypeService = Container.get(DeedTypesService); const deedTypeService = Container.get(DeedTypesService);
const deedType = await deedTypeService.getByUidWithOffice(uid!); const deedType = await deedTypeService.getByUidWithOffice(uid!);

View File

@ -8,12 +8,24 @@ export default async function documentTypeHandler(req: Request, response: Respon
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const office = req.body.office; const office = req.body.office;
const name = req.body.name;
if (office && office.uid != officeId) { if (office && office.uid != officeId) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office"); response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
return; return;
} }
if (name) {
const documentTypeService = Container.get(DocumentTypesService);
const documentType = await documentTypeService.get({
where: { AND: [{ name: { equals: name, mode: "insensitive" } }, { office: { uid: officeId } }] },
});
if (documentType[0] && (!uid || documentType[0].uid != uid)) {
response.status(HttpCodes.VALIDATION_ERROR).send([{ property: "name", constraints: { name: "Nom de document déjà utilisé" } }]);
return;
}
}
if (uid) { if (uid) {
const documentTypeService = Container.get(DocumentTypesService); const documentTypeService = Container.get(DocumentTypesService);
const documentType = await documentTypeService.getByUidWithOffice(uid!); const documentType = await documentTypeService.getByUidWithOffice(uid!);

View File

@ -10,14 +10,25 @@ export default async function folderHandler(req: Request, response: Response, ne
const userId = req.body.user.userId; const userId = req.body.user.userId;
let uid = req.path && req.path.split("/")[5]; let uid = req.path && req.path.split("/")[5];
const office = req.body.office; const office = req.body.office;
const officeFolderNumber = req.body.folder_number;
const deed = req.body.deed; const deed = req.body.deed;
const folderNumber = req.body.folder_number;
if (office && office.uid != officeId) { if (office && office.uid != officeId) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office"); response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
return; return;
} }
if(folderNumber) {
const officeFolderService = Container.get(OfficeFoldersService);
const sameFolderNumber = await officeFolderService.get({
where: { AND: [{ folder_number: folderNumber }, { office_uid: officeId }] },
});
if(sameFolderNumber[0] && (!uid || uid != sameFolderNumber[0]?.uid)) {
response.status(HttpCodes.VALIDATION_ERROR).send([{ property: "folder_number", constraints: { folder_number: "Numéro de dossier déjà utilisé" } }]);
return;
}
}
if (deed && deed.deed_type) { if (deed && deed.deed_type) {
const deedTypeService = Container.get(DeedTypesService); const deedTypeService = Container.get(DeedTypesService);
const deedTypeWithOffice = await deedTypeService.getByUidWithOffice(deed.deed_type.uid!); const deedTypeWithOffice = await deedTypeService.getByUidWithOffice(deed.deed_type.uid!);
@ -25,20 +36,12 @@ export default async function folderHandler(req: Request, response: Response, ne
response.status(HttpCodes.NOT_FOUND).send("Deed type not found"); response.status(HttpCodes.NOT_FOUND).send("Deed type not found");
return; return;
} }
if (deedTypeWithOffice.office.uid != officeId) { if(deedTypeWithOffice.archived_at) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this deed type"); response.status(HttpCodes.FORBIDDEN).send("Deed type is archived");
return; return;
} }
} if (deedTypeWithOffice.office.uid != officeId) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this deed type");
const officeFolderService = Container.get(OfficeFoldersService);
if (officeFolderNumber && req.method == "POST") {
const officeFoldersWithSameNumber = await officeFolderService.get({
where: { folder_number: officeFolderNumber, office: { uid: officeId } },
});
if (officeFoldersWithSameNumber.length) {
response.status(HttpCodes.BAD_REQUEST).send("Office number already used");
return; return;
} }
} }
@ -47,6 +50,8 @@ export default async function folderHandler(req: Request, response: Response, ne
if(uid === "download") { if(uid === "download") {
uid = req.path && req.path.split("/")[6]; uid = req.path && req.path.split("/")[6];
} }
const officeFolderService = Container.get(OfficeFoldersService);
const officeFolder = await officeFolderService.getByUidWithStakeholders(uid!); const officeFolder = await officeFolderService.getByUidWithStakeholders(uid!);
if (!officeFolder) { if (!officeFolder) {

View File

@ -35,8 +35,12 @@ export default class OfficeFoldersRepository extends BaseRepository {
description: officeFolder.description, description: officeFolder.description,
status: EFolderStatus.LIVE, status: EFolderStatus.LIVE,
deed: { deed: {
create: {
deed_type: {
connect: { connect: {
uid: officeFolder.deed?.uid, uid: officeFolder.deed?.deed_type?.uid,
},
},
}, },
}, },
office: { office: {
@ -121,7 +125,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
}, },
include: { include: {
customers: true, customers: true,
} },
}); });
} }

View File

@ -3,16 +3,12 @@ import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { OfficeFolder } from "le-coffre-resources/dist/Admin"; import { OfficeFolder } from "le-coffre-resources/dist/Admin";
import { Service } from "typedi"; import { Service } from "typedi";
import DeedTypesService from "../DeedTypesService/DeedTypesService";
import DeedsRepository from "@Repositories/DeedsRepository";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
@Service() @Service()
export default class OfficeFoldersService extends BaseService { export default class OfficeFoldersService extends BaseService {
constructor( constructor(
private officeFoldersRepository: OfficeFoldersRepository, private officeFoldersRepository: OfficeFoldersRepository,
private deedTypeService: DeedTypesService,
private deedRepository: DeedsRepository,
) { ) {
super(); super();
} }
@ -30,11 +26,6 @@ export default class OfficeFoldersService extends BaseService {
* @throws {Error} If folder cannot be created * @throws {Error} If folder cannot be created
*/ */
public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> { public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
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); return this.officeFoldersRepository.create(officeFolderEntity);
} }

View File

@ -3,16 +3,12 @@ import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { OfficeFolder } from "le-coffre-resources/dist/Notary"; import { OfficeFolder } from "le-coffre-resources/dist/Notary";
import { Service } from "typedi"; import { Service } from "typedi";
import DeedTypesService from "../DeedTypesService/DeedTypesService";
import DeedsRepository from "@Repositories/DeedsRepository";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
@Service() @Service()
export default class OfficeFoldersService extends BaseService { export default class OfficeFoldersService extends BaseService {
constructor( constructor(
private officeFoldersRepository: OfficeFoldersRepository, private officeFoldersRepository: OfficeFoldersRepository,
private deedTypeService: DeedTypesService,
private deedRepository: DeedsRepository,
) { ) {
super(); super();
} }
@ -30,11 +26,6 @@ export default class OfficeFoldersService extends BaseService {
* @throws {Error} If folder cannot be created * @throws {Error} If folder cannot be created
*/ */
public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> { public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
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); return this.officeFoldersRepository.create(officeFolderEntity);
} }

View File

@ -3,16 +3,12 @@ import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import { Service } from "typedi"; import { Service } from "typedi";
import DeedTypesService from "../DeedTypesService/DeedTypesService";
import DeedsRepository from "@Repositories/DeedsRepository";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
@Service() @Service()
export default class OfficeFoldersService extends BaseService { export default class OfficeFoldersService extends BaseService {
constructor( constructor(
private officeFoldersRepository: OfficeFoldersRepository, private officeFoldersRepository: OfficeFoldersRepository,
private deedTypeService: DeedTypesService,
private deedRepository: DeedsRepository,
) { ) {
super(); super();
} }
@ -30,11 +26,6 @@ export default class OfficeFoldersService extends BaseService {
* @throws {Error} If folder cannot be created * @throws {Error} If folder cannot be created
*/ */
public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> { public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
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); return this.officeFoldersRepository.create(officeFolderEntity);
} }

View File

@ -18,15 +18,11 @@ import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import OfficeFolderService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService"; import OfficeFolderService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { initCustomers, initDeedType, initDocumentType, initOffice, initUsers } from "@Test/config/Init"; import { initCustomers, initDeedType, initDocumentType, initOffice, initUsers } from "@Test/config/Init";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService";
import DeedsRepository from "@Repositories/DeedsRepository";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const OfficeFolderServiceTest = new OfficeFolderService( const OfficeFolderServiceTest = new OfficeFolderService(
Container.get(OfficeFoldersRepository), Container.get(OfficeFoldersRepository)
Container.get(DeedTypesService),
Container.get(DeedsRepository),
); );
beforeAll(async () => { beforeAll(async () => {