Skip to content

Commit

Permalink
feat(distribution): add functionality to reopen an inquiry
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Aug 31, 2022
1 parent 90eccbd commit 49cff82
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 8 deletions.
10 changes: 10 additions & 0 deletions packages/distribution/addon/abilities/inquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ export default class InquiryAbility extends Ability {
) ?? true
);
}

get canReopen() {
return (
this.model.isRedoable &&
this.model?.addressedGroups
.map(String)
.includes(String(this.calumaOptions.currentGroupId)) &&
(this.config.permissions.reopenInquiry?.(this.model) ?? true)
);
}
}
30 changes: 30 additions & 0 deletions packages/distribution/addon/components/cd-inquiry-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default class CdInquiryDialogComponent extends Component {
},
});

this._setRedoableStates(response.allWorkItems);

/**
* Sadly this is necessary to handle what happens after the withdraw task in
* the inquiry part component because the mutation triggers a refresh of the
Expand All @@ -68,11 +70,39 @@ export default class CdInquiryDialogComponent extends Component {
if (allWorkItems.edges.every((edge) => edge.node.status === "CANCELED")) {
this.router.transitionTo("index");
}

/**
* Get work item that was redoable in the previous calulation and is now
* not anymore. This indicates that the work item was redone which should
* result in a transition to the answer view.
*/
const redoneWorkItem = this._redoableStates.find((stateObj) => {
const updatedWorkItem = allWorkItems.edges.find(
(edge) => decodeId(edge.node.id) === stateObj.id
);

return (
stateObj.isRedoable && !(updatedWorkItem?.node.isRedoable ?? true)
);
});

if (redoneWorkItem) {
this.router.transitionTo("inquiry.detail.answer", redoneWorkItem.id);
}

this._setRedoableStates(allWorkItems);
});

return response;
}

_setRedoableStates(allWorkItems) {
this._redoableStates = allWorkItems.edges.map((edge) => ({
id: decodeId(edge.node.id),
isRedoable: edge.node.isRedoable,
}));
}

