-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c58cec
commit a984c15
Showing
6 changed files
with
112 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { z } from "zod"; | ||
import { ValidatedRequestHandler } from "../../../shared/middleware/validate"; | ||
import { stringify } from "csv-stringify"; | ||
import { getFlowSchema } from "./service"; | ||
import { ServerError } from "../../../errors"; | ||
|
||
interface DownloadFlowSchemaResponse { | ||
message: string; | ||
alteredNodes: Node[] | null; | ||
description?: string; | ||
} | ||
|
||
export const downloadFlowSchema = z.object({ | ||
params: z.object({ | ||
flowId: z.string(), | ||
}), | ||
}); | ||
|
||
export type DownloadFlowSchemaController = ValidatedRequestHandler< | ||
typeof downloadFlowSchema, | ||
DownloadFlowSchemaResponse | ||
>; | ||
|
||
export const downloadFlowSchemaController: DownloadFlowSchemaController = | ||
async (_res, res, next) => { | ||
try { | ||
const { flowId } = res.locals.parsedReq.params; | ||
const flowSchema = await getFlowSchema(flowId); | ||
|
||
// Build a CSV and stream it | ||
stringify(flowSchema, { header: true }).pipe(res); | ||
res.header("Content-type", "text/csv"); | ||
res.attachment(`${flowId}.csv`); | ||
} catch (error) { | ||
return next( | ||
new ServerError({ | ||
message: `Failed to download flow schema: ${error}`, | ||
}), | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { $public } from "../../../client"; | ||
import { gql } from "graphql-request"; | ||
|
||
interface FlowSchema { | ||
node: string; | ||
type: string; | ||
text: string; | ||
planx_variable: string; | ||
} | ||
|
||
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 }, | ||
); | ||
|
||
if (!flowSchema.length) { | ||
throw Error( | ||
"Can't find a schema for this flow. Make sure it's published or try a different flow id.", | ||
); | ||
} | ||
|
||
return flowSchema; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters