Skip to content

Commit

Permalink
Merge pull request #3114 from ONSdigital/EAR-2405-Enable-one-answer-f…
Browse files Browse the repository at this point in the history
…rom-a-list-collector-follow-up-question-on-a-calculated-summary

EAR-2405-Enable one answer from a list collector follow-up question on a calculated summary
  • Loading branch information
martyncolmer authored May 16, 2024
2 parents a968455 + dbd81a9 commit 297cd4d
Showing 1 changed file with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const createValidationError = require("../createValidationError");

const { ERR_NO_ANSWERS } = require("../../../constants/validationErrorCodes");
const { getPages, getFolders } = require("../../../schema/resolvers/utils");
const { getFolderByAnswerId } = require("../../../schema/resolvers/utils");

module.exports = (ajv) =>
ajv.addKeyword({
Expand All @@ -20,41 +20,26 @@ module.exports = (ajv) =>
) {
if (parentData.summaryAnswers.length > 1) {
return true;
} else {
const folders = getFolders({ questionnaire });
const pages = getPages({ questionnaire });

const allAnswers = pages.reduce(
(acc, page) => (page.answers ? [...acc, ...page.answers] : acc),
[]
);
}

const selectedAnswers = allAnswers.filter((answer) =>
parentData.summaryAnswers.includes(answer.id)
if (parentData.summaryAnswers.length === 1) {
let selectedFolder = getFolderByAnswerId(
{ questionnaire },
parentData.summaryAnswers[0]
);

let selectedFolder;
folders.forEach((folder) => {
folder.pages.forEach((page) => {
if (page.id === selectedAnswers[0]?.questionPageId) {
selectedFolder = folder;
}
});
});

if (parentData.summaryAnswers.length === 1 && selectedFolder?.listId) {
if (selectedFolder?.listId) {
return true;
} else {
isValid.errors = [
createValidationError(
instancePath,
fieldName,
ERR_NO_ANSWERS,
questionnaire
),
];
return false;
}
}

isValid.errors = [
createValidationError(
instancePath,
fieldName,
ERR_NO_ANSWERS,
questionnaire
),
];
return false;
},
});

0 comments on commit 297cd4d

Please sign in to comment.