diff --git a/package.json b/package.json index 8f6c3f38..45a5412f 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,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.47", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.49", "module-alias": "^2.2.2", "multer": "^1.4.5-lts.1", "next": "^13.1.5", diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index ebfccb15..85b64290 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -25,15 +25,15 @@ export default class DocumentsController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: Documents[] = await this.documentsService.get(query); + const documentEntities: Documents[] = await this.documentsService.get(query); //Hydrate ressource with prisma entity - const documents = Document.map(Document, prismaEntity, { strategy: "excludeAll" }); + const documents = Document.hydrateArray(Document, documentEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, documents); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -52,17 +52,17 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["createDocument"] }); //call service to get prisma entity - const prismaEntityCreated = await this.documentsService.create(documentEntity); + const documentEntityCreated = await this.documentsService.create(documentEntity); //Hydrate ressource with prisma entity - const documentEntityCreated = Document.hydrate(prismaEntityCreated, { + const document = Document.hydrate(documentEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, documentEntityCreated); + this.httpSuccess(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -75,7 +75,15 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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 @@ -86,15 +94,15 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["createDocument"] }); //call service to get prisma entity - const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); //Hydrate ressource with prisma entity - const document = Document.hydrate(prismaEntityUpdated, { strategy: "excludeAll" }); + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); //success this.httpSuccess(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -107,19 +115,27 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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 documentEntity: Documents = await this.documentsService.delete(uid); + const documentEntityDeleted: Documents = await this.documentsService.delete(uid); //Hydrate ressource with prisma entity - const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + const document = Document.hydrate(documentEntityDeleted, { strategy: "excludeAll" }); //success this.httpSuccess(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -132,10 +148,11 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let documentEntity: Documents; + let documentEntity: Documents | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -145,13 +162,18 @@ export default class DocumentsController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/idnot-user/UserInfoController.ts b/src/app/api/idnot-user/UserInfoController.ts index f3854a1d..afd5bf22 100644 --- a/src/app/api/idnot-user/UserInfoController.ts +++ b/src/app/api/idnot-user/UserInfoController.ts @@ -24,7 +24,7 @@ import AuthService from "@Services/private-services/AuthService/AuthService"; //success this.httpSuccess(response, user); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/CustomersController.ts b/src/app/api/super-admin/CustomersController.ts index d283cb95..96e1b7b6 100644 --- a/src/app/api/super-admin/CustomersController.ts +++ b/src/app/api/super-admin/CustomersController.ts @@ -24,15 +24,15 @@ export default class CustomersController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const customersEntity = await this.customersService.get(query); + const customersEntities = await this.customersService.get(query); //Hydrate ressource with prisma entity - const customers = Customer.map(Customer, customersEntity, { strategy: "excludeAll" }); + const customers = Customer.hydrateArray(Customer, customersEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, customers); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -49,16 +49,16 @@ export default class CustomersController extends ApiController { await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.customersService.create(customerEntity); + const customerEntityCreated = await this.customersService.create(customerEntity); //Hydrate ressource with prisma entity - const customerEntityCreated = Customer.hydrate(prismaEntityCreated, { + const customer = Customer.hydrate(customerEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, customerEntityCreated); + this.httpCreated(response, customer); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -71,8 +71,17 @@ export default class CustomersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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); @@ -80,17 +89,17 @@ export default class CustomersController extends ApiController { await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityUpdated = await this.customersService.update(uid, customerEntity); + const customerEntityUpdated = await this.customersService.update(uid, customerEntity); //Hydrate ressource with prisma entity - const customerEntityUpdated = Customer.hydrate(prismaEntityUpdated, { + const customer = Customer.hydrate(customerEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, customerEntityUpdated); + this.httpSuccess(response, customer); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -103,10 +112,11 @@ export default class CustomersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let customerEntity: Customers; + let customerEntity: Customers | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -116,13 +126,18 @@ export default class CustomersController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/DeedTypesController.ts b/src/app/api/super-admin/DeedTypesController.ts index aba5721c..974a0d88 100644 --- a/src/app/api/super-admin/DeedTypesController.ts +++ b/src/app/api/super-admin/DeedTypesController.ts @@ -24,15 +24,15 @@ export default class DeedTypesController extends ApiController { //get query const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: DeedTypes[] = await this.deedTypesService.get(query); + const deedTypeEntities: DeedTypes[] = await this.deedTypesService.get(query); //Hydrate ressource with prisma entity - const DeedTypes = DeedType.map(DeedType, prismaEntity, { strategy: "excludeAll" }); + const DeedTypes = DeedType.hydrateArray(DeedType, deedTypeEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, DeedTypes); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -51,17 +51,17 @@ export default class DeedTypesController extends ApiController { await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.deedTypesService.create(deedTypeEntity); + const deedTypeEntityCreated = await this.deedTypesService.create(deedTypeEntity); //Hydrate ressource with prisma entity - const deedTypeEntityCreated = DeedType.hydrate(prismaEntityCreated, { + const deedType = DeedType.hydrate(deedTypeEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, deedTypeEntityCreated); + this.httpCreated(response, deedType); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -75,8 +75,17 @@ export default class DeedTypesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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); @@ -84,17 +93,17 @@ export default class DeedTypesController extends ApiController { await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] }); //call service to get prisma entity - const prismaEntityUpdated = await this.deedTypesService.update(uid, deedTypeEntity); + const deedTypeEntityUpdated = await this.deedTypesService.update(uid, deedTypeEntity); //Hydrate ressource with prisma entity - const deedTypeEntityUpdated = DeedType.hydrate(prismaEntityUpdated, { + const deedType = DeedType.hydrate(deedTypeEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, deedTypeEntityUpdated); + this.httpSuccess(response, deedType); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -108,10 +117,11 @@ export default class DeedTypesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let deedTypeEntity: DeedTypes; + let deedTypeEntity: DeedTypes | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -121,13 +131,18 @@ export default class DeedTypesController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/DeedsController.ts b/src/app/api/super-admin/DeedsController.ts index f8ff9da7..d03d3ee8 100644 --- a/src/app/api/super-admin/DeedsController.ts +++ b/src/app/api/super-admin/DeedsController.ts @@ -24,15 +24,15 @@ export default class DeedsController extends ApiController { //get query const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: Deeds[] = await this.deedsService.get(query); + const deedEntities: Deeds[] = await this.deedsService.get(query); //Hydrate ressource with prisma entity - const deeds = Deed.map(Deed, prismaEntity, { strategy: "excludeAll" }); + const deeds = Deed.hydrateArray(Deed, deedEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, deeds); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -46,10 +46,11 @@ export default class DeedsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let deedEntity: Deeds; + let deedEntity: Deeds | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -59,13 +60,18 @@ export default class DeedsController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -78,8 +84,17 @@ export default class DeedsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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); @@ -87,17 +102,17 @@ export default class DeedsController extends ApiController { await validateOrReject(deedEntity, { groups: ["updateDeed"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityUpdated = await this.deedsService.update(uid, deedEntity); + const deedEntityUpdated = await this.deedsService.update(uid, deedEntity); //Hydrate ressource with prisma entity - const deedEntityUpdated = Deed.hydrate(prismaEntityUpdated, { + const deed = Deed.hydrate(deedEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, deedEntityUpdated); + this.httpSuccess(response, deed); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/DocumentTypesController.ts b/src/app/api/super-admin/DocumentTypesController.ts index 3ef79a93..d03c37ae 100644 --- a/src/app/api/super-admin/DocumentTypesController.ts +++ b/src/app/api/super-admin/DocumentTypesController.ts @@ -25,17 +25,17 @@ export default class DocumentTypesController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: DocumentTypes[] = await this.documentTypesService.get(query); + const documentTypeEntities: DocumentTypes[] = await this.documentTypesService.get(query); //Hydrate ressource with prisma entity - const documentTypes = DocumentType.map(DocumentType, prismaEntity, { + const documentTypes = DocumentType.hydrateArray(DocumentType, documentTypeEntities, { strategy: "excludeAll", }); //success this.httpSuccess(response, documentTypes); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -51,15 +51,15 @@ export default class DocumentTypesController extends ApiController { //validate user await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.documentTypesService.create(documentTypeEntity); + const documentTypeEntityCreated = await this.documentTypesService.create(documentTypeEntity); //Hydrate ressource with prisma entity - const userEntityCreated = DocumentType.hydrate(prismaEntityCreated, { + const userEntityCreated = DocumentType.hydrate(documentTypeEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, userEntityCreated); + this.httpCreated(response, userEntityCreated); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -72,7 +72,15 @@ export default class DocumentTypesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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); @@ -81,17 +89,17 @@ export default class DocumentTypesController extends ApiController { await validateOrReject(documentTypeEntity, { groups: ["update"] }); //call service to get prisma entity - const prismaEntityUpdated = await this.documentTypesService.update(uid, documentTypeEntity); + const documentTypeEntityUpdated = await this.documentTypesService.update(uid, documentTypeEntity); //Hydrate ressource with prisma entity - const documentTypeEntityUpdated = DocumentType.hydrate(prismaEntityUpdated, { + const documentType = DocumentType.hydrate(documentTypeEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, documentTypeEntityUpdated); + this.httpSuccess(response, documentType); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -104,10 +112,11 @@ export default class DocumentTypesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let documentTypeEntity: DocumentTypes; + let documentTypeEntity: DocumentTypes | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -117,13 +126,18 @@ export default class DocumentTypesController extends ApiController { documentTypeEntity = await this.documentTypesService.getByUid(uid); } + if (!documentTypeEntity) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + //Hydrate ressource with prisma entity const user = ObjectHydrate.hydrate(new DocumentType(), documentTypeEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, user); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/DocumentsController.ts b/src/app/api/super-admin/DocumentsController.ts index ae60a9ab..59ee08e5 100644 --- a/src/app/api/super-admin/DocumentsController.ts +++ b/src/app/api/super-admin/DocumentsController.ts @@ -25,15 +25,15 @@ export default class DocumentsController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: Documents[] = await this.documentsService.get(query); + const documentEntities: Documents[] = await this.documentsService.get(query); //Hydrate ressource with prisma entity - const documents = Document.map(Document, prismaEntity, { strategy: "excludeAll" }); + const documents = Document.hydrateArray(Document, documentEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, documents); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -52,17 +52,17 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.documentsService.create(documentEntity); + const documentEntityCreated = await this.documentsService.create(documentEntity); //Hydrate ressource with prisma entity - const documentEntityCreated = Document.hydrate(prismaEntityCreated, { + const document = Document.hydrate(documentEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, documentEntityCreated); + this.httpCreated(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -75,8 +75,16 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); - } + 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); @@ -85,15 +93,15 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["updateDocument"] }); //call service to get prisma entity - const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); //Hydrate ressource with prisma entity - const document = Document.hydrate(prismaEntityUpdated, { strategy: "excludeAll" }); + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); //success this.httpSuccess(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -106,7 +114,15 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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 @@ -118,7 +134,7 @@ export default class DocumentsController extends ApiController { //success this.httpSuccess(response, document); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -131,10 +147,11 @@ export default class DocumentsController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let documentEntity: Documents; + let documentEntity: Documents | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -144,13 +161,18 @@ export default class DocumentsController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/FilesController.ts b/src/app/api/super-admin/FilesController.ts index b3be77f3..745bac2a 100644 --- a/src/app/api/super-admin/FilesController.ts +++ b/src/app/api/super-admin/FilesController.ts @@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import FilesService from "@Services/private-services/FilesService/FilesService"; import { Files } from "@prisma/client"; -import { File, Document } from "le-coffre-resources/dist/SuperAdmin"; +import { File } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; @@ -26,10 +26,10 @@ export default class FilesController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity = await this.filesService.get(query); + const fileEntities = await this.filesService.get(query); //Hydrate ressource with prisma entity - const files = File.map(File, prismaEntity, { strategy: "excludeAll" }); + const files = File.hydrateArray(File, fileEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, files); @@ -53,7 +53,7 @@ export default class FilesController extends ApiController { const fileInfo = await this.filesService.download(uid); if (!fileInfo) { - this.httpNotFoundRequest(response); + this.httpNotFoundRequest(response, "file not found"); return; } @@ -85,19 +85,25 @@ export default class FilesController extends ApiController { await validateOrReject(fileEntity, { groups: ["createFile"] }); //call service to get prisma entity - const prismaEntityCreated = await this.filesService.create(fileEntity, req.file); + const fileEntityCreated = await this.filesService.create(fileEntity, req.file); - const document: Document = await this.documentService.getByUid(fileEntity.document!.uid!); + const document = await this.documentService.getByUid(fileEntity.document!.uid!); + + if(!document){ + this.httpNotFoundRequest(response, "document not found"); + return; + } + document.document_status = "DEPOSITED"; await this.documentService.update(document.uid!, document); //Hydrate ressource with prisma entity - const fileEntityCreated = File.hydrate(prismaEntityCreated, { + const fileEntityHydrated = File.hydrate(fileEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, fileEntityCreated); + this.httpCreated(response, fileEntityHydrated); } catch (error) { this.httpBadRequest(response, error); return; @@ -115,6 +121,13 @@ export default class FilesController extends ApiController { 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); @@ -122,10 +135,10 @@ export default class FilesController extends ApiController { await validateOrReject(fileEntity, { groups: ["updateFile"] }); //call service to get prisma entity - const prismaEntityUpdated: Files = await this.filesService.update(uid, fileEntity); + const fileEntityUpdated: Files = await this.filesService.update(uid, fileEntity); //Hydrate ressource with prisma entity - const file = File.hydrate(prismaEntityUpdated, { strategy: "excludeAll" }); + const file = File.hydrate(fileEntityUpdated, { strategy: "excludeAll" }); //success this.httpSuccess(response, file); @@ -147,6 +160,13 @@ export default class FilesController extends ApiController { 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); @@ -154,6 +174,7 @@ export default class FilesController extends ApiController { this.httpNotFoundRequest(response, "file not found"); return; } + //Hydrate ressource with prisma entity const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); diff --git a/src/app/api/super-admin/OfficeFoldersController.ts b/src/app/api/super-admin/OfficeFoldersController.ts index 989ea9b5..4c642835 100644 --- a/src/app/api/super-admin/OfficeFoldersController.ts +++ b/src/app/api/super-admin/OfficeFoldersController.ts @@ -23,15 +23,15 @@ export default class OfficeFoldersController extends ApiController { //get query const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const prismaEntity: OfficeFolders[] = await this.officeFoldersService.get(query); + const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); //Hydrate ressource with prisma entity - const officeFolders = OfficeFolder.map(OfficeFolder, prismaEntity, { + const officeFolders = OfficeFolder.hydrateArray(OfficeFolder, officeFolderEntities, { strategy: "excludeAll", }); //success this.httpSuccess(response, officeFolders); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -48,15 +48,15 @@ export default class OfficeFoldersController extends ApiController { //validate folder await validateOrReject(officeFolderEntity, { groups: ["createFolder"] , forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.officeFoldersService.create(officeFolderEntity); + const officeFolderEntityCreated = await this.officeFoldersService.create(officeFolderEntity); //Hydrate ressource with prisma entity - const officeFolderEntityCreated = OfficeFolder.hydrate(prismaEntityCreated, { + const officeFolders = OfficeFolder.hydrate(officeFolderEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, officeFolderEntityCreated); + this.httpCreated(response, officeFolders); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -69,8 +69,17 @@ export default class OfficeFoldersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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); @@ -78,17 +87,17 @@ export default class OfficeFoldersController extends ApiController { await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); //Hydrate ressource with prisma entity - const officeFolderEntityUpdated = OfficeFolder.hydrate(prismaEntityUpdated, { + const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, officeFolderEntityUpdated); + this.httpSuccess(response, officeFolders); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -102,10 +111,11 @@ export default class OfficeFoldersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let officeFolderEntity: OfficeFolders; + let officeFolderEntity: OfficeFolders | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -115,13 +125,18 @@ export default class OfficeFoldersController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } this.httpSuccess(response, await this.officeFoldersService.getByUid("uid")); @@ -135,7 +150,15 @@ export default class OfficeFoldersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + 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 @@ -147,7 +170,7 @@ export default class OfficeFoldersController extends ApiController { //success this.httpSuccess(response, officeFolder); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/OfficesController.ts b/src/app/api/super-admin/OfficesController.ts index 8520fe49..820ccade 100644 --- a/src/app/api/super-admin/OfficesController.ts +++ b/src/app/api/super-admin/OfficesController.ts @@ -22,13 +22,13 @@ export default class OfficesController extends ApiController { //get query const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const officesEntity: Offices[] = await this.officesService.get(query); + const officesEntities: Offices[] = await this.officesService.get(query); //Hydrate ressource with prisma entity - const offices = OfficeResource.map(OfficeResource, officesEntity, { strategy: "excludeAll" }); + const offices = OfficeResource.hydrateArray(OfficeResource, officesEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, offices); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -43,15 +43,15 @@ export default class OfficesController extends ApiController { //validate user await validateOrReject(officeEntity, { groups: ["createOffice"], forbidUnknownValues: false }); //call service to get prisma entity - const prismaEntityCreated = await this.officesService.create(officeEntity); + const officeEntityCreated = await this.officesService.create(officeEntity); //Hydrate ressource with prisma entity - const officeEntityCreated = OfficeResource.hydrate(prismaEntityCreated, { + const office = OfficeResource.hydrate(officeEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, officeEntityCreated); + this.httpCreated(response, office); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -63,22 +63,30 @@ export default class OfficesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFound = await this.officesService.getByUid(uid); + + if (!officeFound) { + this.httpNotFoundRequest(response, "office not found"); + return; } //init IUser resource with request body values const officeEntity = OfficeResource.hydrate(req.body); //validate user await validateOrReject(officeEntity, { groups: ["update"] }); //call service to get prisma entity - const prismaEntityUpdated = await this.officesService.update(uid, officeEntity); + const officeEntityUpdated = await this.officesService.update(uid, officeEntity); //Hydrate ressource with prisma entity - const officeEntityUpdated = OfficeResource.hydrate(prismaEntityUpdated, { + const office = OfficeResource.hydrate(officeEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, officeEntityUpdated); + this.httpSuccess(response, office); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -90,9 +98,10 @@ export default class OfficesController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let officeEntity: Offices; + let officeEntity: Offices | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -101,12 +110,18 @@ export default class OfficesController extends ApiController { //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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index aee392e8..7ba6f437 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -24,15 +24,15 @@ export default class UsersController extends ApiController { const query = JSON.parse(req.query["q"] as string); //call service to get prisma entity - const usersEntity = await this.usersService.get(query); + const usersEntities = await this.usersService.get(query); //Hydrate ressource with prisma entity - const users = User.map(User, usersEntity, { strategy: "excludeAll" }); + const users = User.hydrateArray(User, usersEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, users); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -50,17 +50,17 @@ export default class UsersController extends ApiController { await validateOrReject(userEntity, { groups: ["createUser"] }); //call service to get prisma entity - const prismaEntityCreated = await this.usersService.create(userEntity); + const userEntityCreated = await this.usersService.create(userEntity); //Hydrate ressource with prisma entity - const userEntityCreated = User.hydrate(prismaEntityCreated, { + const user = User.hydrate(userEntityCreated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, userEntityCreated); + this.httpCreated(response, user); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -73,8 +73,17 @@ export default class UsersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } + + const userFound = await this.usersService.getByUid(uid); + + if (!userFound) { + this.httpNotFoundRequest(response, "user not found"); + return; + } + //init IUser resource with request body values const userEntity = User.hydrate(req.body); @@ -82,17 +91,17 @@ export default class UsersController extends ApiController { await validateOrReject(userEntity, { groups: ["update"] }); //call service to get prisma entity - const prismaEntityUpdated = await this.usersService.update(uid, userEntity); + const userEntityUpdated = await this.usersService.update(uid, userEntity); //Hydrate ressource with prisma entity - const userEntityUpdated = User.hydrate(prismaEntityUpdated, { + const user = User.hydrate(userEntityUpdated, { strategy: "excludeAll", }); //success - this.httpSuccess(response, userEntityUpdated); + this.httpSuccess(response, user); } catch (error) { - this.httpBadRequest(response, error); + this.httpInternalError(response); return; } } @@ -105,9 +114,10 @@ export default class UsersController extends ApiController { try { const uid = req.params["uid"]; if (!uid) { - throw new Error("No uid provided"); + this.httpBadRequest(response, "No uid provided"); + return; } - let userEntity: Users; + let userEntity: Users | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); @@ -117,13 +127,18 @@ export default class UsersController extends ApiController { 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.httpBadRequest(response, error); + this.httpInternalError(response); return; } } diff --git a/src/common/repositories/AddressesRepository.ts b/src/common/repositories/AddressesRepository.ts index 7bd53e84..268d9059 100644 --- a/src/common/repositories/AddressesRepository.ts +++ b/src/common/repositories/AddressesRepository.ts @@ -26,17 +26,13 @@ export default class AddressesRepository extends BaseRepository { /** * @description : Find one address */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const addressEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!addressEntity) { - throw new Error("Address not found"); - } - return addressEntity; } } diff --git a/src/common/repositories/ContactsRepository.ts b/src/common/repositories/ContactsRepository.ts index b302d1f6..26836b4a 100644 --- a/src/common/repositories/ContactsRepository.ts +++ b/src/common/repositories/ContactsRepository.ts @@ -26,17 +26,13 @@ export default class ContactsRepository extends BaseRepository { /** * @description : Find unique contact */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const contactEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!contactEntity) { - throw new Error("contact not found"); - } - return contactEntity; } } diff --git a/src/common/repositories/CustomersRepository.ts b/src/common/repositories/CustomersRepository.ts index 4f846795..c1b48c22 100644 --- a/src/common/repositories/CustomersRepository.ts +++ b/src/common/repositories/CustomersRepository.ts @@ -98,7 +98,7 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Find unique customer */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.CustomersFindUniqueArgs = { where: { uid: uid, @@ -108,9 +108,6 @@ export default class CustomersRepository extends BaseRepository { findOneArgs.include = query; } const customerEntity = await this.model.findUnique(findOneArgs); - if (!customerEntity) { - throw new Error("Customer not found"); - } return customerEntity; } diff --git a/src/common/repositories/DeedTypesHasDocumentTypesRepository.ts b/src/common/repositories/DeedTypesHasDocumentTypesRepository.ts index 53cb40a7..e724d9d8 100644 --- a/src/common/repositories/DeedTypesHasDocumentTypesRepository.ts +++ b/src/common/repositories/DeedTypesHasDocumentTypesRepository.ts @@ -26,17 +26,13 @@ export default class DeedTypeHasDocumentTypesRepository extends BaseRepository { /** * @description : Find unique relation between deed type and a document type */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const deedTypeHasDoculmentTypesEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!deedTypeHasDoculmentTypesEntity) { - throw new Error("deed type not found"); - } - return deedTypeHasDoculmentTypesEntity; } } diff --git a/src/common/repositories/DeedTypesRepository.ts b/src/common/repositories/DeedTypesRepository.ts index 6f38421d..7a695488 100644 --- a/src/common/repositories/DeedTypesRepository.ts +++ b/src/common/repositories/DeedTypesRepository.ts @@ -91,7 +91,7 @@ export default class DeedTypesRepository extends BaseRepository { /** * @description : Find unique deed type */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.DeedTypesFindUniqueArgs = { where: { uid: uid, @@ -102,10 +102,6 @@ export default class DeedTypesRepository extends BaseRepository { } const deedTypeEntity = await this.model.findUnique(findOneArgs); - if (!deedTypeEntity) { - throw new Error("deed type not found"); - } - return deedTypeEntity; } } diff --git a/src/common/repositories/DeedsHasDocumentTypesRepository.ts b/src/common/repositories/DeedsHasDocumentTypesRepository.ts index 65bac2c0..f95bca45 100644 --- a/src/common/repositories/DeedsHasDocumentTypesRepository.ts +++ b/src/common/repositories/DeedsHasDocumentTypesRepository.ts @@ -26,17 +26,13 @@ export default class DeedHasDocumentTypesRepository extends BaseRepository { /** * @description : Find unique relation between deed and a document type */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const deedHasDocumentTypesEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!deedHasDocumentTypesEntity) { - throw new Error("relation between deed and document type not found"); - } - return deedHasDocumentTypesEntity; } } diff --git a/src/common/repositories/DeedsRepository.ts b/src/common/repositories/DeedsRepository.ts index 945a181d..ce6bef38 100644 --- a/src/common/repositories/DeedsRepository.ts +++ b/src/common/repositories/DeedsRepository.ts @@ -90,7 +90,7 @@ export default class DeedsRepository extends BaseRepository { /** * @description : Find unique deed */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.DeedsFindUniqueArgs = { where: { uid: uid, @@ -101,10 +101,6 @@ export default class DeedsRepository extends BaseRepository { } const deedTypeEntity = await this.model.findUnique(findOneArgs); - if (!deedTypeEntity) { - throw new Error("deed not found"); - } - return deedTypeEntity; } } diff --git a/src/common/repositories/DocumentTypesRepository.ts b/src/common/repositories/DocumentTypesRepository.ts index e9cb3fef..04d785ba 100644 --- a/src/common/repositories/DocumentTypesRepository.ts +++ b/src/common/repositories/DocumentTypesRepository.ts @@ -67,7 +67,7 @@ export default class DocumentTypesRepository extends BaseRepository { /** * @description : find unique document type */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.DocumentTypesFindUniqueArgs = { where: { uid: uid, @@ -78,10 +78,6 @@ export default class DocumentTypesRepository extends BaseRepository { } const documentTypeEntity = await this.model.findUnique(findOneArgs); - if (!documentTypeEntity) { - throw new Error("Document Type not found"); - } - return documentTypeEntity; } } diff --git a/src/common/repositories/DocumentsRepository.ts b/src/common/repositories/DocumentsRepository.ts index 1e1aa340..0c2e3b60 100644 --- a/src/common/repositories/DocumentsRepository.ts +++ b/src/common/repositories/DocumentsRepository.ts @@ -109,7 +109,7 @@ export default class DocumentsRepository extends BaseRepository { /** * @description : Find unique document */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.DocumentsFindUniqueArgs = { where: { uid: uid, @@ -119,10 +119,7 @@ export default class DocumentsRepository extends BaseRepository { findOneArgs.include = query; } const documentEntity = await this.model.findUnique(findOneArgs); - if (!documentEntity) { - throw new Error("Document not found"); - } - + return documentEntity; } } diff --git a/src/common/repositories/FilesRepository.ts b/src/common/repositories/FilesRepository.ts index f3003b40..cc75dbf1 100644 --- a/src/common/repositories/FilesRepository.ts +++ b/src/common/repositories/FilesRepository.ts @@ -81,6 +81,7 @@ export default class FilesRepository extends BaseRepository { uid: uid, }, }); + return fileEntity; } } diff --git a/src/common/repositories/OfficeFoldersHasCustomerRepository.ts b/src/common/repositories/OfficeFoldersHasCustomerRepository.ts index 2d4b403d..932e51fd 100644 --- a/src/common/repositories/OfficeFoldersHasCustomerRepository.ts +++ b/src/common/repositories/OfficeFoldersHasCustomerRepository.ts @@ -26,17 +26,13 @@ export default class OfficeFoldersHasCustomerRepository extends BaseRepository { /** * @description : Find a unique relation between an office folder and customers */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const officeFolderHasCustomersEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!officeFolderHasCustomersEntity) { - throw new Error("relation between office folder and customer not found"); - } - return officeFolderHasCustomersEntity; } } diff --git a/src/common/repositories/OfficeFoldersHasStakeholderRepository.ts b/src/common/repositories/OfficeFoldersHasStakeholderRepository.ts index 02b2bb11..864856cd 100644 --- a/src/common/repositories/OfficeFoldersHasStakeholderRepository.ts +++ b/src/common/repositories/OfficeFoldersHasStakeholderRepository.ts @@ -26,17 +26,13 @@ export default class OfficeFoldersHasStakeholderRepository extends BaseRepositor /** * @description : Find a unique relation between an office folder and stakeholders */ - public async findOneByUid(uid: string): Promise { + public async findOneByUid(uid: string): Promise { const officeFolderHasStakeholdersEntity = await this.model.findUnique({ where: { uid: uid, }, }); - if (!officeFolderHasStakeholdersEntity) { - throw new Error("relation between office folder and stakeholder not found"); - } - return officeFolderHasStakeholdersEntity; } } diff --git a/src/common/repositories/OfficeFoldersRepository.ts b/src/common/repositories/OfficeFoldersRepository.ts index 18f6e894..d148cc46 100644 --- a/src/common/repositories/OfficeFoldersRepository.ts +++ b/src/common/repositories/OfficeFoldersRepository.ts @@ -122,7 +122,7 @@ export default class OfficeFoldersRepository extends BaseRepository { /** * @description : Find one office folder */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.OfficeFoldersFindUniqueArgs = { where: { uid: uid, @@ -133,10 +133,6 @@ export default class OfficeFoldersRepository extends BaseRepository { } const officeFolderEntity = await this.model.findUnique(findOneArgs); - if (!officeFolderEntity) { - throw new Error("office folder not found"); - } - return officeFolderEntity; } diff --git a/src/common/repositories/OfficesRepository.ts b/src/common/repositories/OfficesRepository.ts index 7595a1f5..e74a1d52 100644 --- a/src/common/repositories/OfficesRepository.ts +++ b/src/common/repositories/OfficesRepository.ts @@ -71,7 +71,7 @@ export default class OfficesRepository extends BaseRepository { /** * @description : Find one office */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.OfficesFindUniqueArgs = { where: { uid: uid, @@ -82,10 +82,6 @@ export default class OfficesRepository extends BaseRepository { } const officeEntity = await this.model.findUnique(findOneArgs); - if (!officeEntity) { - throw new Error("office not found"); - } - return officeEntity; } } diff --git a/src/common/repositories/UsersRepository.ts b/src/common/repositories/UsersRepository.ts index b7643e18..b051af69 100644 --- a/src/common/repositories/UsersRepository.ts +++ b/src/common/repositories/UsersRepository.ts @@ -138,7 +138,7 @@ export default class UsersRepository extends BaseRepository { /** * @description : Find one user */ - public async findOneByUid(uid: string, query?: any): Promise { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.UsersFindUniqueArgs = { where: { uid: uid, @@ -149,10 +149,6 @@ export default class UsersRepository extends BaseRepository { } const userEntity = await this.model.findUnique(findOneArgs); - if (!userEntity) { - throw new Error("User not found"); - } - return userEntity; } } diff --git a/src/common/system/controller-pattern/BaseController.ts b/src/common/system/controller-pattern/BaseController.ts index 24906610..27eb5aa9 100644 --- a/src/common/system/controller-pattern/BaseController.ts +++ b/src/common/system/controller-pattern/BaseController.ts @@ -28,7 +28,7 @@ export default abstract class BaseController { return this.httpResponse(response, HttpCodes.INTERNAL_ERROR, responseData); } - protected httpNotImplemented(response: Response, responseData: IResponseData = "http Internal Server Error") { + protected httpNotImplemented(response: Response, responseData: IResponseData = "Not implemented") { return this.httpResponse(response, HttpCodes.NOT_IMPLEMENTED, responseData); } diff --git a/src/services/customer/DocumentsService/DocumentsService.ts b/src/services/customer/DocumentsService/DocumentsService.ts index b5d470e0..e8aeec5e 100644 --- a/src/services/customer/DocumentsService/DocumentsService.ts +++ b/src/services/customer/DocumentsService/DocumentsService.ts @@ -14,7 +14,7 @@ export default class DocumentsService extends BaseService { * @description : Get all documents * @throws {Error} If documents cannot be get */ - public async get(query: any) { + public async get(query: any): Promise { return this.documentsRepository.findMany(query); } @@ -54,7 +54,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?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.documentsRepository.findOneByUid(uid, query); } } diff --git a/src/services/private-services/AddressesService/AddressesService.ts b/src/services/private-services/AddressesService/AddressesService.ts index 41e53765..4054aaba 100644 --- a/src/services/private-services/AddressesService/AddressesService.ts +++ b/src/services/private-services/AddressesService/AddressesService.ts @@ -1,5 +1,6 @@ import AddressesRepository from "@Repositories/AddressesRepository"; import BaseService from "@Services/BaseService"; +import { Addresses } from "@prisma/client"; import { Service } from "typedi"; @Service() @@ -12,7 +13,7 @@ export default class AddressesService extends BaseService { * @description : Get all addresses * @throws {Error} If addresses cannot be get */ - public async get(query: any) { + public async get(query: any): Promise { return this.addressRepository.findMany(query); } @@ -20,7 +21,7 @@ export default class AddressesService extends BaseService { * @description : Get a address by uid * @throws {Error} If address cannot be get */ - public async getByUid(uid: string) { + public async getByUid(uid: string): Promise { return this.addressRepository.findOneByUid(uid); } } diff --git a/src/services/private-services/ContactsService/ContactsService.ts b/src/services/private-services/ContactsService/ContactsService.ts index 4d381a84..116c9cbe 100644 --- a/src/services/private-services/ContactsService/ContactsService.ts +++ b/src/services/private-services/ContactsService/ContactsService.ts @@ -1,5 +1,6 @@ import ContactsRepository from "@Repositories/ContactsRepository"; import BaseService from "@Services/BaseService"; +import { Contacts } from "@prisma/client"; import { Service } from "typedi"; @Service() @@ -12,7 +13,7 @@ export default class ContactsService extends BaseService { * @description : Get all contacts * @throws {Error} If contacts cannot be get */ - public async get(query: any) { + public async get(query: any): Promise { return this.contactRepository.findMany(query); } @@ -20,7 +21,7 @@ export default class ContactsService extends BaseService { * @description : Get a contact by uid * @throws {Error} If contact cannot be get */ - public async getByUid(uid: string) { + public async getByUid(uid: string): Promise { return this.contactRepository.findOneByUid(uid); } } diff --git a/src/services/private-services/FilesService/FilesService.ts b/src/services/private-services/FilesService/FilesService.ts index 6aacc1d0..6e88a91f 100644 --- a/src/services/private-services/FilesService/FilesService.ts +++ b/src/services/private-services/FilesService/FilesService.ts @@ -7,6 +7,7 @@ import IpfsService from "../IpfsService/IpfsService"; import { BackendVariables } from "@Common/config/variables/Variables"; import { Readable } from "stream"; import { v4 } from "uuid"; +import { Files } from "@prisma/client"; @Service() export default class FilesService extends BaseService { @@ -23,7 +24,7 @@ export default class FilesService extends BaseService { * @description : Get all files * @throws {Error} If files cannot be ge */ - public async get(query: any) { + public async get(query: any): Promise { return this.filesRepository.findMany(query); } @@ -31,7 +32,7 @@ export default class FilesService extends BaseService { * @description : Get a file by uid * @throws {Error} If project cannot be created */ - public async getByUid(uid: string) { + public async getByUid(uid: string): Promise { return this.filesRepository.findOneByUid(uid); } @@ -41,6 +42,7 @@ export default class FilesService extends BaseService { */ public async download(uid: string) { const file = await this.filesRepository.findOneByUid(uid); + console.log(file, uid); if (!file?.key) return null; const fileResult = await fetch(file.file_path); const fileContent = await fileResult.arrayBuffer(); @@ -51,7 +53,7 @@ export default class FilesService extends BaseService { * @description : Create a new file * @throws {Error} If file cannot be created */ - public async create(file: File, fileData: Express.Multer.File) { + public async create(file: File, fileData: Express.Multer.File): Promise { const key = v4(); fileData.mimetype; fileData.size; @@ -67,7 +69,7 @@ export default class FilesService extends BaseService { * @description : Modify a new file * @throws {Error} If file cannot be modified */ - public async update(uid: string, file: File) { + public async update(uid: string, file: File): Promise { return this.filesRepository.update(uid, file); } @@ -75,10 +77,10 @@ export default class FilesService extends BaseService { * @description : Delete a file key and archive * @throws {Error} If file key cannot be deleted or archived */ - public async deleteKeyAndArchive(uid: string) { + public async deleteKeyAndArchive(uid: string): Promise { try { const fileToUnpin = await this.filesRepository.findOneByUid(uid); - if(!fileToUnpin) return null; + if(!fileToUnpin) throw new Error("file not found"); const fileHash = fileToUnpin.file_path.substring(this.variables.PINATA_GATEWAY.length); await this.ipfsService.unpinFile(fileHash); } catch(error) { diff --git a/src/services/super-admin/CustomersService/CustomersService.ts b/src/services/super-admin/CustomersService/CustomersService.ts index 80765051..95c702ef 100644 --- a/src/services/super-admin/CustomersService/CustomersService.ts +++ b/src/services/super-admin/CustomersService/CustomersService.ts @@ -38,7 +38,7 @@ 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?: any): Promise { return this.customerRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/DeedTypesService/DeedTypesService.ts b/src/services/super-admin/DeedTypesService/DeedTypesService.ts index da71909e..7bb48f09 100644 --- a/src/services/super-admin/DeedTypesService/DeedTypesService.ts +++ b/src/services/super-admin/DeedTypesService/DeedTypesService.ts @@ -40,7 +40,7 @@ export default class DeedTypesService extends BaseService { * @description : Get a deedtype by uid * @throws {Error} If deed-type cannot be get by uid */ - public async getByUid(uid: string, query?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.deedTypeRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/DeedsService/DeedsService.ts b/src/services/super-admin/DeedsService/DeedsService.ts index 93a6c6bd..055d24cf 100644 --- a/src/services/super-admin/DeedsService/DeedsService.ts +++ b/src/services/super-admin/DeedsService/DeedsService.ts @@ -38,7 +38,7 @@ export default class DeedsService extends BaseService { * @description : Get a deed by uid * @throws {Error} If deed-type cannot be get by uid */ - public async getByUid(uid: string, query?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.deedRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/DocumentTypesService/DocumentTypesService.ts b/src/services/super-admin/DocumentTypesService/DocumentTypesService.ts index dacfe17f..0f93d721 100644 --- a/src/services/super-admin/DocumentTypesService/DocumentTypesService.ts +++ b/src/services/super-admin/DocumentTypesService/DocumentTypesService.ts @@ -14,7 +14,7 @@ export default class DocumentTypesService extends BaseService { * @description : Get all document-types * @throws {Error} If document-types cannot be get */ - public async get(query: any) { + public async get(query: any): Promise { return this.documentTypeRepository.findMany(query); } @@ -38,7 +38,7 @@ export default class DocumentTypesService extends BaseService { * @description : Get a document-type by uid * @throws {Error} If document-type is not found */ - public async getByUid(uid: string, query?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.documentTypeRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/DocumentsService/DocumentsService.ts b/src/services/super-admin/DocumentsService/DocumentsService.ts index 81a0d8bc..f12e253f 100644 --- a/src/services/super-admin/DocumentsService/DocumentsService.ts +++ b/src/services/super-admin/DocumentsService/DocumentsService.ts @@ -48,7 +48,7 @@ export default class DocumentsService extends BaseService { */ 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) { @@ -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?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.documentsRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/OfficeFoldersService/OfficeFoldersService.ts b/src/services/super-admin/OfficeFoldersService/OfficeFoldersService.ts index 128070e4..e0740d28 100644 --- a/src/services/super-admin/OfficeFoldersService/OfficeFoldersService.ts +++ b/src/services/super-admin/OfficeFoldersService/OfficeFoldersService.ts @@ -21,7 +21,7 @@ export default class OfficeFoldersService extends BaseService { * @description : Get all folders * @throws {Error} If folders cannot be get */ - public async get(query: any) { + public async get(query: any): Promise { return this.officeFoldersRepository.findMany(query); } @@ -31,6 +31,7 @@ export default class OfficeFoldersService extends BaseService { */ 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; @@ -49,7 +50,7 @@ export default class OfficeFoldersService extends BaseService { * @description : Get a folder by uid * @throws {Error} If folder cannot be get by uid */ - public async getByUid(uid: string, query?: any) { + public async getByUid(uid: string, query?: any): Promise { return this.officeFoldersRepository.findOneByUid(uid, query); } @@ -57,8 +58,9 @@ export default class OfficeFoldersService extends BaseService { * @description : Delete a folder * @throws {Error} If document cannot be deleted */ - public async delete(uid: string): Promise { + public async delete(uid: string): Promise { const officeFolderEntity = await this.officeFoldersRepository.findOneByUid(uid, { office_folder_has_customers: true }); + if(!officeFolderEntity) throw new Error('office folder not found'); const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); if (officeFolder.office_folder_has_customers && officeFolder.office_folder_has_customers.length !== 0) { diff --git a/src/services/super-admin/OfficesService/OfficesService.ts b/src/services/super-admin/OfficesService/OfficesService.ts index 4de766be..441da71b 100644 --- a/src/services/super-admin/OfficesService/OfficesService.ts +++ b/src/services/super-admin/OfficesService/OfficesService.ts @@ -38,7 +38,7 @@ export default class OfficesService extends BaseService { * @description : Get a office by uid * @throws {Error} If office cannot be get */ - public async getByUid(uid: string, query?: any): Promise { + public async getByUid(uid: string, query?: any): Promise { return this.officeRepository.findOneByUid(uid, query); } } diff --git a/src/services/super-admin/UsersService/UsersService.ts b/src/services/super-admin/UsersService/UsersService.ts index fcdbf428..7846b23d 100644 --- a/src/services/super-admin/UsersService/UsersService.ts +++ b/src/services/super-admin/UsersService/UsersService.ts @@ -39,7 +39,7 @@ export default class UsersService extends BaseService { * @description : Get a user by uid * @throws {Error} If user cannot be get by uid */ - public getByUid(uid: string, query?: any): Promise { + public getByUid(uid: string, query?: any): Promise { return this.userRepository.findOneByUid(uid, query); } }