From cfad8f010c2cee8802a6d454608c0d4cfdb818c9 Mon Sep 17 00:00:00 2001 From: SamGodwin2 Date: Thu, 11 Apr 2019 12:59:53 +0100 Subject: [PATCH] Added migration to bring new metadata values into new resolver format --- eq-author-api/middleware/launch.js | 5 +- eq-author-api/middleware/launch.test.js | 6 +- eq-author-api/migrations/index.js | 2 + .../migrations/updateMetadataValue.js | 13 +++ .../migrations/updateMetadataValue.test.js | 86 +++++++++++++++++++ eq-author-api/schema/resolvers/base.js | 10 +-- eq-author-api/schema/tests/metadata.test.js | 38 ++++---- .../src/businessLogic/updateMetadata.js | 13 ++- eq-author-api/utils/defaultMetadata.js | 26 +++--- 9 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 eq-author-api/migrations/updateMetadataValue.js create mode 100644 eq-author-api/migrations/updateMetadataValue.test.js diff --git a/eq-author-api/middleware/launch.js b/eq-author-api/middleware/launch.js index 8b4d86e33c..93ee2860af 100644 --- a/eq-author-api/middleware/launch.js +++ b/eq-author-api/middleware/launch.js @@ -2,6 +2,7 @@ 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 = { @@ -9,10 +10,12 @@ const buildClaims = metadata => { 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]: diff --git a/eq-author-api/middleware/launch.test.js b/eq-author-api/middleware/launch.test.js index 3f1fe21dd1..c4934ce0cc 100644 --- a/eq-author-api/middleware/launch.test.js +++ b/eq-author-api/middleware/launch.test.js @@ -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", @@ -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({ diff --git a/eq-author-api/migrations/index.js b/eq-author-api/migrations/index.js index 2b38df3791..5116d6b226 100644 --- a/eq-author-api/migrations/index.js +++ b/eq-author-api/migrations/index.js @@ -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; diff --git a/eq-author-api/migrations/updateMetadataValue.js b/eq-author-api/migrations/updateMetadataValue.js new file mode 100644 index 0000000000..48ed4ee201 --- /dev/null +++ b/eq-author-api/migrations/updateMetadataValue.js @@ -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; +}; diff --git a/eq-author-api/migrations/updateMetadataValue.test.js b/eq-author-api/migrations/updateMetadataValue.test.js new file mode 100644 index 0000000000..5d8c6cd7f9 --- /dev/null +++ b/eq-author-api/migrations/updateMetadataValue.test.js @@ -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]); + }); +}); diff --git a/eq-author-api/schema/resolvers/base.js b/eq-author-api/schema/resolvers/base.js index c3238f001a..5af7b458d1 100644 --- a/eq-author-api/schema/resolvers/base.js +++ b/eq-author-api/schema/resolvers/base.js @@ -461,7 +461,6 @@ const Resolvers = { id: uuid.v4(), key: null, type: "Text", - value: null, }; ctx.questionnaire.metadata.push(newMetadata); await saveQuestionnaire(ctx.questionnaire); @@ -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"), }, diff --git a/eq-author-api/schema/tests/metadata.test.js b/eq-author-api/schema/tests/metadata.test.js index 68a8eb1569..90962e3224 100644 --- a/eq-author-api/schema/tests/metadata.test.js +++ b/eq-author-api/schema/tests/metadata.test.js @@ -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); } }); }); diff --git a/eq-author-api/src/businessLogic/updateMetadata.js b/eq-author-api/src/businessLogic/updateMetadata.js index 703a2cc635..b63320015e 100644 --- a/eq-author-api/src/businessLogic/updateMetadata.js +++ b/eq-author-api/src/businessLogic/updateMetadata.js @@ -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) { diff --git a/eq-author-api/utils/defaultMetadata.js b/eq-author-api/utils/defaultMetadata.js index 5c9b0114d5..9ba2beadfa 100644 --- a/eq-author-api/utils/defaultMetadata.js +++ b/eq-author-api/utils/defaultMetadata.js @@ -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", }, ];