-
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.
feat: add send events and API endpoint for uploading submissions to S3 (
- Loading branch information
1 parent
2e81ec2
commit 18ab668
Showing
10 changed files
with
207 additions
and
59 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
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
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,60 @@ | ||
import supertest from "supertest"; | ||
import app from "../../../server"; | ||
import { expectedPlanningPermissionPayload } from "../../../tests/mocks/digitalPlanningDataMocks"; | ||
|
||
jest.mock("../../saveAndReturn/service/utils", () => ({ | ||
markSessionAsSubmitted: jest.fn(), | ||
})); | ||
|
||
jest.mock("@opensystemslab/planx-core", () => { | ||
const actualCoreDomainClient = jest.requireActual( | ||
"@opensystemslab/planx-core", | ||
).CoreDomainClient; | ||
|
||
return { | ||
CoreDomainClient: class extends actualCoreDomainClient { | ||
constructor() { | ||
super(); | ||
this.export.digitalPlanningDataPayload = () => | ||
jest.fn().mockResolvedValue({ | ||
exportData: expectedPlanningPermissionPayload, | ||
}); | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
describe(`uploading an application to S3`, () => { | ||
it("requires auth", async () => { | ||
await supertest(app) | ||
.post("/upload-submission/barnet") | ||
.send({ payload: { sessionId: "123" } }) | ||
.expect(401); | ||
}); | ||
|
||
it("throws an error if payload is missing", async () => { | ||
await supertest(app) | ||
.post("/upload-submission/barnet") | ||
.set({ Authorization: process.env.HASURA_PLANX_API_KEY! }) | ||
.send({ payload: null }) | ||
.expect(400) | ||
.then((res) => { | ||
expect(res.body.error).toMatch(/Missing application payload/); | ||
}); | ||
}); | ||
|
||
it("throws an error if team is unsupported", async () => { | ||
await supertest(app) | ||
.post("/upload-submission/unsupported-team") | ||
.set({ Authorization: process.env.HASURA_PLANX_API_KEY! }) | ||
.send({ payload: { sessionId: "123" } }) | ||
.expect(400) | ||
.then((res) => { | ||
expect(res.body.error).toMatch( | ||
"Send to S3 is not enabled for this local authority (unsupported-team)", | ||
); | ||
}); | ||
}); | ||
|
||
it.todo("succeeds"); // mock uploadPrivateFile ?? | ||
}); |
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,60 @@ | ||
import type { NextFunction, Request, Response } from "express"; | ||
import { $api } from "../../../client"; | ||
import { uploadPrivateFile } from "../../file/service/uploadFile"; | ||
import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils"; | ||
|
||
export async function sendToS3( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
// `/upload-submission/:localAuthority` is only called via Hasura's scheduled event webhook, so body is wrapped in a "payload" key | ||
const { payload } = req.body; | ||
const localAuthority = req.params.localAuthority; | ||
|
||
if (!payload?.sessionId) { | ||
return next({ | ||
status: 400, | ||
message: `Missing application payload data to send to email`, | ||
}); | ||
} | ||
|
||
try { | ||
const { sessionId } = payload; | ||
|
||
// Only prototyping with Barnet to begin | ||
// In future, confirm this local authority has an S3 bucket/folder configured in team_integrations or similar | ||
if (localAuthority !== "barnet") { | ||
return next({ | ||
status: 400, | ||
message: `Send to S3 is not enabled for this local authority (${localAuthority})`, | ||
}); | ||
} | ||
|
||
// Generate the ODP Schema JSON | ||
const exportData = await $api.export.digitalPlanningDataPayload(sessionId); | ||
|
||
// Create and upload the data as an S3 file | ||
const { fileUrl } = await uploadPrivateFile( | ||
exportData, | ||
`${sessionId}.json`, | ||
"barnet-prototype", | ||
); | ||
|
||
// Mark session as submitted so that reminder and expiry emails are not triggered | ||
markSessionAsSubmitted(sessionId); | ||
|
||
// TODO Create and update an audit table entry | ||
|
||
return res.status(200).send({ | ||
message: `Successfully uploaded submission to S3: ${fileUrl}`, | ||
}); | ||
} catch (error) { | ||
return next({ | ||
error, | ||
message: `Failed to upload submission to S3 (${localAuthority}): ${ | ||
(error as Error).message | ||
}`, | ||
}); | ||
} | ||
} |
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