Skip to content

Commit

Permalink
Merge pull request #3099 from ONSdigital/EAR-2305-data-version-selection
Browse files Browse the repository at this point in the history
EAR 2305 data version selection
  • Loading branch information
farres1 authored Mar 13, 2024
2 parents 479092f + fef676b commit 3f3b74d
Show file tree
Hide file tree
Showing 21 changed files with 449 additions and 17 deletions.
1 change: 1 addition & 0 deletions eq-author-api/constants/allDataVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ["1", "3"];
18 changes: 18 additions & 0 deletions eq-author-api/migrations/addAllowableDataVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { getOptions } = require("../schema/resolvers/utils");

module.exports = (questionnaire) => {
const allQuestionnaireOptions = getOptions({ questionnaire });

if (
questionnaire.collectionLists?.lists?.length > 0 ||
questionnaire.supplementaryData ||
allQuestionnaireOptions?.some((option) => option.dynamicAnswer)
) {
questionnaire.dataVersion = "3";
questionnaire.allowableDataVersions = ["3"];
} else {
questionnaire.allowableDataVersions = ["1", "3"];
}

return questionnaire;
};
82 changes: 82 additions & 0 deletions eq-author-api/migrations/addAllowableDataVersions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const addAllowableDataVersions = require("./addAllowableDataVersions");

describe("addAllowableDataVersions", () => {
it("should set dataVersion to 3 and add allowableDataVersions with data version 3 when questionnaire has collection list", () => {
const questionnaire = {
id: "questionnaire-1",
title: "Questionnaire 1",
collectionLists: {
lists: [{ id: "list-1" }],
},
};

const updatedQuestionnaire = addAllowableDataVersions(questionnaire);

expect(updatedQuestionnaire.dataVersion).toEqual("3");
expect(updatedQuestionnaire.allowableDataVersions).toEqual(["3"]);
});

it("should set dataVersion to 3 and add allowableDataVersions with data version 3 when questionnaire has supplementary data", () => {
const questionnaire = {
id: "questionnaire-1",
title: "Questionnaire 1",
supplementaryData: { id: "supplementary-data-1" },
};

const updatedQuestionnaire = addAllowableDataVersions(questionnaire);

expect(updatedQuestionnaire.dataVersion).toEqual("3");
expect(updatedQuestionnaire.allowableDataVersions).toEqual(["3"]);
});

it("should set dataVersion to 3 and add allowableDataVersions with data version 3 when questionnaire has dynamic answer", () => {
const questionnaire = {
id: "questionnaire-1",
title: "Questionnaire 1",
sections: [
{
id: "section-1",
folders: [
{
id: "folder-1",
pages: [
{
id: "page-1",
answers: [
{
id: "answer-1",
options: [
{
id: "option-1",
dynamicAnswer: true,
},
],
},
],
},
],
},
],
},
],
};

const updatedQuestionnaire = addAllowableDataVersions(questionnaire);

expect(updatedQuestionnaire.dataVersion).toEqual("3");
expect(updatedQuestionnaire.allowableDataVersions).toEqual(["3"]);
});

it("should add allowableDataVersions with data versions 1 and 3 without updating dataVersion when questionnaire has no collection lists, supplementary data or dynamic answers", () => {
const questionnaire = {
id: "questionnaire-1",
title: "Questionnaire 1",
dataVersion: "1",
};

const updatedQuestionnaire = addAllowableDataVersions(questionnaire);

expect(updatedQuestionnaire.dataVersion).toEqual("1");
expect(updatedQuestionnaire.allowableDataVersions).toEqual(["1", "3"]);
});
});
1 change: 1 addition & 0 deletions eq-author-api/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const migrations = [
require("./addFieldsToListCollectorFolderContents"),
require("./addAdditonalContentsToAddItemPage"),
require("./updateHealthThemeToPandemicMonitoring"),
require("./addAllowableDataVersions"),
];

const currentVersion = migrations.length;
Expand Down
7 changes: 6 additions & 1 deletion eq-author-api/schema/resolvers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
AWAITING_APPROVAL,
UPDATES_REQUIRED,
} = require("../../constants/publishStatus");
const allDataVersions = require("../../constants/allDataVersions");

