diff --git a/src/core/ResourcesAPI.ts b/src/core/ResourcesAPI.ts index 199e23c..a7d001a 100644 --- a/src/core/ResourcesAPI.ts +++ b/src/core/ResourcesAPI.ts @@ -211,7 +211,6 @@ export class ResourcesAPIBase { const collection = this.collectionName; let result = []; try { - let result = []; // check if all the required fields are present if (this.requiredFields) { const requiredFieldsResult = this.checkRequiredFields( @@ -239,7 +238,7 @@ export class ResourcesAPIBase { await this.db.addVertexCollection(collection); let createVertexResp = await this.db.createVertex(collection, documents); for (let document of documents) { - if (this.edgeCfg && _.isArray(this.edgeCfg) && this.edgeCfg.length > 0) { + if (this.edgeCfg && Array.isArray(this.edgeCfg) && this.edgeCfg.length > 0) { for (let eachEdgeCfg of this.edgeCfg) { const fromIDkey = eachEdgeCfg.from; const from_id = document[fromIDkey]; @@ -254,7 +253,7 @@ export class ResourcesAPIBase { toVerticeName = collection; } if (from_id && to_id) { - if (_.isArray(to_id)) { + if (Array.isArray(to_id)) { for (let toID of to_id) { await this.db.createEdge(eachEdgeCfg.edgeName, null, `${fromVerticeName}/${from_id}`, `${toVerticeName}/${toID}`); @@ -267,7 +266,7 @@ export class ResourcesAPIBase { } } } - if (_.isArray(createVertexResp)) { + if (Array.isArray(createVertexResp)) { createVertexResp.forEach((eachVertexResp) => result.push(eachVertexResp)); } else { result.push(createVertexResp); @@ -314,26 +313,18 @@ export class ResourcesAPIBase { * @param requiredFields * @param documents */ - checkRequiredFields(requiredFields: string[], documents: any, result: any[]): any { - documents.forEach((document) => { - requiredFields.forEach((eachField) => { - const isArray = _.isArray(eachField); - if (!document[eachField]) { + checkRequiredFields(requiredFields: string[], documents: any[], result: any[]): any { + documents = documents.filter((document) => { + return requiredFields.every((eachField) => { + if (document[eachField] === undefined || (Array.isArray(document[eachField]) && document[eachField].length === 0)) { result.push({ error: true, errorNum: 400, errorMessage: `Field ${eachField} is necessary for ${this.resourceName} for documentID ${document.id}` }); - documents = documents.filter(doc => doc.id != document.id); - } - if ((isArray && document[eachField].length == 0)) { - result.push({ - error: true, - errorNum: 400, - errorMessage: `Field ${eachField} is necessary for ${this.resourceName} for documentID ${document.id}` - }); - documents = documents.filter(doc => doc.id != document.id); + return false; } + return true; }); }); return { documents, result }; @@ -347,7 +338,7 @@ export class ResourcesAPIBase { async delete(ids: string[]): Promise { let deleteResponse = []; try { - if (!_.isArray(ids)) { + if (!Array.isArray(ids)) { ids = [ids]; } if (this.isGraphDB(this.db)) { @@ -510,10 +501,10 @@ export class ResourcesAPIBase { const toIDkey = eachEdgeCfg.to; let modified_to_idValues = doc[toIDkey]; let db_to_idValues = dbDoc[toIDkey]; - if (_.isArray(modified_to_idValues)) { + if (Array.isArray(modified_to_idValues)) { modified_to_idValues = _.sortBy(modified_to_idValues); } - if (_.isArray(db_to_idValues)) { + if (Array.isArray(db_to_idValues)) { db_to_idValues = _.sortBy(db_to_idValues); } // delete and recreate only if there is a difference in references @@ -531,16 +522,16 @@ export class ResourcesAPIBase { const edgeCollectionName = eachEdgeCfg.edgeName; let outgoingEdges: any = await db.getOutEdges(edgeCollectionName, `${collectionName}/${dbDoc.id}`); - if (_.isArray(outgoingEdges.edges)) { + if (Array.isArray(outgoingEdges.edges)) { await Promise.all(outgoingEdges.edges.map((outgoingEdge) => db.removeEdge(edgeCollectionName, outgoingEdge._id))); } let incomingEdges: any = await db.getInEdges(edgeCollectionName, `${collectionName}/${dbDoc.id}`); - if (_.isArray(incomingEdges.edges)) { + if (Array.isArray(incomingEdges.edges)) { await Promise.all(incomingEdges.edges.map((incomingEdge) => db.removeEdge(edgeCollectionName, incomingEdge._id))); } // Create new edges if (from_id && modified_to_idValues) { - if (_.isArray(modified_to_idValues)) { + if (Array.isArray(modified_to_idValues)) { await Promise.all(modified_to_idValues.map((toID) => db.createEdge(eachEdgeCfg.edgeName, null, `${fromVerticeName}/${from_id}`, `${toVerticeName}/${toID}`))); } else { diff --git a/src/core/ServiceBase.ts b/src/core/ServiceBase.ts index 30f1161..56a57ea 100644 --- a/src/core/ServiceBase.ts +++ b/src/core/ServiceBase.ts @@ -231,7 +231,7 @@ export class ServiceBase let docs: any = {}; try { const createDocs = _.cloneDeep(request.items); - let createResponse = await this.resourceapi.create(createDocs, request.subject); + const createResponse = await this.resourceapi.create(createDocs, request.subject); const dispatch = []; const events: Topic = this.events; if (this.isEventsEnabled) { @@ -242,7 +242,7 @@ export class ServiceBase }); await Promise.all(dispatch); } - let createResponseWithStatus = this.generateResponseWithStatus(createResponse, createDocs); + const createResponseWithStatus = this.generateResponseWithStatus(createResponse, createDocs); const operation_status = { code: 200, message: 'success' diff --git a/test/cfg/config.json b/test/cfg/config.json index 2e66e07..3e97ede 100644 --- a/test/cfg/config.json +++ b/test/cfg/config.json @@ -80,13 +80,13 @@ }, "client": { "test": { - "address": "localhost:50051" + "address": "localhost:50151" }, "testBufferedService": { - "address": "localhost:50052" + "address": "localhost:50152" }, "graphsTestService": { - "address": "localhost:50051" + "address": "localhost:50151" } }, "events": { @@ -180,7 +180,7 @@ { "name": "pipeline", "provider": "grpc", - "addr": "localhost:50051" + "addr": "localhost:50151" } ] }, @@ -250,7 +250,7 @@ { "name": "pipeline", "provider": "grpc", - "addr": "localhost:50052" + "addr": "localhost:50152" } ] }, @@ -263,7 +263,7 @@ "timeStampFields": [ { "fields": ["created", "meta.created", "meta.modified"], - "entities": ["resources"] + "entities": ["resource"] } ], "requiredFields": { diff --git a/test/crud.spec.ts b/test/crud.spec.ts index 8be23db..5a11293 100644 --- a/test/crud.spec.ts +++ b/test/crud.spec.ts @@ -234,7 +234,7 @@ describe('ServiceBase', () => { } resourceFieldConfig['timeStampFields'] = []; for (let timeStampFiledConfig of timeStampFieldsConfigs) { - if (timeStampFiledConfig.entities.includes(`${resourceName}s`)) { + if (timeStampFiledConfig.entities.includes(resourceName)) { resourceFieldConfig['timeStampFields'].push(...timeStampFiledConfig.fields); } }