Skip to content

Commit

Permalink
✨ EAR 1325 - Add folder skip condition support
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaggaley committed May 11, 2021
1 parent 4b7e44b commit 551339b
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 551339b

Please sign in to comment.