Skip to content

Commit

Permalink
fix(required_fields): checkRequiredFields can't work like. fixing con…
Browse files Browse the repository at this point in the history
…dition and use better filter
  • Loading branch information
Gerald Baulig committed Nov 14, 2024
1 parent 03fbd3f commit c8ef1bf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
39 changes: 15 additions & 24 deletions src/core/ResourcesAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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];
Expand All @@ -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}`);
Expand All @@ -267,7 +266,7 @@ export class ResourcesAPIBase {
}
}
}
if (_.isArray(createVertexResp)) {
if (Array.isArray(createVertexResp)) {
createVertexResp.forEach((eachVertexResp) => result.push(eachVertexResp));
} else {
result.push(createVertexResp);
Expand Down Expand Up @@ -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 };
Expand All @@ -347,7 +338,7 @@ export class ResourcesAPIBase {
async delete(ids: string[]): Promise<any> {
let deleteResponse = [];
try {
if (!_.isArray(ids)) {
if (!Array.isArray(ids)) {
ids = [ids];
}
if (this.isGraphDB(this.db)) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/core/ServiceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class ServiceBase<T extends ResourceListResponse, M extends ResourceList>
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) {
Expand All @@ -242,7 +242,7 @@ export class ServiceBase<T extends ResourceListResponse, M extends ResourceList>
});
await Promise.all(dispatch);
}
let createResponseWithStatus = this.generateResponseWithStatus(createResponse, createDocs);
const createResponseWithStatus = this.generateResponseWithStatus(createResponse, createDocs);
const operation_status = {
code: 200,
message: 'success'
Expand Down
12 changes: 6 additions & 6 deletions test/cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -180,7 +180,7 @@
{
"name": "pipeline",
"provider": "grpc",
"addr": "localhost:50051"
"addr": "localhost:50151"
}
]
},
Expand Down Expand Up @@ -250,7 +250,7 @@
{
"name": "pipeline",
"provider": "grpc",
"addr": "localhost:50052"
"addr": "localhost:50152"
}
]
},
Expand All @@ -263,7 +263,7 @@
"timeStampFields": [
{
"fields": ["created", "meta.created", "meta.modified"],
"entities": ["resources"]
"entities": ["resource"]
}
],
"requiredFields": {
Expand Down
2 changes: 1 addition & 1 deletion test/crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit c8ef1bf

Please sign in to comment.