Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drop publicGraphQLClient in favour of $public client #2300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using spies like this actually feels much nicer than mocks as it keeps the function type safe - it forces us to pass in valid return types for functions we're spying on.

I'll do a pass of these at some point very soon and tidy up the remaining jest.mock("@opensystemslab/planx-core") calls.

.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
Loading