From bebea79366c05bd7757e0e9024b756e0cc72c46d Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Mon, 9 Dec 2024 10:15:06 +0000 Subject: [PATCH 1/7] feat: add metabase_id column to teams table migration --- .../down.sql | 1 + .../up.sql | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/down.sql create mode 100644 hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/up.sql diff --git a/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/down.sql b/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/down.sql new file mode 100644 index 0000000000..c620678461 --- /dev/null +++ b/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/down.sql @@ -0,0 +1 @@ +alter table "public"."teams" drop column "metabase_id"; diff --git a/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/up.sql b/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/up.sql new file mode 100644 index 0000000000..8ab49dd71b --- /dev/null +++ b/hasura.planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/up.sql @@ -0,0 +1,2 @@ +alter table "public"."teams" add column "metabase_id" integer + null; From d477740dbdba3350924d14dcca0ea39755032db2 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Tue, 10 Dec 2024 10:09:15 +0000 Subject: [PATCH 2/7] feat: update Hasura api permissions --- hasura.planx.uk/metadata/tables.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hasura.planx.uk/metadata/tables.yaml b/hasura.planx.uk/metadata/tables.yaml index fe53e49f90..2557257e6c 100644 --- a/hasura.planx.uk/metadata/tables.yaml +++ b/hasura.planx.uk/metadata/tables.yaml @@ -2370,6 +2370,7 @@ - created_at - domain - id + - metabase_id - name - slug - updated_at @@ -2422,6 +2423,13 @@ - updated_at filter: {} update_permissions: + - role: api + permission: + columns: + - metabase_id + filter: {} + check: {} + comment: "" - role: platformAdmin permission: columns: From 1e78174b1616ca407ba9f7aec24077d41202f164 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Wed, 11 Dec 2024 17:10:00 +0000 Subject: [PATCH 3/7] feat: get ids from planx db --- .../collection/getTeamIdAndMetabaseId.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts diff --git a/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts new file mode 100644 index 0000000000..7763dd533e --- /dev/null +++ b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts @@ -0,0 +1,38 @@ +import { gql } from "graphql-request"; +import { $api } from "../../../../client/index.js"; + +interface GetMetabaseId { + teams: { + id: number; + name: string; + metabaseId: number | null; + }[]; +} + +export const getTeamIdAndMetabaseId = async (name: string) => { + try { + const response = await $api.client.request( + gql` + query GetTeamAndMetabaseId($name: String!) { + teams(where: { name: { _ilike: $name } }) { + id + name + metabaseId: metabase_id + } + } + `, + { + name: name, + }, + ); + + const result = response.teams[0]; + return result; + } catch (e) { + console.error( + "Error fetching team's ID / Metabase ID from PlanX DB:", + (e as Error).stack, + ); + throw e; + } +}; From 843a7c44e5f4cc3f6a3f14b0e01ea2ab5554fce0 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Wed, 11 Dec 2024 17:10:57 +0000 Subject: [PATCH 4/7] feat: function to update planx db with new metabase id --- .../metabase/collection/updateMetabaseId.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts diff --git a/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts b/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts new file mode 100644 index 0000000000..29de69e692 --- /dev/null +++ b/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts @@ -0,0 +1,43 @@ +import { gql } from "graphql-request"; +import { $api } from "../../../../client/index.js"; + +interface UpdateMetabaseId { + teams: { + id: number; + name: string; + metabaseId: number; + }; +} + +/** Updates column `metabase_id` in the Planx DB `teams` table */ +export const updateMetabaseId = async (teamId: number, metabaseId: number) => { + try { + const response = await $api.client.request( + gql` + mutation UpdateTeamMetabaseId($id: Int!, $metabaseId: Int!) { + update_teams( + where: { id: { _eq: $id } } + _set: { metabase_id: $metabaseId } + ) { + returning { + id + name + metabase_id + } + } + } + `, + { + id: teamId, + metabaseId: metabaseId, + }, + ); + return response; + } catch (e) { + console.error( + "There's been an error while updating the Metabase ID for this team", + (e as Error).stack, + ); + throw e; + } +}; From 65ed2d1495e564341d8addf9952c124d5976d315 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Wed, 11 Dec 2024 17:12:24 +0000 Subject: [PATCH 5/7] test: test interactions with planx db --- .../metabase/collection/collection.test.ts | 93 ++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts index 1b66e90a88..81bcb1c6c6 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts @@ -1 +1,92 @@ -test.todo("should test collection check and creation"); +import { $api } from "../../../../client/index.js"; +import { updateMetabaseId } from "./updateMetabaseId.js"; +import { getTeamIdAndMetabaseId } from "./getTeamIdAndMetabaseId.js"; + +describe("getTeamIdAndMetabaseId", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + test("successfully gets team and existing metabase id", async () => { + vi.spyOn($api.client, "request").mockResolvedValue({ + teams: [ + { + id: 26, + name: "Barnet", + metabaseId: 20, + }, + ], + }); + + const teamAndMetabaseId = await getTeamIdAndMetabaseId("Barnet"); + + expect(teamAndMetabaseId.id).toEqual(26); + expect(teamAndMetabaseId.metabaseId).toEqual(20); + }); + + test("handles team with null metabase id", async () => { + vi.spyOn($api.client, "request").mockResolvedValue({ + teams: [ + { + id: 26, + name: "Barnet", + metabaseId: null, + }, + ], + }); + + const teamAndMetabaseId = await getTeamIdAndMetabaseId("Barnet"); + + expect(teamAndMetabaseId.id).toEqual(26); + expect(teamAndMetabaseId.metabaseId).toBeNull(); + }); +}); + +describe("updateMetabaseId", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + test("successfully updates metabase ID", async () => { + // Mock the GraphQL request + vi.spyOn($api.client, "request").mockResolvedValue({ + update_teams: { + returning: [ + { + id: 1, + name: "Test Team", + metabase_id: 123, + }, + ], + }, + }); + + const result = await updateMetabaseId(1, 123); + + expect(result).toEqual({ + update_teams: { + returning: [ + { + id: 1, + name: "Test Team", + metabase_id: 123, + }, + ], + }, + }); + + expect($api.client.request).toHaveBeenCalledWith(expect.any(String), { + id: 1, + metabaseId: 123, + }); + }); + + test("handles GraphQL error", async () => { + // Mock a failed GraphQL request + vi.spyOn($api.client, "request").mockRejectedValue( + new Error("GraphQL error"), + ); + + await expect(updateMetabaseId(1, 123)).rejects.toThrow("GraphQL error"); + }); +}); From 1e4112c3660227e76a5fae2ce1247dc7a7682ee7 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Mon, 16 Dec 2024 15:32:51 +0000 Subject: [PATCH 6/7] test: update api role hasura test after teams table migration --- hasura.planx.uk/tests/teams.test.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hasura.planx.uk/tests/teams.test.js b/hasura.planx.uk/tests/teams.test.js index 0c2aa8e35b..53defcf1b0 100644 --- a/hasura.planx.uk/tests/teams.test.js +++ b/hasura.planx.uk/tests/teams.test.js @@ -98,8 +98,15 @@ describe("teams", () => { expect(i.queries).toContain("teams"); }); - test("cannot create, update, or delete teams", () => { - expect(i).toHaveNoMutationsFor("teams"); + test("can update teams", () => { + expect(i.mutations).toContain("update_teams"); + expect(i.mutations).toContain("update_teams_by_pk"); + expect(i.mutations).toContain("update_teams_many"); + }); + + test("cannot create or delete teams", () => { + expect(i.mutations).not.toContain("insert_teams"); + expect(i.mutations).not.toContain("delete_teams"); }); }); }); From ba015dc3fc73801453fecb5dfc694a7ad0163639 Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Mon, 16 Dec 2024 15:39:25 +0000 Subject: [PATCH 7/7] feat: change name to team slug and match in planx db on equality instead --- .../analytics/metabase/collection/collection.test.ts | 10 +++++----- .../metabase/collection/getTeamIdAndMetabaseId.ts | 12 ++++++------ .../metabase/collection/updateMetabaseId.ts | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts index 81bcb1c6c6..4b9829dd69 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts @@ -12,13 +12,13 @@ describe("getTeamIdAndMetabaseId", () => { teams: [ { id: 26, - name: "Barnet", + slug: "barnet", metabaseId: 20, }, ], }); - const teamAndMetabaseId = await getTeamIdAndMetabaseId("Barnet"); + const teamAndMetabaseId = await getTeamIdAndMetabaseId("barnet"); expect(teamAndMetabaseId.id).toEqual(26); expect(teamAndMetabaseId.metabaseId).toEqual(20); @@ -29,7 +29,7 @@ describe("getTeamIdAndMetabaseId", () => { teams: [ { id: 26, - name: "Barnet", + slug: "barnet", metabaseId: null, }, ], @@ -54,7 +54,7 @@ describe("updateMetabaseId", () => { returning: [ { id: 1, - name: "Test Team", + slug: "testteam", metabase_id: 123, }, ], @@ -68,7 +68,7 @@ describe("updateMetabaseId", () => { returning: [ { id: 1, - name: "Test Team", + slug: "testteam", metabase_id: 123, }, ], diff --git a/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts index 7763dd533e..4007bfef64 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts @@ -4,25 +4,25 @@ import { $api } from "../../../../client/index.js"; interface GetMetabaseId { teams: { id: number; - name: string; + slug: string; metabaseId: number | null; }[]; } -export const getTeamIdAndMetabaseId = async (name: string) => { +export const getTeamIdAndMetabaseId = async (slug: string) => { try { const response = await $api.client.request( gql` - query GetTeamAndMetabaseId($name: String!) { - teams(where: { name: { _ilike: $name } }) { + query GetTeamAndMetabaseId($slug: String!) { + teams(where: { slug: { _eq: $slug } }) { id - name + slug metabaseId: metabase_id } } `, { - name: name, + slug: slug, }, ); diff --git a/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts b/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts index 29de69e692..93ffe1042d 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts @@ -4,7 +4,7 @@ import { $api } from "../../../../client/index.js"; interface UpdateMetabaseId { teams: { id: number; - name: string; + slug: string; metabaseId: number; }; } @@ -21,7 +21,7 @@ export const updateMetabaseId = async (teamId: number, metabaseId: number) => { ) { returning { id - name + slug metabase_id } }