From 47452355ef07ec755cc63d8eae6e17b76519fb8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 13 Oct 2023 14:00:03 +0100 Subject: [PATCH] feat: Drop `publicGraphQLClient` in favour of `$public` client (#2300) --- api.planx.uk/hasura/index.ts | 7 +------ .../inviteToPay/sendPaymentEmail.test.ts | 13 ++++--------- api.planx.uk/notify/notify.ts | 10 +++------- .../notify/routeSendEmailRequest.test.ts | 17 ++++++----------- api.planx.uk/saveAndReturn/utils.ts | 13 ++++++++++--- api.planx.uk/tests/mocks/saveAndReturnMocks.ts | 2 +- api.planx.uk/types.ts | 2 +- 7 files changed, 26 insertions(+), 38 deletions(-) diff --git a/api.planx.uk/hasura/index.ts b/api.planx.uk/hasura/index.ts index 2b6b085003..0837de56c4 100644 --- a/api.planx.uk/hasura/index.ts +++ b/api.planx.uk/hasura/index.ts @@ -9,9 +9,4 @@ const adminGraphQLClient = new GraphQLClient(process.env.HASURA_GRAPHQL_URL!, { }, }); -/** - * Connect to Hasura using the "Public" role - */ -const publicGraphQLClient = new GraphQLClient(process.env.HASURA_GRAPHQL_URL!); - -export { adminGraphQLClient, publicGraphQLClient }; +export { adminGraphQLClient }; diff --git a/api.planx.uk/inviteToPay/sendPaymentEmail.test.ts b/api.planx.uk/inviteToPay/sendPaymentEmail.test.ts index a4fff2b3ec..cc73b3cffc 100644 --- a/api.planx.uk/inviteToPay/sendPaymentEmail.test.ts +++ b/api.planx.uk/inviteToPay/sendPaymentEmail.test.ts @@ -6,16 +6,11 @@ import { validatePaymentRequestNotFoundQueryMock, validatePaymentRequestQueryMock, } from "../tests/mocks/inviteToPayMocks"; +import { CoreDomainClient } from "@opensystemslab/planx-core"; -jest.mock("@opensystemslab/planx-core", () => { - return { - CoreDomainClient: jest.fn().mockImplementation(() => ({ - formatRawProjectTypes: jest - .fn() - .mockResolvedValue(["New office premises"]), - })), - }; -}); +jest + .spyOn(CoreDomainClient.prototype, "formatRawProjectTypes") + .mockResolvedValue("New office premises"); const TEST_PAYMENT_REQUEST_ID = "09655c28-3f34-4619-9385-cd57312acc44"; diff --git a/api.planx.uk/notify/notify.ts b/api.planx.uk/notify/notify.ts index 5d8315178e..2824506575 100644 --- a/api.planx.uk/notify/notify.ts +++ b/api.planx.uk/notify/notify.ts @@ -1,11 +1,7 @@ -import { GraphQLClient } from "graphql-request"; import { NotifyClient } from "notifications-node-client"; -import { - adminGraphQLClient as adminClient, - publicGraphQLClient as publicClient, -} from "../hasura"; import { softDeleteSession } from "../saveAndReturn/utils"; import { NotifyConfig } from "../types"; +import { $api, $public } from "../client"; const notifyClient = new NotifyClient(process.env.GOVUK_NOTIFY_API_KEY); @@ -84,7 +80,7 @@ const sendEmail = async ( } }; -const getClientForTemplate = (template: Template): GraphQLClient => - template in privateEmailTemplates ? adminClient : publicClient; +const getClientForTemplate = (template: Template) => + template in privateEmailTemplates ? $api.client : $public.client; export { sendEmail, getClientForTemplate }; diff --git a/api.planx.uk/notify/routeSendEmailRequest.test.ts b/api.planx.uk/notify/routeSendEmailRequest.test.ts index 748fd7e4e2..35bfbcbfb7 100644 --- a/api.planx.uk/notify/routeSendEmailRequest.test.ts +++ b/api.planx.uk/notify/routeSendEmailRequest.test.ts @@ -8,20 +8,15 @@ import { mockSoftDeleteLowcalSession, mockValidateSingleSessionRequest, } from "../tests/mocks/saveAndReturnMocks"; +import { CoreDomainClient } from "@opensystemslab/planx-core"; // https://docs.notifications.service.gov.uk/node.html#email-addresses const TEST_EMAIL = "simulate-delivered@notifications.service.gov.uk"; const SAVE_ENDPOINT = "/send-email/save"; -jest.mock("@opensystemslab/planx-core", () => { - return { - CoreDomainClient: jest.fn().mockImplementation(() => ({ - formatRawProjectTypes: jest - .fn() - .mockResolvedValue(["New office premises"]), - })), - }; -}); +jest + .spyOn(CoreDomainClient.prototype, "formatRawProjectTypes") + .mockResolvedValue("New office premises"); describe("Send Email endpoint", () => { beforeEach(() => { @@ -93,7 +88,7 @@ describe("Send Email endpoint", () => { name: "ValidateSingleSessionRequest", data: { flows_by_pk: mockFlow, - lowcal_sessions: [], + lowcalSessions: [], }, }); @@ -258,7 +253,7 @@ describe("Setting up send email events", () => { name: "ValidateSingleSessionRequest", data: { flows_by_pk: mockFlow, - lowcal_sessions: [{ ...mockLowcalSession, has_user_saved: true }], + lowcalSessions: [{ ...mockLowcalSession, has_user_saved: true }], }, matchOnVariables: false, }); diff --git a/api.planx.uk/saveAndReturn/utils.ts b/api.planx.uk/saveAndReturn/utils.ts index 2de72f806f..b9409058e0 100644 --- a/api.planx.uk/saveAndReturn/utils.ts +++ b/api.planx.uk/saveAndReturn/utils.ts @@ -92,7 +92,10 @@ const validateSingleSessionRequest = async ( try { const query = gql` query ValidateSingleSessionRequest($sessionId: uuid!) { - lowcal_sessions(where: { id: { _eq: $sessionId } }, limit: 1) { + lowcalSessions: lowcal_sessions( + where: { id: { _eq: $sessionId } } + limit: 1 + ) { id data created_at @@ -112,8 +115,12 @@ const validateSingleSessionRequest = async ( const client = getClientForTemplate(template); const headers = getSaveAndReturnPublicHeaders(sessionId, email); const { - lowcal_sessions: [session], - } = await client.request(query, { sessionId }, headers); + lowcalSessions: [session], + } = await client.request<{ lowcalSessions: LowCalSession[] }>( + query, + { sessionId }, + headers, + ); if (!session) throw Error(`Unable to find session: ${sessionId}`); diff --git a/api.planx.uk/tests/mocks/saveAndReturnMocks.ts b/api.planx.uk/tests/mocks/saveAndReturnMocks.ts index 3a92bc01fc..02393437d2 100644 --- a/api.planx.uk/tests/mocks/saveAndReturnMocks.ts +++ b/api.planx.uk/tests/mocks/saveAndReturnMocks.ts @@ -144,7 +144,7 @@ export const mockValidateSingleSessionRequest = { name: "ValidateSingleSessionRequest", data: { flows_by_pk: mockFlow, - lowcal_sessions: [mockLowcalSession], + lowcalSessions: [mockLowcalSession], }, variables: { sessionId: mockLowcalSession.id, diff --git a/api.planx.uk/types.ts b/api.planx.uk/types.ts index ee8d0c46c9..b7e17693f8 100644 --- a/api.planx.uk/types.ts +++ b/api.planx.uk/types.ts @@ -75,7 +75,7 @@ export interface LowCalSession { has_user_saved: boolean; flow: { slug: string; - team?: Team; + team: Team; }; lockedAt?: string; paymentRequests?: Pick[];