Skip to content

Commit

Permalink
chore(migrate initiative item to create recommendedTemplates array): …
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 58 deletions.
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 70 additions & 45 deletions packages/initiatives/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions packages/initiatives/src/migrations/upgrade-two-dot-two.ts
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;
}
4 changes: 3 additions & 1 deletion packages/initiatives/src/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { applyInitialSchema } from "./migrations/apply-schema";
import { upgradeToOneDotOne } from "./migrations/upgrade-one-dot-one";
import { upgradeToTwoDotZero } from "./migrations/upgrade-two-dot-zero";
import { upgradeToTwoDotOne } from "./migrations/upgrade-two-dot-one";
import { upgradeToTwoDotTwo } from "./migrations/upgrade-two-dot-two";

/**
* Current Schema Version
* @protected
*/
export const CURRENT_SCHEMA_VERSION = 2.1;
export const CURRENT_SCHEMA_VERSION = 2.2;

/**
* Handle Initiative Schema Migrations.
Expand Down Expand Up @@ -40,6 +41,7 @@ export function migrateSchema(
model = upgradeToOneDotOne(model, portalUrl);
model = upgradeToTwoDotZero(model, portalUrl);
model = upgradeToTwoDotOne(model);
model = upgradeToTwoDotTwo(model);
// etc
return model;
}
Expand Down
26 changes: 26 additions & 0 deletions packages/initiatives/test/mocks/initiative-versionTwoDotOne.ts
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: []
},
{}
]
}
};
Loading

0 comments on commit 239649c

Please sign in to comment.