From 9c19ed221a7e51c3f11002de0f717907e1a36dd6 Mon Sep 17 00:00:00 2001 From: farres1 Date: Fri, 16 Dec 2022 09:45:05 +0000 Subject: [PATCH 1/3] Add metadata logic --- .../routing2/newRoutingDestination/index.js | 22 +++++++++++++++++++ src/utils/contentUtils/getMetadataKey.js | 11 ++++++++++ src/utils/routingConditionConversion/index.js | 1 + 3 files changed, 34 insertions(+) create mode 100644 src/utils/contentUtils/getMetadataKey.js diff --git a/src/eq_schema/builders/routing2/newRoutingDestination/index.js b/src/eq_schema/builders/routing2/newRoutingDestination/index.js index 4de01264..4e536198 100644 --- a/src/eq_schema/builders/routing2/newRoutingDestination/index.js +++ b/src/eq_schema/builders/routing2/newRoutingDestination/index.js @@ -1,4 +1,8 @@ const routingConditionConversion = require("../../../../utils/routingConditionConversion"); +const { + getMetadataKey, +} = require("../../../../utils/contentUtils/getMetadataKey"); + const { flatMap, filter } = require("lodash"); const authorConditions = { @@ -35,6 +39,10 @@ const checkType = (type) => { return "answers"; } + if (type === "Metadata") { + return "metadata"; + } + return null; }; @@ -126,9 +134,23 @@ const buildAnswerObject = ( return finalVal; }; +const buildMetadataObject = (expression, ctx) => { + const { condition, left, right } = expression; + const returnValue = [ + { + source: checkType(left.type), + identifier: getMetadataKey(ctx, left.metadataId), + }, + right.customValue.text, + ]; + return { [routingConditionConversion(condition)]: returnValue }; +}; + const checkValidRoutingType = (expression, ctx) => { if (expression.left.type === "Answer") { return buildAnswerObject(expression, ctx); + } else if (expression.left.type === "Metadata") { + return buildMetadataObject(expression, ctx); } else { throw new Error( `${expression.left.type} is not a valid routing answer type` diff --git a/src/utils/contentUtils/getMetadataKey.js b/src/utils/contentUtils/getMetadataKey.js new file mode 100644 index 00000000..f6dd7f92 --- /dev/null +++ b/src/utils/contentUtils/getMetadataKey.js @@ -0,0 +1,11 @@ +const { find } = require("lodash"); + +const getMetadataKey = (ctx, metadataId) => { + const metadata = find(ctx.questionnaireJson.metadata, { id: metadataId }); + if (metadata) { + return metadata.key; + } + return null; +}; + +module.exports = { getMetadataKey }; diff --git a/src/utils/routingConditionConversion/index.js b/src/utils/routingConditionConversion/index.js index bcc1003d..c70de2c6 100644 --- a/src/utils/routingConditionConversion/index.js +++ b/src/utils/routingConditionConversion/index.js @@ -10,6 +10,7 @@ const routingConditionConversions = { NotAnyOf: "not", Unanswered: "==", OneOf: "in", + Matches: "==", }; const routingConditionConversion = (conditionString) => { From 1e178ba4f1d18a58d5e4c61f0566355fbefdf3b5 Mon Sep 17 00:00:00 2001 From: farres1 Date: Mon, 19 Dec 2022 14:44:43 +0000 Subject: [PATCH 2/3] Add newRoutingDestination test for metadata logic --- .../builders/basicQuestionnaireJSON.js | 51 +-- .../newRoutingDestination/index.test.js | 325 ++++++++++-------- 2 files changed, 211 insertions(+), 165 deletions(-) diff --git a/src/eq_schema/builders/basicQuestionnaireJSON.js b/src/eq_schema/builders/basicQuestionnaireJSON.js index d3d55c4e..45ba3a25 100644 --- a/src/eq_schema/builders/basicQuestionnaireJSON.js +++ b/src/eq_schema/builders/basicQuestionnaireJSON.js @@ -2,6 +2,15 @@ const questionnaireJson = { id: "1", title: "Basic Questionnaire JSON", summary: false, + metadata: [ + { + id: "metadata-1", + key: "ru_name", + alias: "Ru Name", + type: "Text", + textValue: "ESSENTIAL ENTERPRISE LTD.", + }, + ], sections: [ { id: "1", @@ -21,9 +30,9 @@ const questionnaireJson = { { id: "1", type: "Currency", - label: "Answer 1" - } - ] + label: "Answer 1", + }, + ], }, { id: "2", @@ -35,13 +44,13 @@ const questionnaireJson = { { id: "2", type: "Number", - label: "Answer 2" - } - ] - } - ] - } - ] + label: "Answer 2", + }, + ], + }, + ], + }, + ], }, { id: "2", @@ -61,7 +70,7 @@ const questionnaireJson = { { id: "3", type: "Number", - label: "Answer 3" + label: "Answer 3", }, { id: "4", @@ -74,16 +83,16 @@ const questionnaireJson = { { id: "456", label: "white", - } - ] - } - ] - } - ] - } - ] - } - ] + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], }; const questionnaireJsonWithSummary = { diff --git a/src/eq_schema/builders/routing2/newRoutingDestination/index.test.js b/src/eq_schema/builders/routing2/newRoutingDestination/index.test.js index 767a8f67..ba1ac519 100644 --- a/src/eq_schema/builders/routing2/newRoutingDestination/index.test.js +++ b/src/eq_schema/builders/routing2/newRoutingDestination/index.test.js @@ -3,177 +3,214 @@ const { questionnaireJson } = require("../../basicQuestionnaireJSON"); const checkValidRoutingType = require("."); describe("Should build a runner representation of a binary expression", () => { - it("should throw on unsupported answer type", () => { - const expression = { - left: { - id: "1", - type: "Text", - }, - condition: "Equal", - right: { - number: 5, - }, - }; - - expect(() => - checkValidRoutingType(expression, { questionnaireJson }) - ).toThrow("not a valid routing answer type"); - }); - describe("With Radio answers", () => { - const buildBinaryExpression = (optionIds, condition) => ({ - left: { - answerId: "1", - type: "Answer", - }, - condition, - right: { - type: "SelectedOptions", - optionIds, - }, + describe("With answers", () => { + it("should throw on unsupported answer type", () => { + const expression = { + left: { + id: "1", + type: "Text", + }, + condition: "Equal", + right: { + number: 5, + }, + }; + + expect(() => + checkValidRoutingType(expression, { questionnaireJson }) + ).toThrow("not a valid routing answer type"); }); - it("With a radio answer and single selected option", () => { - const expression = buildBinaryExpression(["123"], "OneOf"); - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, + describe("With Radio answers", () => { + const buildBinaryExpression = (optionIds, condition) => ({ + left: { + answerId: "1", + type: "Answer", + }, + condition, + right: { + type: "SelectedOptions", + optionIds, + }, }); - expect(runnerExpression).toMatchObject({ - "in": [ - { - identifier: "answer1", - source: "answers", - }, - ["red"], - ], + it("With a radio answer and single selected option", () => { + const expression = buildBinaryExpression(["123"], "OneOf"); + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + + expect(runnerExpression).toMatchObject({ + in: [ + { + identifier: "answer1", + source: "answers", + }, + ["red"], + ], + }); }); - }); - it("With a radio answer and no selected options", () => { - const expression = buildBinaryExpression(["123", "456"], "Unanswered"); - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, + it("With a radio answer and no selected options", () => { + const expression = buildBinaryExpression(["123", "456"], "Unanswered"); + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + + expect(runnerExpression).toMatchObject({ + "==": [ + null, + { + identifier: "answer1", + source: "answers", + }, + ], + }); }); - expect(runnerExpression).toMatchObject({ - "==": [ - null, - { - identifier: "answer1", - source: "answers", - }, - ], + it("With a radio answer and multiple selected options", () => { + const expression = buildBinaryExpression(["123", "456"], "OneOf"); + + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + expect(runnerExpression).toMatchObject({ + in: [ + { + identifier: "answer1", + source: "answers", + }, + ["red", "white"], + ], + }); }); }); - it("With a radio answer and multiple selected options", () => { - const expression = buildBinaryExpression(["123", "456"], "OneOf"); - - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, - }); - expect(runnerExpression).toMatchObject({ - "in": [ - { - identifier: "answer1", - source: "answers", + describe("With Number based answers", () => { + it("supports a custom value", () => { + const expression = { + left: { + answerId: "1", + type: "Answer", + }, + condition: "Equal", + right: { + customValue: { + number: 5, + }, }, - ["red", "white"], - ], + }; + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + expect(runnerExpression).toMatchObject({ + "==": [ + { + identifier: "answer1", + source: "answers", + }, + 5, + ], + }); }); - }); - }); - describe("With Number based answers", () => { - it("supports a custom value", () => { - const expression = { - left: { - answerId: "1", - type: "Answer", - }, - condition: "Equal", - right: { - customValue: { - number: 5, + it("Checks that even though there's a number value stored on the right handside, the condition is unanswered, meaning the value should be null on the object", () => { + const expression = { + left: { + answerId: "1", + type: "Answer", }, - }, - }; - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, - }); - expect(runnerExpression).toMatchObject({ - "==": [ - { - identifier: "answer1", - source: "answers", + condition: "Unanswered", + right: { + customValue: { + number: 5, + }, }, - 5, - ], + }; + + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + + expect(runnerExpression).toMatchObject({ + "==": [ + { + identifier: "answer1", + source: "answers", + }, + null, + ], + }); }); }); - it("Checks that even though there's a number value stored on the right handside, the condition is unanswered, meaning the value should be null on the object", () => { - const expression = { - left: { - answerId: "1", - type: "Answer", - }, - condition: "Unanswered", - right: { - customValue: { - number: 5, + describe("With checkbox answers", () => { + it("Excepts a checkbox count of answer and returns the correct object", () => { + const expression = { + secondaryCondition: "Equal", + left: { + answerId: "1", + type: "Answer", }, - }, - }; - - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, - }); - - expect(runnerExpression).toMatchObject({ - "==": [ - { - identifier: "answer1", - source: "answers", + condition: "CountOf", + right: { + customValue: { + number: 5, + }, }, - null, - ], + }; + + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + + expect(runnerExpression).toMatchObject({ + "==": [ + { + count: [ + { + source: "answers", + identifier: "answer1", + }, + ], + }, + 5, + ], + }); }); }); }); - describe("With checkbox answers", () => { - it("Excepts a checkbox count of answer and returns the correct object", () => { - const expression = { - secondaryCondition: "Equal", - left: { - answerId: "1", - type: "Answer", - }, - condition: "CountOf", - right: { - customValue: { - number: 5, + describe("With metadata", () => { + describe("Text metadata", () => { + it("should return correct metadata object for text metadata", () => { + const expression = { + left: { + metadataId: "metadata-1", + type: "Metadata", }, - }, - }; - - const runnerExpression = checkValidRoutingType(expression, { - questionnaireJson, - }); - - expect(runnerExpression).toMatchObject({ - "==": [ - { - count: [ - { - source: "answers", - identifier: "answer1", - }, - ], + condition: "Matches", + right: { + type: "Custom", + customValue: { + text: "Test text", + }, }, - 5, - ], + }; + + const runnerExpression = checkValidRoutingType(expression, { + questionnaireJson, + }); + + expect(runnerExpression).toMatchObject({ + "==": [ + { + source: "metadata", + identifier: "ru_name", + }, + "Test text", + ], + }); }); }); }); From 3ab0da6860c88933ba26268a7b8d7f9e0c88f1f8 Mon Sep 17 00:00:00 2001 From: farres1 Date: Mon, 19 Dec 2022 15:09:22 +0000 Subject: [PATCH 3/3] Add getMetadataKey tests --- src/utils/contentUtils/getMetadataKey.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/contentUtils/getMetadataKey.test.js diff --git a/src/utils/contentUtils/getMetadataKey.test.js b/src/utils/contentUtils/getMetadataKey.test.js new file mode 100644 index 00000000..9439eafb --- /dev/null +++ b/src/utils/contentUtils/getMetadataKey.test.js @@ -0,0 +1,19 @@ +const { getMetadataKey } = require("./getMetadataKey"); +const { + questionnaireJson, +} = require("../../eq_schema/builders/basicQuestionnaireJSON"); + +describe("getMetadataKey", () => { + let ctx = {}; + ctx.questionnaireJson = questionnaireJson; + + it("should return metadata key for valid metadataId", () => { + const metadataKey = getMetadataKey(ctx, "metadata-1"); + expect(metadataKey).toBe("ru_name"); + }); + + it("should return null for invalid metadataId", () => { + const metadataKey = getMetadataKey(ctx, "not-metadata-1"); + expect(metadataKey).toBeNull(); + }); +});