Skip to content

Commit

Permalink
Merge pull request #27 from ONSdigital/EAR-1325-publisher-v3-folder-s…
Browse files Browse the repository at this point in the history
…kip-cond

✨ EAR 1325 - Add folder skip condition support
  • Loading branch information
tbaggaley authored May 17, 2021
2 parents 4b7e44b + 551339b commit 448aeda
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 123 deletions.
29 changes: 17 additions & 12 deletions src/eq_schema/schema/Group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ const { isEmpty, reject, flatten, uniqWith, isEqual } = require("lodash");
const {
buildAuthorConfirmationQuestion,
} = require("../../builders/confirmationPage/ConfirmationPage");
const translateAuthorSkipconditions = require("../../builders/skipConditions");

class Group {
constructor(title, folder, ctx) {
this.id = `group${folder.id}`;
this.title = ctx.questionnaireJson.navigation ? title : "";
this.blocks = this.buildBlocks(folder, ctx);

this.skip_conditions = [];

if (folder.skipConditions) {
this.skip_conditions.push(
...translateAuthorSkipconditions(folder.skipConditions, ctx)
);
}

if (!isEmpty(ctx.routingGotos)) {
this.filterContext(this.id, ctx);
const skipConditions = uniqWith(
this.buildSkipConditions(this.id, ctx),
isEqual
);
this.skip_conditions.push(...skipConditions);
}

if (!isEmpty(skipConditions)) {
this.skip_conditions = skipConditions;
}
if (!this.skip_conditions.length) {
delete this.skip_conditions;
}
}

filterContext(currentId, ctx) {
ctx.routingGotos = reject(
ctx.routingGotos,
rule => rule.group === currentId
(rule) => rule.group === currentId
);
}

buildSkipConditions(currentId, ctx) {
return reject(ctx.routingGotos, goto => goto.groupId === currentId).map(
return reject(ctx.routingGotos, (goto) => goto.groupId === currentId).map(
({ when }) => ({
when,
})
Expand All @@ -40,17 +50,12 @@ class Group {

buildBlocks(folder, ctx) {
return flatten(
folder.pages.map(page => {
folder.pages.map((page) => {
const block = new Block(page, folder.id, ctx);
if (page.confirmation) {
return [
block,
buildAuthorConfirmationQuestion(
page,
folder.id,
page.routing,
ctx
),
buildAuthorConfirmationQuestion(page, folder.id, page.routing, ctx),
];
}
return block;
Expand Down
Loading

0 comments on commit 448aeda

Please sign in to comment.