From 8caa0ea89ba6e1325e656629996a56570a96e690 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 15 Jan 2024 17:18:18 +0100 Subject: [PATCH] fix: calculation of variable mixing array and aggregates Fix #848 --- .../commons/variables/lunatic-variables-store.spec.ts | 11 +++++++++++ .../commons/variables/lunatic-variables-store.ts | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts b/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts index 363ee57b1..838141eb5 100644 --- a/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts +++ b/src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts @@ -204,6 +204,17 @@ describe('lunatic-variables-store', () => { ); expect(variables.get('FULLNAME')).toEqual(['John 1', 'Jane 2']); }); + it('should handle aggregate functions', () => { + variables.set('AGE', [1, 2, 3]); + variables.setCalculated('MAXAGE', 'max(AGE)'); + variables.setCalculated('AGE_AND_MAX', 'AGE + MAXAGE', { + shapeFrom: 'AGE', + }); + expect(variables.get('AGE_AND_MAX', [0])).toEqual(4); + variables.set('AGE', 12, { iteration: [1] }); + expect(variables.get('AGE', [1])).toEqual(12); + expect(variables.get('AGE_AND_MAX', [0])).toEqual(13); + }); }); describe('resizing', () => { diff --git a/src/use-lunatic/commons/variables/lunatic-variables-store.ts b/src/use-lunatic/commons/variables/lunatic-variables-store.ts index e1b463db3..a9764190e 100644 --- a/src/use-lunatic/commons/variables/lunatic-variables-store.ts +++ b/src/use-lunatic/commons/variables/lunatic-variables-store.ts @@ -395,7 +395,12 @@ class LunaticVariable { 0, ...this.getDependencies().map( (dep) => - this.dictionary?.get(dep)?.updatedAt.get(iteration?.join('.')) ?? 0 + // Check when a value at the same iteration was calculated + this.dictionary?.get(dep)?.updatedAt.get(iteration?.join('.')) ?? + // For aggregated value (max / min) look the global updatedAt time + this.dictionary?.get(dep)?.updatedAt.get(undefined) ?? + // Otherwise this is a static value that never changes + 0 ) ); return (