B3: catalog taxonomy CRUD (categories, vendors, technologies, functions)
Create/rename/deactivate for the four taxonomy tables. Deactivate not delete; slug frozen after creation; FKs ON DELETE RESTRICT. - src/db/schema.ts: four tables + relations (ON DELETE RESTRICT FKs) - src/lib/slug.ts: slugify (lowercase, ASCII-fold, collapse runs) + 6 tests - src/db/catalog.ts: listCatalog (active filter), deactivateCatalog (soft delete), per-entity create*/update* (slug generated at create, frozen on update; update patches only present fields) - src/app/api/dev/catalog/route.ts: GET/POST/PATCH; Zod per entity; DB constraint errors mapped to 409 (dup slug) / 400 (dangling FK) via cause chain - drizzle/0003_whole_crystal.sql Verification: typecheck, lint, 36 unit tests (+6), build, db:migrate all exit 0. Runtime smoke: create -> rename keeps slug -> deactivate hides from pickers but row persists (all four types); partial PATCH preserves omitted fields; validation 400/400/400/409.
This commit is contained in:
+156
-3
@@ -5,9 +5,8 @@
|
||||
* users → roles → services → service_instances → slots → assignments
|
||||
*
|
||||
* Deliberately NOT here (added in later units, do not add "while I'm in the schema"):
|
||||
* - notification_preferences (B2), notification_templates/rules/outbox (later)
|
||||
* - Catalog taxonomy: skill_categories, vendors, technologies, functions,
|
||||
* skills, position_skills, user_skills, skill_assessments, growth_goals (B3)
|
||||
* - notification_templates/rules/outbox (later)
|
||||
* - skills, position_skills, user_skills, skill_assessments, growth_goals (C1-C6)
|
||||
* - service_role_requirements, unavailability, events, notifications outbox
|
||||
*
|
||||
* Conventions (ARCHITECTURE §4.2):
|
||||
@@ -252,6 +251,123 @@ export const assignments = pgTable(
|
||||
],
|
||||
);
|
||||
|
||||
/* ------------------------------------------------------------- catalog */
|
||||
|
||||
/**
|
||||
* Skill taxonomy — five admin-defined tables with zero hardcoded lists
|
||||
* (ARCHITECTURE §4.3). B3 writes the four leaf tables; `skills` (composed from
|
||||
* them) arrives in C1. Integrity rules enforced here: deactivate-only (no hard
|
||||
* delete — `ON DELETE RESTRICT` on every FK), slugs frozen after creation.
|
||||
*/
|
||||
|
||||
export const skillCategories = pgTable(
|
||||
"skill_categories",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
slug: text("slug").unique().notNull(),
|
||||
/** Editable — the display name an admin renames. */
|
||||
name: text("name").notNull(),
|
||||
description: text("description"),
|
||||
colorHex: text("color_hex"),
|
||||
icon: text("icon"),
|
||||
sortOrder: integer("sort_order").notNull().default(0),
|
||||
active: boolean("active").notNull().default(true),
|
||||
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { mode: "date", withTimezone: true }),
|
||||
},
|
||||
(table) => [
|
||||
index("skill_categories_slug_idx").on(table.slug),
|
||||
index("skill_categories_active_idx").on(table.active),
|
||||
],
|
||||
);
|
||||
|
||||
export const vendors = pgTable(
|
||||
"vendors",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
slug: text("slug").unique().notNull(),
|
||||
name: text("name").notNull(),
|
||||
supportUrl: text("support_url"),
|
||||
supportPhone: text("support_phone"),
|
||||
notes: text("notes"),
|
||||
sortOrder: integer("sort_order").notNull().default(0),
|
||||
active: boolean("active").notNull().default(true),
|
||||
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { mode: "date", withTimezone: true }),
|
||||
},
|
||||
(table) => [
|
||||
index("vendors_slug_idx").on(table.slug),
|
||||
index("vendors_active_idx").on(table.active),
|
||||
],
|
||||
);
|
||||
|
||||
export const technologies = pgTable(
|
||||
"technologies",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
slug: text("slug").unique().notNull(),
|
||||
name: text("name").notNull(),
|
||||
vendorId: text("vendor_id")
|
||||
.notNull()
|
||||
.references(() => vendors.id, { onDelete: "restrict" }),
|
||||
categoryId: text("category_id")
|
||||
.notNull()
|
||||
.references(() => skillCategories.id, { onDelete: "restrict" }),
|
||||
modelNumber: text("model_number"),
|
||||
versionLabel: text("version_label"),
|
||||
docsUrl: text("docs_url"),
|
||||
/** Local-date asset tracking, stored as "YYYY-MM-DD" (string mode). */
|
||||
purchasedOn: date("purchased_on", { mode: "string" }),
|
||||
serialNumber: text("serial_number"),
|
||||
retiredAt: timestamp("retired_at", { mode: "date", withTimezone: true }),
|
||||
/** "ATEM Mini Pro → ATEM SDI Pro ISO" migration hint. Plain text (no
|
||||
* self-FK): the pointer is a soft hint validated at the app layer, so a
|
||||
* self-referential FK does not create a circular schema type. */
|
||||
successorTechnologyId: text("successor_technology_id"),
|
||||
sortOrder: integer("sort_order").notNull().default(0),
|
||||
active: boolean("active").notNull().default(true),
|
||||
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { mode: "date", withTimezone: true }),
|
||||
},
|
||||
(table) => [
|
||||
index("technologies_slug_idx").on(table.slug),
|
||||
index("technologies_active_idx").on(table.active),
|
||||
index("technologies_vendor_idx").on(table.vendorId),
|
||||
index("technologies_category_idx").on(table.categoryId),
|
||||
],
|
||||
);
|
||||
|
||||
export const functions = pgTable(
|
||||
"functions",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
slug: text("slug").unique().notNull(),
|
||||
name: text("name").notNull(),
|
||||
/** Functions are grouped, not enumerated — one category each. */
|
||||
categoryId: text("category_id")
|
||||
.notNull()
|
||||
.references(() => skillCategories.id, { onDelete: "restrict" }),
|
||||
description: text("description"),
|
||||
sortOrder: integer("sort_order").notNull().default(0),
|
||||
active: boolean("active").notNull().default(true),
|
||||
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { mode: "date", withTimezone: true }),
|
||||
},
|
||||
(table) => [
|
||||
index("functions_slug_idx").on(table.slug),
|
||||
index("functions_active_idx").on(table.active),
|
||||
index("functions_category_idx").on(table.categoryId),
|
||||
],
|
||||
);
|
||||
|
||||
/* ------------------------------------------------------------- relations */
|
||||
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
@@ -279,6 +395,39 @@ export const assignmentRelations = relations(assignments, ({ one }) => ({
|
||||
user: one(users),
|
||||
}));
|
||||
|
||||
export const skillCategoriesRelations = relations(
|
||||
skillCategories,
|
||||
({ many }) => ({
|
||||
technologies: many(technologies),
|
||||
functions: many(functions),
|
||||
}),
|
||||
);
|
||||
|
||||
export const vendorsRelations = relations(vendors, ({ many }) => ({
|
||||
technologies: many(technologies),
|
||||
}));
|
||||
|
||||
export const technologiesRelations = relations(
|
||||
technologies,
|
||||
({ one }) => ({
|
||||
vendor: one(vendors, { fields: [technologies.vendorId], references: [vendors.id] }),
|
||||
category: one(
|
||||
skillCategories,
|
||||
{ fields: [technologies.categoryId], references: [skillCategories.id] },
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
export const functionsRelations = relations(
|
||||
functions,
|
||||
({ one }) => ({
|
||||
category: one(
|
||||
skillCategories,
|
||||
{ fields: [functions.categoryId], references: [skillCategories.id] },
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* The schema object Drizzle uses for migrations and the query layer.
|
||||
* Phase A2 core only; catalog + downstream tables are added in their own units.
|
||||
@@ -291,6 +440,10 @@ export const schema = {
|
||||
serviceInstances,
|
||||
slots,
|
||||
assignments,
|
||||
skillCategories,
|
||||
vendors,
|
||||
technologies,
|
||||
functions,
|
||||
};
|
||||
|
||||
export type Schema = typeof schema;
|
||||
|
||||
Reference in New Issue
Block a user