Skip to content

Commit

Permalink
chore: Convert download-schema endpoint to public client (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 18, 2023
1 parent 08f4e1b commit 3515034
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
} from "./modules/auth/middleware";

import airbrake from "./airbrake";
import { adminGraphQLClient as adminClient } from "./hasura";
import { sendEmailLimiter, apiLimiter } from "./rateLimit";
import {
privateDownloadController,
Expand Down Expand Up @@ -73,6 +72,7 @@ import webhookRoutes from "./modules/webhooks/routes";
import analyticsRoutes from "./modules/analytics/routes";
import { useSwaggerDocs } from "./docs";
import { Role } from "@opensystemslab/planx-core/types";
import { $public } from "./client";

const router = express.Router();

Expand Down Expand Up @@ -279,13 +279,21 @@ app.get(
copyPortalAsFlow,
);

// unauthenticated because accessing flow schema only, no user data
interface FlowSchema {
node: string;
type: string;
text: string;
planx_variable: string;
}

app.get("/flows/:flowId/download-schema", async (req, res, next) => {
try {
const schema = await adminClient.request(
const { flowSchema } = await $public.client.request<{
flowSchema: FlowSchema[];
}>(
gql`
query ($flow_id: String!) {
get_flow_schema(args: { published_flow_id: $flow_id }) {
flowSchema: get_flow_schema(args: { published_flow_id: $flow_id }) {
node
type
text
Expand All @@ -296,15 +304,15 @@ app.get("/flows/:flowId/download-schema", async (req, res, next) => {
{ flow_id: req.params.flowId },
);

if (schema.get_flow_schema.length < 1) {
if (!flowSchema.length) {
next({
status: 404,
message:
"Can't find a schema for this flow. Make sure it's published or try a different flow id.",
});
} else {
// build a CSV and stream it
stringify(schema.get_flow_schema, { header: true }).pipe(res);
stringify(flowSchema, { header: true }).pipe(res);

res.header("Content-type", "text/csv");
res.attachment(`${req.params.flowId}.csv`);
Expand Down

0 comments on commit 3515034

Please sign in to comment.