diff --git a/packages/form/addon/gql/fragments/case-form-and-workflow.graphql b/packages/form/addon/gql/fragments/case-form-and-workflow.graphql new file mode 100644 index 000000000..ab2b8d932 --- /dev/null +++ b/packages/form/addon/gql/fragments/case-form-and-workflow.graphql @@ -0,0 +1,28 @@ +fragment CaseFormAndWorkflow on Case { + id + workflow { + id + slug + } + document { + id + form { + id + slug + } + } + family { + id + workflow { + id + slug + } + document { + id + form { + id + slug + } + } + } +} diff --git a/packages/form/addon/gql/queries/document-answers.graphql b/packages/form/addon/gql/queries/document-answers.graphql index 51bf419ec..81600bfd5 100644 --- a/packages/form/addon/gql/queries/document-answers.graphql +++ b/packages/form/addon/gql/queries/document-answers.graphql @@ -1,4 +1,5 @@ #import * from '../fragments/field.graphql' +#import * from '../fragments/case-form-and-workflow.graphql' query DocumentAnswers($id: ID!) { allDocuments(filter: [{ id: $id }]) { @@ -12,21 +13,11 @@ query DocumentAnswers($id: ID!) { workItem { id case { - id - family { - id - document { - id - form { - id - slug - } - } - } + ...CaseFormAndWorkflow } } case { - id + ...CaseFormAndWorkflow workItems { edges { node { diff --git a/packages/form/addon/lib/document.js b/packages/form/addon/lib/document.js index eb4097959..664ae5a1f 100644 --- a/packages/form/addon/lib/document.js +++ b/packages/form/addon/lib/document.js @@ -198,6 +198,7 @@ export default class Document extends Base { * @property {Object} jexlContext */ get jexlContext() { + const _case = this.raw.workItem?.case ?? this.raw.case; return ( this.parentDocument?.jexlContext ?? { // JEXL interprets null in an expression as variable instead of a @@ -209,6 +210,14 @@ export default class Document extends Base { form: this.rootForm.slug, formMeta: this.rootForm.raw.meta, }, + case: { + form: _case?.document?.form.slug, + workflow: _case?.workflow.slug, + root: { + form: _case?.family.document?.form.slug, + workflow: _case?.family.workflow.slug, + }, + }, }, } ); diff --git a/packages/form/addon/lib/field.js b/packages/form/addon/lib/field.js index 2816f09f2..652e9467a 100644 --- a/packages/form/addon/lib/field.js +++ b/packages/form/addon/lib/field.js @@ -413,27 +413,26 @@ export default class Field extends Base { * - `form`: Legacy property pointing to the root form. * - `info.form`: The form this question is attached to. * - `info.formMeta`: The meta of the form this question is attached to. - * - `info.mainCaseForm`: The main cases' form (can give useful context in task forms). * - `info.parent.form`: The parent form if applicable. * - `info.parent.formMeta`: The parent form meta if applicable. * - `info.root.form`: The new property for the root form. * - `info.root.formMeta`: The new property for the root form meta. + * - `info.case.form`: The cases' form (works for task forms and case forms). + * - `info.case.workflow`: The cases' workflow (works for task forms and case forms). + * - `info.case.root.form`: The _root_ cases' form (works for task forms and case forms). + * - `info.case.root.workflow`: The _root_ cases' workflow (works for task forms and case forms). * * @property {Object} jexlContext */ get jexlContext() { const parent = this.fieldset.field?.fieldset.form; - const rootDocument = this.document.parentDocument ?? this.document; - return { ...this.document.jexlContext, info: { ...this.document.jexlContext.info, form: this.fieldset.form.slug, formMeta: this.fieldset.form.raw.meta, - mainCaseForm: - rootDocument.raw.workItem?.case.family.document?.form.slug, parent: parent ? { form: parent.slug, diff --git a/packages/form/tests/integration/components/document-validity-test.js b/packages/form/tests/integration/components/document-validity-test.js index 51bdba31b..59a72e8e3 100644 --- a/packages/form/tests/integration/components/document-validity-test.js +++ b/packages/form/tests/integration/components/document-validity-test.js @@ -3,7 +3,7 @@ import { hbs } from "ember-cli-htmlbars"; import { setupMirage } from "ember-cli-mirage/test-support"; import { module, test } from "qunit"; -import data from "../../unit/lib/data"; +import { rawDocumentWithWorkItem } from "../../unit/lib/data"; import { parseDocument } from "@projectcaluma/ember-form/lib/parsers"; import { setupRenderingTest } from "dummy/tests/helpers"; @@ -14,7 +14,7 @@ module("Integration | Component | document-validity", function (hooks) { hooks.beforeEach(function () { this.document = new (this.owner.factoryFor("caluma-model:document").class)({ - raw: parseDocument(data), + raw: parseDocument(rawDocumentWithWorkItem), owner: this.owner, }); diff --git a/packages/form/tests/unit/lib/data.js b/packages/form/tests/unit/lib/data.js index 577d1e619..0f7cc9a3c 100644 --- a/packages/form/tests/unit/lib/data.js +++ b/packages/form/tests/unit/lib/data.js @@ -236,26 +236,59 @@ const answers = { ], }; -const workItem = { - id: id("WorkItem"), - case: { +const _case = { + id: id("Case"), + workflow: { + id: id("Workflow", "child-case-workflow"), + slug: "child-case-workflow", + }, + document: { + id: id("Document"), + form: { + id: id("Form", "child-case-form"), + slug: "child-case-form", + }, + }, + family: { id: id("Case"), - family: { - id: id("Case"), - document: { - id: id("Document"), - form: { - slug: "main-case-form", - }, + workflow: { + id: id("Workflow", "root-case-workflow"), + slug: "root-case-workflow", + }, + document: { + id: id("Document"), + form: { + id: id("Form", "root-case-form"), + slug: "root-case-form", }, }, }, }; -export default { +const workItem = { + id: id("WorkItem"), + case: _case, +}; + +export const rawDocumentWithCase = { + id: id("Document"), + answers, + form, + case: _case, + __typename: "Document", +}; + +export const rawDocumentWithWorkItem = { id: id("Document"), answers, form, workItem, __typename: "Document", }; + +export const rawUnlinkedDocument = { + id: id("Document"), + answers, + form, + __typename: "Document", +}; diff --git a/packages/form/tests/unit/lib/document-test.js b/packages/form/tests/unit/lib/document-test.js index ddf1c0b04..327f8473d 100644 --- a/packages/form/tests/unit/lib/document-test.js +++ b/packages/form/tests/unit/lib/document-test.js @@ -1,7 +1,11 @@ import { settled } from "@ember/test-helpers"; import { module, test, skip } from "qunit"; -import data from "./data"; +import { + rawDocumentWithCase, + rawDocumentWithWorkItem, + rawUnlinkedDocument, +} from "./data"; import { parseDocument } from "@projectcaluma/ember-form/lib/parsers"; import { setupTest } from "dummy/tests/helpers"; @@ -23,7 +27,7 @@ module("Unit | Library | document", function (hooks) { this.set( "document", new (this.owner.factoryFor("caluma-model:document").class)({ - raw: parseDocument(data), + raw: parseDocument(rawDocumentWithWorkItem), owner: this.owner, }), ); @@ -261,13 +265,73 @@ module("Unit | Library | document", function (hooks) { ); }); - test("computes the correct jexl context", async function (assert) { + test("computes the correct jexl context (task form)", async function (assert) { assert.expect(1); assert.deepEqual(this.document.jexlContext, { null: null, form: "form", info: { + case: { + form: "child-case-form", + root: { + form: "root-case-form", + workflow: "root-case-workflow", + }, + workflow: "child-case-workflow", + }, + root: { form: "form", formMeta: { "is-top-form": true, level: 0 } }, + }, + }); + }); + + test("computes the correct jexl context (case form)", async function (assert) { + assert.expect(1); + + const documentWithCase = new (this.owner.factoryFor( + "caluma-model:document", + ).class)({ + raw: parseDocument(rawDocumentWithCase), + owner: this.owner, + }); + assert.deepEqual(documentWithCase.jexlContext, { + null: null, + form: "form", + info: { + case: { + form: "child-case-form", + root: { + form: "root-case-form", + workflow: "root-case-workflow", + }, + workflow: "child-case-workflow", + }, + root: { form: "form", formMeta: { "is-top-form": true, level: 0 } }, + }, + }); + }); + + test("computes the correct jexl context (unlinked document)", async function (assert) { + assert.expect(1); + + const documentWithCase = new (this.owner.factoryFor( + "caluma-model:document", + ).class)({ + raw: parseDocument(rawUnlinkedDocument), + owner: this.owner, + }); + assert.deepEqual(documentWithCase.jexlContext, { + null: null, + form: "form", + info: { + case: { + form: undefined, + root: { + form: undefined, + workflow: undefined, + }, + workflow: undefined, + }, root: { form: "form", formMeta: { "is-top-form": true, level: 0 } }, }, }); diff --git a/packages/form/tests/unit/lib/field-test.js b/packages/form/tests/unit/lib/field-test.js index a0a85f207..924dda4f0 100644 --- a/packages/form/tests/unit/lib/field-test.js +++ b/packages/form/tests/unit/lib/field-test.js @@ -3,7 +3,7 @@ import { setupMirage } from "ember-cli-mirage/test-support"; import { setupIntl } from "ember-intl/test-support"; import { module, test } from "qunit"; -import data from "./data"; +import { rawDocumentWithWorkItem } from "./data"; import { getDependenciesFromJexl } from "@projectcaluma/ember-form/lib/dependencies"; import { parseDocument } from "@projectcaluma/ember-form/lib/parsers"; @@ -18,7 +18,7 @@ module("Unit | Library | field", function (hooks) { this.set( "document", new (this.owner.factoryFor("caluma-model:document").class)({ - raw: parseDocument(data), + raw: parseDocument(rawDocumentWithWorkItem), owner: this.owner, }), ); @@ -146,7 +146,6 @@ module("Unit | Library | field", function (hooks) { "is-top-form": false, level: 1, }, - mainCaseForm: "main-case-form", parent: null, root: { form: "form", @@ -155,6 +154,14 @@ module("Unit | Library | field", function (hooks) { level: 0, }, }, + case: { + form: "child-case-form", + workflow: "child-case-workflow", + root: { + form: "root-case-form", + workflow: "root-case-workflow", + }, + }, }, }); });