diff --git a/generics/services/users.js b/generics/services/users.js index c5e5a04d..53f8edae 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -141,7 +141,43 @@ const locationSearch = function ( filterData, formatResult = false ) { } }) } +/** + * get Parent Entities of an entity. + * @method + * @name getParentEntities + * @param {String} entityId - entity id + * @returns {Array} - parent entities. +*/ + +async function getParentEntities( entityId, iteration = 0, parentEntities ) { + + if ( iteration == 0 ) { + parentEntities = []; + } + + let filterQuery = { + "id" : entityId + }; + + let entityDetails = await locationSearch(filterQuery); + if ( !entityDetails.success ) { + return parentEntities; + } else { + + let entityData = entityDetails.data[0]; + if ( iteration > 0 ) parentEntities.push(entityData); + if ( entityData.parentId ) { + iteration = iteration + 1; + entityId = entityData.parentId; + await getParentEntities(entityId, iteration, parentEntities); + } + } + + return parentEntities; + +} module.exports = { profile : profile, - locationSearch : locationSearch + locationSearch : locationSearch, + getParentEntities : getParentEntities }; diff --git a/models/programs.js b/models/programs.js index 67b6a227..cb411bb3 100644 --- a/models/programs.js +++ b/models/programs.js @@ -29,7 +29,6 @@ module.exports = { }, scope : { entityType : String, - entityTypeId : "ObjectId", entities : { type : Array, index : true diff --git a/models/project-templates.js b/models/project-templates.js index 45d1604e..4960c60d 100644 --- a/models/project-templates.js +++ b/models/project-templates.js @@ -75,9 +75,6 @@ module.exports = { entityType : { type : String }, - entityTypeId : { - type : "ObjectId" - }, taskSequence : { type : Array, default : [] diff --git a/models/solutions.js b/models/solutions.js index 4e3c4c9f..c55a0508 100644 --- a/models/solutions.js +++ b/models/solutions.js @@ -22,7 +22,6 @@ module.exports = { themes: Array, flattenedThemes : Array, questionSequenceByEcm: Object, - entityTypeId: "ObjectId", entityType: String, type: String, subType: String, @@ -74,7 +73,6 @@ module.exports = { referenceFrom : String, scope : { entityType : String, - entityTypeId : "ObjectId", entities : { type : Array, index : true diff --git a/module/project/templates/helper.js b/module/project/templates/helper.js index 877880b1..51bdcaef 100644 --- a/module/project/templates/helper.js +++ b/module/project/templates/helper.js @@ -219,7 +219,6 @@ module.exports = class ProjectTemplatesHelper { if( parsedData.entityType && parsedData.entityType !== "" ) { parsedData.entityType = csvInformation.entityTypes[parsedData.entityType].name; - parsedData.entityTypeId = csvInformation.entityTypes[parsedData.entityType]._id; } let learningResources = diff --git a/module/userProjects/helper.js b/module/userProjects/helper.js index de6e4b76..65106c60 100644 --- a/module/userProjects/helper.js +++ b/module/userProjects/helper.js @@ -1194,7 +1194,7 @@ module.exports = class UserProjectsHelper { return resolve(entityInformation); } - projectCreation.data["entityInformation"] = _entitiesMetaInformation( + projectCreation.data["entityInformation"] = await _entitiesMetaInformation( entityInformation.data )[0]; @@ -2716,9 +2716,9 @@ function _entitiesInformation(entityIds) { } let entitiesData = []; if ( entityInformations.length > 0 ) { - entitiesData = _entitiesMetaInformation(entityInformations); + entitiesData = await _entitiesMetaInformation(entityInformations); } - + return resolve({ success: true, data: entitiesData @@ -2985,13 +2985,18 @@ function _observationDetails(observationData, userRoleAndProfileInformation = {} */ function _entitiesMetaInformation(entitiesData) { - entitiesData = entitiesData.map(entity => { - entity.metaInformation._id = entity._id; - entity.metaInformation.entityType = entity.entityType; - entity.metaInformation.registryDetails = entity.registryDetails; - return entity.metaInformation; - }); - return entitiesData; + return new Promise(async (resolve, reject) => { + let entityInformation = [] + for ( let index = 0; index < entitiesData.length; index++ ) { + let entityHierarchy = await userProfileService.getParentEntities( entitiesData[index]._id ); + entitiesData[index].metaInformation.hierarchy = entityHierarchy; + entitiesData[index].metaInformation._id = entitiesData[index]._id; + entitiesData[index].metaInformation.entityType = entitiesData[index].entityType; + entitiesData[index].metaInformation.registryDetails = entitiesData[index].registryDetails; + entityInformation.push(entitiesData[index].metaInformation) + } + return resolve (entityInformation); + }) }