Skip to content

Commit

Permalink
feat: include the ODP schema as "application.json" in submission zips…
Browse files Browse the repository at this point in the history
… for supported application types (#2804)
  • Loading branch information
jessicamcinchak authored Feb 22, 2024
1 parent e0f5114 commit 4484938
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 139 deletions.
78 changes: 78 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,8 @@ jest.mock("../../../client", () => {
]),
csvDataRedacted: jest.fn().mockResolvedValue([]),
oneAppPayload: () => mockGenerateOneAppXML(),
digitalPlanningDataPayload: () =>
mockGenerateDigitalPlanningDataPayload(),
},
},
};
Expand All @@ -94,6 +100,17 @@ 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 +176,58 @@ 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("ODP schema json is excluded if no 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": undefined,
},
},
},
};
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 +273,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/);
});
});
});
29 changes: 27 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,30 @@ 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"];
const applicationType = passport.data?.["application.type"]?.[0];
if (
applicationType &&
supportedApplicationPrefixes.includes(applicationType.split(".")?.[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 +102,9 @@ 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
2 changes: 1 addition & 1 deletion api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@airbrake/node": "^2.1.8",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b184ea9",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#a68d840",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
"aws-sdk": "^2.1467.0",
Expand Down
20 changes: 10 additions & 10 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
2 changes: 1 addition & 1 deletion e2e/tests/api-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"dependencies": {
"@cucumber/cucumber": "^9.3.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b184ea9",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#a68d840",
"axios": "^1.6.0",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
Expand Down
20 changes: 10 additions & 10 deletions e2e/tests/api-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/tests/ui-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"postinstall": "./install-dependencies.sh"
},
"dependencies": {
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b184ea9",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#a68d840",
"axios": "^1.6.2",
"dotenv": "^16.3.1",
"eslint": "^8.56.0",
Expand Down
20 changes: 10 additions & 10 deletions e2e/tests/ui-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion editor.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@mui/styles": "^5.15.2",
"@mui/utils": "^5.15.2",
"@opensystemslab/map": "^0.8.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b184ea9",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#a68d840",
"@tiptap/core": "^2.0.3",
"@tiptap/extension-bold": "^2.0.3",
"@tiptap/extension-bubble-menu": "^2.1.13",
Expand Down
Loading

0 comments on commit 4484938

Please sign in to comment.