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

@@ -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;
}