-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3084 from ONSdigital/EAR-1789-looping-questions-c…
…alculated-summary EAR-1789 - Looping questions calculated summary
- Loading branch information
Showing
18 changed files
with
545 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
eq-author-api/src/validation/customKeywords/calculatedSummaryMinAnswers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const createValidationError = require("../createValidationError"); | ||
|
||
const { ERR_NO_ANSWERS } = require("../../../constants/validationErrorCodes"); | ||
const { getPages, getFolders } = require("../../../schema/resolvers/utils"); | ||
|
||
module.exports = (ajv) => | ||
ajv.addKeyword({ | ||
keyword: "calculatedSummaryMinAnswers", | ||
$data: true, | ||
validate: function isValid( | ||
schema, | ||
_data, | ||
_parentSchema, | ||
{ | ||
instancePath, | ||
rootData: questionnaire, | ||
parentData, | ||
parentDataProperty: fieldName, | ||
} | ||
) { | ||
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) | ||
); | ||
|
||
let selectedFolder; | ||
folders.forEach((folder) => { | ||
folder.pages.forEach((page) => { | ||
if (page.id === selectedAnswers[0]?.questionPageId) { | ||
selectedFolder = folder; | ||
} | ||
}); | ||
}); | ||
|
||
if (parentData.summaryAnswers.length === 1 && selectedFolder?.listId) { | ||
return true; | ||
} else { | ||
isValid.errors = [ | ||
createValidationError( | ||
instancePath, | ||
fieldName, | ||
ERR_NO_ANSWERS, | ||
questionnaire | ||
), | ||
]; | ||
return false; | ||
} | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.