diff --git a/api.planx.uk/editor/publish.test.ts b/api.planx.uk/editor/publish.test.ts index 41f6165887..58a9bb4033 100644 --- a/api.planx.uk/editor/publish.test.ts +++ b/api.planx.uk/editor/publish.test.ts @@ -67,6 +67,20 @@ it("requires a user to have the 'teamEditor' role", async () => { }); describe("publish", () => { + it("publishes for the first time", async () => { + queryMock.mockQuery({ + name: "GetMostRecentPublishedFlow", + matchOnVariables: false, + data: { + flow: { + publishedFlows: [], + }, + }, + }); + + await supertest(app).post("/flows/1/publish").set(auth).expect(200); + }); + it("does not update if there are no new changes", async () => { await supertest(app) .post("/flows/1/publish") diff --git a/api.planx.uk/helpers.ts b/api.planx.uk/helpers.ts index 9075fc891b..585c922c63 100644 --- a/api.planx.uk/helpers.ts +++ b/api.planx.uk/helpers.ts @@ -101,7 +101,7 @@ interface PublishedFlows { // Get the most recent version of a published flow's data (flattened, with external portal nodes) const getMostRecentPublishedFlow = async ( id: string, -): Promise => { +): Promise => { const { flow } = await $public.client.request( gql` query GetMostRecentPublishedFlow($id: uuid!) { @@ -119,8 +119,6 @@ const getMostRecentPublishedFlow = async ( ); const mostRecent = flow?.publishedFlows?.[0]?.data; - if (!mostRecent) throw Error(`Published flow not found for flow ${id}`); - return mostRecent; }; diff --git a/api.planx.uk/saveAndReturn/validateSession.ts b/api.planx.uk/saveAndReturn/validateSession.ts index 374ad2d7e7..0c95d08cfd 100644 --- a/api.planx.uk/saveAndReturn/validateSession.ts +++ b/api.planx.uk/saveAndReturn/validateSession.ts @@ -138,6 +138,8 @@ async function reconcileSessionData({ const alteredSectionIds = new Set(); const currentFlow = await getMostRecentPublishedFlow(sessionData.id); + if (!currentFlow) + throw Error(`Unable to find published flow for flow ${sessionData.id}`); // create ordered breadcrumbs to be able to look up section IDs later const orderedBreadcrumbs: OrderedBreadcrumbs = sortBreadcrumbs(