-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(migrate initiative item to create recommendedTemplates array): …
…migrate initiative item to crea This migration bumps the initiative item schema version from 2.1 to 2.2. It will traverse the initiative item's steps array, adding any present template ids to an array called recommendedTemplates that sits at the model.data node. Importantly, this migration only runs once, so subsequent templates added to the steps array post-migration will not be re added to recommendedTemplates. AFFECTS PACKAGES: @esri/hub-initiatives
- Loading branch information
Robert Steilberg
committed
Feb 27, 2020
1 parent
101d29b
commit 239649c
Showing
7 changed files
with
225 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
packages/initiatives/src/migrations/upgrade-two-dot-two.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright (c) 2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import { getProp, cloneObject } from "@esri/hub-common"; | ||
import { IInitiativeModel } from "@esri/hub-common"; | ||
|
||
/** | ||
* Apply the 2.1 --> 2.2 Migration to an Initiative Model | ||
* Note: we need this migration to run every time for now, so we | ||
* will always run it | ||
* | ||
* @param model | ||
* @protected | ||
*/ | ||
export function upgradeToTwoDotTwo(model: IInitiativeModel): IInitiativeModel { | ||
// const currVersion = getProp(model, "item.properties.schemaVersion"); | ||
// if (currVersion < 2.2) { | ||
const clone = cloneObject(model) as IInitiativeModel; | ||
// store the schemaVersion | ||
// clone.item.properties.schemaVersion = 2.2; | ||
const steps = getProp(clone, "data.steps"); | ||
const templateIdsFromSteps = getTemplateIdsFromSteps(steps); | ||
const recommendedTemplates = | ||
getProp(clone, "data.recommendedTemplates") || []; | ||
const allTemplateIds = templateIdsFromSteps.concat(recommendedTemplates); | ||
// strip out duplicates | ||
clone.data.recommendedTemplates = allTemplateIds.reduce( | ||
(acc: string[], id: string) => { | ||
if (acc.indexOf(id) < 0) { | ||
acc.push(id); | ||
} | ||
return acc; | ||
}, | ||
[] | ||
); | ||
return clone; | ||
// } else { | ||
// return model; | ||
// } | ||
} | ||
|
||
/** | ||
* Reduce the solution template ids out of the steps array | ||
* @param steps is the steps array from an initiative item model.data | ||
*/ | ||
function getTemplateIdsFromSteps(steps: any): string[] { | ||
let templateIds: string[] = []; | ||
if (Array.isArray(steps)) { | ||
templateIds = steps.reduce((acc: string[], step: any) => { | ||
if (getProp(step, "templateIds.length")) { | ||
return acc.concat(step.templateIds); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
return templateIds; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/initiatives/test/mocks/initiative-versionTwoDotOne.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* Copyright (c) 2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import { IInitiativeModel, IInitiativeItem } from "@esri/hub-common"; | ||
|
||
export const initiativeVersionTwoDotOne: IInitiativeModel = { | ||
item: { | ||
properties: { | ||
schemaVersion: 2.1 | ||
} | ||
} as IInitiativeItem, | ||
data: { | ||
recommendedTemplates: ["4ef", "1ef"], | ||
steps: [ | ||
{ | ||
templateIds: ["1ef", "2ef"] | ||
}, | ||
{ | ||
templateIds: ["3ef"] | ||
}, | ||
{ | ||
templateIds: [] | ||
}, | ||
{} | ||
] | ||
} | ||
}; |
Oops, something went wrong.