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

chore: Convert /download-schema endpoint to $public client #2305

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
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
Loading