refacto errors and naming (#44)

refacto errors: add http errors and status

renaming: entities variables declaration and hydrate arrays method
This commit is contained in:
Arnaud D. Natali 2023-05-15 14:10:35 +02:00 committed by GitHub
commit dcb7052a32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 374 additions and 244 deletions

View File

@ -48,7 +48,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "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", "module-alias": "^2.2.2",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"next": "^13.1.5", "next": "^13.1.5",

View File

@ -25,15 +25,15 @@ export default class DocumentsController extends ApiController {
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documents = Document.map<Document>(Document, prismaEntity, { strategy: "excludeAll" }); const documents = Document.hydrateArray<Document>(Document, documentEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, documents); this.httpSuccess(response, documents);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -52,17 +52,17 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["createDocument"] }); await validateOrReject(documentEntity, { groups: ["createDocument"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documentEntityCreated = Document.hydrate<Document>(prismaEntityCreated, { const document = Document.hydrate<Document>(documentEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, documentEntityCreated); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -75,7 +75,15 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init Document resource with request body values
@ -86,15 +94,15 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["createDocument"] }); await validateOrReject(documentEntity, { groups: ["createDocument"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(prismaEntityUpdated, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -107,19 +115,27 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //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 //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityDeleted, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -132,10 +148,11 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); documentEntity = await this.documentsService.getByUid(uid);
} }
if (!documentEntity) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -24,7 +24,7 @@ import AuthService from "@Services/private-services/AuthService/AuthService";
//success //success
this.httpSuccess(response, user); this.httpSuccess(response, user);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -24,15 +24,15 @@ export default class CustomersController extends ApiController {
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const customers = Customer.map<Customer>(Customer, customersEntity, { strategy: "excludeAll" }); const customers = Customer.hydrateArray<Customer>(Customer, customersEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, customers); this.httpSuccess(response, customers);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -49,16 +49,16 @@ export default class CustomersController extends ApiController {
await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const customerEntityCreated = Customer.hydrate<Customer>(prismaEntityCreated, { const customer = Customer.hydrate<Customer>(customerEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, customerEntityCreated); this.httpCreated(response, customer);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -71,8 +71,17 @@ export default class CustomersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init IUser resource with request body values
const customerEntity = Customer.hydrate<Customer>(req.body); const customerEntity = Customer.hydrate<Customer>(req.body);
@ -80,17 +89,17 @@ export default class CustomersController extends ApiController {
await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false }); await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const customerEntityUpdated = Customer.hydrate<Customer>(prismaEntityUpdated, { const customer = Customer.hydrate<Customer>(customerEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, customerEntityUpdated); this.httpSuccess(response, customer);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -103,10 +112,11 @@ export default class CustomersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); customerEntity = await this.customersService.getByUid(uid);
} }
if (!customerEntity) {
this.httpNotFoundRequest(response, "customer not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const customer = Customer.hydrate<Customer>(customerEntity, { strategy: "excludeAll" }); const customer = Customer.hydrate<Customer>(customerEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, customer); this.httpSuccess(response, customer);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -24,15 +24,15 @@ export default class DeedTypesController extends ApiController {
//get query //get query
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const DeedTypes = DeedType.map<DeedType>(DeedType, prismaEntity, { strategy: "excludeAll" }); const DeedTypes = DeedType.hydrateArray<DeedType>(DeedType, deedTypeEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, DeedTypes); this.httpSuccess(response, DeedTypes);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -51,17 +51,17 @@ export default class DeedTypesController extends ApiController {
await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const deedTypeEntityCreated = DeedType.hydrate<DeedType>(prismaEntityCreated, { const deedType = DeedType.hydrate<DeedType>(deedTypeEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, deedTypeEntityCreated); this.httpCreated(response, deedType);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -75,8 +75,17 @@ export default class DeedTypesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init DeedType resource with request body values
const deedTypeEntity = DeedType.hydrate<DeedType>(req.body); const deedTypeEntity = DeedType.hydrate<DeedType>(req.body);
@ -84,17 +93,17 @@ export default class DeedTypesController extends ApiController {
await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] }); await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const deedTypeEntityUpdated = DeedType.hydrate<DeedType>(prismaEntityUpdated, { const deedType = DeedType.hydrate<DeedType>(deedTypeEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, deedTypeEntityUpdated); this.httpSuccess(response, deedType);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -108,10 +117,11 @@ export default class DeedTypesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); deedTypeEntity = await this.deedTypesService.getByUid(uid);
} }
if (!deedTypeEntity) {
this.httpNotFoundRequest(response, "deed type not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const deedType = DeedType.hydrate<DeedType>(deedTypeEntity, { strategy: "excludeAll" }); const deedType = DeedType.hydrate<DeedType>(deedTypeEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, deedType); this.httpSuccess(response, deedType);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -24,15 +24,15 @@ export default class DeedsController extends ApiController {
//get query //get query
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const deeds = Deed.map<Deed>(Deed, prismaEntity, { strategy: "excludeAll" }); const deeds = Deed.hydrateArray<Deed>(Deed, deedEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, deeds); this.httpSuccess(response, deeds);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -46,10 +46,11 @@ export default class DeedsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); deedEntity = await this.deedsService.getByUid(uid);
} }
if (!deedEntity) {
this.httpNotFoundRequest(response, "deed not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const deed = Deed.hydrate<Deed>(deedEntity, { strategy: "excludeAll" }); const deed = Deed.hydrate<Deed>(deedEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, deed); this.httpSuccess(response, deed);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -78,8 +84,17 @@ export default class DeedsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init OfficeFolder resource with request body values
const deedEntity = Deed.hydrate<Deed>(req.body); const deedEntity = Deed.hydrate<Deed>(req.body);
@ -87,17 +102,17 @@ export default class DeedsController extends ApiController {
await validateOrReject(deedEntity, { groups: ["updateDeed"], forbidUnknownValues: false }); await validateOrReject(deedEntity, { groups: ["updateDeed"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const deedEntityUpdated = Deed.hydrate<Deed>(prismaEntityUpdated, { const deed = Deed.hydrate<Deed>(deedEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, deedEntityUpdated); this.httpSuccess(response, deed);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -25,17 +25,17 @@ export default class DocumentTypesController extends ApiController {
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documentTypes = DocumentType.map<DocumentType>(DocumentType, prismaEntity, { const documentTypes = DocumentType.hydrateArray<DocumentType>(DocumentType, documentTypeEntities, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, documentTypes); this.httpSuccess(response, documentTypes);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -51,15 +51,15 @@ export default class DocumentTypesController extends ApiController {
//validate user //validate user
await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false }); await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const userEntityCreated = DocumentType.hydrate<DocumentType>(prismaEntityCreated, { const userEntityCreated = DocumentType.hydrate<DocumentType>(documentTypeEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, userEntityCreated); this.httpCreated(response, userEntityCreated);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -72,7 +72,15 @@ export default class DocumentTypesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init DocumentType resource with request body values
const documentTypeEntity = DocumentType.hydrate<DocumentType>(req.body); const documentTypeEntity = DocumentType.hydrate<DocumentType>(req.body);
@ -81,17 +89,17 @@ export default class DocumentTypesController extends ApiController {
await validateOrReject(documentTypeEntity, { groups: ["update"] }); await validateOrReject(documentTypeEntity, { groups: ["update"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documentTypeEntityUpdated = DocumentType.hydrate<DocumentType>(prismaEntityUpdated, { const documentType = DocumentType.hydrate<DocumentType>(documentTypeEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, documentTypeEntityUpdated); this.httpSuccess(response, documentType);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -104,10 +112,11 @@ export default class DocumentTypesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); documentTypeEntity = await this.documentTypesService.getByUid(uid);
} }
if (!documentTypeEntity) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const user = ObjectHydrate.hydrate<DocumentType>(new DocumentType(), documentTypeEntity, { strategy: "excludeAll" }); const user = ObjectHydrate.hydrate<DocumentType>(new DocumentType(), documentTypeEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, user); this.httpSuccess(response, user);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -25,15 +25,15 @@ export default class DocumentsController extends ApiController {
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documents = Document.map<Document>(Document, prismaEntity, { strategy: "excludeAll" }); const documents = Document.hydrateArray<Document>(Document, documentEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, documents); this.httpSuccess(response, documents);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -52,17 +52,17 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const documentEntityCreated = Document.hydrate<Document>(prismaEntityCreated, { const document = Document.hydrate<Document>(documentEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, documentEntityCreated); this.httpCreated(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -75,8 +75,16 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body); const documentEntity = Document.hydrate<Document>(req.body);
@ -85,15 +93,15 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["updateDocument"] }); await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(prismaEntityUpdated, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -106,7 +114,15 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //call service to get prisma entity
@ -118,7 +134,7 @@ export default class DocumentsController extends ApiController {
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -131,10 +147,11 @@ export default class DocumentsController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); documentEntity = await this.documentsService.getByUid(uid);
} }
if (!documentEntity) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, document); this.httpSuccess(response, document);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -4,7 +4,7 @@ import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import FilesService from "@Services/private-services/FilesService/FilesService"; import FilesService from "@Services/private-services/FilesService/FilesService";
import { Files } from "@prisma/client"; 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 { validateOrReject } from "class-validator";
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; 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); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const files = File.map<File>(File, prismaEntity, { strategy: "excludeAll" }); const files = File.hydrateArray<File>(File, fileEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, files); this.httpSuccess(response, files);
@ -53,7 +53,7 @@ export default class FilesController extends ApiController {
const fileInfo = await this.filesService.download(uid); const fileInfo = await this.filesService.download(uid);
if (!fileInfo) { if (!fileInfo) {
this.httpNotFoundRequest(response); this.httpNotFoundRequest(response, "file not found");
return; return;
} }
@ -85,19 +85,25 @@ export default class FilesController extends ApiController {
await validateOrReject(fileEntity, { groups: ["createFile"] }); await validateOrReject(fileEntity, { groups: ["createFile"] });
//call service to get prisma entity //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"; document.document_status = "DEPOSITED";
await this.documentService.update(document.uid!, document); await this.documentService.update(document.uid!, document);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const fileEntityCreated = File.hydrate<File>(prismaEntityCreated, { const fileEntityHydrated = File.hydrate<File>(fileEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, fileEntityCreated); this.httpCreated(response, fileEntityHydrated);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpBadRequest(response, error);
return; return;
@ -115,6 +121,13 @@ export default class FilesController extends ApiController {
throw new Error("No uid provided"); 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 //init File resource with request body values
const fileEntity = File.hydrate<File>(req.body); const fileEntity = File.hydrate<File>(req.body);
@ -122,10 +135,10 @@ export default class FilesController extends ApiController {
await validateOrReject(fileEntity, { groups: ["updateFile"] }); await validateOrReject(fileEntity, { groups: ["updateFile"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const file = File.hydrate<File>(prismaEntityUpdated, { strategy: "excludeAll" }); const file = File.hydrate<File>(fileEntityUpdated, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, file); this.httpSuccess(response, file);
@ -147,6 +160,13 @@ export default class FilesController extends ApiController {
return; return;
} }
const fileFound = await this.filesService.getByUid(uid);
if (!fileFound) {
this.httpNotFoundRequest(response, "file not found");
return;
}
//call service to get prisma entity //call service to get prisma entity
const fileEntity = await this.filesService.deleteKeyAndArchive(uid); const fileEntity = await this.filesService.deleteKeyAndArchive(uid);
@ -154,6 +174,7 @@ export default class FilesController extends ApiController {
this.httpNotFoundRequest(response, "file not found"); this.httpNotFoundRequest(response, "file not found");
return; return;
} }
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" }); const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" });

View File

@ -23,15 +23,15 @@ export default class OfficeFoldersController extends ApiController {
//get query //get query
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.map<OfficeFolder>(OfficeFolder, prismaEntity, { const officeFolders = OfficeFolder.hydrateArray<OfficeFolder>(OfficeFolder, officeFolderEntities, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, officeFolders); this.httpSuccess(response, officeFolders);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -48,15 +48,15 @@ export default class OfficeFoldersController extends ApiController {
//validate folder //validate folder
await validateOrReject(officeFolderEntity, { groups: ["createFolder"] , forbidUnknownValues: false }); await validateOrReject(officeFolderEntity, { groups: ["createFolder"] , forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const officeFolderEntityCreated = OfficeFolder.hydrate<OfficeFolder>(prismaEntityCreated, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, officeFolderEntityCreated); this.httpCreated(response, officeFolders);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -69,8 +69,17 @@ export default class OfficeFoldersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init OfficeFolder resource with request body values
const officeFolderEntity = OfficeFolder.hydrate<OfficeFolder>(req.body); const officeFolderEntity = OfficeFolder.hydrate<OfficeFolder>(req.body);
@ -78,17 +87,17 @@ 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
const prismaEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolderEntityUpdated = OfficeFolder.hydrate<OfficeFolder>(prismaEntityUpdated, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, officeFolderEntityUpdated); this.httpSuccess(response, officeFolders);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -102,10 +111,11 @@ export default class OfficeFoldersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); officeFolderEntity = await this.officeFoldersService.getByUid(uid);
} }
if (!officeFolderEntity) {
this.httpNotFoundRequest(response, "folder not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" }); const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, officeFolder); this.httpSuccess(response, officeFolder);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
this.httpSuccess(response, await this.officeFoldersService.getByUid("uid")); this.httpSuccess(response, await this.officeFoldersService.getByUid("uid"));
@ -135,7 +150,15 @@ export default class OfficeFoldersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //call service to get prisma entity
@ -147,7 +170,7 @@ export default class OfficeFoldersController extends ApiController {
//success //success
this.httpSuccess(response, officeFolder); this.httpSuccess(response, officeFolder);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -22,13 +22,13 @@ export default class OfficesController extends ApiController {
//get query //get query
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const offices = OfficeResource.map<OfficeResource>(OfficeResource, officesEntity, { strategy: "excludeAll" }); const offices = OfficeResource.hydrateArray<OfficeResource>(OfficeResource, officesEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, offices); this.httpSuccess(response, offices);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -43,15 +43,15 @@ export default class OfficesController extends ApiController {
//validate user //validate user
await validateOrReject(officeEntity, { groups: ["createOffice"], forbidUnknownValues: false }); await validateOrReject(officeEntity, { groups: ["createOffice"], forbidUnknownValues: false });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const officeEntityCreated = OfficeResource.hydrate<OfficeResource>(prismaEntityCreated, { const office = OfficeResource.hydrate<OfficeResource>(officeEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, officeEntityCreated); this.httpCreated(response, office);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -63,22 +63,30 @@ export default class OfficesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init IUser resource with request body values
const officeEntity = OfficeResource.hydrate<OfficeResource>(req.body); const officeEntity = OfficeResource.hydrate<OfficeResource>(req.body);
//validate user //validate user
await validateOrReject(officeEntity, { groups: ["update"] }); await validateOrReject(officeEntity, { groups: ["update"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const officeEntityUpdated = OfficeResource.hydrate<OfficeResource>(prismaEntityUpdated, { const office = OfficeResource.hydrate<OfficeResource>(officeEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, officeEntityUpdated); this.httpSuccess(response, office);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -90,9 +98,10 @@ export default class OfficesController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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 //call service to get prisma entity
officeEntity = await this.officesService.getByUid(uid); officeEntity = await this.officesService.getByUid(uid);
} }
if (!officeEntity) {
this.httpNotFoundRequest(response, "office not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const office = OfficeResource.hydrate<OfficeResource>(officeEntity, { strategy: "excludeAll" }); const office = OfficeResource.hydrate<OfficeResource>(officeEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, office); this.httpSuccess(response, office);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -24,15 +24,15 @@ export default class UsersController extends ApiController {
const query = JSON.parse(req.query["q"] as string); const query = JSON.parse(req.query["q"] as string);
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const users = User.map<User>(User, usersEntity, { strategy: "excludeAll" }); const users = User.hydrateArray<User>(User, usersEntities, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, users); this.httpSuccess(response, users);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -50,17 +50,17 @@ export default class UsersController extends ApiController {
await validateOrReject(userEntity, { groups: ["createUser"] }); await validateOrReject(userEntity, { groups: ["createUser"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const userEntityCreated = User.hydrate<User>(prismaEntityCreated, { const user = User.hydrate<User>(userEntityCreated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, userEntityCreated); this.httpCreated(response, user);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -73,8 +73,17 @@ export default class UsersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //init IUser resource with request body values
const userEntity = User.hydrate<User>(req.body); const userEntity = User.hydrate<User>(req.body);
@ -82,17 +91,17 @@ export default class UsersController extends ApiController {
await validateOrReject(userEntity, { groups: ["update"] }); await validateOrReject(userEntity, { groups: ["update"] });
//call service to get prisma entity //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 //Hydrate ressource with prisma entity
const userEntityUpdated = User.hydrate<User>(prismaEntityUpdated, { const user = User.hydrate<User>(userEntityUpdated, {
strategy: "excludeAll", strategy: "excludeAll",
}); });
//success //success
this.httpSuccess(response, userEntityUpdated); this.httpSuccess(response, user);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }
@ -105,9 +114,10 @@ export default class UsersController extends ApiController {
try { try {
const uid = req.params["uid"]; const uid = req.params["uid"];
if (!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 //get query
if (req.query["q"]) { if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string); 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); userEntity = await this.usersService.getByUid(uid);
} }
if (!userEntity) {
this.httpNotFoundRequest(response, "user not found");
return;
}
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const user = User.hydrate<User>(userEntity, { strategy: "excludeAll" }); const user = User.hydrate<User>(userEntity, { strategy: "excludeAll" });
//success //success
this.httpSuccess(response, user); this.httpSuccess(response, user);
} catch (error) { } catch (error) {
this.httpBadRequest(response, error); this.httpInternalError(response);
return; return;
} }
} }

View File

@ -26,17 +26,13 @@ export default class AddressesRepository extends BaseRepository {
/** /**
* @description : Find one address * @description : Find one address
*/ */
public async findOneByUid(uid: string): Promise<Addresses> { public async findOneByUid(uid: string): Promise<Addresses | null> {
const addressEntity = await this.model.findUnique({ const addressEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!addressEntity) {
throw new Error("Address not found");
}
return addressEntity; return addressEntity;
} }
} }

View File

@ -26,17 +26,13 @@ export default class ContactsRepository extends BaseRepository {
/** /**
* @description : Find unique contact * @description : Find unique contact
*/ */
public async findOneByUid(uid: string): Promise<Contacts> { public async findOneByUid(uid: string): Promise<Contacts | null> {
const contactEntity = await this.model.findUnique({ const contactEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!contactEntity) {
throw new Error("contact not found");
}
return contactEntity; return contactEntity;
} }
} }

View File

@ -98,7 +98,7 @@ export default class CustomersRepository extends BaseRepository {
/** /**
* @description : Find unique customer * @description : Find unique customer
*/ */
public async findOneByUid(uid: string, query?: any): Promise<Customers> { public async findOneByUid(uid: string, query?: any): Promise<Customers | null> {
const findOneArgs: Prisma.CustomersFindUniqueArgs = { const findOneArgs: Prisma.CustomersFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -108,9 +108,6 @@ export default class CustomersRepository extends BaseRepository {
findOneArgs.include = query; findOneArgs.include = query;
} }
const customerEntity = await this.model.findUnique(findOneArgs); const customerEntity = await this.model.findUnique(findOneArgs);
if (!customerEntity) {
throw new Error("Customer not found");
}
return customerEntity; return customerEntity;
} }

View File

@ -26,17 +26,13 @@ export default class DeedTypeHasDocumentTypesRepository extends BaseRepository {
/** /**
* @description : Find unique relation between deed type and a document type * @description : Find unique relation between deed type and a document type
*/ */
public async findOneByUid(uid: string): Promise<DeedTypeHasDocumentTypes> { public async findOneByUid(uid: string): Promise<DeedTypeHasDocumentTypes | null> {
const deedTypeHasDoculmentTypesEntity = await this.model.findUnique({ const deedTypeHasDoculmentTypesEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!deedTypeHasDoculmentTypesEntity) {
throw new Error("deed type not found");
}
return deedTypeHasDoculmentTypesEntity; return deedTypeHasDoculmentTypesEntity;
} }
} }

View File

@ -91,7 +91,7 @@ export default class DeedTypesRepository extends BaseRepository {
/** /**
* @description : Find unique deed type * @description : Find unique deed type
*/ */
public async findOneByUid(uid: string, query?: any): Promise<DeedTypes> { public async findOneByUid(uid: string, query?: any): Promise<DeedTypes | null> {
const findOneArgs: Prisma.DeedTypesFindUniqueArgs = { const findOneArgs: Prisma.DeedTypesFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -102,10 +102,6 @@ export default class DeedTypesRepository extends BaseRepository {
} }
const deedTypeEntity = await this.model.findUnique(findOneArgs); const deedTypeEntity = await this.model.findUnique(findOneArgs);
if (!deedTypeEntity) {
throw new Error("deed type not found");
}
return deedTypeEntity; return deedTypeEntity;
} }
} }

View File

@ -26,17 +26,13 @@ export default class DeedHasDocumentTypesRepository extends BaseRepository {
/** /**
* @description : Find unique relation between deed and a document type * @description : Find unique relation between deed and a document type
*/ */
public async findOneByUid(uid: string): Promise<DeedHasDocumentTypes> { public async findOneByUid(uid: string): Promise<DeedHasDocumentTypes | null> {
const deedHasDocumentTypesEntity = await this.model.findUnique({ const deedHasDocumentTypesEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!deedHasDocumentTypesEntity) {
throw new Error("relation between deed and document type not found");
}
return deedHasDocumentTypesEntity; return deedHasDocumentTypesEntity;
} }
} }

View File

@ -90,7 +90,7 @@ export default class DeedsRepository extends BaseRepository {
/** /**
* @description : Find unique deed * @description : Find unique deed
*/ */
public async findOneByUid(uid: string, query?: any): Promise<Deeds> { public async findOneByUid(uid: string, query?: any): Promise<Deeds | null> {
const findOneArgs: Prisma.DeedsFindUniqueArgs = { const findOneArgs: Prisma.DeedsFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -101,10 +101,6 @@ export default class DeedsRepository extends BaseRepository {
} }
const deedTypeEntity = await this.model.findUnique(findOneArgs); const deedTypeEntity = await this.model.findUnique(findOneArgs);
if (!deedTypeEntity) {
throw new Error("deed not found");
}
return deedTypeEntity; return deedTypeEntity;
} }
} }

View File

@ -67,7 +67,7 @@ export default class DocumentTypesRepository extends BaseRepository {
/** /**
* @description : find unique document type * @description : find unique document type
*/ */
public async findOneByUid(uid: string, query?: any): Promise<DocumentTypes> { public async findOneByUid(uid: string, query?: any): Promise<DocumentTypes | null> {
const findOneArgs: Prisma.DocumentTypesFindUniqueArgs = { const findOneArgs: Prisma.DocumentTypesFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -78,10 +78,6 @@ export default class DocumentTypesRepository extends BaseRepository {
} }
const documentTypeEntity = await this.model.findUnique(findOneArgs); const documentTypeEntity = await this.model.findUnique(findOneArgs);
if (!documentTypeEntity) {
throw new Error("Document Type not found");
}
return documentTypeEntity; return documentTypeEntity;
} }
} }

View File

@ -109,7 +109,7 @@ export default class DocumentsRepository extends BaseRepository {
/** /**
* @description : Find unique document * @description : Find unique document
*/ */
public async findOneByUid(uid: string, query?: any): Promise<Documents> { public async findOneByUid(uid: string, query?: any): Promise<Documents | null> {
const findOneArgs: Prisma.DocumentsFindUniqueArgs = { const findOneArgs: Prisma.DocumentsFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -119,10 +119,7 @@ export default class DocumentsRepository extends BaseRepository {
findOneArgs.include = query; findOneArgs.include = query;
} }
const documentEntity = await this.model.findUnique(findOneArgs); const documentEntity = await this.model.findUnique(findOneArgs);
if (!documentEntity) {
throw new Error("Document not found");
}
return documentEntity; return documentEntity;
} }
} }

View File

@ -81,6 +81,7 @@ export default class FilesRepository extends BaseRepository {
uid: uid, uid: uid,
}, },
}); });
return fileEntity; return fileEntity;
} }
} }

View File

@ -26,17 +26,13 @@ export default class OfficeFoldersHasCustomerRepository extends BaseRepository {
/** /**
* @description : Find a unique relation between an office folder and customers * @description : Find a unique relation between an office folder and customers
*/ */
public async findOneByUid(uid: string): Promise<OfficeFolderHasCustomers> { public async findOneByUid(uid: string): Promise<OfficeFolderHasCustomers | null> {
const officeFolderHasCustomersEntity = await this.model.findUnique({ const officeFolderHasCustomersEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!officeFolderHasCustomersEntity) {
throw new Error("relation between office folder and customer not found");
}
return officeFolderHasCustomersEntity; return officeFolderHasCustomersEntity;
} }
} }

View File

@ -26,17 +26,13 @@ export default class OfficeFoldersHasStakeholderRepository extends BaseRepositor
/** /**
* @description : Find a unique relation between an office folder and stakeholders * @description : Find a unique relation between an office folder and stakeholders
*/ */
public async findOneByUid(uid: string): Promise<OfficeFolderHasStakeholders> { public async findOneByUid(uid: string): Promise<OfficeFolderHasStakeholders | null> {
const officeFolderHasStakeholdersEntity = await this.model.findUnique({ const officeFolderHasStakeholdersEntity = await this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
}); });
if (!officeFolderHasStakeholdersEntity) {
throw new Error("relation between office folder and stakeholder not found");
}
return officeFolderHasStakeholdersEntity; return officeFolderHasStakeholdersEntity;
} }
} }

View File

@ -122,7 +122,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
/** /**
* @description : Find one office folder * @description : Find one office folder
*/ */
public async findOneByUid(uid: string, query?: any): Promise<OfficeFolders> { public async findOneByUid(uid: string, query?: any): Promise<OfficeFolders | null> {
const findOneArgs: Prisma.OfficeFoldersFindUniqueArgs = { const findOneArgs: Prisma.OfficeFoldersFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -133,10 +133,6 @@ export default class OfficeFoldersRepository extends BaseRepository {
} }
const officeFolderEntity = await this.model.findUnique(findOneArgs); const officeFolderEntity = await this.model.findUnique(findOneArgs);
if (!officeFolderEntity) {
throw new Error("office folder not found");
}
return officeFolderEntity; return officeFolderEntity;
} }

View File

@ -71,7 +71,7 @@ export default class OfficesRepository extends BaseRepository {
/** /**
* @description : Find one office * @description : Find one office
*/ */
public async findOneByUid(uid: string, query?: any): Promise<Offices> { public async findOneByUid(uid: string, query?: any): Promise<Offices | null> {
const findOneArgs: Prisma.OfficesFindUniqueArgs = { const findOneArgs: Prisma.OfficesFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -82,10 +82,6 @@ export default class OfficesRepository extends BaseRepository {
} }
const officeEntity = await this.model.findUnique(findOneArgs); const officeEntity = await this.model.findUnique(findOneArgs);
if (!officeEntity) {
throw new Error("office not found");
}
return officeEntity; return officeEntity;
} }
} }

View File

@ -138,7 +138,7 @@ export default class UsersRepository extends BaseRepository {
/** /**
* @description : Find one user * @description : Find one user
*/ */
public async findOneByUid(uid: string, query?: any): Promise<Users> { public async findOneByUid(uid: string, query?: any): Promise<Users | null> {
const findOneArgs: Prisma.UsersFindUniqueArgs = { const findOneArgs: Prisma.UsersFindUniqueArgs = {
where: { where: {
uid: uid, uid: uid,
@ -149,10 +149,6 @@ export default class UsersRepository extends BaseRepository {
} }
const userEntity = await this.model.findUnique(findOneArgs); const userEntity = await this.model.findUnique(findOneArgs);
if (!userEntity) {
throw new Error("User not found");
}
return userEntity; return userEntity;
} }
} }

View File

@ -28,7 +28,7 @@ export default abstract class BaseController {
return this.httpResponse(response, HttpCodes.INTERNAL_ERROR, responseData); 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); return this.httpResponse(response, HttpCodes.NOT_IMPLEMENTED, responseData);
} }

View File

@ -14,7 +14,7 @@ export default class DocumentsService extends BaseService {
* @description : Get all documents * @description : Get all documents
* @throws {Error} If documents cannot be get * @throws {Error} If documents cannot be get
*/ */
public async get(query: any) { public async get(query: any): Promise<Documents[]> {
return this.documentsRepository.findMany(query); return this.documentsRepository.findMany(query);
} }
@ -54,7 +54,7 @@ export default class DocumentsService extends BaseService {
* @description : Get a document by uid * @description : Get a document by uid
* @throws {Error} If document cannot be get 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<Documents | null> {
return this.documentsRepository.findOneByUid(uid, query); return this.documentsRepository.findOneByUid(uid, query);
} }
} }

View File

@ -1,5 +1,6 @@
import AddressesRepository from "@Repositories/AddressesRepository"; import AddressesRepository from "@Repositories/AddressesRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Addresses } from "@prisma/client";
import { Service } from "typedi"; import { Service } from "typedi";
@Service() @Service()
@ -12,7 +13,7 @@ export default class AddressesService extends BaseService {
* @description : Get all addresses * @description : Get all addresses
* @throws {Error} If addresses cannot be get * @throws {Error} If addresses cannot be get
*/ */
public async get(query: any) { public async get(query: any): Promise<Addresses[]> {
return this.addressRepository.findMany(query); return this.addressRepository.findMany(query);
} }
@ -20,7 +21,7 @@ export default class AddressesService extends BaseService {
* @description : Get a address by uid * @description : Get a address by uid
* @throws {Error} If address cannot be get * @throws {Error} If address cannot be get
*/ */
public async getByUid(uid: string) { public async getByUid(uid: string): Promise<Addresses | null> {
return this.addressRepository.findOneByUid(uid); return this.addressRepository.findOneByUid(uid);
} }
} }

View File

@ -1,5 +1,6 @@
import ContactsRepository from "@Repositories/ContactsRepository"; import ContactsRepository from "@Repositories/ContactsRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Contacts } from "@prisma/client";
import { Service } from "typedi"; import { Service } from "typedi";
@Service() @Service()
@ -12,7 +13,7 @@ export default class ContactsService extends BaseService {
* @description : Get all contacts * @description : Get all contacts
* @throws {Error} If contacts cannot be get * @throws {Error} If contacts cannot be get
*/ */
public async get(query: any) { public async get(query: any): Promise<Contacts[]> {
return this.contactRepository.findMany(query); return this.contactRepository.findMany(query);
} }
@ -20,7 +21,7 @@ export default class ContactsService extends BaseService {
* @description : Get a contact by uid * @description : Get a contact by uid
* @throws {Error} If contact cannot be get * @throws {Error} If contact cannot be get
*/ */
public async getByUid(uid: string) { public async getByUid(uid: string): Promise<Contacts | null> {
return this.contactRepository.findOneByUid(uid); return this.contactRepository.findOneByUid(uid);
} }
} }

View File

@ -7,6 +7,7 @@ import IpfsService from "../IpfsService/IpfsService";
import { BackendVariables } from "@Common/config/variables/Variables"; import { BackendVariables } from "@Common/config/variables/Variables";
import { Readable } from "stream"; import { Readable } from "stream";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { Files } from "@prisma/client";
@Service() @Service()
export default class FilesService extends BaseService { export default class FilesService extends BaseService {
@ -23,7 +24,7 @@ export default class FilesService extends BaseService {
* @description : Get all files * @description : Get all files
* @throws {Error} If files cannot be ge * @throws {Error} If files cannot be ge
*/ */
public async get(query: any) { public async get(query: any): Promise<Files[]> {
return this.filesRepository.findMany(query); return this.filesRepository.findMany(query);
} }
@ -31,7 +32,7 @@ export default class FilesService extends BaseService {
* @description : Get a file by uid * @description : Get a file by uid
* @throws {Error} If project cannot be created * @throws {Error} If project cannot be created
*/ */
public async getByUid(uid: string) { public async getByUid(uid: string): Promise<Files | null> {
return this.filesRepository.findOneByUid(uid); return this.filesRepository.findOneByUid(uid);
} }
@ -41,6 +42,7 @@ export default class FilesService extends BaseService {
*/ */
public async download(uid: string) { public async download(uid: string) {
const file = await this.filesRepository.findOneByUid(uid); const file = await this.filesRepository.findOneByUid(uid);
console.log(file, uid);
if (!file?.key) return null; if (!file?.key) return null;
const fileResult = await fetch(file.file_path); const fileResult = await fetch(file.file_path);
const fileContent = await fileResult.arrayBuffer(); const fileContent = await fileResult.arrayBuffer();
@ -51,7 +53,7 @@ export default class FilesService extends BaseService {
* @description : Create a new file * @description : Create a new file
* @throws {Error} If file cannot be created * @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<Files> {
const key = v4(); const key = v4();
fileData.mimetype; fileData.mimetype;
fileData.size; fileData.size;
@ -67,7 +69,7 @@ export default class FilesService extends BaseService {
* @description : Modify a new file * @description : Modify a new file
* @throws {Error} If file cannot be modified * @throws {Error} If file cannot be modified
*/ */
public async update(uid: string, file: File) { public async update(uid: string, file: File): Promise<Files> {
return this.filesRepository.update(uid, file); return this.filesRepository.update(uid, file);
} }
@ -75,10 +77,10 @@ export default class FilesService extends BaseService {
* @description : Delete a file key and archive * @description : Delete a file key and archive
* @throws {Error} If file key cannot be deleted or archived * @throws {Error} If file key cannot be deleted or archived
*/ */
public async deleteKeyAndArchive(uid: string) { public async deleteKeyAndArchive(uid: string): Promise<Files> {
try { try {
const fileToUnpin = await this.filesRepository.findOneByUid(uid); 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); const fileHash = fileToUnpin.file_path.substring(this.variables.PINATA_GATEWAY.length);
await this.ipfsService.unpinFile(fileHash); await this.ipfsService.unpinFile(fileHash);
} catch(error) { } catch(error) {

View File

@ -38,7 +38,7 @@ export default class CustomersService extends BaseService {
* @description : Get a customer by uid * @description : Get a customer by uid
* @throws {Error} If customer cannot be get by uid * @throws {Error} If customer cannot be get by uid
*/ */
public async getByUid(uid: string, query?: any): Promise<Customers> { public async getByUid(uid: string, query?: any): Promise<Customers | null> {
return this.customerRepository.findOneByUid(uid, query); return this.customerRepository.findOneByUid(uid, query);
} }
} }

View File

@ -40,7 +40,7 @@ export default class DeedTypesService extends BaseService {
* @description : Get a deedtype by uid * @description : Get a deedtype by uid
* @throws {Error} If deed-type cannot be get 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<DeedTypes | null> {
return this.deedTypeRepository.findOneByUid(uid, query); return this.deedTypeRepository.findOneByUid(uid, query);
} }
} }

View File

@ -38,7 +38,7 @@ export default class DeedsService extends BaseService {
* @description : Get a deed by uid * @description : Get a deed by uid
* @throws {Error} If deed-type cannot be get 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<Deeds | null> {
return this.deedRepository.findOneByUid(uid, query); return this.deedRepository.findOneByUid(uid, query);
} }
} }

View File

@ -14,7 +14,7 @@ export default class DocumentTypesService extends BaseService {
* @description : Get all document-types * @description : Get all document-types
* @throws {Error} If document-types cannot be get * @throws {Error} If document-types cannot be get
*/ */
public async get(query: any) { public async get(query: any): Promise<DocumentTypes[]> {
return this.documentTypeRepository.findMany(query); return this.documentTypeRepository.findMany(query);
} }
@ -38,7 +38,7 @@ export default class DocumentTypesService extends BaseService {
* @description : Get a document-type by uid * @description : Get a document-type by uid
* @throws {Error} If document-type is not found * @throws {Error} If document-type is not found
*/ */
public async getByUid(uid: string, query?: any) { public async getByUid(uid: string, query?: any): Promise<DocumentTypes | null> {
return this.documentTypeRepository.findOneByUid(uid, query); return this.documentTypeRepository.findOneByUid(uid, query);
} }
} }

View File

@ -48,7 +48,7 @@ export default class DocumentsService extends BaseService {
*/ */
public async delete(uid: string): Promise<Documents> { public async delete(uid: string): Promise<Documents> {
const documentEntity = await this.documentsRepository.findOneByUid(uid, { files: true }); const documentEntity = await this.documentsRepository.findOneByUid(uid, { files: true });
if(!documentEntity) throw new Error("document not found");
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
if (document.files && document.files.length !== 0) { if (document.files && document.files.length !== 0) {
@ -61,7 +61,7 @@ export default class DocumentsService extends BaseService {
* @description : Get a document by uid * @description : Get a document by uid
* @throws {Error} If document cannot be get 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<Documents | null> {
return this.documentsRepository.findOneByUid(uid, query); return this.documentsRepository.findOneByUid(uid, query);
} }
} }

View File

@ -21,7 +21,7 @@ export default class OfficeFoldersService extends BaseService {
* @description : Get all folders * @description : Get all folders
* @throws {Error} If folders cannot be get * @throws {Error} If folders cannot be get
*/ */
public async get(query: any) { public async get(query: any): Promise<OfficeFolders[]> {
return this.officeFoldersRepository.findMany(query); return this.officeFoldersRepository.findMany(query);
} }
@ -31,6 +31,7 @@ export default class OfficeFoldersService extends BaseService {
*/ */
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!); 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'); if(deedType.archived_at) throw new Error('deed type is archived');
const deed = await this.deedRepository.create(officeFolderEntity.deed!); const deed = await this.deedRepository.create(officeFolderEntity.deed!);
officeFolderEntity.deed!.uid = deed.uid; officeFolderEntity.deed!.uid = deed.uid;
@ -49,7 +50,7 @@ export default class OfficeFoldersService extends BaseService {
* @description : Get a folder by uid * @description : Get a folder by uid
* @throws {Error} If folder cannot be get 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<OfficeFolders | null> {
return this.officeFoldersRepository.findOneByUid(uid, query); return this.officeFoldersRepository.findOneByUid(uid, query);
} }
@ -57,8 +58,9 @@ export default class OfficeFoldersService extends BaseService {
* @description : Delete a folder * @description : Delete a folder
* @throws {Error} If document cannot be deleted * @throws {Error} If document cannot be deleted
*/ */
public async delete(uid: string): Promise<OfficeFolders> { public async delete(uid: string): Promise<OfficeFolders> {
const officeFolderEntity = await this.officeFoldersRepository.findOneByUid(uid, { office_folder_has_customers: true }); 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<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" }); const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" });
if (officeFolder.office_folder_has_customers && officeFolder.office_folder_has_customers.length !== 0) { if (officeFolder.office_folder_has_customers && officeFolder.office_folder_has_customers.length !== 0) {

View File

@ -38,7 +38,7 @@ export default class OfficesService extends BaseService {
* @description : Get a office by uid * @description : Get a office by uid
* @throws {Error} If office cannot be get * @throws {Error} If office cannot be get
*/ */
public async getByUid(uid: string, query?: any): Promise<Offices> { public async getByUid(uid: string, query?: any): Promise<Offices | null> {
return this.officeRepository.findOneByUid(uid, query); return this.officeRepository.findOneByUid(uid, query);
} }
} }

View File

@ -39,7 +39,7 @@ export default class UsersService extends BaseService {
* @description : Get a user by uid * @description : Get a user by uid
* @throws {Error} If user cannot be get by uid * @throws {Error} If user cannot be get by uid
*/ */
public getByUid(uid: string, query?: any): Promise<Users> { public getByUid(uid: string, query?: any): Promise<Users | null> {
return this.userRepository.findOneByUid(uid, query); return this.userRepository.findOneByUid(uid, query);
} }
} }