diff --git a/api.planx.uk/modules/analytics/metabase/collection/service.ts b/api.planx.uk/modules/analytics/metabase/collection/service.ts index b246a1dbfc..49d6301ada 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/service.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/service.ts @@ -1,6 +1,6 @@ import { MetabaseError, metabaseClient } from "../shared/client.js"; import type { NewCollectionParams } from "./types.js"; -import axios from "axios"; +import { toSnakeCase } from "../shared/utils.js"; export async function authentication(): Promise { try { @@ -28,11 +28,11 @@ export async function newCollection({ } // If no existing collection, create new one - const requestBody: any = { + const requestBody = toSnakeCase({ name, description, parent_id, - }; + }); // Remove undefined properties Object.keys(requestBody).forEach( diff --git a/api.planx.uk/modules/analytics/metabase/shared/utils.ts b/api.planx.uk/modules/analytics/metabase/shared/utils.ts new file mode 100644 index 0000000000..084aca384b --- /dev/null +++ b/api.planx.uk/modules/analytics/metabase/shared/utils.ts @@ -0,0 +1,34 @@ +/** + * Converts object keys from camelCase to snake_case + */ +export function toSnakeCase(obj: T): Record { + return Object.entries(obj).reduce( + (acc, [key, value]) => { + // Convert camelCase to snake_case + const snakeKey = key.replace( + /[A-Z]/g, + (letter) => `_${letter.toLowerCase()}`, + ); + acc[snakeKey] = value; + return acc; + }, + {} as Record, + ); +} + +/** + * Converts object keys from snake_case to camelCase + */ +export function toCamelCase(obj: T): Record { + return Object.entries(obj).reduce( + (acc, [key, value]) => { + // Convert snake_case to camelCase + const camelKey = key.replace(/_([a-z])/g, (_, letter) => + letter.toUpperCase(), + ); + acc[camelKey] = value; + return acc; + }, + {} as Record, + ); +}