Skip to content

Commit

Permalink
add ODP schema to export zip
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Feb 19, 2024
1 parent 9686dc6 commit 90bbcb0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
48 changes: 48 additions & 0 deletions api.planx.uk/modules/send/utils/exportZip.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mockLowcalSession } from "../../../tests/mocks/saveAndReturnMocks";
import { buildSubmissionExportZip } from "./exportZip";
import type { LowCalSession } from "../../../types";
import { expectedPlanningPermissionPayload } from "../../../tests/mocks/digitalPlanningDataMocks";

jest.mock("fs", () => ({
mkdtempSync: () => "tmpdir",
Expand Down Expand Up @@ -53,6 +54,9 @@ jest.mock("@opensystemslab/planx-core", () => {
const mockGenerateOneAppXML = jest
.fn()
.mockResolvedValue({ trim: () => "<dummy:xml></dummy:xml>" });
const mockGenerateDigitalPlanningDataPayload = jest
.fn()
.mockResolvedValue(expectedPlanningPermissionPayload);

jest.mock("../../../client", () => {
return {
Expand All @@ -73,6 +77,7 @@ jest.mock("../../../client", () => {
]),
csvDataRedacted: jest.fn().mockResolvedValue([]),
oneAppPayload: () => mockGenerateOneAppXML(),
digitalPlanningDataPayload: () => mockGenerateDigitalPlanningDataPayload(),
},
},
};
Expand All @@ -94,6 +99,14 @@ describe("buildSubmissionExportZip", () => {
expect(mockAddFile).toHaveBeenCalledWith("Overview.htm", expect.anything());
});

test("ODP schema json is added to the zip", async () => {
const schema = expectedPlanningPermissionPayload;
const expectedBuffer = Buffer.from(JSON.stringify(schema, null, 2));

await buildSubmissionExportZip({ sessionId: "1234" });
expect(mockAddFile).toHaveBeenCalledWith("application.json", expectedBuffer);
});

test("boundary GeoJSON is added to zip", async () => {
const geojson = {
type: "Feature",
Expand Down Expand Up @@ -159,6 +172,32 @@ describe("buildSubmissionExportZip", () => {
);
});

test("ODP schema json is excluded if unsupported application type", async () => {
// set-up mock session passport overwriting "application.type"
const lowcalSessionUnsupportedAppType: Partial<LowCalSession> = {
...mockLowcalSession,
id: "1234",
data: {
...mockLowcalSession.data,
id: "1234",
passport: {
data: {
...mockLowcalSession.data!.passport.data,
"application.type": ["listedBuildingConsent"],
},
},
},
};
mockGetSessionById.mockResolvedValueOnce(lowcalSessionUnsupportedAppType);

await buildSubmissionExportZip({ sessionId: "1234" });

expect(mockAddFile).not.toHaveBeenCalledWith(
"application.json",
expect.anything(),
);
});

test("a document template is added when the template is supported", async () => {
await buildSubmissionExportZip({ sessionId: "1234" });
expect(mockAddLocalFile).toHaveBeenCalledWith(
Expand Down Expand Up @@ -204,5 +243,14 @@ describe("buildSubmissionExportZip", () => {
}),
).rejects.toThrow(/Failed to generate OneApp XML/);
});

it("throws an error when ODP schema generation fails", async () => {
mockGenerateDigitalPlanningDataPayload.mockRejectedValueOnce(
new Error("validation test error"),
);
await expect(
buildSubmissionExportZip({ sessionId: "1234" })
).rejects.toThrow(/Failed to generate ODP Schema JSON/);
});
});
});
19 changes: 17 additions & 2 deletions api.planx.uk/modules/send/utils/exportZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@ export async function buildSubmissionExportZip({
stream: xmlStream,
});
} catch (error) {
throw Error(`Failed to generate OneApp XML. Error - ${error}`);
throw new Error(`Failed to generate OneApp XML for ${sessionId}. Error - ${error}`);
}
}

// add ODP Schema JSON to the zip for supported application types
const supportedApplicationPrefixes = ["ldc", "pa", "pp"];
if (supportedApplicationPrefixes.includes(passport.data?.["application.type"]?.[0])) {
try {
const schema = await $api.export.digitalPlanningDataPayload(sessionId);
const schemaBuff = Buffer.from(JSON.stringify(schema, null, 2));
zip.addFile({
name: "application.json",
buffer: schemaBuff,
});
} catch (error) {
throw new Error (`Failed to generate ODP Schema JSON for ${sessionId} zip. Error - ${error}`);
}
}

Expand Down Expand Up @@ -79,7 +94,7 @@ export async function buildSubmissionExportZip({
stream: csvStream,
});
} catch (error) {
throw Error(`Failed to generate CSV. Error - ${error}`);
throw new Error(`Failed to generate CSV for ${sessionId} zip. Error - ${error}`);
}

// add template files to zip
Expand Down
1 change: 1 addition & 0 deletions api.planx.uk/tests/mocks/saveAndReturnMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const mockLowcalSession: LowCalSession = {
data: {
passport: {
data: {
"application.type": ["ldc.proposed"],
_address: {
single_line_address: "1 High Street",
},
Expand Down

0 comments on commit 90bbcb0

Please sign in to comment.