From d51efca9669041927960cacfc8c6d9f317f5d83d Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Thu, 7 Dec 2023 12:03:23 +0000 Subject: [PATCH] fix: drop pg function `get_flow_schema` in favor of simpler js data munging (#2540) --- .../flows/downloadSchema/controller.ts | 4 +- .../modules/flows/downloadSchema/service.ts | 39 ++++++++----------- hasura.planx.uk/metadata/functions.yaml | 3 -- hasura.planx.uk/metadata/tables.yaml | 13 ------- .../down.sql | 4 ++ .../up.sql | 2 + 6 files changed, 23 insertions(+), 42 deletions(-) create mode 100644 hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/down.sql create mode 100644 hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/up.sql diff --git a/api.planx.uk/modules/flows/downloadSchema/controller.ts b/api.planx.uk/modules/flows/downloadSchema/controller.ts index 95113bacc4..f93551817b 100644 --- a/api.planx.uk/modules/flows/downloadSchema/controller.ts +++ b/api.planx.uk/modules/flows/downloadSchema/controller.ts @@ -6,8 +6,6 @@ import { ServerError } from "../../../errors"; interface DownloadFlowSchemaResponse { message: string; - alteredNodes: Node[] | null; - description?: string; } export const downloadFlowSchema = z.object({ @@ -34,7 +32,7 @@ export const downloadFlowSchemaController: DownloadFlowSchemaController = } catch (error) { return next( new ServerError({ - message: `Failed to download flow schema: ${error}`, + message: `Failed to download schema for flow ${res.locals.parsedReq.params?.flowId}: ${error}`, }), ); } diff --git a/api.planx.uk/modules/flows/downloadSchema/service.ts b/api.planx.uk/modules/flows/downloadSchema/service.ts index 8c5ae5211c..32760c4b48 100644 --- a/api.planx.uk/modules/flows/downloadSchema/service.ts +++ b/api.planx.uk/modules/flows/downloadSchema/service.ts @@ -1,5 +1,4 @@ -import { $public } from "../../../client"; -import { gql } from "graphql-request"; +import { getFlowData } from "../../../helpers"; interface FlowSchema { node: string; @@ -9,27 +8,21 @@ interface FlowSchema { } export const getFlowSchema = async (flowId: string) => { - const { flowSchema } = await $public.client.request<{ - flowSchema: FlowSchema[]; - }>( - gql` - query ($flow_id: String!) { - flowSchema: get_flow_schema(args: { published_flow_id: $flow_id }) { - node - type - text - planx_variable - } - } - `, - { flow_id: flowId }, - ); + const flow = await getFlowData(flowId); - if (!flowSchema.length) { - throw Error( - "Can't find a schema for this flow. Make sure it's published or try a different flow id.", - ); - } + const data: FlowSchema[] = []; + Object.entries(flow.data).map(([nodeId, nodeData]) => + data.push({ + node: nodeId, + type: nodeData?.type?.toString() || "_root", + text: nodeData.data?.text, + planx_variable: + nodeData.data?.fn || + nodeData.data?.val || + nodeData.data?.output || + nodeData.data?.dataFieldBoundary, + }), + ); - return flowSchema; + return data; }; diff --git a/hasura.planx.uk/metadata/functions.yaml b/hasura.planx.uk/metadata/functions.yaml index da885f5fc3..401e12cc42 100644 --- a/hasura.planx.uk/metadata/functions.yaml +++ b/hasura.planx.uk/metadata/functions.yaml @@ -1,6 +1,3 @@ - function: schema: public name: diff_latest_published_flow -- function: - schema: public - name: get_flow_schema diff --git a/hasura.planx.uk/metadata/tables.yaml b/hasura.planx.uk/metadata/tables.yaml index c69a9345a3..09bbe2a170 100644 --- a/hasura.planx.uk/metadata/tables.yaml +++ b/hasura.planx.uk/metadata/tables.yaml @@ -236,19 +236,6 @@ - document_template - flow_id filter: {} -- table: - schema: public - name: flow_schemas - select_permissions: - - role: public - permission: - columns: - - flow_id - - node - - planx_variable - - text - - type - filter: {} - table: schema: public name: flows diff --git a/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/down.sql b/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/down.sql new file mode 100644 index 0000000000..e3ceaec568 --- /dev/null +++ b/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP FUNCTION public.get_flow_schema CASCADE; +-- DROP TABLE public.flow_schemas CASCADE; diff --git a/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/up.sql b/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/up.sql new file mode 100644 index 0000000000..a29166de9f --- /dev/null +++ b/hasura.planx.uk/migrations/1701881725039_drop_function_get_flow_schema/up.sql @@ -0,0 +1,2 @@ +DROP FUNCTION public.get_flow_schema CASCADE; +DROP TABLE public.flow_schemas CASCADE;