-
Notifications
You must be signed in to change notification settings - Fork 2
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: analytics module gets and updates PlanX DB #4071
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bebea79
feat: add metabase_id column to teams table migration
zz-hh-aa d477740
feat: update Hasura api permissions
zz-hh-aa 1e78174
feat: get ids from planx db
zz-hh-aa 843a7c4
feat: function to update planx db with new metabase id
zz-hh-aa 65ed2d1
test: test interactions with planx db
zz-hh-aa 1e4112c
test: update api role hasura test
zz-hh-aa ba015dc
feat: change name to team slug
zz-hh-aa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 92 additions & 1 deletion
93
api.planx.uk/modules/analytics/metabase/collection/collection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GetMetabaseId>( | ||
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; | ||
} | ||
}; |
43 changes: 43 additions & 0 deletions
43
api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UpdateMetabaseId>( | ||
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...lanx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alter table "public"."teams" drop column "metabase_id"; |
2 changes: 2 additions & 0 deletions
2
....planx.uk/migrations/1733738650997_alter_table_public_teams_add_column_metabase_id/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table "public"."teams" add column "metabase_id" integer | ||
null; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ilike
could match both "North Somerset" and "South Somerset" - it would be better to match using_eq
on team slugs if possible which are guaranteed to be unique.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! Updated in ba015dc.