Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Toggle flow online or offline #3170

Merged
merged 8 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: Flow status history

@regression @flow-status-history
Scenario: Creating a flow
Given a flow exists
Then the status of the flow is offline by default
And a flow_status_history record is created
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved

@regression @flow-status-history
Scenario: Updating a flow
Given a flow exists
When the flow status is changed to online
Then the open flow_status_history record is updated
And a new flow_status_history record is created
72 changes: 72 additions & 0 deletions e2e/tests/api-driven/src/flowStatusHistory/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { FlowStatus } from "@opensystemslab/planx-core/types";
import { $admin } from "../client";
import { createTeam } from "../globalHelpers";
import gql from "graphql-tag";

export const setup = async () => {
const teamId = await createTeam();

const world = { teamId };

return world;
};

export const cleanup = async () => {
await $admin.user._destroyAll();
await $admin.flow._destroyAll();
await $admin.team._destroyAll();
};

interface GetFlowStatus {
flows: {
status: FlowStatus;
};
}

export const getFlowStatus = async (flowId: string) => {
const {
flows: { status },
} = await $admin.client.request<GetFlowStatus>(
gql`
query GetFlowStatus($flowId: uuid!) {
flows: flows_by_pk(id: $flowId) {
status
}
}
`,
{ flowId },
);

return status;
};

interface GetFlowStatusHistory {
flowStatusHistory: {
id: number;
status: FlowStatus;
eventStart: string;
eventEnd: string;
}[];
}

export const getFlowStatusHistory = async (flowId: string) => {
const { flowStatusHistory } =
await $admin.client.request<GetFlowStatusHistory>(
gql`
query GetFlowStatusHistory($flowId: uuid!) {
flowStatusHistory: flow_status_history(
where: { flow_id: { _eq: $flowId } }
order_by: { event_start: asc }
) {
id
status
eventStart: event_start
eventEnd: event_end
}
}
`,
{ flowId },
);

return flowStatusHistory;
};
110 changes: 110 additions & 0 deletions e2e/tests/api-driven/src/flowStatusHistory/steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { When, Then, World, After, Before, Given } from "@cucumber/cucumber";
import assert from "assert";
import { cleanup, getFlowStatus, getFlowStatusHistory, setup } from "./helpers";
import { createFlow } from "../globalHelpers";
import { $admin } from "../client";

export class CustomWorld extends World {
teamId!: number;
flowId!: string;
}

After("@flow-status-history", async function () {
await cleanup();
});

Before<CustomWorld>("@flow-status-history", async function () {
const { teamId } = await setup();
this.teamId = teamId;
});

Given<CustomWorld>("a flow exists", async function () {
const flowId = await createFlow({ teamId: this.teamId, slug: "test-flow" });

assert.ok(flowId, "flowId is not defined");

this.flowId = flowId;
});

Then("the status of the flow is offline by default", async function () {
const status = await getFlowStatus(this.flowId);

assert.equal(
status,
"offline",
`Flow status is ${status} - it should be "offline"`,
);
});
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved

Then("a flow_status_history record is created", async function () {
const flowStatusHistory = await getFlowStatusHistory(this.flowId);

assert.notEqual(
flowStatusHistory.length,
0,
"No records found for flow_status_history",
);
assert.equal(
flowStatusHistory.length,
1,
"Multiple records found for flow_status_history",
);
assert.ok(flowStatusHistory[0], "flow_status_history record not created");
assert.equal(
flowStatusHistory[0].status,
"offline",
`Flow status is ${flowStatusHistory[0].status} - it should be "offline"`,
);
assert.notEqual(
flowStatusHistory[0].eventStart,
null,
"Event start should be set on INSERT",
);
assert.equal(
flowStatusHistory[0].eventEnd,
null,
"Event end should not be set on INSERT",
);
});

When("the flow status is changed to online", async function () {
const flow = await $admin.flow.setStatus({
flow: { id: this.flowId },
status: "online",
});
assert.ok(flow, "Flow not defined after setStatus()");
assert.equal(
flow.status,
"online",
`Flow status is ${flow.status} - it should be "online`,
);
});

Then("the open flow_status_history record is updated", async function () {
const flowStatusHistory = await getFlowStatusHistory(this.flowId);
assert.ok(
flowStatusHistory[0].eventEnd,
"Event end should be set on update to status column",
);
});

Then("a new flow_status_history record is created", async function () {
const flowStatusHistory = await getFlowStatusHistory(this.flowId);
assert.equal(flowStatusHistory.length, 2, "New record not created on UPDATE");
assert.ok(flowStatusHistory[1], "flow_status_history record not created");
assert.equal(
flowStatusHistory[1].status,
"offline",
`Flow status is ${flowStatusHistory[1].status} - it should be "offline"`,
);
assert.notEqual(
flowStatusHistory[1].eventStart,
null,
"Event start should be set on INSERT",
);
assert.equal(
flowStatusHistory[1].eventEnd,
null,
"Event end should not be set on INSERT",
);
});
48 changes: 31 additions & 17 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@
- document_template
- flow_id
filter: {}
- table:
name: flow_status_enum
schema: public
is_enum: true
- table:
name: flow_status_history
schema: public
- table:
name: flows
schema: public
Expand Down Expand Up @@ -406,16 +413,16 @@
permission:
check: {}
columns:
- copied_from
- created_at
- creator_id
- team_id
- data
- id
- settings
- slug
- created_at
- team_id
- updated_at
- copied_from
- id
- version
- data
validate_input:
definition:
forward_client_headers: false
Expand Down Expand Up @@ -482,30 +489,32 @@
- role: api
permission:
columns:
- copied_from
- created_at
- creator_id
- team_id
- data
- id
- settings
- slug
- created_at
- status
- team_id
- updated_at
- copied_from
- id
- version
- data
computed_fields:
- data_merged
filter: {}
allow_aggregations: true
- role: platformAdmin
permission:
columns:
- analytics_link
- created_at
- creator_id
- data
- id
- analytics_link
- settings
- slug
- status
- team_id
- updated_at
- version
Expand All @@ -522,6 +531,7 @@
- id
- settings
- slug
- status
- team_id
- updated_at
- version
Expand All @@ -532,13 +542,14 @@
- role: teamEditor
permission:
columns:
- analytics_link
- created_at
- creator_id
- data
- id
- analytics_link
- settings
- slug
- status
- team_id
- updated_at
- version
Expand All @@ -550,16 +561,17 @@
- role: api
permission:
columns:
- copied_from
- created_at
- creator_id
- team_id
- data
- id
- settings
- slug
- created_at
- status
- team_id
- updated_at
- copied_from
- id
- version
- data
filter: {}
check: {}
validate_input:
Expand All @@ -577,6 +589,7 @@
- data
- settings
- slug
- status
- team_id
filter: {}
check: null
Expand All @@ -595,6 +608,7 @@
- data
- settings
- slug
- status
- team_id
filter:
team:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TRIGGER IF EXISTS flow_status_history_trigger on flows;
DROP FUNCTION IF EXISTS track_flow_status_history();

DROP TABLE "public"."flow_status_history";

alter table
"public"."flows" drop constraint "flows_status_fkey";

ALTER TABLE flows DROP COLUMN "status";

DROP TABLE "public"."flow_status_enum";
Loading
Loading