diff --git a/eq-author-api/middleware/runQuestionnaireMigrations.js b/eq-author-api/middleware/runQuestionnaireMigrations.js index b25801bd53..86d5808a1c 100644 --- a/eq-author-api/middleware/runQuestionnaireMigrations.js +++ b/eq-author-api/middleware/runQuestionnaireMigrations.js @@ -1,4 +1,8 @@ -const { saveQuestionnaire } = require("../utils/datastore"); +const { + saveQuestionnaire, + getQuestionnaireMetaById, +} = require("../utils/datastore"); +const { merge } = require("lodash"); module.exports = logger => ({ currentVersion, migrations }) => async ( req, @@ -16,6 +20,11 @@ module.exports = logger => ({ currentVersion, migrations }) => async ( } try { + const questionnaireMeta = await getQuestionnaireMetaById( + req.questionnaire.id + ); + merge(questionnaireMeta, { ...req.questionnaire }); + req.questionnaire = questionnaireMeta; req.questionnaire = await migrations .slice(req.questionnaire.version) .reduce((questionnaire, migration) => { diff --git a/eq-author-api/middleware/runQuestionnaireMigrations.test.js b/eq-author-api/middleware/runQuestionnaireMigrations.test.js index b49ad331d2..1d8b795f25 100644 --- a/eq-author-api/middleware/runQuestionnaireMigrations.test.js +++ b/eq-author-api/middleware/runQuestionnaireMigrations.test.js @@ -1,5 +1,22 @@ const runQuestionnaireMigrations = require("./runQuestionnaireMigrations"); const { buildContext } = require("../tests/utils/contextBuilder"); +require("../utils/datastore"); + +jest.mock("../utils/datastore", () => ({ + ...require.requireActual("../utils/datastore"), + getQuestionnaireMetaById: jest.fn(() => ({ + createdAt: 1576579844508, + createdBy: "a5570fd6-af3a-4192-8286-f66ac304ba39", + isPublic: true, + history: + '[{"id":"creationEvent","publishStatus":"Questionnaire created","questionnaireTitle":"Helo","userId":"a5570fd6-af3a-4192-8286-f66ac304ba39","time":"2019-12-17T10:50:44.508Z","type":"system"}]', + id: "f8b144f5-a723-4c4c-acac-1c23f9b9dddf", + title: "Helo", + type: "Social", + publishStatus: "Unpublished", + updatedAt: 1576590370548, + })), +})); describe("runQuestionnaireMigrations", () => { let res, req, next, migrations, logger; @@ -23,7 +40,6 @@ describe("runQuestionnaireMigrations", () => { req = {}; let migrationOne = jest.fn(() => req.questionnaire); migrations = [migrationOne]; - await runQuestionnaireMigrations(logger)({ migrations, currentVersion: migrations.length, @@ -89,6 +105,22 @@ describe("runQuestionnaireMigrations", () => { expect(req.questionnaire.version).toEqual(1); }); + it("Should merge the meta and questionnaire objects", async () => { + req.questionnaire.version = 0; + let migrationOne = jest.fn(() => req.questionnaire); + migrations = [migrationOne]; + await runQuestionnaireMigrations(logger)({ + migrations, + currentVersion: migrations.length, + })(req, res, next); + + expect(next).toHaveBeenCalled(); + expect(migrationOne).not.toHaveBeenCalledWith({ + publishStatus: "Unpublished", + sections: expect.any(Array), + }); + }); + it("should migrate data and set version correctly", async () => { req.questionnaire.version = 1; diff --git a/eq-author-api/migrations/addTypeToHistoryEvent.js b/eq-author-api/migrations/addTypeToHistoryEvent.js index 5c0b9e6c1c..68342cb583 100644 --- a/eq-author-api/migrations/addTypeToHistoryEvent.js +++ b/eq-author-api/migrations/addTypeToHistoryEvent.js @@ -1,17 +1,10 @@ -const { getQuestionnaireMetaById, saveModel } = require("../utils/datastore"); -const { QuestionnaireModel } = require("../db/models/DynamoDB"); - module.exports = async function addTypeToHistoryEvent(questionnaire) { - const metadata = await getQuestionnaireMetaById(questionnaire.id); - - metadata.history.map(item => { + questionnaire.history.map(item => { if (!item.type) { item.type = item.hasOwnProperty("bodyText") ? "note" : "system"; } return item; }); - await saveModel(new QuestionnaireModel(metadata)); - return questionnaire; }; diff --git a/eq-author-api/migrations/addTypeToHistoryEvent.test.js b/eq-author-api/migrations/addTypeToHistoryEvent.test.js index 35301ecf7b..14bd754add 100644 --- a/eq-author-api/migrations/addTypeToHistoryEvent.test.js +++ b/eq-author-api/migrations/addTypeToHistoryEvent.test.js @@ -1,15 +1,11 @@ const { cloneDeep } = require("lodash"); const addTypeToHistoryEvent = require("./addTypeToHistoryEvent.js"); -const { getQuestionnaireMetaById, saveModel } = require("../utils/datastore"); -const { QuestionnaireModel } = require("../db/models/DynamoDB"); - describe("addTypeToHistoryEvent", () => { - let questionnaire, historyEvent, metadata, creationEvent; + let questionnaire, historyEvent, noteEvent, creationEvent; const questionnaireId = "aca64645-98ef-43ae-8b77-d749b4e89a05"; beforeEach(async () => { - questionnaire = { id: questionnaireId }; creationEvent = { id: "123", publishStatus: "Questionnaire created", @@ -24,16 +20,17 @@ describe("addTypeToHistoryEvent", () => { userId: "12345", time: new Date(), }; - metadata = { + noteEvent = { + id: "fdfsdfds-sdfsdf-dfdsf-dsfdsf", + bodyText: "Some note..", + questionnaireTitle: `This is a test`, + userId: "12345", + time: new Date(), + }; + questionnaire = { id: questionnaireId, - isPublic: true, - createdAt: 1574682388976, - updatedAt: 1574682388976, - type: "Social", - history: [creationEvent], + history: [creationEvent, historyEvent, noteEvent], }; - - await saveModel(new QuestionnaireModel(metadata)); }); it("should be deterministic", () => { @@ -43,60 +40,40 @@ describe("addTypeToHistoryEvent", () => { }); it("should add system type when there is no body text", async () => { - metadata.history[1] = historyEvent; - - const createdMetadata = await saveModel(new QuestionnaireModel(metadata)); - expect(createdMetadata.history[1].type).toBeUndefined(); - await addTypeToHistoryEvent(questionnaire); - const migratedMetadata = await getQuestionnaireMetaById(questionnaireId); - expect(migratedMetadata.history[0].type).toBe("system"); + expect(questionnaire.history[0].type).toBe("system"); }); it("should add note type when there is body text", async () => { - metadata.history[1] = { ...historyEvent, bodyText: "Some note.." }; - - const createdMetadata = await saveModel(new QuestionnaireModel(metadata)); - expect(createdMetadata.history[1].type).toBeUndefined(); - await addTypeToHistoryEvent(questionnaire); - const migratedMetadata = await getQuestionnaireMetaById(questionnaireId); - expect(migratedMetadata.history[1].type).toBe("note"); + expect(questionnaire.history[2].type).toBe("note"); }); it("should not change type when it already exists", async () => { - metadata.history[1] = { + questionnaire.history[1] = { ...historyEvent, bodyText: "Some note..", type: "note", }; - const createdMetadata = await saveModel(new QuestionnaireModel(metadata)); - expect(createdMetadata.history[1].type).toBe("note"); - await addTypeToHistoryEvent(questionnaire); - const migratedMetadata = await getQuestionnaireMetaById(questionnaireId); - expect(migratedMetadata.history[0].type).toBe("system"); - expect(migratedMetadata.history[1].type).toBe("note"); + expect(questionnaire.history[0].type).toBe("system"); + expect(questionnaire.history[1].type).toBe("note"); }); it("should work with multiple history items", async () => { - metadata.history = [ + questionnaire.history = [ creationEvent, { ...historyEvent, bodyText: "Some note.." }, { ...historyEvent, type: "testType" }, ]; - await saveModel(new QuestionnaireModel(metadata)); - await addTypeToHistoryEvent(questionnaire); - - const migratedMetadata = await getQuestionnaireMetaById(questionnaireId); - expect(migratedMetadata.history[0].type).toBe("system"); - expect(migratedMetadata.history[1].type).toBe("note"); - expect(migratedMetadata.history[2].type).toBe("testType"); + expect(questionnaire.history[0].type).toBe("system"); + expect(questionnaire.history[1].type).toBe("note"); + expect(questionnaire.history[2].type).toBe("testType"); }); });