Skip to content

Commit

Permalink
fix: Handle bug on first publish
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Nov 7, 2023
1 parent 3fa2efe commit 06873b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions api.planx.uk/editor/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions api.planx.uk/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Flow["data"]> => {
): Promise<Flow["data"] | undefined> => {
const { flow } = await $public.client.request<PublishedFlows>(
gql`
query GetMostRecentPublishedFlow($id: uuid!) {
Expand All @@ -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;
};

Expand Down
2 changes: 2 additions & 0 deletions api.planx.uk/saveAndReturn/validateSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ async function reconcileSessionData({
const alteredSectionIds = new Set<string>();

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(
Expand Down

0 comments on commit 06873b1

Please sign in to comment.