diff --git a/api.planx.uk/modules/webhooks/service/sanitiseApplicationData/operations.ts b/api.planx.uk/modules/webhooks/service/sanitiseApplicationData/operations.ts index b3c19658fd..c5d3a0697c 100644 --- a/api.planx.uk/modules/webhooks/service/sanitiseApplicationData/operations.ts +++ b/api.planx.uk/modules/webhooks/service/sanitiseApplicationData/operations.ts @@ -2,10 +2,10 @@ import { gql } from "graphql-request"; import { subMonths } from "date-fns"; import { Operation, OperationResult, QueryResult } from "./types"; -import { runSQL } from "../../../../hasura/schema"; -import { getFilesForSession } from "../../../../session/files"; +import { runSQL } from "../../../../lib/hasura/schema"; import { deleteFilesByURL } from "../../../../s3/deleteFile"; import { $api } from "../../../../client"; +import { Passport } from "@opensystemslab/planx-core"; const RETENTION_PERIOD_MONTHS = 6; export const getRetentionPeriod = () => @@ -97,7 +97,11 @@ export const deleteApplicationFiles: Operation = async () => { const sessionIds = await getExpiredSessionIds(); for (const sessionId of sessionIds) { - const files = await getFilesForSession(sessionId); + const session = await $api.session.find(sessionId); + if (!session) { + throw Error(`Unable to find session matching id ${sessionId}`); + } + const files = new Passport(session.data.passport).files(); if (files.length) { const deleted = await deleteFilesByURL(files); deletedFiles.push(...deleted); diff --git a/api.planx.uk/session/files.test.ts b/api.planx.uk/session/files.test.ts deleted file mode 100644 index 824df21f6e..0000000000 --- a/api.planx.uk/session/files.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { getFilesForSession } from "./files"; - -const mockFindSession = jest.fn(); - -jest.mock("../client", () => { - return { - $api: { - session: { - find: jest.fn().mockImplementation(() => mockFindSession()), - }, - }, - }; -}); - -describe("getFilesForSession()", () => { - it("handles sessions without files", async () => { - mockFindSession.mockResolvedValue({ - data: { passport: {} }, - }); - expect(await getFilesForSession("sessionId")).toEqual([]); - }); - - it("handles sessions with files", async () => { - mockFindSession.mockResolvedValue({ - data: { - passport: { - data: { - "file.key": [ - { - url: "https://my.test.file", - }, - ], - }, - }, - }, - }); - expect(await getFilesForSession("sessionId")).toEqual([ - "https://my.test.file", - ]); - }); -}); diff --git a/api.planx.uk/session/files.ts b/api.planx.uk/session/files.ts deleted file mode 100644 index 7f5550afc5..0000000000 --- a/api.planx.uk/session/files.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Passport } from "@opensystemslab/planx-core"; -import { $api } from "../client"; - -export const getFilesForSession = async ( - sessionId: string, -): Promise => { - const session = await $api.session.find(sessionId); - if (!session?.data.passport?.data) return []; - - const files = new Passport(session.data.passport).files(); - return files; -};