Skip to content

Commit

Permalink
re-calculate node defs hierarchy when survey is loaded (#3494)
Browse files Browse the repository at this point in the history
* re-calculate node defs hierarchy when survey is loaded

* code cleanup

---------

Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Jul 3, 2024
1 parent b56d0ea commit 3c84489
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/survey/nodeDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const keysPropsAdvanced = {
itemsFilter: 'itemsFilter',
}

const metaKeys = {
export const metaKeys = {
h: 'h',
}

Expand Down
20 changes: 19 additions & 1 deletion server/modules/nodeDef/manager/nodeDefManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as R from 'ramda'
import { db } from '@server/db/db'

import { Objects } from '@openforis/arena-core'

import * as Survey from '@core/survey/survey'
import * as NodeDef from '@core/survey/nodeDef'
import * as NodeDefLayout from '@core/survey/nodeDefLayout'
Expand All @@ -13,7 +15,6 @@ import * as ActivityLogRepository from '@server/modules/activityLog/repository/a
import * as NodeDefRepository from '../repository/nodeDefRepository'
import { markSurveyDraft } from '../../survey/repository/surveySchemaRepositoryUtils'
import { NodeDefAreaBasedEstimateManager } from './nodeDefAreaBasedEstimateManager'
import { Objects } from '@openforis/arena-core'

export {
addNodeDefsCycles,
Expand Down Expand Up @@ -150,6 +151,17 @@ const _filterOutInvalidNodeDefs = (nodeDefsByUuid) => {
return nodeDefsByUuid
}

const _calculateNodeDefHierarchy = ({ nodeDef, nodeDefsByUuid }) => {
const hiearchy = []
let currentParentUuid = NodeDef.getParentUuid(nodeDef)
while (currentParentUuid) {
hiearchy.unshift(currentParentUuid)
const currentParentNode = nodeDefsByUuid[currentParentUuid]
currentParentUuid = NodeDef.getParentUuid(currentParentNode)
}
return hiearchy
}

export const fetchNodeDefsBySurveyId = async (
{
surveyId,
Expand All @@ -168,6 +180,12 @@ export const fetchNodeDefsBySurveyId = async (
)
const nodeDefsByUuid = ObjectUtils.toUuidIndexedObj(nodeDefsDb)

// re-calculate node defs hierarchy (it could be wrong)
nodeDefsDb.forEach((nodeDef) => {
const hierarchy = _calculateNodeDefHierarchy({ nodeDef, nodeDefsByUuid })
Objects.setInPath({ obj: nodeDef, path: [NodeDef.keys.meta, NodeDef.metaKeys.h], value: hierarchy })
})

return _filterOutInvalidNodeDefs(nodeDefsByUuid)
}

Expand Down

0 comments on commit 3c84489

Please sign in to comment.