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: Metabase module controller and service #4072

Merged
merged 26 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
71fb982
feat: new createCollection metabase api call
zz-hh-aa Dec 11, 2024
530f60e
feat: new get collection from metabase function
zz-hh-aa Dec 11, 2024
50f0c18
feat: createCollectionIfDoesNotExist function
zz-hh-aa Dec 11, 2024
6c34c73
feat: new types for collection service
zz-hh-aa Dec 11, 2024
28b3de6
fix: import in metabase client
zz-hh-aa Dec 11, 2024
07a969c
feat: new collection route in api
zz-hh-aa Dec 11, 2024
521689b
fix: controller type error
zz-hh-aa Dec 11, 2024
b097861
test: test for full metabase collection service
zz-hh-aa Dec 11, 2024
30cfbb4
chore: remove unnecessary client import
zz-hh-aa Dec 16, 2024
6f251b1
test: remove unused axios spy
zz-hh-aa Dec 16, 2024
398d9b8
chore: change functions to camelCase
zz-hh-aa Dec 19, 2024
08d38f2
chore: tidy types
zz-hh-aa Dec 19, 2024
632834e
feat: update function name for clarity
zz-hh-aa Dec 19, 2024
15ad49c
fix: update name to slug
zz-hh-aa Dec 19, 2024
8f12f20
chore: replace metabase client w singleton instance
zz-hh-aa Dec 19, 2024
85d73f4
feat: update route and swagger docs
zz-hh-aa Dec 19, 2024
445a576
feat: use team name for metabase collection name
zz-hh-aa Dec 19, 2024
f4dea96
feat: change route to include team slug
zz-hh-aa Dec 19, 2024
5d8818c
chore: remove console.logs
zz-hh-aa Dec 19, 2024
55d089b
test(api): Set up mock for metabase client
DafyddLlyr Dec 20, 2024
c9e9740
chore: remove metabase tag from swagger docs
zz-hh-aa Dec 23, 2024
6d8cd9c
chore: tidy imports and variables
zz-hh-aa Dec 23, 2024
d472c6a
fix: lint issues in analytics yaml
zz-hh-aa Jan 13, 2025
16b1728
nit: tidy comments
zz-hh-aa Jan 20, 2025
b2d7fe5
feat: move ApiResponse type to shared
zz-hh-aa Jan 20, 2025
e86955c
nit: fix comment and validation
zz-hh-aa Jan 28, 2025
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
Prev Previous commit
Next Next commit
feat: use team name for metabase collection name
not team slug
zz-hh-aa committed Dec 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 445a576aa268bb6704ae13bc525d6579b5f5a30d
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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,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,
});

@@ -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);
});
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(
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved
`New collection: ${response.data.slug}, new collection ID: ${response.data.id}`,
);
Original file line number Diff line number Diff line change
@@ -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
}
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";

@@ -14,15 +14,24 @@ export async function createTeamCollection(
params: NewCollectionParams,
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved
): 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;
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved
const metabaseParams = {
name,
...rest,
} as const;
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved

const newMetabaseId = await createCollection(metabaseParams);

await updateMetabaseId(teamId, newMetabaseId);
return newMetabaseId;
} catch (error) {
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
@@ -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 **/
zz-hh-aa marked this conversation as resolved.
Show resolved Hide resolved
// const COUNCILS_COLLECTION_ID = 58;
@@ -45,5 +43,5 @@ export interface NewCollectionResponse {
export interface GetCollectionResponse {
id: number;
slug: string;
parent_id: number;
parentId: number;
}