@dropTask
*createInquiry(e) {
e.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
</LinkTo>
</li>
{{/if}}
{{#if (and (eq @type "answer") (can "reopen inquiry" @inquiry))}}
<li>
<a href="" {{on "click" (perform this.reopen)}} data-test-reopen>
{{t "caluma.distribution.reopen-inquiry.link"}}
</a>
</li>
{{/if}}
{{/unless}}
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { confirm } from "ember-uikit";

import { decodeId } from "@projectcaluma/ember-core/helpers/decode-id";
import config from "@projectcaluma/ember-distribution/config";
import reopenInquiryMutation from "@projectcaluma/ember-distribution/gql/mutations/reopen-inquiry.graphql";
import withdrawInquiryMutation from "@projectcaluma/ember-distribution/gql/mutations/withdraw-inquiry.graphql";
import inquiryAnswerStatus from "@projectcaluma/ember-distribution/utils/inquiry-answer-status";

Expand Down Expand Up @@ -73,4 +74,33 @@ export default class CdInquiryDialogInquiryPartComponent extends Component {
);
}
}

@dropTask
*reopen(e) {
e.preventDefault();

/* istanbul ignore next */
if (
!(yield confirm(
this.intl.t("caluma.distribution.reopen-inquiry.confirm")
))
) {
return;
}

try {
yield this.apollo.mutate({
mutation: reopenInquiryMutation,
variables: {
workItem: decodeId(this.args.inquiry.id),
statusQuestion: this.config.inquiry.answer.statusQuestion,
buttonTasks: Object.keys(this.config.inquiry.answer.buttons),
},
});
} catch (error) {
this.notification.danger(
this.intl.t("caluma.distribution.reopen-inquiry.error")
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { gql } from "graphql-tag";
import { decodeId } from "@projectcaluma/ember-core/helpers/decode-id";
import config from "@projectcaluma/ember-distribution/config";
import completeWorkItemMutation from "@projectcaluma/ember-distribution/gql/mutations/complete-work-item.graphql";
import redoWorkItemMutation from "@projectcaluma/ember-distribution/gql/mutations/redo-work-item.graphql";
import reopenDistributionMutation from "@projectcaluma/ember-distribution/gql/mutations/reopen-distribution.graphql";
import incompleteInquiriesQuery from "@projectcaluma/ember-distribution/gql/queries/incomplete-inquiries.graphql";

export default class CdNavigationControlsComponent extends Component {
Expand Down Expand Up @@ -76,7 +76,7 @@ export default class CdNavigationControlsComponent extends Component {
);

yield this.apollo.mutate({
mutation: redoWorkItemMutation,
mutation: reopenDistributionMutation,
variables: {
workItem: distributionWorkItemId,
},
Expand Down
1 change: 1 addition & 0 deletions packages/distribution/addon/gql/fragments/inquiry.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fragment InquiryDialog on WorkItem {
createdAt
closedAt
status
isRedoable
task {
id
slug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mutation CompleteInquiryWorkItem(
parentWorkItem {
id
status
isRedoable
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mutation RedoWorkItem($workItem: ID!) {
mutation ReopenDistribution($workItem: ID!) {
redoWorkItem(input: { id: $workItem }) {
workItem {
id
Expand Down
33 changes: 33 additions & 0 deletions packages/distribution/addon/gql/mutations/reopen-inquiry.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#import InquiryStatusDocument from '../fragments/inquiry.graphql'

mutation ReopenInquiry(
$workItem: ID!
$statusQuestion: ID!
$buttonTasks: [String]!
) {
redoWorkItem(input: { id: $workItem }) {
workItem {
id
status
isRedoable
childCase {
id
document {
id
...InquiryStatusDocument
}
workItems(filter: [{ tasks: $buttonTasks }, { status: READY }]) {
edges {
node {
id
task {
id
slug
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,38 @@ module("Integration | Component | cd-inquiry-dialog", function (hooks) {

assert.verifySteps(["transition"]);
});

test("it can reopen an inquiry", async function (assert) {
assert.expect(6);

const inquiry = confirmInquiry({
inquiry: answerInquiry(this.server, {
inquiry: sendInquiry(this.server, {
inquiry: createInquiry(this.server, this.distributionCase, {
from: { id: "group2" },
to: { id: "group1" },
}),
}),
}),
});

assert.strictEqual(inquiry.status, "COMPLETED");

await render(
hbs`<CdInquiryDialog @from="group2" @to="group1" @caseId={{this.caseId}} />`
);

this.owner.lookup("service:router").transitionTo = (route, id) => {
assert.strictEqual(route, "inquiry.detail.answer");
assert.strictEqual(id, inquiry.id);
assert.step("transition");
};

await click("[data-test-reopen]");
await confirm();

assert.strictEqual(inquiry.status, "READY");

assert.verifySteps(["transition"]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module("Integration | Component | cd-navigation/controls", function (hooks) {
);
});

test("it can redo the current distribution", async function (assert) {
test("it can reopen the current distribution", async function (assert) {
await assert.expect(3);

await render(hbs`<CdNavigation::Controls @caseId={{this.caseId}} />`);
Expand Down
5 changes: 5 additions & 0 deletions packages/distribution/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ caluma:
confirm: "Wollen Sie die Anfrage wirklich zurückziehen?"
error: "Fehler beim Zurückziehen der Anfrage"
status: "Zurückgezogen"

reopen-inquiry:
link: "Wiedereröffnen"
confirm: "Wollen Sie die Anfrage wirklich wiedereröffnen?"
error: "Fehler beim Wiedereröffnen der Anfrage"
5 changes: 5 additions & 0 deletions packages/distribution/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ caluma:
confirm: "Do you really want to withdraw the inquiry?"
error: "Error while withdrawing the inquiry"
status: "Withdrawn"

reopen-inquiry:
link: "Reopen"
confirm: "Do you really want to reopen the inquiry?"
error: "Error while reopening the inquiry"
5 changes: 5 additions & 0 deletions packages/distribution/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ caluma:
confirm: "Voulez-vous vraiment retirer la demande ?"
error: "Erreur lors du retrait de la demande"
status: "Retirée"

reopen-inquiry:
link: "Rouvrir"
confirm: "Voulez-vous vraiment rouvrir la demande ?"
error: "Erreur lors de la réouverture de la demande"
11 changes: 8 additions & 3 deletions packages/testing/addon/mirage-graphql/mocks/work-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ export default class WorkItemMock extends BaseMock {
handleRedoWorkItem(_, { input }) {
const { id } = deserialize(input);
const workItem = this.collection.find(id);
const caseId = workItem?.childCaseId;

if (workItem.taskId === "distribution") {
const caseId = workItem.childCaseId;

this.collection
.where({ caseId, taskId: "complete-distribution" })
.update({ status: "READY" });
this.collection
.where({ caseId, taskId: "create-inquiry" })
.update({ status: "READY" });
} else if (workItem.taskId === "inquiry") {
this.server.create("work-item", {
caseId,
status: "READY",
taskId: "adjust-inquiry-answer",
});
}

return this.handleSavePayload.fn.call(this, _, {
Expand Down Expand Up @@ -81,7 +86,7 @@ export default class WorkItemMock extends BaseMock {
.update({ status: "CANCELED" });
this.collection
.findBy({ childCaseId: caseId })
.update({ status: "COMPLETED" });
.update({ status: "COMPLETED", isRedoable: true });
this.schema.cases.find(caseId).update({ status: "COMPLETED" });
} else if (taskId === "revise-inquiry-answer") {
this.collection
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/addon/scenarios/distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export function answerInquiry(server, { inquiry, status, reason, hint }) {
}

export function confirmInquiry({ inquiry }) {
inquiry.update({ status: "COMPLETED" });
inquiry.update({ status: "COMPLETED", isRedoable: true });
inquiry.childCase.update({
status: "COMPLETED",
closedAt: faker.date.recent(),
Expand Down

0 comments on commit 49cff82

Please sign in to comment.