From 7e26245e2d0d2c7d2fa5710c53f00ff36f98f6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 21 May 2024 21:09:01 +0100 Subject: [PATCH] feat: Setup `flow.setStatus()` function (#388) Please see https://github.com/theopensystemslab/planx-new/pull/3170 for context. This PR adds a helper method to toggle the status of the flow. This is used in E2E tests, and will be called by Editors in the frontend. --- src/requests/flow.ts | 47 +++++++++++++++++++++++++++++++++++++++++++- src/types/flow.ts | 2 ++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/requests/flow.ts b/src/requests/flow.ts index da788cd0..3b75d286 100644 --- a/src/requests/flow.ts +++ b/src/requests/flow.ts @@ -2,7 +2,7 @@ import type { GraphQLClient } from "graphql-request"; import { gql } from "graphql-request"; import { capitalize } from "lodash"; -import type { FlowGraph } from "../types"; +import type { FlowGraph, FlowStatus } from "../types"; export class FlowClient { protected client: GraphQLClient; @@ -26,6 +26,10 @@ export class FlowClient { return publishFlow(this.client, args); } + async setStatus(args: { flow: { id: string }; status: FlowStatus }) { + return setStatus(this.client, args); + } + /** * Only used in test environments */ @@ -264,3 +268,44 @@ export async function _destroyPublishedFlow( ); return Boolean(response.delete_published_flows_by_pk?.id); } + +interface SetFlowStatus { + flow: { + id: string; + status: FlowStatus; + }; +} + +async function setStatus( + client: GraphQLClient, + args: { flow: { id: string }; status: FlowStatus }, +) { + try { + const { flow } = await client.request( + gql` + mutation SetFlowStatus( + $flowId: uuid! + $status: flow_status_enum_enum! + ) { + flow: update_flows_by_pk( + pk_columns: { id: $flowId } + _set: { status: $status } + ) { + id + status + } + } + `, + { + flowId: args.flow.id, + status: args.status, + }, + ); + + return flow; + } catch (error) { + new Error( + `Failed to update flow status to "${args.status}". Error: ${error}`, + ); + } +} diff --git a/src/types/flow.ts b/src/types/flow.ts index 310cbeae..16d6f9fa 100644 --- a/src/types/flow.ts +++ b/src/types/flow.ts @@ -36,3 +36,5 @@ export interface NormalizedNode extends IndexedNode { } export type NormalizedFlow = Array; + +export type FlowStatus = "online" | "offline";