From 3effd09a7598ce73d47cb6c0ea663e2c9e36a899 Mon Sep 17 00:00:00 2001 From: Stefano Ricci <1219739+SteRiccio@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:04:46 +0200 Subject: [PATCH] schema summary: sort node definitions in the same order as in the form designer (#3546) * schema summary: sort node definitions in the same order as in the form designer * fixing test * fixing tests --------- Co-authored-by: Stefano Ricci Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- core/survey/_survey/surveyNodeDefs.js | 3 +- .../service/schemaSummary/schemaSummary.js | 31 ++++++++----------- test/e2e/tests/surveySchemaSummary.js | 19 ++++-------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/core/survey/_survey/surveyNodeDefs.js b/core/survey/_survey/surveyNodeDefs.js index 043c1346dd..5bbe23fc64 100644 --- a/core/survey/_survey/surveyNodeDefs.js +++ b/core/survey/_survey/surveyNodeDefs.js @@ -350,11 +350,12 @@ export const traverseHierarchyItemSync = (nodeDefItem, visitorFn, depth = 0) => } export const visitDescendantsAndSelf = - ({ nodeDef = null, visitorFn, traverseMethod = TraverseMethod.bfs }) => + ({ visitorFn, nodeDef = null, cycle = null, traverseMethod = TraverseMethod.bfs }) => (survey) => { const nodeDefToVisit = nodeDef ?? getNodeDefRoot(survey) return Surveys.visitDescendantsAndSelfNodeDef({ survey, + cycle, nodeDef: nodeDefToVisit, visitor: visitorFn, traverseMethod, diff --git a/server/modules/survey/service/schemaSummary/schemaSummary.js b/server/modules/survey/service/schemaSummary/schemaSummary.js index 648fb32977..f0de2875f8 100644 --- a/server/modules/survey/service/schemaSummary/schemaSummary.js +++ b/server/modules/survey/service/schemaSummary/schemaSummary.js @@ -54,23 +54,18 @@ const getValidationsSummary = ({ nodeDef }) => { export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => { const survey = await SurveyManager.fetchSurveyAndNodeDefsBySurveyId({ surveyId, draft: true, advanced: true }) - const nodeDefs = Survey.getNodeDefsArray(survey).filter( - (nodeDef) => - // exclude "weight" node def created by the processing chain - !(NodeDef.isAnalysis(nodeDef) && SamplingNodeDefs.isWeightNodeDef(nodeDef)) - ) - const pathByNodeDefUuid = nodeDefs.reduce( - (paths, nodeDef) => ({ ...paths, [nodeDef.uuid]: getNodeDefPath({ survey, nodeDef }) }), - {} - ) - // sort node defs by path - nodeDefs.sort((nodeDef1, nodeDef2) => { - const path1 = pathByNodeDefUuid[nodeDef1.uuid] - const path2 = pathByNodeDefUuid[nodeDef2.uuid] - if (path1 > path2) return 1 - if (path2 > path1) return -1 - return 0 - }) + const nodeDefs = [] + Survey.visitDescendantsAndSelf({ + cycle, + visitorFn: (nodeDef) => { + if ( + // exclude "weight" node def created by the processing chain + !(NodeDef.isAnalysis(nodeDef) && SamplingNodeDefs.isWeightNodeDef(nodeDef)) + ) { + nodeDefs.push(nodeDef) + } + }, + })(survey) const getCategoryName = (nodeDef) => { if (!NodeDef.isCode(nodeDef)) return '' @@ -114,7 +109,7 @@ export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => { return { uuid, name: NodeDef.getName(nodeDef), - path: pathByNodeDefUuid[uuid], + path: getNodeDefPath({ survey, nodeDef }), parentEntity: NodeDef.getName(Survey.getNodeDefParent(nodeDef)(survey)), // labels ...languages.reduce( diff --git a/test/e2e/tests/surveySchemaSummary.js b/test/e2e/tests/surveySchemaSummary.js index 69383d40ee..1e35cfd3e7 100644 --- a/test/e2e/tests/surveySchemaSummary.js +++ b/test/e2e/tests/surveySchemaSummary.js @@ -7,7 +7,7 @@ import { gotoFormDesigner } from './_navigation' import { cluster } from '../mock/nodeDefs' import { parseCsvAsync } from '../../utils/csvUtils' -const getTestNodeDefsOrderedByPath = () => { +const getTestNodeDefs = () => { const items = [] const stack = [{ parentPath: null, nodeDef: cluster }] @@ -27,14 +27,7 @@ const getTestNodeDefsOrderedByPath = () => { ) } } - // sort items by path - return items.sort((item1, item2) => { - const path1 = item1.path - const path2 = item2.path - if (path1 > path2) return 1 - if (path2 > path1) return -1 - return 0 - }) + return items } export default () => @@ -58,21 +51,21 @@ export default () => await expect(fs.existsSync(filePath)).toBeTruthy() }) - const nodeDefsOrderedByPath = getTestNodeDefsOrderedByPath() + const nodeDefs = getTestNodeDefs() test(`Check generated schema summary`, async () => { await expect(fs.existsSync(filePath)).toBeTruthy() data = await parseCsvAsync(filePath) - await expect(data.length).toBe(nodeDefsOrderedByPath.length) + await expect(data.length).toBe(nodeDefs.length) }) - nodeDefsOrderedByPath.forEach((nodeDefItem, index) => { + nodeDefs.forEach((nodeDefItem) => { const { nodeDef, path: nodeDefPath } = nodeDefItem test(`Check ${nodeDef.name} def`, async () => { await expect(data).toBeDefined() - const nodeDefData = data[index] + const nodeDefData = data.find((dataItem) => dataItem.name === nodeDef.name) await expect(Object.keys(nodeDefData)).toEqual([ 'uuid', 'name',