Skip to content

Commit

Permalink
chore: rename to createCollectionIfDoesNotExist for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-hh-aa committed Dec 11, 2024
1 parent ecca6d6 commit 7203db0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
});
Expand Down Expand Up @@ -247,7 +247,7 @@ describe("edge cases", () => {

test("handles missing name", async () => {
await expect(
checkCollections({
createCollectionIfDoesNotExist({
name: "",
}),
).rejects.toThrow();
Expand Down Expand Up @@ -288,7 +288,7 @@ describe("edge cases", () => {
});

await expect(
checkCollections({
createCollectionIfDoesNotExist({
name: longName,
}),
).rejects.toThrow();
Expand Down
32 changes: 16 additions & 16 deletions api.planx.uk/modules/analytics/metabase/collection/controller.ts
Original file line number Diff line number Diff line change
@@ -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",
});
}
};
4 changes: 2 additions & 2 deletions api.planx.uk/modules/analytics/metabase/collection/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
try {
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions api.planx.uk/modules/analytics/metabase/collection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -39,7 +39,7 @@ export const checkCollectionsSchema = z.object({
});

export type NewCollectionRequestHandler = ValidatedRequestHandler<
typeof checkCollectionsSchema,
typeof createCollectionIfDoesNotExistSchema,
ApiResponse<NewCollectionResponse>
>;

Expand Down
8 changes: 4 additions & 4 deletions api.planx.uk/modules/analytics/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -22,8 +22,8 @@ router.post(
);
router.post(
"/collection",
validate(checkCollectionsSchema),
checkCollectionsController,
validate(createCollectionIfDoesNotExistSchema),
createCollectionIfDoesNotExistController,
);

export default router;

0 comments on commit 7203db0

Please sign in to comment.