Merge branch 'worktree-agent-a7e6e4b2' into Develop

# Conflicts:
#	.planning/REQUIREMENTS.md
#	.planning/ROADMAP.md
#	.planning/STATE.md
#	drizzle/meta/_journal.json
#	src/db/schema.ts
#	src/db/seed.ts
#	src/shared/schemas.ts
#	src/shared/types.ts
This commit is contained in:
2026-04-05 13:13:26 +02:00
18 changed files with 1863 additions and 232 deletions

View File

@@ -1,90 +1,58 @@
import {
boolean,
doublePrecision,
integer,
pgTable,
primaryKey,
serial,
text,
timestamp,
unique,
} from "drizzle-orm/pg-core";
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
// ── Users ───────────────────────────────────────────────────────────
export const users = pgTable("users", {
id: serial("id").primaryKey(),
logtoSub: text("logto_sub").notNull().unique(),
displayName: text("display_name"),
avatarUrl: text("avatar_url"),
bio: text("bio"),
createdAt: timestamp("created_at").defaultNow().notNull(),
export const categories = sqliteTable("categories", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull().unique(),
icon: text("icon").notNull().default("package"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── Categories ──────────────────────────────────────────────────────
export const categories = pgTable(
"categories",
{
id: serial("id").primaryKey(),
name: text("name").notNull(),
icon: text("icon").notNull().default("package"),
userId: integer("user_id")
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
},
(table) => [unique().on(table.userId, table.name)],
);
// ── Items ───────────────────────────────────────────────────────────
export const items = pgTable("items", {
id: serial("id").primaryKey(),
export const items = sqliteTable("items", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
weightGrams: doublePrecision("weight_grams"),
weightGrams: real("weight_grams"),
priceCents: integer("price_cents"),
categoryId: integer("category_id")
.notNull()
.references(() => categories.id),
userId: integer("user_id")
.notNull()
.references(() => users.id),
notes: text("notes"),
productUrl: text("product_url"),
imageFilename: text("image_filename"),
imageSourceUrl: text("image_source_url"),
quantity: integer("quantity").notNull().default(1),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── Threads ─────────────────────────────────────────────────────────
export const threads = pgTable("threads", {
id: serial("id").primaryKey(),
export const threads = sqliteTable("threads", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
status: text("status").notNull().default("active"),
resolvedCandidateId: integer("resolved_candidate_id"),
categoryId: integer("category_id")
.notNull()
.references(() => categories.id),
userId: integer("user_id")
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── Thread Candidates ───────────────────────────────────────────────
export const threadCandidates = pgTable("thread_candidates", {
id: serial("id").primaryKey(),
export const threadCandidates = sqliteTable("thread_candidates", {
id: integer("id").primaryKey({ autoIncrement: true }),
threadId: integer("thread_id")
.notNull()
.references(() => threads.id, { onDelete: "cascade" }),
name: text("name").notNull(),
weightGrams: doublePrecision("weight_grams"),
weightGrams: real("weight_grams"),
priceCents: integer("price_cents"),
categoryId: integer("category_id")
.notNull()
@@ -96,28 +64,28 @@ export const threadCandidates = pgTable("thread_candidates", {
status: text("status").notNull().default("researching"),
pros: text("pros"),
cons: text("cons"),
sortOrder: doublePrecision("sort_order").notNull().default(0),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});
// ── Setups ──────────────────────────────────────────────────────────
export const setups = pgTable("setups", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
userId: integer("user_id")
sortOrder: real("sort_order").notNull().default(0),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.references(() => users.id),
isPublic: boolean("is_public").notNull().default(false),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── Setup Items ─────────────────────────────────────────────────────
export const setups = sqliteTable("setups", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const setupItems = pgTable("setup_items", {
id: serial("id").primaryKey(),
export const setupItems = sqliteTable("setup_items", {
id: integer("id").primaryKey({ autoIncrement: true }),
setupId: integer("setup_id")
.notNull()
.references(() => setups.id, { onDelete: "cascade" }),
@@ -127,24 +95,54 @@ export const setupItems = pgTable("setup_items", {
classification: text("classification").notNull().default("base"),
});
// ── Global Items ────────────────────────────────────────────────────
export const settings = sqliteTable("settings", {
key: text("key").primaryKey(),
value: text("value").notNull(),
});
export const globalItems = pgTable("global_items", {
id: serial("id").primaryKey(),
export const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
username: text("username").notNull().unique(),
passwordHash: text("password_hash").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const sessions = sqliteTable("sessions", {
id: text("id").primaryKey(),
userId: integer("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
});
export const apiKeys = sqliteTable("api_keys", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
keyHash: text("key_hash").notNull(),
keyPrefix: text("key_prefix").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const globalItems = sqliteTable("global_items", {
id: integer("id").primaryKey({ autoIncrement: true }),
brand: text("brand").notNull(),
model: text("model").notNull(),
category: text("category"),
weightGrams: doublePrecision("weight_grams"),
weightGrams: real("weight_grams"),
priceCents: integer("price_cents"),
imageUrl: text("image_url"),
description: text("description"),
createdAt: timestamp("created_at").defaultNow().notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── Item Global Links ───────────────────────────────────────────────
export const itemGlobalLinks = pgTable("item_global_links", {
id: serial("id").primaryKey(),
export const itemGlobalLinks = sqliteTable("item_global_links", {
id: integer("id").primaryKey({ autoIncrement: true }),
itemId: integer("item_id")
.notNull()
.references(() => items.id, { onDelete: "cascade" })
@@ -154,67 +152,37 @@ export const itemGlobalLinks = pgTable("item_global_links", {
.references(() => globalItems.id, { onDelete: "cascade" }),
});
// ── Settings ────────────────────────────────────────────────────────
export const settings = pgTable(
"settings",
{
userId: integer("user_id")
.notNull()
.references(() => users.id),
key: text("key").notNull(),
value: text("value").notNull(),
},
(table) => [primaryKey({ columns: [table.userId, table.key] })],
);
// ── API Keys ────────────────────────────────────────────────────────
export const apiKeys = pgTable("api_keys", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
keyHash: text("key_hash").notNull(),
keyPrefix: text("key_prefix").notNull(),
userId: integer("user_id")
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
// ── OAuth Clients ───────────────────────────────────────────────────
export const oauthClients = pgTable("oauth_clients", {
id: serial("id").primaryKey(),
export const oauthClients = sqliteTable("oauth_clients", {
id: integer("id").primaryKey({ autoIncrement: true }),
clientId: text("client_id").notNull().unique(),
clientName: text("client_name"),
redirectUris: text("redirect_uris").notNull(), // JSON array
createdAt: timestamp("created_at").defaultNow().notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
// ── OAuth Authorization Codes ───────────────────────────────────────
export const oauthCodes = pgTable("oauth_codes", {
id: serial("id").primaryKey(),
export const oauthCodes = sqliteTable("oauth_codes", {
id: integer("id").primaryKey({ autoIncrement: true }),
code: text("code").notNull().unique(),
clientId: text("client_id").notNull(),
codeChallenge: text("code_challenge").notNull(),
codeChallengeMethod: text("code_challenge_method").notNull().default("S256"),
redirectUri: text("redirect_uri").notNull(),
expiresAt: timestamp("expires_at").notNull(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
used: integer("used").notNull().default(0),
});
// ── OAuth Tokens ────────────────────────────────────────────────────
export const oauthTokens = pgTable("oauth_tokens", {
id: serial("id").primaryKey(),
export const oauthTokens = sqliteTable("oauth_tokens", {
id: integer("id").primaryKey({ autoIncrement: true }),
accessTokenHash: text("access_token_hash").notNull().unique(),
refreshTokenHash: text("refresh_token_hash").notNull().unique(),
clientId: text("client_id").notNull(),
userId: integer("user_id")
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), // access token expiry
refreshExpiresAt: integer("refresh_expires_at", {
mode: "timestamp",
}).notNull(), // refresh token expiry
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.references(() => users.id),
expiresAt: timestamp("expires_at").notNull(),
refreshExpiresAt: timestamp("refresh_expires_at").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
.$defaultFn(() => new Date()),
});

View File

@@ -0,0 +1,27 @@
import { db as prodDb } from "./index.ts";
import { globalItems } from "./schema.ts";
import seedData from "./global-items-seed.json";
type Db = typeof prodDb;
/**
* Seed the global items table with initial bikepacking gear data.
* Idempotent: skips if any rows already exist.
*/
export function seedGlobalItems(db: Db = prodDb) {
const existing = db.select().from(globalItems).limit(1).all();
if (existing.length > 0) return;
for (const item of seedData) {
db.insert(globalItems)
.values({
brand: item.brand,
model: item.model,
category: item.category ?? null,
weightGrams: item.weightGrams ?? null,
priceCents: item.priceCents ?? null,
description: item.description ?? null,
})
.run();
}
}

View File

@@ -1,4 +1,18 @@
import { db } from "./index.ts";
import { categories } from "./schema.ts";
import { seedGlobalItems } from "./seed-global-items.ts";
export function seedDefaults() {
// Per-user default categories are created on first login (Phase 16)
// The getOrCreateUncategorized helper in category.service.ts handles this lazily.
const existing = db.select().from(categories).all();
if (existing.length === 0) {
db.insert(categories)
.values({
name: "Uncategorized",
icon: "package",
})
.run();
}
// Seed global items catalog
seedGlobalItems(db);
}

View File

@@ -13,6 +13,7 @@ import { requireAuth } from "./middleware/auth.ts";
import { authRoutes } from "./routes/auth.ts";
import { categoryRoutes } from "./routes/categories.ts";
import { imageRoutes } from "./routes/images.ts";
import { globalItemRoutes } from "./routes/global-items.ts";
import { itemRoutes } from "./routes/items.ts";
import { oauthRoutes, wellKnownRoute } from "./routes/oauth.ts";
import { settingsRoutes } from "./routes/settings.ts";
@@ -86,6 +87,7 @@ app.route("/api/images", imageRoutes);
app.route("/api/settings", settingsRoutes);
app.route("/api/threads", threadRoutes);
app.route("/api/setups", setupRoutes);
app.route("/api/global-items", globalItemRoutes);
// MCP server (conditionally mounted)
if (process.env.GEARBOX_MCP !== "false") {

View File

@@ -0,0 +1,30 @@
import { Hono } from "hono";
import { parseId } from "../lib/params.ts";
import {
getGlobalItemWithOwnerCount,
searchGlobalItems,
} from "../services/global-item.service.ts";
type Env = { Variables: { db?: any } };
const app = new Hono<Env>();
app.get("/", (c) => {
const db = c.get("db");
const q = c.req.query("q");
const items = searchGlobalItems(db, q || undefined);
return c.json(items);
});
app.get("/:id", (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid global item ID" }, 400);
const item = getGlobalItemWithOwnerCount(db, id);
if (!item) return c.json({ error: "Global item not found" }, 404);
return c.json(item);
});
export { app as globalItemRoutes };

View File

@@ -1,8 +1,16 @@
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { createItemSchema, updateItemSchema } from "../../shared/schemas.ts";
import {
createItemSchema,
linkItemSchema,
updateItemSchema,
} from "../../shared/schemas.ts";
import { parseId } from "../lib/params.ts";
import { exportItemsCsv, importItemsCsv } from "../services/csv.service.ts";
import {
linkItemToGlobal,
unlinkItemFromGlobal,
} from "../services/global-item.service.ts";
import {
createItem,
deleteItem,
@@ -114,4 +122,32 @@ app.delete("/:id", async (c) => {
return c.json({ success: true });
});
app.post("/:id/link", zValidator("json", linkItemSchema), (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid item ID" }, 400);
const item = getItemById(db, id);
if (!item) return c.json({ error: "Item not found" }, 404);
try {
const link = linkItemToGlobal(db, id, c.req.valid("json").globalItemId);
return c.json(link, 201);
} catch {
return c.json({ error: "Item already linked to a global item" }, 409);
}
});
app.delete("/:id/link", (c) => {
const db = c.get("db");
const id = parseId(c.req.param("id"));
if (!id) return c.json({ error: "Invalid item ID" }, 400);
const item = getItemById(db, id);
if (!item) return c.json({ error: "Item not found" }, 404);
unlinkItemFromGlobal(db, id);
return c.json({ success: true });
});
export { app as itemRoutes };

View File

@@ -0,0 +1,79 @@
import { count, eq, like, or, sql } from "drizzle-orm";
import { db as prodDb } from "../../db/index.ts";
import { globalItems, itemGlobalLinks } from "../../db/schema.ts";
type Db = typeof prodDb;
/**
* Search global items by brand or model. SQLite LIKE is case-insensitive for ASCII.
* Escapes % and _ wildcard characters in user input.
*/
export function searchGlobalItems(db: Db = prodDb, query?: string) {
if (!query) {
return db.select().from(globalItems).all();
}
// Escape SQL LIKE wildcards
const escaped = query.replace(/%/g, "\\%").replace(/_/g, "\\_");
const pattern = `%${escaped}%`;
return db
.select()
.from(globalItems)
.where(
or(
like(globalItems.brand, pattern),
like(globalItems.model, pattern),
),
)
.all();
}
/**
* Get a single global item by ID with the count of user items linked to it.
*/
export function getGlobalItemWithOwnerCount(db: Db = prodDb, id: number) {
const item = db
.select()
.from(globalItems)
.where(eq(globalItems.id, id))
.get();
if (!item) return null;
const result = db
.select({ ownerCount: count() })
.from(itemGlobalLinks)
.where(eq(itemGlobalLinks.globalItemId, id))
.get();
return { ...item, ownerCount: result?.ownerCount ?? 0 };
}
/**
* Link a user's item to a global item. Throws on duplicate (unique constraint on itemId).
*/
export function linkItemToGlobal(
db: Db = prodDb,
itemId: number,
globalItemId: number,
) {
return db
.insert(itemGlobalLinks)
.values({ itemId, globalItemId })
.returning()
.get();
}
/**
* Remove the link between a user's item and any global item.
*/
export function unlinkItemFromGlobal(db: Db = prodDb, itemId: number) {
const result = db
.delete(itemGlobalLinks)
.where(eq(itemGlobalLinks.itemId, itemId))
.returning()
.all();
return result.length;
}

View File

@@ -73,12 +73,10 @@ export const reorderCandidatesSchema = z.object({
// Setup schemas
export const createSetupSchema = z.object({
name: z.string().min(1, "Setup name is required"),
isPublic: z.boolean().optional().default(false),
});
export const updateSetupSchema = z.object({
name: z.string().min(1, "Setup name is required"),
isPublic: z.boolean().optional(),
});
export const syncSetupItemsSchema = z.object({
@@ -100,10 +98,3 @@ export const searchGlobalItemsSchema = z.object({
export const linkItemSchema = z.object({
globalItemId: z.number().int().positive(),
});
// Profile schemas
export const updateProfileSchema = z.object({
displayName: z.string().max(100).optional(),
avatarUrl: z.string().optional(),
bio: z.string().max(500).optional(),
});

View File

@@ -24,7 +24,6 @@ import type {
updateCategorySchema,
updateClassificationSchema,
updateItemSchema,
updateProfileSchema,
updateSetupSchema,
updateThreadSchema,
} from "./schemas.ts";
@@ -47,11 +46,6 @@ export type UpdateSetup = z.infer<typeof updateSetupSchema>;
export type SyncSetupItems = z.infer<typeof syncSetupItemsSchema>;
export type UpdateClassification = z.infer<typeof updateClassificationSchema>;
// Global item types
export type SearchGlobalItems = z.infer<typeof searchGlobalItemsSchema>;
export type LinkItem = z.infer<typeof linkItemSchema>;
export type UpdateProfile = z.infer<typeof updateProfileSchema>;
// Types inferred from Drizzle schema
export type Item = typeof items.$inferSelect;
export type Category = typeof categories.$inferSelect;
@@ -61,3 +55,5 @@ export type Setup = typeof setups.$inferSelect;
export type SetupItem = typeof setupItems.$inferSelect;
export type GlobalItem = typeof globalItems.$inferSelect;
export type ItemGlobalLink = typeof itemGlobalLinks.$inferSelect;
export type SearchGlobalItems = z.infer<typeof searchGlobalItemsSchema>;
export type LinkItem = z.infer<typeof linkItemSchema>;