asma-core-helpers 0.32.0 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/collaboration/applyOperation.d.ts +5 -13
- package/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +450 -20
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/operations.d.ts +113 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +7 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +54 -0
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +74 -1
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +194 -0
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js +104 -0
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +24 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +180 -2
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/lib/collaboration/typeContracts.d.ts +73 -1
- package/lib/collaboration/typeContracts.d.ts.map +1 -1
- package/lib/g-definitions.d.ts +17 -0
- package/lib/g-definitions.d.ts.map +1 -1
- package/lib/g-definitions.js +13 -0
- package/lib/g-definitions.js.map +1 -1
- package/lib/helpers/sectionPath.helpers.d.ts +64 -0
- package/lib/helpers/sectionPath.helpers.d.ts.map +1 -0
- package/lib/helpers/sectionPath.helpers.js +68 -0
- package/lib/helpers/sectionPath.helpers.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.ts +517 -17
- package/src/collaboration/operations.ts +95 -0
- package/src/collaboration/property.test.ts +49 -0
- package/src/collaboration/schemas.ts +82 -0
- package/src/collaboration/templateDocument.ts +238 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +201 -2
- package/src/collaboration/typeContracts.ts +106 -1
- package/src/collaboration/typedAuthoring.test.ts +1091 -0
- package/src/collaboration/typedAuthoringHardening.test.ts +344 -1
- package/src/g-definitions.ts +29 -0
- package/src/helpers/sectionPath.helpers.test.ts +159 -0
- package/src/helpers/sectionPath.helpers.ts +129 -0
- package/src/index.ts +1 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { TemplateOp } from './operations.js'
|
|
2
2
|
import type {
|
|
3
3
|
ActionMetadata,
|
|
4
|
+
AlternativeChartLegend,
|
|
4
5
|
BindingCardinality,
|
|
6
|
+
ChartLegend,
|
|
5
7
|
KnownActionKind,
|
|
6
8
|
BindingOnMany,
|
|
7
9
|
BindingOnMissing,
|
|
@@ -9,18 +11,24 @@ import type {
|
|
|
9
11
|
MappingBinding,
|
|
10
12
|
MappingFilter,
|
|
11
13
|
MappingNode,
|
|
14
|
+
QnrAlternative,
|
|
12
15
|
QnrDataMapping,
|
|
13
16
|
QnrAction,
|
|
14
17
|
QnrQuestion,
|
|
18
|
+
QnrTab,
|
|
15
19
|
QnrTemplateDocument,
|
|
16
20
|
QuestionId,
|
|
17
21
|
} from './templateDocument.js'
|
|
22
|
+
import type { QuestionType } from './questionTypes.js'
|
|
18
23
|
import {
|
|
19
24
|
ACTION_TYPES,
|
|
20
25
|
BINDING_OPTION_DEFAULTS,
|
|
21
26
|
MAPPING_FILTER_OPERATORS,
|
|
22
27
|
bindingTargetKey,
|
|
23
28
|
isKnownActionKind,
|
|
29
|
+
makeExpressionAlternativeToken,
|
|
30
|
+
makeExpressionTargetToken,
|
|
31
|
+
parseLegacyBindingOverride,
|
|
24
32
|
} from './templateDocument.js'
|
|
25
33
|
|
|
26
34
|
/** Runtime narrowing for the two closed vocabularies the reducer must re-check on replay. */
|
|
@@ -242,16 +250,72 @@ const gridsOwningAction = (doc: QnrTemplateDocument, actionId: string): Question
|
|
|
242
250
|
* question's placement in one edit. Both directions of overlap are refused; `label` and any other
|
|
243
251
|
* authored scalar stay ordinary field edits.
|
|
244
252
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
253
|
+
/**
|
|
254
|
+
* The tab-owned collections `tab.updateField` may not write, and the operation that owns each.
|
|
255
|
+
*
|
|
256
|
+
* All three are maintained one member at a time. A whole-snapshot write through `updateField` would
|
|
257
|
+
* reintroduce exactly the lost update the member-wise operations exist to prevent: two authors editing
|
|
258
|
+
* different members would produce two full-collection patches and the second would discard the first.
|
|
259
|
+
*/
|
|
260
|
+
const TAB_OWNED_FIELD_PATHS = [
|
|
261
|
+
['layout.placementsByQuestionId', 'tab.setLayout'],
|
|
262
|
+
['questionIds', 'tab.setQuestion'],
|
|
263
|
+
['rowCountQuestionIds', 'tab.setRowCountQuestion'],
|
|
264
|
+
] as const
|
|
247
265
|
|
|
248
|
-
|
|
249
|
-
|
|
266
|
+
/**
|
|
267
|
+
* The reserved path `field` would write, if any.
|
|
268
|
+
*
|
|
269
|
+
* Both directions count, as they do for the grid-owned paths: the path itself, a path *below* it
|
|
270
|
+
* (`questionIds.0`), and an *ancestor* whose write would carry the reserved collection with it
|
|
271
|
+
* (`layout`). Checking only exact equality would leave `layout` and `questionIds.0` open, which is a
|
|
272
|
+
* whole-snapshot overwrite by another spelling.
|
|
273
|
+
*/
|
|
274
|
+
export const tabFieldPathRefusal = (field: string): string | undefined => {
|
|
275
|
+
for (const [reserved, owner] of TAB_OWNED_FIELD_PATHS) {
|
|
276
|
+
if (field === reserved || field.startsWith(`${reserved}.`) || reserved.startsWith(`${field}.`)) {
|
|
277
|
+
return `Field "${field}" writes "${reserved}", which only ${owner} may author`
|
|
278
|
+
}
|
|
250
279
|
}
|
|
251
280
|
|
|
252
281
|
return undefined
|
|
253
282
|
}
|
|
254
283
|
|
|
284
|
+
/** One tab-owned primitive id list, defensively filtered: the tab bag is open and may hold anything. */
|
|
285
|
+
const tabListOf = (tab: QnrTab, key: 'questionIds' | 'rowCountQuestionIds'): string[] => {
|
|
286
|
+
const value = tab[key]
|
|
287
|
+
return Array.isArray(value) ? value.filter((member): member is string => typeof member === 'string') : []
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Writes one tab-owned list back, omitting it when empty — DOC-LAW-2 admits no stored `[]`. */
|
|
291
|
+
const writeTabList = (
|
|
292
|
+
tab: Record<string, unknown>,
|
|
293
|
+
key: 'questionIds' | 'rowCountQuestionIds',
|
|
294
|
+
next: readonly string[],
|
|
295
|
+
): Record<string, unknown> => {
|
|
296
|
+
const copy = { ...tab }
|
|
297
|
+
if (next.length === 0) delete copy[key]
|
|
298
|
+
else copy[key] = [...next]
|
|
299
|
+
return copy
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Drops a question from a tab's layout, pruning the emptied placements map and layout object. */
|
|
303
|
+
const withoutTabPlacement = (tab: Record<string, unknown>, questionId: QuestionId): Record<string, unknown> => {
|
|
304
|
+
const layout = (tab['layout'] ?? {}) as Record<string, unknown>
|
|
305
|
+
const placements = (layout['placementsByQuestionId'] ?? {}) as Record<string, LayoutPlacement>
|
|
306
|
+
if (placements[questionId] === undefined) return tab
|
|
307
|
+
|
|
308
|
+
const nextPlacements = omitKey(placements, questionId)
|
|
309
|
+
const nextLayout = { ...layout }
|
|
310
|
+
if (Object.keys(nextPlacements).length === 0) delete nextLayout['placementsByQuestionId']
|
|
311
|
+
else nextLayout['placementsByQuestionId'] = nextPlacements
|
|
312
|
+
|
|
313
|
+
const next = { ...tab }
|
|
314
|
+
if (Object.keys(nextLayout).length === 0) delete next['layout']
|
|
315
|
+
else next['layout'] = nextLayout
|
|
316
|
+
return next
|
|
317
|
+
}
|
|
318
|
+
|
|
255
319
|
/**
|
|
256
320
|
* Validates a `mappingFilter.setTyped` payload in the REDUCER, not only at the wire.
|
|
257
321
|
*
|
|
@@ -487,33 +551,216 @@ const scrubGridActionRefs = (
|
|
|
487
551
|
}
|
|
488
552
|
|
|
489
553
|
/** Drops a deleted question's placement from every tab layout, and every emptied container with it. */
|
|
490
|
-
const
|
|
554
|
+
const scrubTabQuestionRefs = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
491
555
|
const tabs = Object.entries(doc.tabsById ?? {})
|
|
492
556
|
if (tabs.length === 0) return doc
|
|
493
557
|
|
|
494
558
|
let changed = false
|
|
495
559
|
const nextTabs = Object.fromEntries(
|
|
496
560
|
tabs.map(([tabId, tab]) => {
|
|
561
|
+
const members = tabListOf(tab, 'questionIds')
|
|
562
|
+
const counted = tabListOf(tab, 'rowCountQuestionIds')
|
|
497
563
|
const layout = (tab.layout ?? {}) as Record<string, unknown>
|
|
498
564
|
const placements = (layout['placementsByQuestionId'] ?? {}) as Record<string, LayoutPlacement>
|
|
499
|
-
if (placements[questionId] === undefined) return [tabId, tab]
|
|
500
565
|
|
|
501
|
-
|
|
502
|
-
const
|
|
503
|
-
const
|
|
504
|
-
if (
|
|
505
|
-
else nextLayout['placementsByQuestionId'] = nextPlacements
|
|
566
|
+
const inMembers = members.includes(questionId)
|
|
567
|
+
const inCounted = counted.includes(questionId)
|
|
568
|
+
const inLayout = placements[questionId] !== undefined
|
|
569
|
+
if (!inMembers && !inCounted && !inLayout) return [tabId, tab]
|
|
506
570
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
571
|
+
changed = true
|
|
572
|
+
let next: Record<string, unknown> = { ...tab }
|
|
573
|
+
if (inMembers) next = writeTabList(next, 'questionIds', members.filter((id) => id !== questionId))
|
|
574
|
+
if (inCounted) {
|
|
575
|
+
next = writeTabList(next, 'rowCountQuestionIds', counted.filter((id) => id !== questionId))
|
|
576
|
+
}
|
|
577
|
+
if (inLayout) next = withoutTabPlacement(next, questionId)
|
|
578
|
+
return [tabId, next as QnrTab]
|
|
511
579
|
}),
|
|
512
580
|
)
|
|
513
581
|
|
|
514
582
|
return changed ? { ...doc, tabsById: nextTabs } : doc
|
|
515
583
|
}
|
|
516
584
|
|
|
585
|
+
/**
|
|
586
|
+
* The question types that may be an Expression/Chart target: they carry alternatives to score, plus
|
|
587
|
+
* `LinearScale`, whose numeric range legacy also admits.
|
|
588
|
+
*
|
|
589
|
+
* `Chart` is excluded even though it carries alternatives — a chart scoring a chart is the cycle legacy
|
|
590
|
+
* never allowed.
|
|
591
|
+
*
|
|
592
|
+
* **A deliberately explicit opt-in literal, not derived from anything.** There is no selectable-type
|
|
593
|
+
* register in `questionTypes.ts` to derive it from, and inventing one would make targetability a
|
|
594
|
+
* side-effect of how a type is classified elsewhere. Adding a question type must be a decision taken
|
|
595
|
+
* here, in this list.
|
|
596
|
+
*
|
|
597
|
+
* `satisfies readonly QuestionType[]` is what stops that literal from rotting: a typo'd or removed
|
|
598
|
+
* question type fails `ts:check` instead of becoming silently dead code that refuses a legitimate
|
|
599
|
+
* target forever. The membership itself — which six types, and no others — is pinned in both directions
|
|
600
|
+
* by a table-driven test, because the type system cannot tell a deliberate list from a wrong one.
|
|
601
|
+
*/
|
|
602
|
+
const EXPRESSION_TARGET_TYPES = [
|
|
603
|
+
'BooleanQuestion',
|
|
604
|
+
'CheckBoxes',
|
|
605
|
+
'Dropdown',
|
|
606
|
+
'Emoticons',
|
|
607
|
+
'LinearScale',
|
|
608
|
+
'RadioButtons',
|
|
609
|
+
] as const satisfies readonly QuestionType[]
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Whether one question may be cited as an Expression target or a Chart assignment target.
|
|
613
|
+
*
|
|
614
|
+
* `flags.is_expression` must be explicitly `true`. The flags bag is open, so a merely *present* flag is
|
|
615
|
+
* not enough: a question carrying `is_expression: 'no'` or an imported non-boolean would otherwise pass.
|
|
616
|
+
*/
|
|
617
|
+
const isExpressionTarget = (question: QnrQuestion | undefined): boolean => {
|
|
618
|
+
if (!question) return false
|
|
619
|
+
if (!(EXPRESSION_TARGET_TYPES as readonly string[]).includes(question.type)) return false
|
|
620
|
+
const flags = question['flags']
|
|
621
|
+
if (typeof flags !== 'object' || flags === null || Array.isArray(flags)) return false
|
|
622
|
+
return (flags as Record<string, unknown>)['is_expression'] === true
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** A question of the required type, or a refusal naming what was wrong. */
|
|
626
|
+
const requireQuestionOfType = (doc: QnrTemplateDocument, questionId: QuestionId, type: string): QnrQuestion => {
|
|
627
|
+
const question = doc.questionsById?.[questionId]
|
|
628
|
+
if (!question) throw new OperationConflictError(`Unknown question "${questionId}"`)
|
|
629
|
+
if (question.type !== type) {
|
|
630
|
+
throw new OperationConflictError(`Question "${questionId}" is not a ${type}`)
|
|
631
|
+
}
|
|
632
|
+
return question
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** Refuses unless the alternative is in that exact question's order — ownership, not mere existence. */
|
|
636
|
+
const requireOwnedAlternative = (
|
|
637
|
+
doc: QnrTemplateDocument,
|
|
638
|
+
questionId: QuestionId,
|
|
639
|
+
alternativeId: string,
|
|
640
|
+
): QnrAlternative => {
|
|
641
|
+
if (!doc.alternativeOrderByQuestionId?.[questionId]?.includes(alternativeId)) {
|
|
642
|
+
throw new OperationConflictError(
|
|
643
|
+
`Alternative "${alternativeId}" does not belong to question "${questionId}"`,
|
|
644
|
+
)
|
|
645
|
+
}
|
|
646
|
+
return doc.alternativesById?.[alternativeId] ?? {}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** One Chart question's legend record and order, defensively read out of the open `chart` bag. */
|
|
650
|
+
const chartOf = (question: QnrQuestion): { chart: Record<string, unknown>; legendsById: Record<string, ChartLegend>; order: string[] } => {
|
|
651
|
+
const chart = (question['chart'] ?? {}) as Record<string, unknown>
|
|
652
|
+
const legendsById = (chart['legendsById'] ?? {}) as Record<string, ChartLegend>
|
|
653
|
+
const rawOrder = chart['legendsOrder']
|
|
654
|
+
const order = Array.isArray(rawOrder) ? rawOrder.filter((id): id is string => typeof id === 'string') : []
|
|
655
|
+
return { chart, legendsById, order }
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/** Writes a Chart bag back onto its question, pruning emptied containers without deleting the question. */
|
|
659
|
+
const writeChart = (
|
|
660
|
+
doc: QnrTemplateDocument,
|
|
661
|
+
questionId: QuestionId,
|
|
662
|
+
question: QnrQuestion,
|
|
663
|
+
chart: Record<string, unknown>,
|
|
664
|
+
): QnrTemplateDocument => {
|
|
665
|
+
const next: Record<string, unknown> = { ...question }
|
|
666
|
+
if (Object.keys(chart).length === 0) delete next['chart']
|
|
667
|
+
else next['chart'] = chart
|
|
668
|
+
return { ...doc, questionsById: { ...doc.questionsById, [questionId]: next as QnrQuestion } }
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Removes one alternative's exact canonical `<exp_...>` token from its owning question's base formula.
|
|
673
|
+
*
|
|
674
|
+
* Scoped to the owner and to the canonical spelling on purpose: a substring scan would corrupt author
|
|
675
|
+
* text, and rewriting another question's formula would be an edit no operation asked for.
|
|
676
|
+
*/
|
|
677
|
+
const withoutExpressionAlternativeToken = (
|
|
678
|
+
doc: QnrTemplateDocument,
|
|
679
|
+
questionId: QuestionId,
|
|
680
|
+
alternativeId: string,
|
|
681
|
+
): QnrTemplateDocument => {
|
|
682
|
+
const question = doc.questionsById?.[questionId]
|
|
683
|
+
const expression = question?.['expression']
|
|
684
|
+
if (typeof expression !== 'object' || expression === null || Array.isArray(expression)) return doc
|
|
685
|
+
|
|
686
|
+
const base = (expression as Record<string, unknown>)['base']
|
|
687
|
+
const token = makeExpressionAlternativeToken(alternativeId)
|
|
688
|
+
if (typeof base !== 'string' || !base.includes(token)) return doc
|
|
689
|
+
|
|
690
|
+
const nextBase = base.split(token).join('')
|
|
691
|
+
const nextExpression = { ...(expression as Record<string, unknown>) }
|
|
692
|
+
if (nextBase === '') delete nextExpression['base']
|
|
693
|
+
else nextExpression['base'] = nextBase
|
|
694
|
+
|
|
695
|
+
const nextQuestion: Record<string, unknown> = { ...question }
|
|
696
|
+
if (Object.keys(nextExpression).length === 0) delete nextQuestion['expression']
|
|
697
|
+
else nextQuestion['expression'] = nextExpression
|
|
698
|
+
|
|
699
|
+
return {
|
|
700
|
+
...doc,
|
|
701
|
+
questionsById: { ...doc.questionsById, [questionId]: nextQuestion as QnrQuestion },
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** One question's canonical `expressionTargets` list, defensively filtered — the bag is open. */
|
|
706
|
+
const expressionTargetsOf = (alternative: QnrAlternative): string[] => {
|
|
707
|
+
const value = alternative['expressionTargets']
|
|
708
|
+
return Array.isArray(value) ? value.filter((member): member is string => typeof member === 'string') : []
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Drops a deleted question from every canonical Expression target list and Chart assignment.
|
|
713
|
+
*
|
|
714
|
+
* Three edits on one alternative, all keyed on the **exact canonical** form:
|
|
715
|
+
* - the id leaves `expressionTargets`;
|
|
716
|
+
* - its canonical `<target_...>` token in `value` becomes `0`, so the formula still parses as
|
|
717
|
+
* arithmetic instead of carrying a reference to a question that no longer exists;
|
|
718
|
+
* - a `chartLegend` whose `questionIdMap` cites it is cleared whole.
|
|
719
|
+
*
|
|
720
|
+
* **Exact token replacement, never substring matching.** A legacy formula holds truncated
|
|
721
|
+
* last-eight-character tokens and arbitrary author text; scanning for a fragment would rewrite text
|
|
722
|
+
* this reducer has no claim on. Only the full canonical token is replaced, which is why it is generated
|
|
723
|
+
* here from the same constructor the authoring path uses rather than pattern-matched.
|
|
724
|
+
*/
|
|
725
|
+
const scrubExpressionAndChartRefs = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
726
|
+
const alternatives = Object.entries(doc.alternativesById ?? {})
|
|
727
|
+
if (alternatives.length === 0) return doc
|
|
728
|
+
|
|
729
|
+
const token = makeExpressionTargetToken(questionId)
|
|
730
|
+
let changed = false
|
|
731
|
+
|
|
732
|
+
const nextAlternatives = Object.fromEntries(
|
|
733
|
+
alternatives.map(([alternativeId, alternative]) => {
|
|
734
|
+
const targets = expressionTargetsOf(alternative)
|
|
735
|
+
const hasTarget = targets.includes(questionId)
|
|
736
|
+
const formula = alternative['value']
|
|
737
|
+
const hasToken = typeof formula === 'string' && formula.includes(token)
|
|
738
|
+
const assignment = alternative['chartLegend']
|
|
739
|
+
const hasAssignment =
|
|
740
|
+
typeof assignment === 'object' &&
|
|
741
|
+
assignment !== null &&
|
|
742
|
+
!Array.isArray(assignment) &&
|
|
743
|
+
(assignment as Record<string, unknown>)['questionIdMap'] === questionId
|
|
744
|
+
|
|
745
|
+
if (!hasTarget && !hasToken && !hasAssignment) return [alternativeId, alternative]
|
|
746
|
+
|
|
747
|
+
changed = true
|
|
748
|
+
const next: Record<string, unknown> = { ...alternative }
|
|
749
|
+
if (hasTarget) {
|
|
750
|
+
const remaining = targets.filter((id) => id !== questionId)
|
|
751
|
+
if (remaining.length === 0) delete next['expressionTargets']
|
|
752
|
+
else next['expressionTargets'] = remaining
|
|
753
|
+
}
|
|
754
|
+
// `split`/`join` replaces every occurrence: one formula may cite the same target twice.
|
|
755
|
+
if (hasToken) next['value'] = (formula as string).split(token).join('0')
|
|
756
|
+
if (hasAssignment) delete next['chartLegend']
|
|
757
|
+
return [alternativeId, next as QnrAlternative]
|
|
758
|
+
}),
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
return changed ? { ...doc, alternativesById: nextAlternatives } : doc
|
|
762
|
+
}
|
|
763
|
+
|
|
517
764
|
/** Drops a deleted tab from every grid that named it as its header. */
|
|
518
765
|
const scrubHeaderTabRefs = (doc: QnrTemplateDocument, tabId: string): QnrTemplateDocument => {
|
|
519
766
|
const questions = Object.entries(doc.questionsById ?? {})
|
|
@@ -1028,7 +1275,10 @@ const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTe
|
|
|
1028
1275
|
// presentation references a column earns: its filter chip, its row-editor placement, its default
|
|
1029
1276
|
// width, and its placement inside any tab's positional grid.
|
|
1030
1277
|
const withoutRefs = dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, questionId), questionId)
|
|
1031
|
-
return
|
|
1278
|
+
return scrubExpressionAndChartRefs(
|
|
1279
|
+
scrubTabQuestionRefs(scrubColumnPresentationRefs(withoutRefs, questionId), questionId),
|
|
1280
|
+
questionId,
|
|
1281
|
+
)
|
|
1032
1282
|
}
|
|
1033
1283
|
|
|
1034
1284
|
/**
|
|
@@ -1452,7 +1702,7 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1452
1702
|
)
|
|
1453
1703
|
}
|
|
1454
1704
|
const remaining = order.filter((id) => id !== op.alternativeId)
|
|
1455
|
-
|
|
1705
|
+
const withoutAlternative = patchDocument(doc, {
|
|
1456
1706
|
alternativesById: doc.alternativesById && omitKey(doc.alternativesById, op.alternativeId),
|
|
1457
1707
|
alternativeOrderByQuestionId: setOrRemoveOrder(
|
|
1458
1708
|
doc.alternativeOrderByQuestionId,
|
|
@@ -1460,6 +1710,175 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1460
1710
|
remaining,
|
|
1461
1711
|
),
|
|
1462
1712
|
})
|
|
1713
|
+
// An ExpressionQuestion's `expression.base` cites its alternatives by canonical token. The
|
|
1714
|
+
// token must go with the alternative, and only from THIS question's formula: another
|
|
1715
|
+
// question's text is not this operation's to rewrite, and a legacy truncated token is not
|
|
1716
|
+
// canonical so it is deliberately left alone for publication to report.
|
|
1717
|
+
return withoutExpressionAlternativeToken(withoutAlternative, op.questionId, op.alternativeId)
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
case 'alternative.setExpressionFormula': {
|
|
1721
|
+
requireQuestionOfType(doc, op.questionId, 'ExpressionQuestion')
|
|
1722
|
+
const alternative = requireOwnedAlternative(doc, op.questionId, op.alternativeId)
|
|
1723
|
+
|
|
1724
|
+
// Duplicates first: the list is an ordered set, and a repeated target would make one
|
|
1725
|
+
// question contribute twice to a formula that names it once.
|
|
1726
|
+
const seen = new Set<string>()
|
|
1727
|
+
for (const targetId of op.expressionTargets) {
|
|
1728
|
+
if (seen.has(targetId)) {
|
|
1729
|
+
throw new OperationConflictError(`Expression target "${targetId}" is listed twice`)
|
|
1730
|
+
}
|
|
1731
|
+
seen.add(targetId)
|
|
1732
|
+
if (!doc.questionsById?.[targetId]) {
|
|
1733
|
+
throw new OperationConflictError(`Unknown question "${targetId}"`)
|
|
1734
|
+
}
|
|
1735
|
+
if (!isExpressionTarget(doc.questionsById[targetId])) {
|
|
1736
|
+
throw new OperationConflictError(
|
|
1737
|
+
`Question "${targetId}" is not a selectable or LinearScale question with flags.is_expression`,
|
|
1738
|
+
)
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
// Formula text and target list land together. The two are NOT cross-checked: a partially
|
|
1743
|
+
// typed formula must save, so a token with no target and a target with no token are both
|
|
1744
|
+
// legal drafts that publication reports rather than the reducer refusing.
|
|
1745
|
+
const next: Record<string, unknown> = { ...alternative, value: op.value }
|
|
1746
|
+
if (op.expressionTargets.length === 0) delete next['expressionTargets']
|
|
1747
|
+
else next['expressionTargets'] = [...op.expressionTargets]
|
|
1748
|
+
|
|
1749
|
+
return {
|
|
1750
|
+
...doc,
|
|
1751
|
+
alternativesById: { ...doc.alternativesById, [op.alternativeId]: next as QnrAlternative },
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
case 'alternative.setChartLegend': {
|
|
1756
|
+
const question = requireQuestionOfType(doc, op.questionId, 'Chart')
|
|
1757
|
+
const alternative = requireOwnedAlternative(doc, op.questionId, op.alternativeId)
|
|
1758
|
+
|
|
1759
|
+
if (op.chartLegend === null) {
|
|
1760
|
+
// Clearing needs no valid stored value: that is what makes it able to repair an imported
|
|
1761
|
+
// dangling or non-radar assignment.
|
|
1762
|
+
if (alternative['chartLegend'] === undefined) return doc
|
|
1763
|
+
const cleared: Record<string, unknown> = { ...alternative }
|
|
1764
|
+
delete cleared['chartLegend']
|
|
1765
|
+
return {
|
|
1766
|
+
...doc,
|
|
1767
|
+
alternativesById: { ...doc.alternativesById, [op.alternativeId]: cleared as QnrAlternative },
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
// Authoring a new assignment requires the canonical subtype; a stored `pie` or unknown type
|
|
1772
|
+
// stays readable and clearable but never becomes writable through the typed path.
|
|
1773
|
+
const { legendsById } = chartOf(question)
|
|
1774
|
+
if (question['chart'] === undefined || (question['chart'] as Record<string, unknown>)['type'] !== 'radar') {
|
|
1775
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not a radar chart`)
|
|
1776
|
+
}
|
|
1777
|
+
const legend = legendsById[op.chartLegend.id]
|
|
1778
|
+
if (!legend) {
|
|
1779
|
+
throw new OperationConflictError(
|
|
1780
|
+
`Unknown chart legend "${op.chartLegend.id}" on question "${op.questionId}"`,
|
|
1781
|
+
)
|
|
1782
|
+
}
|
|
1783
|
+
const target = doc.questionsById?.[op.chartLegend.questionIdMap]
|
|
1784
|
+
if (!target) {
|
|
1785
|
+
throw new OperationConflictError(`Unknown question "${op.chartLegend.questionIdMap}"`)
|
|
1786
|
+
}
|
|
1787
|
+
if (!isExpressionTarget(target)) {
|
|
1788
|
+
throw new OperationConflictError(
|
|
1789
|
+
`Question "${op.chartLegend.questionIdMap}" is not a selectable or LinearScale question with flags.is_expression`,
|
|
1790
|
+
)
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// One write of the whole value, with `label` derived here rather than taken from the client:
|
|
1794
|
+
// a client label would let one legend id carry two labels, i.e. two hashes for one state.
|
|
1795
|
+
const assignment: AlternativeChartLegend = {
|
|
1796
|
+
id: op.chartLegend.id,
|
|
1797
|
+
questionIdMap: op.chartLegend.questionIdMap,
|
|
1798
|
+
label: legend.label,
|
|
1799
|
+
}
|
|
1800
|
+
return {
|
|
1801
|
+
...doc,
|
|
1802
|
+
alternativesById: {
|
|
1803
|
+
...doc.alternativesById,
|
|
1804
|
+
[op.alternativeId]: { ...alternative, chartLegend: assignment } as QnrAlternative,
|
|
1805
|
+
},
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
case 'chartLegend.create': {
|
|
1810
|
+
const question = requireQuestionOfType(doc, op.questionId, 'Chart')
|
|
1811
|
+
if (op.legendId === '') throw new OperationConflictError('A chart legend id may not be empty')
|
|
1812
|
+
if (op.label === '') throw new OperationConflictError('A chart legend label may not be empty')
|
|
1813
|
+
|
|
1814
|
+
const { chart, legendsById, order } = chartOf(question)
|
|
1815
|
+
// Creation is the one Chart operation that requires the canonical subtype: deletion and
|
|
1816
|
+
// clearing must stay available to repair imported `pie`/unknown content.
|
|
1817
|
+
if (chart['type'] !== 'radar') {
|
|
1818
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not a radar chart`)
|
|
1819
|
+
}
|
|
1820
|
+
if (legendsById[op.legendId] !== undefined) {
|
|
1821
|
+
throw new OperationConflictError(`Chart legend "${op.legendId}" already exists`)
|
|
1822
|
+
}
|
|
1823
|
+
// Legacy compares labels by exact equality, and two legends with one label are
|
|
1824
|
+
// indistinguishable in the radar the author is looking at.
|
|
1825
|
+
if (Object.values(legendsById).some((existing) => existing.label === op.label)) {
|
|
1826
|
+
throw new OperationConflictError(`Chart legend label "${op.label}" is already used`)
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
// Record and order in one write: a record with no order entry is unreachable state that
|
|
1830
|
+
// still moves the hash, and an order entry with no record dangles.
|
|
1831
|
+
const nextChart: Record<string, unknown> = {
|
|
1832
|
+
...chart,
|
|
1833
|
+
legendsById: { ...legendsById, [op.legendId]: { id: op.legendId, label: op.label } },
|
|
1834
|
+
legendsOrder: insertAt(order, op.legendId, op.atIndex),
|
|
1835
|
+
}
|
|
1836
|
+
return writeChart(doc, op.questionId, question, nextChart)
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
case 'chartLegend.delete': {
|
|
1840
|
+
const question = requireQuestionOfType(doc, op.questionId, 'Chart')
|
|
1841
|
+
const { chart, legendsById, order } = chartOf(question)
|
|
1842
|
+
if (legendsById[op.legendId] === undefined) {
|
|
1843
|
+
throw new OperationConflictError(
|
|
1844
|
+
`Unknown chart legend "${op.legendId}" on question "${op.questionId}"`,
|
|
1845
|
+
)
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
const remainingLegends = omitKey(legendsById, op.legendId)
|
|
1849
|
+
const remainingOrder = order.filter((id) => id !== op.legendId)
|
|
1850
|
+
const nextChart: Record<string, unknown> = { ...chart }
|
|
1851
|
+
if (Object.keys(remainingLegends).length === 0) delete nextChart['legendsById']
|
|
1852
|
+
else nextChart['legendsById'] = remainingLegends
|
|
1853
|
+
if (remainingOrder.length === 0) delete nextChart['legendsOrder']
|
|
1854
|
+
else nextChart['legendsOrder'] = remainingOrder
|
|
1855
|
+
|
|
1856
|
+
const withoutLegend = writeChart(doc, op.questionId, question, nextChart)
|
|
1857
|
+
|
|
1858
|
+
// The cascade matches the legacy effect: a legend that disappears stops appearing on
|
|
1859
|
+
// alternatives. Scoped to THIS Chart's own alternatives — an identical legend id on another
|
|
1860
|
+
// question is a different legend, and imported ids do collide across questions.
|
|
1861
|
+
const ownedAlternativeIds = withoutLegend.alternativeOrderByQuestionId?.[op.questionId] ?? []
|
|
1862
|
+
let alternativesById = withoutLegend.alternativesById
|
|
1863
|
+
let changed = false
|
|
1864
|
+
for (const alternativeId of ownedAlternativeIds) {
|
|
1865
|
+
const alternative = alternativesById?.[alternativeId]
|
|
1866
|
+
const assignment = alternative?.['chartLegend']
|
|
1867
|
+
if (
|
|
1868
|
+
typeof assignment !== 'object' ||
|
|
1869
|
+
assignment === null ||
|
|
1870
|
+
Array.isArray(assignment) ||
|
|
1871
|
+
(assignment as Record<string, unknown>)['id'] !== op.legendId
|
|
1872
|
+
) {
|
|
1873
|
+
continue
|
|
1874
|
+
}
|
|
1875
|
+
const cleared: Record<string, unknown> = { ...alternative }
|
|
1876
|
+
delete cleared['chartLegend']
|
|
1877
|
+
alternativesById = { ...alternativesById, [alternativeId]: cleared as QnrAlternative }
|
|
1878
|
+
changed = true
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
return changed && alternativesById ? { ...withoutLegend, alternativesById } : withoutLegend
|
|
1463
1882
|
}
|
|
1464
1883
|
|
|
1465
1884
|
case 'tab.create': {
|
|
@@ -1514,6 +1933,62 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1514
1933
|
return { ...doc, tabsById: { ...doc.tabsById, [op.tabId]: nextTab } }
|
|
1515
1934
|
}
|
|
1516
1935
|
|
|
1936
|
+
case 'tab.setQuestion': {
|
|
1937
|
+
const tab = doc.tabsById?.[op.tabId]
|
|
1938
|
+
if (!tab) throw new OperationConflictError(`Unknown tab "${op.tabId}"`)
|
|
1939
|
+
// Only inclusion needs a live question. Exclusion stays available with no question at all so
|
|
1940
|
+
// an imported document naming a question it never had can be repaired.
|
|
1941
|
+
if (op.include && !doc.questionsById?.[op.questionId]) {
|
|
1942
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
let next: Record<string, unknown> = writeTabList(
|
|
1946
|
+
{ ...tab },
|
|
1947
|
+
'questionIds',
|
|
1948
|
+
setListMember(tabListOf(tab, 'questionIds'), op.questionId, op.include, op.atIndex),
|
|
1949
|
+
)
|
|
1950
|
+
// Dropping membership drops what membership entitled the question to. A counted row or a
|
|
1951
|
+
// placement for a question the tab no longer shows is a reference to nothing, and it still
|
|
1952
|
+
// changes `document_hash`.
|
|
1953
|
+
if (!op.include) {
|
|
1954
|
+
next = writeTabList(
|
|
1955
|
+
next,
|
|
1956
|
+
'rowCountQuestionIds',
|
|
1957
|
+
tabListOf(tab, 'rowCountQuestionIds').filter((id) => id !== op.questionId),
|
|
1958
|
+
)
|
|
1959
|
+
next = withoutTabPlacement(next, op.questionId)
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
return { ...doc, tabsById: { ...doc.tabsById, [op.tabId]: next as QnrTab } }
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
case 'tab.setRowCountQuestion': {
|
|
1966
|
+
const tab = doc.tabsById?.[op.tabId]
|
|
1967
|
+
if (!tab) throw new OperationConflictError(`Unknown tab "${op.tabId}"`)
|
|
1968
|
+
|
|
1969
|
+
if (op.include) {
|
|
1970
|
+
const question = doc.questionsById?.[op.questionId]
|
|
1971
|
+
if (!question) throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1972
|
+
// Only a grid has rows to count, and only a member of this tab contributes to its count.
|
|
1973
|
+
if (question.type !== 'QuestionGrid') {
|
|
1974
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not a question grid`)
|
|
1975
|
+
}
|
|
1976
|
+
if (!tabListOf(tab, 'questionIds').includes(op.questionId)) {
|
|
1977
|
+
throw new OperationConflictError(
|
|
1978
|
+
`Question "${op.questionId}" is not a member of tab "${op.tabId}"`,
|
|
1979
|
+
)
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
// Membership and layout are untouched: this operation owns exactly one list.
|
|
1984
|
+
const next = writeTabList(
|
|
1985
|
+
{ ...tab },
|
|
1986
|
+
'rowCountQuestionIds',
|
|
1987
|
+
setListMember(tabListOf(tab, 'rowCountQuestionIds'), op.questionId, op.include, op.atIndex),
|
|
1988
|
+
)
|
|
1989
|
+
return { ...doc, tabsById: { ...doc.tabsById, [op.tabId]: next as QnrTab } }
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1517
1992
|
case 'tab.delete': {
|
|
1518
1993
|
if (!doc.tabsById?.[op.tabId]) {
|
|
1519
1994
|
throw new OperationConflictError(`Unknown tab "${op.tabId}"`)
|
|
@@ -1864,6 +2339,31 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1864
2339
|
}
|
|
1865
2340
|
}
|
|
1866
2341
|
|
|
2342
|
+
case 'mappingBinding.setLegacyOverride': {
|
|
2343
|
+
const binding = doc.mappingBindingsById?.[op.bindingId]
|
|
2344
|
+
if (!binding) {
|
|
2345
|
+
throw new OperationConflictError(`Unknown mapping binding "${op.bindingId}"`)
|
|
2346
|
+
}
|
|
2347
|
+
// Re-validated on replay, not only at the schema boundary: a log replays through here with
|
|
2348
|
+
// no validator in front of it, and a malformed value must not become canonical by replay.
|
|
2349
|
+
if (op.legacyOverride !== null && parseLegacyBindingOverride(op.legacyOverride).status !== 'known') {
|
|
2350
|
+
throw new OperationConflictError(
|
|
2351
|
+
`Operation mappingBinding.setLegacyOverride carries a value that is not a canonical legacy override`,
|
|
2352
|
+
)
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
// Member-only: every other member is carried over untouched, so no node, field, target,
|
|
2356
|
+
// behaviour option or mapping `bindingOrder` can move when an exception is set or cleared.
|
|
2357
|
+
const next: Record<string, unknown> = { ...binding }
|
|
2358
|
+
if (op.legacyOverride === null) delete next['legacyOverride']
|
|
2359
|
+
else next['legacyOverride'] = op.legacyOverride
|
|
2360
|
+
|
|
2361
|
+
return {
|
|
2362
|
+
...doc,
|
|
2363
|
+
mappingBindingsById: { ...doc.mappingBindingsById, [op.bindingId]: next as MappingBinding },
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
|
|
1867
2367
|
case 'mappingBinding.delete':
|
|
1868
2368
|
return scrubBindingOrders(
|
|
1869
2369
|
patchDocument(doc, {
|