Skip to content

Commit

Permalink
feat: feat: refactor updateFilter to separate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-hh-aa committed Jan 30, 2025
1 parent 5257556 commit 2d6993a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
34 changes: 17 additions & 17 deletions api.planx.uk/modules/analytics/metabase/dashboard/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("Dashboard Operations", () => {
{
name: filterName,
type: "string/=",
default: ["old_value"],
value: ["old_value"],
},
],
});
Expand All @@ -110,7 +110,7 @@ describe("Dashboard Operations", () => {
{
name: filterName,
type: "string/=",
default: [filterValue],
value: [filterValue],
},
],
})
Expand All @@ -119,19 +119,22 @@ describe("Dashboard Operations", () => {
{
name: filterName,
type: "string/=",
default: [filterValue],
value: [filterValue],
},
],
param_fields: {},
});

await expect(
updateFilter({
dashboardId: dashboardId,
filter: filterName,
value: filterValue,
}),
).resolves.not.toThrow();
const result = await updateFilter({
dashboardId: dashboardId,
filter: filterName,
value: filterValue,
});

expect(result).toEqual({
success: true,
updatedFilter: filterName,
});
});

test("handles non-string filter type appropriately", async () => {
Expand All @@ -141,18 +144,15 @@ describe("Dashboard Operations", () => {
parameters: [
{
name: filterName,
slug: "event",
id: "30a24538",
type: "number/=",
sectionId: "number",
default: [42],
value: [42],
},
],
});

nock(BASE_URL!).put(`/api/dashboard/${dashboardId}`).reply(400, {
message: "Invalid parameter type. Expected number, got string.",
});
// nock(BASE_URL!).put(`/api/dashboard/${dashboardId}`).reply(400, {
// message: "Invalid parameter type. Expected number, got string.",
// });

await expect(
updateFilter({
Expand Down
18 changes: 16 additions & 2 deletions api.planx.uk/modules/analytics/metabase/dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export type MetabaseCopyDashboardParams = {
is_deep_copy?: boolean;
};

// Convert to Metabase API structure
export function toMetabaseParams(
params: CopyDashboardParams,
): MetabaseCopyDashboardParams {
Expand Down Expand Up @@ -77,10 +76,11 @@ export type NewDashboardHandler = ValidatedRequestHandler<
ApiResponse<string>
>;

export interface GetDashboardResponse {
export interface MetabaseDashboardResponse {
name: string;
id: number;
collection_id: number;
parameters: FilterParam[];
}

export interface UpdateFilterResponse {
Expand All @@ -91,4 +91,18 @@ export interface UpdateFilterResponse {
export interface FilterParam {
name: string;
type: string;
// The Metabase API expects filter default values as arrays, even if there's only one (eg for multi-select filters)
value: string[];
}

export type GetDashboardResponse = Pick<
MetabaseDashboardResponse,
"name" | "id" | "collection_id"
>;

export type GetFilterResponse = Pick<MetabaseDashboardResponse, "parameters">;

export interface UpdatedFilterResponse {
parameter: FilterParam;
updatedValue: string | undefined;
}
60 changes: 42 additions & 18 deletions api.planx.uk/modules/analytics/metabase/dashboard/updateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,57 @@ import type {
UpdateFilterParams,
UpdateFilterResponse,
FilterParam,
UpdatedFilterResponse,
GetFilterResponse,
} from "./types.js";

function populateUpdatedFilterResponse(
param: FilterParam,
filterName: string,
filterValue: string,
): UpdatedFilterResponse {
if (param.name === filterName) {
if (!param.type.startsWith("string/")) {
throw new Error(
`Filter type '${param.type}' is not supported. Only string filters are currently supported.`,
);
}
return {
parameter: { ...param, value: [filterValue] },
updatedValue: param.name,
};
}
return {
parameter: param,
updatedValue: undefined,
};
}

/** Takes the ID of the dashboard to be updated, the name of the filter (a string, must be an exact match), and the new value to be filtered on.
* Currently only works for strings. */
export async function updateFilter(
params: UpdateFilterParams,
): Promise<UpdateFilterResponse> {
// Get existing dashboard data
const response = await $metabase.get(`/api/dashboard/${params.dashboardId}`);

// Update filter default value parameter
let updatedFilter;
const updatedParameters = response.data.parameters.map(
(param: FilterParam) => {
if (param.name === params.filter) {
// Check if the filter is a string type
if (!param.type.startsWith("string/")) {
throw new Error(
`Filter type '${param.type}' is not supported. Only string filters are currently supported.`,
);
}
updatedFilter = param.name;
return { ...param, default: [params.value] };
}
return param;
},
const response = await $metabase.get<GetFilterResponse>(
`/api/dashboard/${params.dashboardId}`,
);
console.log({ response });
// Update filter default value parameter
let updatedFilter: string | undefined;
const updatedParameters = response.data.parameters.map((param) => {
const result = populateUpdatedFilterResponse(
param,
params.filter,
params.value,
);
console.log({ result });
if (result.updatedValue) {
updatedFilter = result.updatedValue;
}
return result.parameter;
});
console.log({ updatedParameters });

if (!updatedFilter) {
throw new Error(
Expand Down

0 comments on commit 2d6993a

Please sign in to comment.