Skip to content

Commit

Permalink
fix: also convert simulation poll and group attr
Browse files Browse the repository at this point in the history
  • Loading branch information
Clemog committed Jun 11, 2024
1 parent bb18d60 commit 7cf5757
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/helpers/simulation/generateSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ export function generateSimulation({
savedViaEmail,
} as Simulation

if (migrationInstructions) {
try {
simulation = migrateSimulation(simulation, migrationInstructions)
} catch (error) {
console.warn('Error trying to migrate LocalStorage:', error)
captureException(error)
}
try {
simulation = migrateSimulation(simulation, migrationInstructions)
} catch (error) {
console.warn('Error trying to migrate LocalStorage:', error)
captureException(error)
}

return simulation
Expand Down
42 changes: 27 additions & 15 deletions src/publicodes-state/helpers/migrateSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@ import { Migration, migrateSituation } from '@publicodes/tools/migration'
import { Simulation } from '../types'

export function migrateSimulation(
simulation: Simulation,
migrationInstructions: Migration
simulation: Simulation & { group?: string; poll?: string },
migrationInstructions: Migration | undefined
): Simulation {
simulation.situation = migrateSituation(
simulation.situation,
migrationInstructions
)

// NOTE: folded steps (i.e. answered rules) are can be map to a situation,
// where the keys are the rule names and the value is undefined.
simulation.foldedSteps = Object.keys(
migrateSituation(
Object.fromEntries(
simulation.foldedSteps.map((step) => [step, undefined])
),
if (migrationInstructions) {
simulation.situation = migrateSituation(
simulation.situation,
migrationInstructions
)
)

// NOTE: folded steps (i.e. answered rules) are can be map to a situation,
// where the keys are the rule names and the value is undefined.
simulation.foldedSteps = Object.keys(
migrateSituation(
Object.fromEntries(
simulation.foldedSteps.map((step) => [step, undefined])
),
migrationInstructions
)
)
}
// If group or poll is defined, we convert it to groups or polls and delete it
if (simulation.group) {
simulation.groups = [simulation.group]
delete simulation.group
}

if (simulation.poll) {
simulation.polls = [simulation.poll]
delete simulation.poll
}

return simulation
}

0 comments on commit 7cf5757

Please sign in to comment.