From 445a576aa268bb6704ae13bc525d6579b5f5a30d Mon Sep 17 00:00:00 2001 From: zz-hh-aa Date: Thu, 19 Dec 2024 16:13:44 +0000 Subject: [PATCH] feat: use team name for metabase collection name not team slug --- .../metabase/collection/collection.test.ts | 42 ++++++++++--------- .../metabase/collection/createCollection.ts | 12 ++---- .../collection/getTeamIdAndMetabaseId.ts | 2 + .../analytics/metabase/collection/service.ts | 19 ++++++--- .../analytics/metabase/collection/types.ts | 8 ++-- 5 files changed, 45 insertions(+), 38 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 baeb6c6107..b45efae0ed 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts @@ -27,15 +27,15 @@ describe("createTeamCollection", () => { // Mock Metabase API calls const metabaseMock = nock(process.env.METABASE_URL_EXT!) .post("/api/collection/", { - slug: "barnet", + name: "Barnet", }) .reply(200, { id: 123, - slug: "barnet", + name: "Barnet", }); const collectionId = await createCollection({ - slug: "barnet", + name: "Barnet", }); expect(collectionId).toBe(123); @@ -49,6 +49,7 @@ describe("createTeamCollection", () => { returning: [ { id: 26, + name: "Barnet", slug: "barnet", metabase_id: 123, }, @@ -56,30 +57,30 @@ describe("createTeamCollection", () => { }, }); - const testSlug = "example-council"; + const testName = "Example Council"; const metabaseMock = nock(process.env.METABASE_URL_EXT!); // Mock collection creation endpoint metabaseMock .post("/api/collection/", { - slug: testSlug, - parent_id: 100, + name: testName, + parentId: 100, }) .reply(200, { id: 123, - slug: testSlug, - parent_id: 100, + name: testName, + parentId: 100, }); // Mock GET request for verifying the new collection metabaseMock.get("/api/collection/123").reply(200, { id: 123, - slug: testSlug, - parent_id: 100, + name: testName, + parentId: 100, }); const collectionId = await createCollection({ - slug: testSlug, + name: testName, parentId: 100, }); @@ -88,7 +89,8 @@ describe("createTeamCollection", () => { // Verify the collection details using the service function const collection = await getCollection(collectionId); - expect(collection.parent_id).toBe(100); + console.log({ collection }); + expect(collection.parentId).toBe(100); expect(metabaseMock.isDone()).toBe(true); }); @@ -97,6 +99,7 @@ describe("createTeamCollection", () => { teams: [ { id: 26, + name: "Barnet", slug: "barnet", metabaseId: 20, }, @@ -114,7 +117,7 @@ describe("createTeamCollection", () => { await expect( createCollection({ - slug: "test-collection", + name: "Test Collection", }), ).rejects.toThrow("Network error occurred"); }); @@ -126,7 +129,7 @@ describe("createTeamCollection", () => { await expect( createCollection({ - slug: "test-collection", + name: "Test Collection", }), ).rejects.toThrow(MetabaseError); }); @@ -142,6 +145,7 @@ describe("getTeamIdAndMetabaseId", () => { teams: [ { id: 26, + name: "Barnet", slug: "barnet", metabaseId: 20, }, @@ -236,22 +240,22 @@ describe("edge cases", () => { ).rejects.toThrow(); }); - test("handles slug with special characters", async () => { - const specialSlug = "@#$%^&*"; + test("handles name with special characters", async () => { + const specialName = "@#$%^&*"; nock(process.env.METABASE_URL_EXT!).get("/api/collection/").reply(200, []); nock(process.env.METABASE_URL_EXT!) .post("/api/collection/", { - slug: specialSlug, + name: specialName, }) .reply(200, { id: 789, - slug: specialSlug, + name: specialName, }); const collection = await createCollection({ - slug: specialSlug, + name: specialName, }); expect(collection).toBe(789); }); diff --git a/api.planx.uk/modules/analytics/metabase/collection/createCollection.ts b/api.planx.uk/modules/analytics/metabase/collection/createCollection.ts index 79fbe9d7be..24ae4bd0e0 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/createCollection.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/createCollection.ts @@ -1,18 +1,12 @@ import { createMetabaseClient } from "../shared/client.js"; -import type { NewCollectionParams } from "./types.js"; +import type { MetabaseCollectionParams, NewCollectionParams } from "./types.js"; const client = createMetabaseClient(); export async function createCollection( - params: NewCollectionParams, + params: MetabaseCollectionParams, ): Promise { - const transformedParams = { - slug: params.slug, - parent_id: params.parentId, - }; - - const response = await client.post(`/api/collection/`, transformedParams); - const slug = response.data.slug; + const response = await client.post(`/api/collection/`, params); console.log( `New collection: ${response.data.slug}, new collection ID: ${response.data.id}`, ); diff --git a/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts index 4007bfef64..5d711158cc 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts @@ -4,6 +4,7 @@ import { $api } from "../../../../client/index.js"; interface GetMetabaseId { teams: { id: number; + name: string; slug: string; metabaseId: number | null; }[]; @@ -16,6 +17,7 @@ export const getTeamIdAndMetabaseId = async (slug: string) => { query GetTeamAndMetabaseId($slug: String!) { teams(where: { slug: { _eq: $slug } }) { id + name slug metabaseId: metabase_id } diff --git a/api.planx.uk/modules/analytics/metabase/collection/service.ts b/api.planx.uk/modules/analytics/metabase/collection/service.ts index 84718c4833..80cde51b03 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/service.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/service.ts @@ -1,5 +1,5 @@ import { updateMetabaseId } from "./updateMetabaseId.js"; -import type { NewCollectionParams } from "./types.js"; +import type { NewCollectionParams, MetabaseCollectionParams } from "./types.js"; import { getTeamIdAndMetabaseId } from "./getTeamIdAndMetabaseId.js"; import { createCollection } from "./createCollection.js"; @@ -14,15 +14,24 @@ export async function createTeamCollection( params: NewCollectionParams, ): Promise { try { - const { metabaseId, id: teamId } = await getTeamIdAndMetabaseId( - params.slug, - ); + const { + metabaseId, + name, + id: teamId, + } = await getTeamIdAndMetabaseId(params.slug); if (metabaseId) { return metabaseId; } - const newMetabaseId = await createCollection(params); + const { slug, ...rest } = params; + const metabaseParams = { + name, + ...rest, + } as const; + + const newMetabaseId = await createCollection(metabaseParams); + await updateMetabaseId(teamId, newMetabaseId); return newMetabaseId; } catch (error) { diff --git a/api.planx.uk/modules/analytics/metabase/collection/types.ts b/api.planx.uk/modules/analytics/metabase/collection/types.ts index d7eb1a2070..651c96b468 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/types.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/types.ts @@ -15,11 +15,9 @@ export interface NewCollectionParams { } /** Interface for request after transforming to snake case (Metabase takes snake while PlanX API takes camel) */ -export interface MetabaseCollectionParams { +export type MetabaseCollectionParams = Omit & { name: string; - description?: string; - parent_id?: number; -} +}; /** Metbase collection ID for the the "Council" collection **/ // const COUNCILS_COLLECTION_ID = 58; @@ -45,5 +43,5 @@ export interface NewCollectionResponse { export interface GetCollectionResponse { id: number; slug: string; - parent_id: number; + parentId: number; }