diff --git a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts index 4498326f78..a4c064ab62 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/collection.test.ts @@ -1,4 +1,4 @@ -import { checkCollections } from "./service.js"; +import { createCollectionIfDoesNotExist } from "./service.js"; import { getCollection } from "./getCollection.js"; import nock from "nock"; import { MetabaseError } from "../shared/client.js"; @@ -7,7 +7,7 @@ import { updateMetabaseId } from "./updateMetabaseId.js"; import { getTeamIdAndMetabaseId } from "./getTeamIdAndMetabaseId.js"; import { createCollection } from "./createCollection.js"; -describe("checkCollections", () => { +describe("createCollectionIfDoesNotExist", () => { beforeEach(() => { nock.cleanAll(); }); @@ -247,7 +247,7 @@ describe("edge cases", () => { test("handles missing name", async () => { await expect( - checkCollections({ + createCollectionIfDoesNotExist({ name: "", }), ).rejects.toThrow(); @@ -288,7 +288,7 @@ describe("edge cases", () => { }); await expect( - checkCollections({ + createCollectionIfDoesNotExist({ name: longName, }), ).rejects.toThrow(); diff --git a/api.planx.uk/modules/analytics/metabase/collection/controller.ts b/api.planx.uk/modules/analytics/metabase/collection/controller.ts index 38af98e500..c621cb15fb 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/controller.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/controller.ts @@ -1,18 +1,18 @@ -import { checkCollections } from "./service.js"; +import { createCollectionIfDoesNotExist } from "./service.js"; import type { NewCollectionRequestHandler } from "./types.js"; -export const checkCollectionsController: NewCollectionRequestHandler = async ( - _req, - res, -) => { - try { - const params = res.locals.parsedReq.body; - const collection = await checkCollections(params); - res.status(201).json({ data: collection }); - } catch (error) { - res.status(400).json({ - error: - error instanceof Error ? error.message : "An unexpected error occurred", - }); - } -}; +export const createCollectionIfDoesNotExistController: NewCollectionRequestHandler = + async (_req, res) => { + try { + const params = res.locals.parsedReq.body; + const collection = await createCollectionIfDoesNotExist(params); + res.status(201).json({ data: collection }); + } catch (error) { + res.status(400).json({ + error: + error instanceof Error + ? error.message + : "An unexpected error occurred", + }); + } + }; diff --git a/api.planx.uk/modules/analytics/metabase/collection/service.ts b/api.planx.uk/modules/analytics/metabase/collection/service.ts index 51cfb1216d..e4d7e59ccd 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/service.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/service.ts @@ -12,7 +12,7 @@ const client = createMetabaseClient(); * @params `name` is required, but `description` and `parent_id` are optional. * @returns `response.data`, so use dot notation to access `id` or `parent_id`. */ -export async function checkCollections( +export async function createCollectionIfDoesNotExist( params: NewCollectionParams, ): Promise { try { @@ -30,7 +30,7 @@ export async function checkCollections( console.log({ newMetabaseId }); return newMetabaseId; } catch (error) { - console.error("Error in checkCollections:", error); + console.error("Error in createCollectionIfDoesNotExist:", error); throw error; } } diff --git a/api.planx.uk/modules/analytics/metabase/collection/types.ts b/api.planx.uk/modules/analytics/metabase/collection/types.ts index cade18fc8d..966449cc31 100644 --- a/api.planx.uk/modules/analytics/metabase/collection/types.ts +++ b/api.planx.uk/modules/analytics/metabase/collection/types.ts @@ -24,7 +24,7 @@ export interface MetabaseCollectionParams { /** Metbase collection ID for the the "Council" collection **/ // const COUNCILS_COLLECTION_ID = 58; -export const checkCollectionsSchema = z.object({ +export const createCollectionIfDoesNotExistSchema = z.object({ body: z .object({ name: z.string(), @@ -39,7 +39,7 @@ export const checkCollectionsSchema = z.object({ }); export type NewCollectionRequestHandler = ValidatedRequestHandler< - typeof checkCollectionsSchema, + typeof createCollectionIfDoesNotExistSchema, ApiResponse >; diff --git a/api.planx.uk/modules/analytics/routes.ts b/api.planx.uk/modules/analytics/routes.ts index 2065837e27..f88633568d 100644 --- a/api.planx.uk/modules/analytics/routes.ts +++ b/api.planx.uk/modules/analytics/routes.ts @@ -5,8 +5,8 @@ import { logUserExitController, logUserResumeController, } from "./analyticsLog/controller.js"; -import { checkCollectionsController } from "./metabase/collection/controller.js"; -import { checkCollectionsSchema } from "./metabase/collection/types.js"; +import { createCollectionIfDoesNotExistController } from "./metabase/collection/controller.js"; +import { createCollectionIfDoesNotExistSchema } from "./metabase/collection/types.js"; const router = Router(); @@ -22,8 +22,8 @@ router.post( ); router.post( "/collection", - validate(checkCollectionsSchema), - checkCollectionsController, + validate(createCollectionIfDoesNotExistSchema), + createCollectionIfDoesNotExistController, ); export default router;