Skip to content

Commit

Permalink
feat: Drop publicGraphQLClient in favour of $public client (#2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 13, 2023
1 parent 2717e01 commit 4745235
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
7 changes: 1 addition & 6 deletions api.planx.uk/hasura/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
13 changes: 4 additions & 9 deletions api.planx.uk/inviteToPay/sendPaymentEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
10 changes: 3 additions & 7 deletions api.planx.uk/notify/notify.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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 };
17 changes: 6 additions & 11 deletions api.planx.uk/notify/routeSendEmailRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
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(() => {
Expand Down Expand Up @@ -93,7 +88,7 @@ describe("Send Email endpoint", () => {
name: "ValidateSingleSessionRequest",
data: {
flows_by_pk: mockFlow,
lowcal_sessions: [],
lowcalSessions: [],
},
});

Expand Down Expand Up @@ -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,
});
Expand Down
13 changes: 10 additions & 3 deletions api.planx.uk/saveAndReturn/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);

Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/tests/mocks/saveAndReturnMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const mockValidateSingleSessionRequest = {
name: "ValidateSingleSessionRequest",
data: {
flows_by_pk: mockFlow,
lowcal_sessions: [mockLowcalSession],
lowcalSessions: [mockLowcalSession],
},
variables: {
sessionId: mockLowcalSession.id,
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface LowCalSession {
has_user_saved: boolean;
flow: {
slug: string;
team?: Team;
team: Team;
};
lockedAt?: string;
paymentRequests?: Pick<PaymentRequest, "id" | "payeeEmail" | "payeeName">[];
Expand Down

0 comments on commit 4745235

Please sign in to comment.