Skip to content

Commit

Permalink
fixed area weight attributes creation (#3185)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Dec 11, 2023
1 parent 1b25f89 commit f4fa810
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
49 changes: 25 additions & 24 deletions common/analysis/samplingNodeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,38 @@ const determinePlotAreaNodeDefs = ({ survey, chain }) => {
NodeDef.isEntity(descendantEntity) && (NodeDef.isMultiple(descendantEntity) || NodeDef.isRoot(descendantEntity))
)

const createAreaNodeDefIfNecessary = ({ existingNodeDef, nodeDefParent, isWeight }) => {
if (existingNodeDef) {
// weight def already existing
validNodeDefsAlreadyExisting.push(existingNodeDef)
} else {
const newSamplingNodeDef = newEntityAreaNodeDef({
nodeDefParent,
baseUnitNodeDef,
chainUuid,
cycleKeys,
isWeight,
})
nodeDefsToCreate.push(newSamplingNodeDef)
}
}

descendantEntities.forEach((nodeDefParent) => {
const childDefs = Survey.getNodeDefChildren(nodeDefParent, true)(survey)
const existingEntityAreaNodeDef = childDefs.find((childDef) =>
isEntityAreaNodeDef({ nodeDef: childDef, nodeDefParent })
)
const parentIsBaseUnit = NodeDef.isEqual(nodeDefParent)(baseUnitNodeDef)
const existingWeightNodeDef = parentIsBaseUnit ? childDefs.find(isWeightNodeDef) : null
const isBaseUnit = NodeDef.isEqual(nodeDefParent)(baseUnitNodeDef)
const existingNodeDef = isBaseUnit ? childDefs.find(isWeightNodeDef) : null
const hasAreaBasedDef = childDefs.some((childDef) => NodeDef.isAreaBasedEstimatedOf(childDef))

if (hasAreaBasedDef || parentIsBaseUnit) {
if (existingWeightNodeDef) {
// weight def already existing
validNodeDefsAlreadyExisting.push(existingWeightNodeDef)
} else if (parentIsBaseUnit) {
if (hasAreaBasedDef || isBaseUnit) {
if (isBaseUnit) {
// create new weight node def only for base unit entity
const newSamplingNodeDef = newEntityAreaNodeDef({
nodeDefParent,
baseUnitNodeDef,
chainUuid,
cycleKeys,
isWeight: true,
})
nodeDefsToCreate.push(newSamplingNodeDef)
}
if (existingEntityAreaNodeDef) {
// entity area node def already existing
validNodeDefsAlreadyExisting.push(existingEntityAreaNodeDef)
} else {
// create new entity area node def
const newSamplingNodeDef = newEntityAreaNodeDef({ nodeDefParent, baseUnitNodeDef, chainUuid, cycleKeys })
nodeDefsToCreate.push(newSamplingNodeDef)
createAreaNodeDefIfNecessary({ existingNodeDef, nodeDefParent, isWeight: true })
} else if (hasAreaBasedDef) {
// create new plot_area node def only if there is an area based node def
createAreaNodeDefIfNecessary({ existingNodeDef, nodeDefParent, isWeight: false })
}
} else if (existingEntityAreaNodeDef) {
// delete entity area node defs when entity doesn't have any area based node def
Expand Down Expand Up @@ -163,8 +164,8 @@ const getSamplingDefsInEntities = ({ survey, chain, entities, analysisNodeDefs }
}

export const SamplingNodeDefs = {
SAMPLING_PLOT_AREA_NODE_DEF_BASE_UNIT_NAME: WEIGHT_NODE_DEF_NAME,
getEntityAreaNodeDefName,
isWeightNodeDef,
isEntityAreaNodeDef,
isBaseUnitEntityAreaNodeDef,
determinePlotAreaNodeDefs,
Expand Down
5 changes: 1 addition & 4 deletions server/modules/survey/service/schemaSummary/schemaSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => {
const nodeDefs = Survey.getNodeDefsArray(survey).filter(
(nodeDef) =>
// exclude "weight" node def created by the processing chain
!(
NodeDef.isAnalysis(nodeDef) &&
NodeDef.getName(nodeDef) === SamplingNodeDefs.SAMPLING_PLOT_AREA_NODE_DEF_BASE_UNIT_NAME
)
!(NodeDef.isAnalysis(nodeDef) && SamplingNodeDefs.isWeightNodeDef(nodeDef))
)
const pathByNodeDefUuid = nodeDefs.reduce(
(paths, nodeDef) => ({ ...paths, [nodeDef.uuid]: getNodeDefPath({ survey, nodeDef }) }),
Expand Down

0 comments on commit f4fa810

Please sign in to comment.