Skip to content

Commit

Permalink
Merge pull request #308 from ONSdigital/1026-metadata-example-values-…
Browse files Browse the repository at this point in the history
…missing-after-data-migration

Added migration to bring new metadata values into new resolver format
  • Loading branch information
SamGodwin2 authored Apr 18, 2019
2 parents 4205d08 + cfad8f0 commit cb2447f
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 45 deletions.
5 changes: 4 additions & 1 deletion eq-author-api/middleware/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ const { generateToken } = require("../utils/jwtHelper");
const { assign, isNil, isEmpty } = require("lodash");
const { sanitiseMetadata } = require("../utils/sanitiseMetadata");
const { getQuestionnaire } = require("../utils/datastore");
const { defaultTypeValueNames } = require("../utils/defaultMetadata");

const buildClaims = metadata => {
const result = {
claims: {},
errors: [],
};

metadata.map(({ key, value, id, type }) => {
metadata.map(metadata => {
const { key, id, type } = metadata;
if (isNil(key) || key.trim() === "") {
result.errors.push(id);
}
const value = metadata[defaultTypeValueNames[type]];

return assign(result.claims, {
[key]:
Expand Down
6 changes: 4 additions & 2 deletions eq-author-api/middleware/launch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe("launcher middleware", () => {

it("should convert date values to ISO string dates when building claims", () => {
expect(
buildClaims([{ id: 1, key: "hello", type: "Date", value: "01/01/2018" }])
buildClaims([
{ id: 1, key: "hello", type: "Date", dateValue: "01/01/2018" },
])
).toMatchObject({
claims: {
hello: "2018-01-01",
Expand All @@ -91,7 +93,7 @@ describe("launcher middleware", () => {
id: 1,
key: "hello",
type: "Date",
value: "2018-01-01T00:00:00+00:00",
dateValue: "2018-01-01T00:00:00+00:00",
},
])
).toMatchObject({
Expand Down
2 changes: 2 additions & 0 deletions eq-author-api/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const addVersion = require("./addVersion");
const addOptionalFieldProperties = require("./addOptionalFieldProperties");
const addQuestionnaireType = require("./addQuestionnaireType");
const updateMetadataValue = require("./updateMetadataValue");

const migrations = [
addVersion,
addOptionalFieldProperties,
addQuestionnaireType,
updateMetadataValue,
];

const currentVersion = migrations.length;
Expand Down
13 changes: 13 additions & 0 deletions eq-author-api/migrations/updateMetadataValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//This is an auto-generated file. Do NOT modify the method signature.
const { defaultTypeValueNames } = require("../utils/defaultMetadata");

module.exports = function updateMetadataValue(questionnaire) {
questionnaire.metadata.map(metadata => {
if (!metadata.value) {
return;
}
metadata[defaultTypeValueNames[metadata.type]] = metadata.value;
delete metadata.value;
});
return questionnaire;
};
86 changes: 86 additions & 0 deletions eq-author-api/migrations/updateMetadataValue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const updateMetadataValue = require("./updateMetadataValue.js");

describe("updateMetadataValue", () => {
it("should update metadata to correct new format", () => {
const metadata = [
{
alias: "text",
dateValue: null,
id: "296",
key: "ru_ref",
languageValue: null,
regionValue: null,
textValue: "old Value",
type: "Text",
value: "new Value",
},
{
alias: "date",
dateValue: "2019-07-01",
id: "294",
key: "date",
languageValue: null,
regionValue: null,
textValue: null,
type: "Date",
value: "2019-04-09",
},
{
alias: "region",
dateValue: null,
id: "297",
key: "region",
languageValue: null,
regionValue: "GB_GBN",
textValue: null,
type: "Region",
value: "GB_ENG",
},
{
alias: "language",
dateValue: null,
id: "297",
key: "language",
languageValue: "cy",
regionValue: null,
textValue: null,
type: "Language",
value: "en",
},
{
alias: "language",
dateValue: null,
id: "297",
key: "language",
languageValue: "cy",
regionValue: null,
textValue: null,
type: "Language",
},
];
const updatedQuestionnaire = updateMetadataValue({ metadata });
updatedQuestionnaire.metadata.forEach(metadata => {
expect(metadata).not.toHaveProperty("value");
});
expect(updatedQuestionnaire.metadata[0].textValue).toEqual("new Value");
expect(updatedQuestionnaire.metadata[1].dateValue).toEqual("2019-04-09");
expect(updatedQuestionnaire.metadata[2].regionValue).toEqual("GB_ENG");
expect(updatedQuestionnaire.metadata[3].languageValue).toEqual("en");
});
it("should not update if metadata is already in correct format", () => {
const metadata = [
{
alias: "language",
dateValue: null,
id: "297",
key: "language",
languageValue: "cy",
regionValue: null,
textValue: null,
type: "Language",
},
];
const updatedQuestionnaire = updateMetadataValue({ metadata });
expect(updatedQuestionnaire.metadata[0]).toMatchObject(metadata[0]);
});
});
10 changes: 3 additions & 7 deletions eq-author-api/schema/resolvers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ const Resolvers = {
id: uuid.v4(),
key: null,
type: "Text",
value: null,
};
ctx.questionnaire.metadata.push(newMetadata);
await saveQuestionnaire(ctx.questionnaire);
Expand Down Expand Up @@ -796,14 +795,11 @@ const Resolvers = {
},

Metadata: {
textValue: ({ type, value }) => (type === "Text" ? value : null),
languageValue: ({ type, value }) => (type === "Language" ? value : null),
regionValue: ({ type, value }) => (type === "Region" ? value : null),
dateValue: ({ type, value }) => {
if (type !== "Date" || !value) {
dateValue: ({ type, dateValue }) => {
if (type !== "Date" || !dateValue) {
return null;
}
return new Date(value);
return new Date(dateValue);
},
displayName: metadata => getName(metadata, "Metadata"),
},
Expand Down
38 changes: 19 additions & 19 deletions eq-author-api/schema/tests/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,29 @@ describe("metadata", () => {

it("should default metadata for known keys", async () => {
for (let defaultValue of defaultValues) {
const { key, alias, type, value } = defaultValue;
updatedMetadata = await updateMetadata(questionnaire, {
const {
key,
alias,
type,
dateValue,
regionValue,
languageValue,
textValue,
} = defaultValue;

const expected = {
id: metadata.id,
key,
alias,
type,
dateValue: type === DATE ? value : null,
regionValue: type === REGION ? value : null,
languageValue: type === LANGUAGE ? value : null,
textValue: type === TEXT ? value : null,
});
expect(updatedMetadata).toEqual(
expect.objectContaining({
id: metadata.id,
key,
alias,
type,
dateValue: type === DATE ? value : null,
regionValue: type === REGION ? value : null,
languageValue: type === LANGUAGE ? value : null,
textValue: type === TEXT ? value : null,
})
);
dateValue: type === DATE ? dateValue : null,
regionValue: type === REGION ? regionValue : null,
languageValue: type === LANGUAGE ? languageValue : null,
textValue: type === TEXT ? textValue : null,
};

updatedMetadata = await updateMetadata(questionnaire, expected);
expect(updatedMetadata).toMatchObject(expected);
}
});
});
Expand Down
13 changes: 10 additions & 3 deletions eq-author-api/src/businessLogic/updateMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ module.exports = (metadataToUpdate, { key, alias, type, ...values }) => {
key,
alias,
type,
value: values[defaultTypeValueNames[type]],
textValue: null,
regionValue: null,
dateValue: null,
languageValue: null,
[defaultTypeValueNames[type]]: values[defaultTypeValueNames[type]],
};

if (metadataToUpdate.type !== type && !newValues.value) {
newValues.value = defaultTypeValues[type];
if (
metadataToUpdate.type !== type &&
!newValues[defaultTypeValueNames[type]]
) {
newValues[defaultTypeValueNames[type]] = defaultTypeValues[type];
}

if (metadataToUpdate.key !== key) {
Expand Down
26 changes: 13 additions & 13 deletions eq-author-api/utils/defaultMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,79 +21,79 @@ const defaultValues = [
key: "ru_ref",
alias: "Ru Ref",
type: "Text",
value: "12346789012A",
textValue: "12346789012A",
},
{
key: "ru_name",
alias: "Ru Name",
type: "Text",
value: "ESSENTIAL ENTERPRISE LTD.",
textValue: "ESSENTIAL ENTERPRISE LTD.",
},
{
key: "trad_as",
alias: "Trad As",
type: "Text",
value: "ESSENTIAL ENTERPRISE LTD.",
textValue: "ESSENTIAL ENTERPRISE LTD.",
},
{
key: "period_id",
alias: "Period Id",
type: "Text",
value: "201605",
textValue: "201605",
},
{
key: "period_str",
alias: "Period Str",
type: "Text",
value: "May 2017",
textValue: "May 2017",
},
{
key: "language_code",
alias: "Language",
type: "Language",
value: "en",
languageValue: "en",
},
{
key: "ref_p_start_date",
alias: "Start Date",
type: "Date",
value: "2016-05-01",
dateValue: "2016-05-01",
},
{
key: "ref_p_end_date",
alias: "End Date",
type: "Date",
value: "2016-06-12",
dateValue: "2016-06-12",
},
{
key: "return_by",
alias: "Return By",
type: "Date",
value: "2016-06-12",
dateValue: "2016-06-12",
},
{
key: "employmentDate",
alias: "Employment Date",
type: "Date",
value: "2016-06-10",
dateValue: "2016-06-10",
},
{
key: "region_code",
alias: "Region",
type: "Region",
value: "GB_ENG",
regionValue: "GB_ENG",
},
{
key: "display_address",
alias: "Display Address",
type: "Text",
value: "68 Abingdon Road, Goathill, PE12 5EH",
textValue: "68 Abingdon Road, Goathill, PE12 5EH",
},
{
key: "country",
alias: "Country",
type: "Text",
value: "E",
textValue: "E",
},
];

Expand Down

0 comments on commit cb2447f

Please sign in to comment.