const { DURATION_LOOKUP } = require("../../constants/durationTypes");
const {
Expand Down Expand Up @@ -149,7 +150,8 @@ const createNewQuestionnaire = (input) => {
qcodes: true,
navigation: false,
hub: false,
dataVersion: "1",
dataVersion: "3",
allowableDataVersions: allDataVersions,
createdAt: new Date(),
metadata: [],
sections: [createSection()],
Expand Down Expand Up @@ -658,6 +660,7 @@ const Resolvers = {
const pages = getPages(ctx);

onSectionDeleted(ctx, removedSection, pages);
setDataVersion(ctx);

if (!ctx.questionnaire.sections.length) {
ctx.questionnaire.sections.push(createSection());
Expand Down Expand Up @@ -727,6 +730,7 @@ const Resolvers = {
const pages = getPages(ctx);

onFolderDeleted(ctx, removedFolder, pages);
setDataVersion(ctx);

if (!section.folders.length) {
section.folders.push(createFolder());
Expand Down Expand Up @@ -862,6 +866,7 @@ const Resolvers = {
const deletedAnswer = first(remove(page.answers, { id: input.id }));

onAnswerDeleted(ctx, page, deletedAnswer, pages);
setDataVersion(ctx);

return page;
}),
Expand Down
5 changes: 5 additions & 0 deletions eq-author-api/schema/resolvers/importing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const { UserInputError } = require("apollo-server-express");

const { createMutation } = require("./createMutation");

const { setDataVersion } = require("./utils");

module.exports = {
Mutation: {
importQuestions: createMutation(
Expand Down Expand Up @@ -91,6 +93,7 @@ module.exports = {
...strippedPages.map((page) => createFolder({ pages: [page] }))
);
}
setDataVersion(ctx);

return section;
}
Expand Down Expand Up @@ -178,6 +181,8 @@ module.exports = {

destinationSections.splice(insertionIndex, 0, ...strippedSections);
ctx.questionnaire.hub = ctx.questionnaire.sections.length > 1;
setDataVersion(ctx);

return destinationSections;
}
),
Expand Down
2 changes: 2 additions & 0 deletions eq-author-api/schema/resolvers/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { createQuestionPage } = require("./questionPage");
const deleteFirstPageSkipConditions = require("../../../src/businessLogic/deleteFirstPageSkipConditions");
const deleteLastPageRouting = require("../../../src/businessLogic/deleteLastPageRouting");
const onFolderDeleted = require("../../../src/businessLogic/onFolderDeleted");
const { setDataVersion } = require("../utils/setDataVersion");

const Resolvers = {};

Expand Down Expand Up @@ -86,6 +87,7 @@ Resolvers.Mutation = {

deleteFirstPageSkipConditions(ctx);
deleteLastPageRouting(ctx);
setDataVersion(ctx);

return section;
}),
Expand Down
6 changes: 5 additions & 1 deletion eq-author-api/schema/resolvers/utils/setDataVersion.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const { getAnswers } = require("./answerGetters");
const allDataVersions = require("../../../constants/allDataVersions");

const setDataVersion = ({ questionnaire }) => {
questionnaire.dataVersion = "1";
questionnaire.allowableDataVersions = allDataVersions;

if (questionnaire.collectionLists?.lists?.length) {
questionnaire.dataVersion = "3";
questionnaire.allowableDataVersions = ["3"];
return;
}

if (questionnaire.supplementaryData) {
questionnaire.dataVersion = "3";
questionnaire.allowableDataVersions = ["3"];
return;
}

Expand All @@ -18,6 +21,7 @@ const setDataVersion = ({ questionnaire }) => {
return answer.options?.some((option) => {
if (option.dynamicAnswer) {
questionnaire.dataVersion = "3";
questionnaire.allowableDataVersions = ["3"];
return true;
}
return false;
Expand Down
64 changes: 64 additions & 0 deletions eq-author-api/schema/tests/questionnaire.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { last, findIndex, find } = require("lodash");
const { CHECKBOX, RADIO } = require("../../constants/answerTypes");

jest.mock("node-fetch");
const fetch = require("node-fetch");
Expand Down Expand Up @@ -35,6 +36,9 @@ const {
setQuestionnaireLocked,
updateSubmission,
} = require("../../tests/utils/contextBuilder/questionnaire");
const {
updateOption,
} = require("../../tests/utils/contextBuilder/option/updateOption");

const {
createAnswer,
Expand Down Expand Up @@ -164,6 +168,66 @@ describe("questionnaire", () => {
expect(queriedShortTitleQuestionnaire.displayName).toEqual("short title");
});

it("should set data version to 3 and remove data version 1 from allowable data versions if questionnaire contains dynamic answers", async () => {
const ctx = await buildContext({
supplementaryData: null,
sections: [
{
folders: [
{
pages: [
{
answers: [
{
type: CHECKBOX,
options: [
{
label: "checkbox-option-1",
},
],
},
],
},
{
answers: [
{
type: RADIO,
options: [
{
id: "radio-option-1",
},
],
},
],
},
],
},
],
},
],
});

expect(ctx.questionnaire.dataVersion).toEqual("3");
expect(ctx.questionnaire.allowableDataVersions).toEqual(["1", "3"]);

const option =
ctx.questionnaire.sections[0].folders[0].pages[1].answers[0].options[0];

const update = {
id: option.id,
label: "Dynamic option 1",
description: "Dynamic option description",
value: "dynamic-option-1",
qCode: "dynamic-option-1",
dynamicAnswer: true,
};

updateOption(ctx, update);

expect(ctx.questionnaire.dataVersion).toEqual("3");
expect(ctx.questionnaire.allowableDataVersions).toEqual(["3"]);
});

describe("starring", () => {
it("should throw user input error if questionnaire ID doesn't exist", () => {
expect(
Expand Down
2 changes: 2 additions & 0 deletions eq-author-api/schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Questionnaire {
navigation: Boolean
hub: Boolean
dataVersion: String
allowableDataVersions: [String]
createdAt: DateTime
updatedAt: DateTime
createdBy: User!
Expand Down Expand Up @@ -1311,6 +1312,7 @@ input UpdateQuestionnaireInput {
editors: [ID!]
isPublic: Boolean
permission: String
dataVersion: String
}
Expand Down
3 changes: 2 additions & 1 deletion eq-author-api/tests/utils/contextBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const buildContext = async (questionnaireConfig, userConfig = {}) => {
const { questionnaire } = ctx;
ctx.questionnaire.sections = [];
ctx.comments = comments || {};
ctx.questionnaire.supplementaryData = supplementaryData || {};
ctx.questionnaire.supplementaryData =
supplementaryData === null ? null : supplementaryData || {};

if (Array.isArray(sections)) {
for (let section of sections) {
Expand Down
Loading

0 comments on commit 3f3b74d

Please sign in to comment.