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

fix: Convert Digital Planning payload to JSON file before upload #4054

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 21 additions & 3 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { customAlphabet } from "nanoid";
import { isLiveEnv } from "../../../helpers.js";
import { s3Factory } from "./utils.js";
import { Readable } from "stream";
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 8);

export const uploadPublicFile = async (
Expand All @@ -28,10 +29,14 @@
};

export const uploadPrivateFile = async (
file: Express.Multer.File,
file: Express.Multer.File | Buffer,
filename: string,
filekey?: string,
) => {
if (file instanceof Buffer) {
file = convertToMulterFile(file);
}

const s3 = s3Factory();

const { params, key, fileType } = generateFileParams(file, filename, filekey);
Expand Down Expand Up @@ -63,6 +68,19 @@
return `${process.env.API_URL_EXT}/file/${path}${s3Pathname}`;
};

const convertToMulterFile = (buffer: Buffer): Express.Multer.File => ({
buffer: buffer,
originalname: "${data.id}.json",
mimetype: "application/json",
size: buffer.length,
Fixed Show fixed Hide fixed
fieldname: "file",
encoding: "7bit",
stream: Readable.from(buffer),
destination: "",
filename: "",
path: "",
});

export function generateFileParams(
file: Express.Multer.File,
filename: string,
Expand All @@ -79,9 +97,9 @@
ACL: "public-read",
Bucket: process.env.AWS_S3_BUCKET,
Key: key,
Body: file.buffer || JSON.stringify(file),
Body: file.buffer,
ContentDisposition: `inline;filename="${filename}"`,
ContentType: file.mimetype || "application/json",
ContentType: file.mimetype,
};

return {
Expand Down
16 changes: 9 additions & 7 deletions api.planx.uk/modules/send/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils.js";
import { isApplicationTypeSupported } from "../utils/helpers.js";
import type { SendIntegrationController } from "../types.js";

// TS fails to validate this complex type when awaited
// Represent as a simple object to allow enough typechecking to kick in
type DigitalPlanningPayload = Record<string, unknown>;

interface CreateS3Application {
insertS3Application: {
id: string;
Expand Down Expand Up @@ -45,15 +49,13 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => {

// Generate the ODP Schema JSON, skipping validation if not a supported application type
const doValidation = isApplicationTypeSupported(passport);
const exportData = doValidation
? await $api.export.digitalPlanningDataPayload(sessionId)
: await $api.export.digitalPlanningDataPayload(sessionId, true);
const exportData: DigitalPlanningPayload =
await $api.export.digitalPlanningDataPayload(sessionId, doValidation);

const buffer = Buffer.from(JSON.stringify(exportData));

// Create and upload the data as an S3 file
const { fileUrl } = await uploadPrivateFile(
exportData,
`${sessionId}.json`,
);
const { fileUrl } = await uploadPrivateFile(buffer, `${sessionId}.json`);

// Send a notification with the file URL to the Power Automate webhook
const webhookRequest: AxiosRequestConfig = {
Expand Down
Loading