From c1746ca19544d8de1ea5cd17f0e08358052b0b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Wed, 1 Nov 2023 11:36:25 +0000 Subject: [PATCH] fix: Get session by pk for public client (with headers) --- api.planx.uk/saveAndReturn/validateSession.ts | 46 +++++++++---------- .../tests/mocks/saveAndReturnMocks.ts | 16 +++---- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/api.planx.uk/saveAndReturn/validateSession.ts b/api.planx.uk/saveAndReturn/validateSession.ts index 739075ac0e..22a02b3c87 100644 --- a/api.planx.uk/saveAndReturn/validateSession.ts +++ b/api.planx.uk/saveAndReturn/validateSession.ts @@ -227,6 +227,10 @@ async function diffLatestPublishedFlow({ return response.diff_latest_published_flow.data; } +interface FindSession { + session: Partial | null; +} + async function findSession({ sessionId, email, @@ -235,32 +239,26 @@ async function findSession({ email: string; }): Promise | undefined> { const headers = getSaveAndReturnPublicHeaders(sessionId, email); - const response: { lowcal_sessions: Partial[] } = - await $public.client.request( - gql` - query FindSession($sessionId: uuid!, $email: String!) { - lowcal_sessions( - where: { id: { _eq: $sessionId }, email: { _eq: $email } } - limit: 1 - ) { - flow_id - data - updated_at - lockedAt: locked_at - paymentRequests: payment_requests { - id - payeeName: payee_name - payeeEmail: payee_email - } + const response = await $public.client.request( + gql` + query FindSession($sessionId: uuid!) { + session: lowcal_sessions_by_pk(id: $sessionId) { + flow_id + data + updated_at + lockedAt: locked_at + paymentRequests: payment_requests { + id + payeeName: payee_name + payeeEmail: payee_email } } - `, - { sessionId, email }, - headers, - ); - return response.lowcal_sessions.length - ? response.lowcal_sessions[0] - : undefined; + } + `, + { sessionId }, + headers, + ); + return response.session ?? undefined; } async function createAuditEntry( diff --git a/api.planx.uk/tests/mocks/saveAndReturnMocks.ts b/api.planx.uk/tests/mocks/saveAndReturnMocks.ts index 75604b7b92..f435d8a4e2 100644 --- a/api.planx.uk/tests/mocks/saveAndReturnMocks.ts +++ b/api.planx.uk/tests/mocks/saveAndReturnMocks.ts @@ -73,15 +73,13 @@ export const mockLowcalSession: LowCalSession = { export const mockFindSession = (breadcrumbs = {}) => ({ name: "FindSession", data: { - lowcal_sessions: [ - { - ...mockLowcalSession, - data: { - ...mockLowcalSession.data, - breadcrumbs, - }, + session: { + ...mockLowcalSession, + data: { + ...mockLowcalSession.data, + breadcrumbs, }, - ], + }, }, variables: { sessionId: mockLowcalSession.id, @@ -92,7 +90,7 @@ export const mockFindSession = (breadcrumbs = {}) => ({ export const mockNotFoundSession = { name: "FindSession", data: { - lowcal_sessions: [], + session: null, }, variables: { sessionId: "not-found-id",