From 8cefdf625be61cf801b6ca76956f2c6ce099a645 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sun, 19 Apr 2026 22:26:47 +0200 Subject: [PATCH] feat(38-01): schema parentId + tag service CRUD + cycle detection - Add parentId self-ref FK to tags table (ON DELETE SET NULL) - Generate Drizzle migration 0010_yielding_random.sql - Extend tag.service.ts with getAdminTags, getTagWithCounts, createTag, updateTag, deleteTag, isDescendant - Add service tests (14 tests, all pass) --- drizzle-pg/0010_yielding_random.sql | 2 + src/db/schema.ts | 1 + src/server/services/tag.service.ts | 92 ++++++++++++++++- tests/services/tag.service.test.ts | 153 +++++++++++++++++++++++++++- 4 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 drizzle-pg/0010_yielding_random.sql diff --git a/drizzle-pg/0010_yielding_random.sql b/drizzle-pg/0010_yielding_random.sql new file mode 100644 index 0000000..b0e2455 --- /dev/null +++ b/drizzle-pg/0010_yielding_random.sql @@ -0,0 +1,2 @@ +ALTER TABLE "tags" ADD COLUMN "parent_id" integer;--> statement-breakpoint +ALTER TABLE "tags" ADD CONSTRAINT "tags_parent_id_tags_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."tags"("id") ON DELETE set null ON UPDATE no action; \ No newline at end of file diff --git a/src/db/schema.ts b/src/db/schema.ts index fe299f0..a851e39 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -204,6 +204,7 @@ export const globalItems = pgTable( export const tags = pgTable("tags", { id: serial("id").primaryKey(), name: text("name").notNull().unique(), + parentId: integer("parent_id").references(() => tags.id, { onDelete: "set null" }), createdAt: timestamp("created_at").defaultNow().notNull(), }); diff --git a/src/server/services/tag.service.ts b/src/server/services/tag.service.ts index 17e5f6c..6fef4be 100644 --- a/src/server/services/tag.service.ts +++ b/src/server/services/tag.service.ts @@ -1,6 +1,6 @@ -import { asc } from "drizzle-orm"; +import { asc, count, eq } from "drizzle-orm"; import { db as prodDb } from "../../db/index.ts"; -import { tags } from "../../db/schema.ts"; +import { globalItemTags, tags } from "../../db/schema.ts"; type Db = typeof prodDb; @@ -10,3 +10,91 @@ export async function getAllTags(db: Db = prodDb) { .from(tags) .orderBy(asc(tags.name)); } + +export async function getAdminTags(db: Db = prodDb) { + return db + .select({ + id: tags.id, + name: tags.name, + parentId: tags.parentId, + itemCount: count(globalItemTags.globalItemId), + }) + .from(tags) + .leftJoin(globalItemTags, eq(globalItemTags.tagId, tags.id)) + .groupBy(tags.id, tags.name, tags.parentId) + .orderBy(asc(tags.name)); +} + +export async function getTagWithCounts(db: Db, id: number) { + const [tag] = await db + .select({ + id: tags.id, + name: tags.name, + parentId: tags.parentId, + itemCount: count(globalItemTags.globalItemId), + }) + .from(tags) + .leftJoin(globalItemTags, eq(globalItemTags.tagId, tags.id)) + .where(eq(tags.id, id)) + .groupBy(tags.id, tags.name, tags.parentId); + return tag ?? null; +} + +export async function createTag( + db: Db, + data: { name: string; parentId?: number | null }, +) { + const [tag] = await db + .insert(tags) + .values({ name: data.name, parentId: data.parentId ?? null }) + .returning(); + return tag!; +} + +function isDescendant( + allTags: { id: number; parentId: number | null }[], + candidateParentId: number, + tagId: number, +): boolean { + let current: number | null = candidateParentId; + const visited = new Set(); + while (current !== null) { + if (current === tagId) return true; + if (visited.has(current)) break; + visited.add(current); + const node = allTags.find((t) => t.id === current); + current = node?.parentId ?? null; + } + return false; +} + +export async function updateTag( + db: Db, + id: number, + data: { name?: string; parentId?: number | null }, +) { + if (data.parentId != null) { + const allTags = await db + .select({ id: tags.id, parentId: tags.parentId }) + .from(tags); + if (isDescendant(allTags, data.parentId, id)) { + throw new Error( + "Cycle detected: the selected parent is a descendant of this tag.", + ); + } + } + const [updated] = await db + .update(tags) + .set({ ...(data.name && { name: data.name }), parentId: data.parentId }) + .where(eq(tags.id, id)) + .returning(); + return updated ?? null; +} + +export async function deleteTag(db: Db, id: number) { + const [deleted] = await db + .delete(tags) + .where(eq(tags.id, id)) + .returning({ id: tags.id }); + return deleted != null; +} diff --git a/tests/services/tag.service.test.ts b/tests/services/tag.service.test.ts index 50ee4d3..37fba06 100644 --- a/tests/services/tag.service.test.ts +++ b/tests/services/tag.service.test.ts @@ -1,8 +1,27 @@ import { beforeEach, describe, expect, it } from "bun:test"; import { tags } from "../../src/db/schema.ts"; -import { getAllTags } from "../../src/server/services/tag.service.ts"; +import { + createTag, + deleteTag, + getAdminTags, + getAllTags, + getTagWithCounts, + updateTag, +} from "../../src/server/services/tag.service.ts"; import { createTestDb } from "../helpers/db.ts"; +async function insertTag( + db: any, + name: string, + parentId?: number | null, +) { + const [row] = await db + .insert(tags) + .values({ name, parentId: parentId ?? null }) + .returning(); + return row!; +} + describe("Tag Service", () => { let db: Awaited>["db"]; @@ -34,3 +53,135 @@ describe("Tag Service", () => { expect(result[0]).toEqual({ id: expect.any(Number), name: "accessories" }); }); }); + +describe("getAdminTags", () => { + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + }); + + it("returns tags with parentId and itemCount fields", async () => { + await insertTag(db, "bikepacking"); + const result = await getAdminTags(db); + expect(result).toHaveLength(1); + expect(result[0]).toMatchObject({ + id: expect.any(Number), + name: "bikepacking", + parentId: null, + itemCount: expect.any(Number), + }); + }); + + it("returns parentId as null for top-level tags", async () => { + await insertTag(db, "ultralight"); + const result = await getAdminTags(db); + expect(result[0].parentId).toBeNull(); + }); + + it("returns correct parentId for child tags", async () => { + const parent = await insertTag(db, "gear"); + const child = await insertTag(db, "clothing", parent.id); + const result = await getAdminTags(db); + const childResult = result.find((t) => t.id === child.id); + expect(childResult?.parentId).toBe(parent.id); + }); +}); + +describe("createTag", () => { + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + }); + + it("creates a tag without parent and returns it", async () => { + const tag = await createTag(db, { name: "bikepacking" }); + expect(tag).toMatchObject({ + id: expect.any(Number), + name: "bikepacking", + parentId: null, + }); + }); + + it("creates a tag with parentId set to an existing tag id", async () => { + const parent = await createTag(db, { name: "gear" }); + const child = await createTag(db, { name: "clothing", parentId: parent.id }); + expect(child.parentId).toBe(parent.id); + }); +}); + +describe("updateTag / cycle detection", () => { + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + }); + + it("renames a tag", async () => { + const tag = await insertTag(db, "old-name"); + const updated = await updateTag(db, tag.id, { name: "new-name" }); + expect(updated?.name).toBe("new-name"); + }); + + it("sets parentId on a tag", async () => { + const parent = await insertTag(db, "parent"); + const child = await insertTag(db, "child"); + const updated = await updateTag(db, child.id, { parentId: parent.id }); + expect(updated?.parentId).toBe(parent.id); + }); + + it("sets parentId to null (reparent to top-level)", async () => { + const parent = await insertTag(db, "parent"); + const child = await insertTag(db, "child", parent.id); + const updated = await updateTag(db, child.id, { parentId: null }); + expect(updated?.parentId).toBeNull(); + }); + + it("throws 'Cycle detected' when setting a descendant as parent", async () => { + const a = await insertTag(db, "A"); + const b = await insertTag(db, "B", a.id); + const c = await insertTag(db, "C", b.id); + // Try to set C (a descendant of A) as parent of A — cycle + await expect(updateTag(db, a.id, { parentId: c.id })).rejects.toThrow( + "Cycle detected", + ); + }); +}); + +describe("deleteTag", () => { + let db: Awaited>["db"]; + + beforeEach(async () => { + const testDb = await createTestDb(); + db = testDb.db; + }); + + it("deletes a tag and returns true", async () => { + const tag = await insertTag(db, "bikepacking"); + const result = await deleteTag(db, tag.id); + expect(result).toBe(true); + }); + + it("returns false for a non-existent tag", async () => { + const result = await deleteTag(db, 99999); + expect(result).toBe(false); + }); + + it("deleting a parent causes children parentId to become null", async () => { + const parent = await insertTag(db, "parent"); + const child = await insertTag(db, "child", parent.id); + await deleteTag(db, parent.id); + const [childRow] = await db + .select({ parentId: tags.parentId }) + .from(tags) + .where((t: any) => t.id === child.id); + // Re-fetch child to verify parentId is now null + const adminTags = await getAdminTags(db); + const childResult = adminTags.find((t) => t.id === child.id); + expect(childResult?.parentId).toBeNull(); + }); +});