diff --git a/api.planx.uk/admin/session/bops.ts b/api.planx.uk/admin/session/bops.ts
index 74aa8feb0a..56d2e78f8f 100644
--- a/api.planx.uk/admin/session/bops.ts
+++ b/api.planx.uk/admin/session/bops.ts
@@ -1,12 +1,12 @@
-import { Request, Response, NextFunction } from "express";
-import { getClient } from "../../client";
+import { NextFunction, Request, Response } from "express";
+import { $api } from "../../client";
/**
* @swagger
* /admin/session/{sessionId}/bops:
* get:
* summary: Generates a Back Office Planning System (BOPS) payload
- * description: Generates a BOPS payload, relies on a submission record in `bops_applications`
+ * description: Generates a BOPS payload
* tags:
* - admin
* parameters:
@@ -20,10 +20,7 @@ export const getBOPSPayload = async (
next: NextFunction,
) => {
try {
- const $client = getClient();
- const { exportData } = await $client.export.bopsPayload(
- req.params.sessionId,
- );
+ const { exportData } = await $api.export.bopsPayload(req.params.sessionId);
res.set("content-type", "application/json");
return res.send(exportData);
} catch (error) {
diff --git a/api.planx.uk/admin/session/oneAppXML.test.ts b/api.planx.uk/admin/session/oneAppXML.test.ts
index 7b05e6a7d2..8a67e626b2 100644
--- a/api.planx.uk/admin/session/oneAppXML.test.ts
+++ b/api.planx.uk/admin/session/oneAppXML.test.ts
@@ -1,40 +1,26 @@
import supertest from "supertest";
import app from "../../server";
-import { queryMock } from "../../tests/graphqlQueryMock";
import { authHeader } from "../../tests/mockJWT";
const endpoint = (strings: TemplateStringsArray) =>
`/admin/session/${strings[0]}/xml`;
-describe("OneApp XML endpoint", () => {
- beforeEach(() => {
- queryMock.mockQuery({
- name: "GetMostRecentUniformApplicationBySessionID",
- variables: {
- submission_reference: "abc123",
- },
- data: {
- uniform_applications: [{ xml: "" }],
- },
- });
-
- queryMock.mockQuery({
- name: "GetMostRecentUniformApplicationBySessionID",
- variables: {
- submission_reference: "xyz789",
- },
- data: {
- uniform_applications: [],
- },
- });
- });
+const mockGenerateOneAppXML = jest
+ .fn()
+ .mockResolvedValue("");
- afterEach(() => jest.clearAllMocks());
- const auth = authHeader({ role: "platformAdmin" });
+jest.mock("../../client", () => {
+ return {
+ $api: {
+ generateOneAppXML: () => mockGenerateOneAppXML(),
+ },
+ };
+});
+describe("OneApp XML endpoint", () => {
it("requires a user to be logged in", async () => {
await supertest(app)
- .get(endpoint`abc123`)
+ .get(endpoint`123`)
.expect(401)
.then((res) =>
expect(res.body).toEqual({
@@ -45,23 +31,15 @@ describe("OneApp XML endpoint", () => {
it("requires a user to have the 'platformAdmin' role", async () => {
await supertest(app)
- .get(endpoint`abc123`)
+ .get(endpoint`123`)
.set(authHeader({ role: "teamEditor" }))
.expect(403);
});
- it("returns an error if sessionID is invalid", async () => {
- await supertest(app)
- .get(endpoint`xyz789`)
- .set(auth)
- .expect(500)
- .then((res) => expect(res.body.error).toMatch(/Invalid sessionID/));
- });
-
it("returns XML", async () => {
await supertest(app)
- .get(endpoint`abc123`)
- .set(auth)
+ .get(endpoint`123`)
+ .set(authHeader({ role: "platformAdmin" }))
.expect(200)
.expect("content-type", "text/xml; charset=utf-8")
.then((res) => {
diff --git a/api.planx.uk/admin/session/oneAppXML.ts b/api.planx.uk/admin/session/oneAppXML.ts
index 2f95b144cc..d1c1e51fb8 100644
--- a/api.planx.uk/admin/session/oneAppXML.ts
+++ b/api.planx.uk/admin/session/oneAppXML.ts
@@ -1,13 +1,12 @@
-import { gql } from "graphql-request";
import { Request, Response, NextFunction } from "express";
-import { adminGraphQLClient as client } from "../../hasura";
+import { $api } from "../../client";
/**
* @swagger
* /admin/session/{sessionId}/xml:
* get:
* summary: Generates a OneApp XML
- * description: Generates a OneApp XML, relies on a submission record in `uniform_applications`
+ * description: Generates a OneApp XML
* tags:
* - admin
* parameters:
@@ -21,7 +20,7 @@ export const getOneAppXML = async (
next: NextFunction,
) => {
try {
- const xml = await fetchSessionXML(req.params.sessionId);
+ const xml = await $api.generateOneAppXML(req.params.sessionId);
res.set("content-type", "text/xml");
return res.send(xml);
} catch (error) {
@@ -30,30 +29,3 @@ export const getOneAppXML = async (
});
}
};
-
-const fetchSessionXML = async (sessionId: string) => {
- try {
- const query = gql`
- query GetMostRecentUniformApplicationBySessionID(
- $submission_reference: String
- ) {
- uniform_applications(
- where: { submission_reference: { _eq: $submission_reference } }
- order_by: { created_at: desc }
- limit: 1
- ) {
- xml
- }
- }
- `;
- const { uniform_applications } = await client.request(query, {
- submission_reference: sessionId,
- });
-
- if (!uniform_applications?.length) throw Error("Invalid sessionID");
-
- return uniform_applications[0].xml;
- } catch (error) {
- throw Error("Unable to fetch session XML: " + (error as Error).message);
- }
-};