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 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
4 changes: 2 additions & 2 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export function generateFileParams(
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
21 changes: 21 additions & 0 deletions api.planx.uk/modules/file/service/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { S3 } from "@aws-sdk/client-s3";
import { isLiveEnv } from "../../../helpers.js";
import { Readable } from "stream";

export function s3Factory() {
return new S3({
Expand Down Expand Up @@ -40,3 +41,23 @@ export function getS3KeyFromURL(fileURL: string): string {
const key = [folder, file].map(decodeURIComponent).join("/");
return key;
}

export const convertObjectToMulterJSONFile = (
data: Record<string, unknown>,
fileName: string,
): Express.Multer.File => {
const buffer = Buffer.from(JSON.stringify(data));

return {
buffer: buffer,
originalname: fileName,
mimetype: "application/json",
size: buffer.length,
fieldname: "file",
encoding: "7bit",
stream: Readable.from(buffer),
destination: "",
filename: "",
path: "",
};
};
21 changes: 11 additions & 10 deletions api.planx.uk/modules/send/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { uploadPrivateFile } from "../../file/service/uploadFile.js";
import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils.js";
import { isApplicationTypeSupported } from "../utils/helpers.js";
import type { SendIntegrationController } from "../types.js";
import { convertObjectToMulterJSONFile } from "../../file/service/utils.js";

interface CreateS3Application {
insertS3Application: {
Expand Down Expand Up @@ -44,16 +45,16 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => {
const flowName = session?.flow?.name;

// 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 skipValidation = !isApplicationTypeSupported(passport);
const exportData = await $api.export.digitalPlanningDataPayload(
sessionId,
skipValidation,
);

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

// Send a notification with the file URL to the Power Automate webhook
const webhookRequest: AxiosRequestConfig = {
Expand All @@ -69,7 +70,7 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => {
service: flowName,
environment: env,
file: fileUrl,
payload: doValidation ? "Validated ODP Schema" : "Discretionary",
payload: skipValidation ? "Discretionary" : "Validated ODP Schema",
},
};
const webhookResponse = await axios(webhookRequest)
Expand Down Expand Up @@ -125,7 +126,7 @@ const sendToS3: SendIntegrationController = async (_req, res, next) => {

res.status(200).send({
message: `Successfully uploaded submission to S3: ${fileUrl}`,
payload: doValidation ? "Validated ODP Schema" : "Discretionary",
payload: skipValidation ? "Discretionary" : "Validated ODP Schema",
webhookResponse: webhookResponse.axiosResponse.status,
auditEntryId: webhookResponse.id,
});
Expand Down
Loading