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

chore: Drop adminClient (1/2) #2356

Merged
merged 10 commits into from
Nov 3, 2023
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
2 changes: 1 addition & 1 deletion api.planx.uk/admin/session/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Session summary admin endpoint", () => {
sessionId: "abc123",
},
data: {
lowcal_sessions_by_pk: {},
lowcalSession: {},
},
});
});
Expand Down
18 changes: 10 additions & 8 deletions api.planx.uk/admin/session/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
import { NextFunction, Request, Response } from "express";
import { gql } from "graphql-request";

import { adminGraphQLClient as adminClient } from "../../hasura";
import { Breadcrumb, Flow, LowCalSession, Passport, Team } from "../../types";
import { $api } from "../../client";

/**
* @swagger
Expand All @@ -28,9 +28,9 @@ export async function getSessionSummary(
next: NextFunction,
) {
try {
const data = await getSessionSummaryById(req.params.sessionId);
if (data) {
res.send(data);
const session = await getSessionSummaryById(req.params.sessionId);
if (session) {
res.send(session);
} else {
res.send({
message: `Cannot find data for this session; double-check your sessionId is correct`,
Expand Down Expand Up @@ -72,11 +72,13 @@ interface SessionSummary {

const getSessionSummaryById = async (
sessionId: Session["id"],
): Promise<SessionSummary> => {
const data = await adminClient.request(
): Promise<SessionSummary | null> => {
const { lowcalSession } = await $api.client.request<
Record<"lowcalSession", SessionSummary | null>
>(
gql`
query GetSessionSummary($sessionId: uuid!) {
lowcal_sessions_by_pk(id: $sessionId) {
lowcalSession: lowcal_sessions_by_pk(id: $sessionId) {
flow {
id
slug
Expand Down Expand Up @@ -112,5 +114,5 @@ const getSessionSummaryById = async (
},
);

return data?.lowcal_sessions_by_pk;
return lowcalSession;
};
6 changes: 3 additions & 3 deletions api.planx.uk/editor/copyFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beforeEach(() => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: mockFlowData,
},
},
Expand All @@ -20,7 +20,7 @@ beforeEach(() => {
name: "InsertFlow",
matchOnVariables: false,
data: {
insert_flows_one: {
flow: {
id: 2,
},
},
Expand All @@ -30,7 +30,7 @@ beforeEach(() => {
name: "InsertOperation",
matchOnVariables: false,
data: {
insert_operations_one: {
operation: {
id: 1,
},
},
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/editor/copyPortalAsFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beforeEach(() => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: mockFlowData,
},
},
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/editor/findReplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beforeEach(() => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: mockFlowData,
slug: "test",
},
Expand All @@ -21,7 +21,7 @@ beforeEach(() => {
name: "UpdateFlow",
matchOnVariables: false,
data: {
update_flows_by_pk: {
flow: {
data: replacedFlowData,
slug: "test",
},
Expand Down
25 changes: 19 additions & 6 deletions api.planx.uk/editor/findReplace.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Flow } from "./../types";
import { adminGraphQLClient as adminClient } from "../hasura";
import { gql } from "graphql-request";
import { getFlowData } from "../helpers";
import { Request, Response, NextFunction } from "express";
import { getClient } from "../client";
import { FlowGraph } from "@opensystemslab/planx-core/types";

interface MatchResult {
matches: Flow["data"];
Expand Down Expand Up @@ -49,6 +50,15 @@ const getMatches = (
};
};

interface UpdateFlow {
flow: {
id: string;
slug: string;
data: FlowGraph;
updatedAt: string;
};
}

/**
* @swagger
* /flows/{flowId}/search:
Expand Down Expand Up @@ -136,14 +146,18 @@ const findAndReplaceInFlow = async (
}

// if matches, proceed with mutation to update flow data
const response = await adminClient.request(
const { client: $client } = getClient();
const response = await $client.request<UpdateFlow>(
gql`
mutation UpdateFlow($data: jsonb = {}, $id: uuid!) {
update_flows_by_pk(pk_columns: { id: $id }, _set: { data: $data }) {
flow: update_flows_by_pk(
pk_columns: { id: $id }
_set: { data: $data }
) {
id
slug
data
updated_at
updatedAt: updated_at
}
}
`,
Expand All @@ -153,8 +167,7 @@ const findAndReplaceInFlow = async (
},
);

const updatedFlow =
response.update_flows_by_pk && response.update_flows_by_pk.data;
const updatedFlow = response.flow && response.flow.data;

res.json({
message: `Found ${
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/editor/moveFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeEach(() => {
team_id: "1",
},
data: {
update_flows_by_pk: {
flow: {
id: "1",
},
},
Expand Down
19 changes: 14 additions & 5 deletions api.planx.uk/editor/moveFlow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response, NextFunction } from "express";
import { gql } from "graphql-request";
import { adminGraphQLClient as adminClient } from "../hasura";
import { Flow, Team } from "../types";
import { $public, getClient } from "../client";

const moveFlow = async (
req: Request,
Expand Down Expand Up @@ -36,8 +36,12 @@ const moveFlow = async (
}
};

interface GetTeam {
teams: Pick<Team, "id">[];
}

const getTeamIdBySlug = async (slug: Team["slug"]): Promise<Team["id"]> => {
const data = await adminClient.request(
const data = await $public.client.request<GetTeam>(
gql`
query GetTeam($slug: String!) {
teams(where: { slug: { _eq: $slug } }) {
Expand All @@ -53,14 +57,19 @@ const getTeamIdBySlug = async (slug: Team["slug"]): Promise<Team["id"]> => {
return data?.teams[0].id;
};

interface UpdateFlow {
flow: Pick<Flow, "id">;
}

const updateFlow = async (
flowId: Flow["id"],
teamId: Team["id"],
): Promise<Flow["id"]> => {
const data = await adminClient.request(
const { client: $client } = getClient();
const { flow } = await $client.request<UpdateFlow>(
gql`
mutation UpdateFlow($id: uuid!, $team_id: Int!) {
update_flows_by_pk(
flow: update_flows_by_pk(
pk_columns: { id: $id }
_set: { team_id: $team_id }
) {
Expand All @@ -74,7 +83,7 @@ const updateFlow = async (
},
);

return data?.update_flows_by_pk?.id;
return flow.id;
};

export { moveFlow };
39 changes: 25 additions & 14 deletions api.planx.uk/editor/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import supertest from "supertest";

import { queryMock } from "../tests/graphqlQueryMock";
import { authHeader } from "../tests/mockJWT";
import { authHeader, getJWT } from "../tests/mockJWT";
import app from "../server";
import { flowWithInviteToPay } from "../tests/mocks/inviteToPayData";
import { FlowGraph } from "@opensystemslab/planx-core/types";
import { userContext } from "../modules/auth/middleware";

beforeAll(() => {
const getStoreMock = jest.spyOn(userContext, "getStore");
getStoreMock.mockReturnValue({
user: {
sub: "123",
jwt: getJWT({ role: "teamEditor" }),
},
});
});

beforeEach(() => {
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: mockFlowData,
},
},
Expand All @@ -21,8 +32,8 @@ beforeEach(() => {
name: "GetMostRecentPublishedFlow",
matchOnVariables: false,
data: {
flows_by_pk: {
published_flows: [
flow: {
publishedFlows: [
{
data: mockFlowData,
},
Expand All @@ -35,7 +46,7 @@ beforeEach(() => {
name: "PublishFlow",
matchOnVariables: false,
data: {
insert_published_flows_one: {
publishedFlow: {
data: mockFlowData,
},
},
Expand Down Expand Up @@ -89,7 +100,7 @@ describe("publish", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: alteredFlow,
},
},
Expand All @@ -99,7 +110,7 @@ describe("publish", () => {
name: "PublishFlow",
matchOnVariables: false,
data: {
insert_published_flows_one: {
publishedFlow: {
data: alteredFlow,
},
},
Expand Down Expand Up @@ -147,7 +158,7 @@ describe("sections validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: alteredFlow,
},
},
Expand Down Expand Up @@ -182,7 +193,7 @@ describe("sections validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: flowWithSections,
},
},
Expand Down Expand Up @@ -214,7 +225,7 @@ describe("invite to pay validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: invalidatedFlow,
},
},
Expand Down Expand Up @@ -247,7 +258,7 @@ describe("invite to pay validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: alteredFlow,
},
},
Expand Down Expand Up @@ -276,7 +287,7 @@ describe("invite to pay validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: invalidatedFlow,
},
},
Expand Down Expand Up @@ -307,7 +318,7 @@ describe("invite to pay validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: invalidFlow,
},
},
Expand Down Expand Up @@ -340,7 +351,7 @@ describe("invite to pay validation on diff", () => {
name: "GetFlowData",
matchOnVariables: false,
data: {
flows_by_pk: {
flow: {
data: invalidatedFlow,
},
},
Expand Down
Loading
Loading