Skip to content

Commit

Permalink
feat: use team name for metabase collection name
Browse files Browse the repository at this point in the history
not team slug
  • Loading branch information
zz-hh-aa committed Dec 19, 2024
1 parent 85d73f4 commit 445a576
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,37 +49,38 @@ describe("createTeamCollection", () => {
returning: [
{
id: 26,
name: "Barnet",
slug: "barnet",
metabase_id: 123,
},
],
},
});

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,
});

Expand All @@ -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);
});

Expand All @@ -97,6 +99,7 @@ describe("createTeamCollection", () => {
teams: [
{
id: 26,
name: "Barnet",
slug: "barnet",
metabaseId: 20,
},
Expand All @@ -114,7 +117,7 @@ describe("createTeamCollection", () => {

await expect(
createCollection({
slug: "test-collection",
name: "Test Collection",
}),
).rejects.toThrow("Network error occurred");
});
Expand All @@ -126,7 +129,7 @@ describe("createTeamCollection", () => {

await expect(
createCollection({
slug: "test-collection",
name: "Test Collection",
}),
).rejects.toThrow(MetabaseError);
});
Expand All @@ -142,6 +145,7 @@ describe("getTeamIdAndMetabaseId", () => {
teams: [
{
id: 26,
name: "Barnet",
slug: "barnet",
metabaseId: 20,
},
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<number> {
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}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { $api } from "../../../../client/index.js";
interface GetMetabaseId {
teams: {
id: number;
name: string;
slug: string;
metabaseId: number | null;
}[];
Expand All @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions api.planx.uk/modules/analytics/metabase/collection/service.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -14,15 +14,24 @@ export async function createTeamCollection(
params: NewCollectionParams,
): Promise<number> {
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) {
Expand Down
8 changes: 3 additions & 5 deletions api.planx.uk/modules/analytics/metabase/collection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NewCollectionParams, "slug"> & {
name: string;
description?: string;
parent_id?: number;
}
};

/** Metbase collection ID for the the "Council" collection **/
// const COUNCILS_COLLECTION_ID = 58;
Expand All @@ -45,5 +43,5 @@ export interface NewCollectionResponse {
export interface GetCollectionResponse {
id: number;
slug: string;
parent_id: number;
parentId: number;
}

0 comments on commit 445a576

Please sign in to comment.