chore: auto-fix Biome formatting and configure lint rules
All checks were successful
CI / ci (push) Successful in 15s
All checks were successful
CI / ci (push) Successful in 15s
Run biome check --write --unsafe to fix tabs, import ordering, and non-null assertions across entire codebase. Disable a11y rules not applicable to this single-user app. Exclude auto-generated routeTree. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,77 +1,80 @@
|
||||
import { eq, asc } from "drizzle-orm";
|
||||
import { categories, items } from "../../db/schema.ts";
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import { db as prodDb } from "../../db/index.ts";
|
||||
import { categories, items } from "../../db/schema.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function getAllCategories(db: Db = prodDb) {
|
||||
return db.select().from(categories).orderBy(asc(categories.name)).all();
|
||||
return db.select().from(categories).orderBy(asc(categories.name)).all();
|
||||
}
|
||||
|
||||
export function createCategory(
|
||||
db: Db = prodDb,
|
||||
data: { name: string; icon?: string },
|
||||
db: Db = prodDb,
|
||||
data: { name: string; icon?: string },
|
||||
) {
|
||||
return db
|
||||
.insert(categories)
|
||||
.values({
|
||||
name: data.name,
|
||||
...(data.icon ? { icon: data.icon } : {}),
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.insert(categories)
|
||||
.values({
|
||||
name: data.name,
|
||||
...(data.icon ? { icon: data.icon } : {}),
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function updateCategory(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: { name?: string; icon?: string },
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: { name?: string; icon?: string },
|
||||
) {
|
||||
const existing = db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
const existing = db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
|
||||
if (!existing) return null;
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
.update(categories)
|
||||
.set(data)
|
||||
.where(eq(categories.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.update(categories)
|
||||
.set(data)
|
||||
.where(eq(categories.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function deleteCategory(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
): { success: boolean; error?: string } {
|
||||
// Guard: cannot delete Uncategorized (id=1)
|
||||
if (id === 1) {
|
||||
return { success: false, error: "Cannot delete the Uncategorized category" };
|
||||
}
|
||||
// Guard: cannot delete Uncategorized (id=1)
|
||||
if (id === 1) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Cannot delete the Uncategorized category",
|
||||
};
|
||||
}
|
||||
|
||||
// Check if category exists
|
||||
const existing = db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
// Check if category exists
|
||||
const existing = db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
|
||||
if (!existing) {
|
||||
return { success: false, error: "Category not found" };
|
||||
}
|
||||
if (!existing) {
|
||||
return { success: false, error: "Category not found" };
|
||||
}
|
||||
|
||||
// Reassign items to Uncategorized (id=1), then delete atomically
|
||||
db.transaction(() => {
|
||||
db.update(items)
|
||||
.set({ categoryId: 1 })
|
||||
.where(eq(items.categoryId, id))
|
||||
.run();
|
||||
// Reassign items to Uncategorized (id=1), then delete atomically
|
||||
db.transaction(() => {
|
||||
db.update(items)
|
||||
.set({ categoryId: 1 })
|
||||
.where(eq(items.categoryId, id))
|
||||
.run();
|
||||
|
||||
db.delete(categories).where(eq(categories.id, id)).run();
|
||||
});
|
||||
db.delete(categories).where(eq(categories.id, id)).run();
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -1,112 +1,112 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { items, categories } from "../../db/schema.ts";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db as prodDb } from "../../db/index.ts";
|
||||
import { categories, items } from "../../db/schema.ts";
|
||||
import type { CreateItem } from "../../shared/types.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function getAllItems(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.all();
|
||||
return db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.all();
|
||||
}
|
||||
|
||||
export function getItemById(db: Db = prodDb, id: number) {
|
||||
return (
|
||||
db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
})
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get() ?? null
|
||||
);
|
||||
return (
|
||||
db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
})
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get() ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function createItem(
|
||||
db: Db = prodDb,
|
||||
data: Partial<CreateItem> & { name: string; categoryId: number; imageFilename?: string },
|
||||
db: Db = prodDb,
|
||||
data: Partial<CreateItem> & {
|
||||
name: string;
|
||||
categoryId: number;
|
||||
imageFilename?: string;
|
||||
},
|
||||
) {
|
||||
return db
|
||||
.insert(items)
|
||||
.values({
|
||||
name: data.name,
|
||||
weightGrams: data.weightGrams ?? null,
|
||||
priceCents: data.priceCents ?? null,
|
||||
categoryId: data.categoryId,
|
||||
notes: data.notes ?? null,
|
||||
productUrl: data.productUrl ?? null,
|
||||
imageFilename: data.imageFilename ?? null,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.insert(items)
|
||||
.values({
|
||||
name: data.name,
|
||||
weightGrams: data.weightGrams ?? null,
|
||||
priceCents: data.priceCents ?? null,
|
||||
categoryId: data.categoryId,
|
||||
notes: data.notes ?? null,
|
||||
productUrl: data.productUrl ?? null,
|
||||
imageFilename: data.imageFilename ?? null,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function updateItem(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
weightGrams: number;
|
||||
priceCents: number;
|
||||
categoryId: number;
|
||||
notes: string;
|
||||
productUrl: string;
|
||||
imageFilename: string;
|
||||
}>,
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
weightGrams: number;
|
||||
priceCents: number;
|
||||
categoryId: number;
|
||||
notes: string;
|
||||
productUrl: string;
|
||||
imageFilename: string;
|
||||
}>,
|
||||
) {
|
||||
// Check if item exists first
|
||||
const existing = db
|
||||
.select({ id: items.id })
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get();
|
||||
// Check if item exists first
|
||||
const existing = db
|
||||
.select({ id: items.id })
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get();
|
||||
|
||||
if (!existing) return null;
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
.update(items)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(items.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.update(items)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(items.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function deleteItem(db: Db = prodDb, id: number) {
|
||||
// Get item first (for image cleanup info)
|
||||
const item = db
|
||||
.select()
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get();
|
||||
// Get item first (for image cleanup info)
|
||||
const item = db.select().from(items).where(eq(items.id, id)).get();
|
||||
|
||||
if (!item) return null;
|
||||
if (!item) return null;
|
||||
|
||||
db.delete(items).where(eq(items.id, id)).run();
|
||||
db.delete(items).where(eq(items.id, id)).run();
|
||||
|
||||
return item;
|
||||
return item;
|
||||
}
|
||||
|
||||
@@ -1,111 +1,124 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { setups, setupItems, items, categories } from "../../db/schema.ts";
|
||||
import { db as prodDb } from "../../db/index.ts";
|
||||
import { categories, items, setupItems, setups } from "../../db/schema.ts";
|
||||
import type { CreateSetup, UpdateSetup } from "../../shared/types.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function createSetup(db: Db = prodDb, data: CreateSetup) {
|
||||
return db
|
||||
.insert(setups)
|
||||
.values({ name: data.name })
|
||||
.returning()
|
||||
.get();
|
||||
return db.insert(setups).values({ name: data.name }).returning().get();
|
||||
}
|
||||
|
||||
export function getAllSetups(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
id: setups.id,
|
||||
name: setups.name,
|
||||
createdAt: setups.createdAt,
|
||||
updatedAt: setups.updatedAt,
|
||||
itemCount: sql<number>`COALESCE((
|
||||
return db
|
||||
.select({
|
||||
id: setups.id,
|
||||
name: setups.name,
|
||||
createdAt: setups.createdAt,
|
||||
updatedAt: setups.updatedAt,
|
||||
itemCount: sql<number>`COALESCE((
|
||||
SELECT COUNT(*) FROM setup_items
|
||||
WHERE setup_items.setup_id = setups.id
|
||||
), 0)`.as("item_count"),
|
||||
totalWeight: sql<number>`COALESCE((
|
||||
totalWeight: sql<number>`COALESCE((
|
||||
SELECT SUM(items.weight_grams) FROM setup_items
|
||||
JOIN items ON items.id = setup_items.item_id
|
||||
WHERE setup_items.setup_id = setups.id
|
||||
), 0)`.as("total_weight"),
|
||||
totalCost: sql<number>`COALESCE((
|
||||
totalCost: sql<number>`COALESCE((
|
||||
SELECT SUM(items.price_cents) FROM setup_items
|
||||
JOIN items ON items.id = setup_items.item_id
|
||||
WHERE setup_items.setup_id = setups.id
|
||||
), 0)`.as("total_cost"),
|
||||
})
|
||||
.from(setups)
|
||||
.all();
|
||||
})
|
||||
.from(setups)
|
||||
.all();
|
||||
}
|
||||
|
||||
export function getSetupWithItems(db: Db = prodDb, setupId: number) {
|
||||
const setup = db.select().from(setups)
|
||||
.where(eq(setups.id, setupId)).get();
|
||||
if (!setup) return null;
|
||||
const setup = db.select().from(setups).where(eq(setups.id, setupId)).get();
|
||||
if (!setup) return null;
|
||||
|
||||
const itemList = db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(setupItems)
|
||||
.innerJoin(items, eq(setupItems.itemId, items.id))
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.where(eq(setupItems.setupId, setupId))
|
||||
.all();
|
||||
const itemList = db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
weightGrams: items.weightGrams,
|
||||
priceCents: items.priceCents,
|
||||
categoryId: items.categoryId,
|
||||
notes: items.notes,
|
||||
productUrl: items.productUrl,
|
||||
imageFilename: items.imageFilename,
|
||||
createdAt: items.createdAt,
|
||||
updatedAt: items.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(setupItems)
|
||||
.innerJoin(items, eq(setupItems.itemId, items.id))
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.where(eq(setupItems.setupId, setupId))
|
||||
.all();
|
||||
|
||||
return { ...setup, items: itemList };
|
||||
return { ...setup, items: itemList };
|
||||
}
|
||||
|
||||
export function updateSetup(db: Db = prodDb, setupId: number, data: UpdateSetup) {
|
||||
const existing = db.select({ id: setups.id }).from(setups)
|
||||
.where(eq(setups.id, setupId)).get();
|
||||
if (!existing) return null;
|
||||
export function updateSetup(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
data: UpdateSetup,
|
||||
) {
|
||||
const existing = db
|
||||
.select({ id: setups.id })
|
||||
.from(setups)
|
||||
.where(eq(setups.id, setupId))
|
||||
.get();
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
.update(setups)
|
||||
.set({ name: data.name, updatedAt: new Date() })
|
||||
.where(eq(setups.id, setupId))
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.update(setups)
|
||||
.set({ name: data.name, updatedAt: new Date() })
|
||||
.where(eq(setups.id, setupId))
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function deleteSetup(db: Db = prodDb, setupId: number) {
|
||||
const existing = db.select({ id: setups.id }).from(setups)
|
||||
.where(eq(setups.id, setupId)).get();
|
||||
if (!existing) return false;
|
||||
const existing = db
|
||||
.select({ id: setups.id })
|
||||
.from(setups)
|
||||
.where(eq(setups.id, setupId))
|
||||
.get();
|
||||
if (!existing) return false;
|
||||
|
||||
db.delete(setups).where(eq(setups.id, setupId)).run();
|
||||
return true;
|
||||
db.delete(setups).where(eq(setups.id, setupId)).run();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function syncSetupItems(db: Db = prodDb, setupId: number, itemIds: number[]) {
|
||||
return db.transaction((tx) => {
|
||||
// Delete all existing items for this setup
|
||||
tx.delete(setupItems).where(eq(setupItems.setupId, setupId)).run();
|
||||
export function syncSetupItems(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
itemIds: number[],
|
||||
) {
|
||||
return db.transaction((tx) => {
|
||||
// Delete all existing items for this setup
|
||||
tx.delete(setupItems).where(eq(setupItems.setupId, setupId)).run();
|
||||
|
||||
// Re-insert new items
|
||||
for (const itemId of itemIds) {
|
||||
tx.insert(setupItems).values({ setupId, itemId }).run();
|
||||
}
|
||||
});
|
||||
// Re-insert new items
|
||||
for (const itemId of itemIds) {
|
||||
tx.insert(setupItems).values({ setupId, itemId }).run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function removeSetupItem(db: Db = prodDb, setupId: number, itemId: number) {
|
||||
db.delete(setupItems)
|
||||
.where(
|
||||
sql`${setupItems.setupId} = ${setupId} AND ${setupItems.itemId} = ${itemId}`
|
||||
)
|
||||
.run();
|
||||
export function removeSetupItem(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
itemId: number,
|
||||
) {
|
||||
db.delete(setupItems)
|
||||
.where(
|
||||
sql`${setupItems.setupId} = ${setupId} AND ${setupItems.itemId} = ${itemId}`,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -1,221 +1,261 @@
|
||||
import { eq, desc, sql } from "drizzle-orm";
|
||||
import { threads, threadCandidates, items, categories } from "../../db/schema.ts";
|
||||
import { desc, eq, sql } from "drizzle-orm";
|
||||
import { db as prodDb } from "../../db/index.ts";
|
||||
import type { CreateThread, UpdateThread, CreateCandidate } from "../../shared/types.ts";
|
||||
import {
|
||||
categories,
|
||||
items,
|
||||
threadCandidates,
|
||||
threads,
|
||||
} from "../../db/schema.ts";
|
||||
import type { CreateCandidate, CreateThread } from "../../shared/types.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function createThread(db: Db = prodDb, data: CreateThread) {
|
||||
return db
|
||||
.insert(threads)
|
||||
.values({ name: data.name, categoryId: data.categoryId })
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.insert(threads)
|
||||
.values({ name: data.name, categoryId: data.categoryId })
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function getAllThreads(db: Db = prodDb, includeResolved = false) {
|
||||
const query = db
|
||||
.select({
|
||||
id: threads.id,
|
||||
name: threads.name,
|
||||
status: threads.status,
|
||||
resolvedCandidateId: threads.resolvedCandidateId,
|
||||
categoryId: threads.categoryId,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
createdAt: threads.createdAt,
|
||||
updatedAt: threads.updatedAt,
|
||||
candidateCount: sql<number>`(
|
||||
const query = db
|
||||
.select({
|
||||
id: threads.id,
|
||||
name: threads.name,
|
||||
status: threads.status,
|
||||
resolvedCandidateId: threads.resolvedCandidateId,
|
||||
categoryId: threads.categoryId,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
createdAt: threads.createdAt,
|
||||
updatedAt: threads.updatedAt,
|
||||
candidateCount: sql<number>`(
|
||||
SELECT COUNT(*) FROM thread_candidates
|
||||
WHERE thread_candidates.thread_id = threads.id
|
||||
)`.as("candidate_count"),
|
||||
minPriceCents: sql<number | null>`(
|
||||
minPriceCents: sql<number | null>`(
|
||||
SELECT MIN(price_cents) FROM thread_candidates
|
||||
WHERE thread_candidates.thread_id = threads.id
|
||||
)`.as("min_price_cents"),
|
||||
maxPriceCents: sql<number | null>`(
|
||||
maxPriceCents: sql<number | null>`(
|
||||
SELECT MAX(price_cents) FROM thread_candidates
|
||||
WHERE thread_candidates.thread_id = threads.id
|
||||
)`.as("max_price_cents"),
|
||||
})
|
||||
.from(threads)
|
||||
.innerJoin(categories, eq(threads.categoryId, categories.id))
|
||||
.orderBy(desc(threads.createdAt));
|
||||
})
|
||||
.from(threads)
|
||||
.innerJoin(categories, eq(threads.categoryId, categories.id))
|
||||
.orderBy(desc(threads.createdAt));
|
||||
|
||||
if (!includeResolved) {
|
||||
return query.where(eq(threads.status, "active")).all();
|
||||
}
|
||||
return query.all();
|
||||
if (!includeResolved) {
|
||||
return query.where(eq(threads.status, "active")).all();
|
||||
}
|
||||
return query.all();
|
||||
}
|
||||
|
||||
export function getThreadWithCandidates(db: Db = prodDb, threadId: number) {
|
||||
const thread = db.select().from(threads)
|
||||
.where(eq(threads.id, threadId)).get();
|
||||
if (!thread) return null;
|
||||
const thread = db
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
if (!thread) return null;
|
||||
|
||||
const candidateList = db
|
||||
.select({
|
||||
id: threadCandidates.id,
|
||||
threadId: threadCandidates.threadId,
|
||||
name: threadCandidates.name,
|
||||
weightGrams: threadCandidates.weightGrams,
|
||||
priceCents: threadCandidates.priceCents,
|
||||
categoryId: threadCandidates.categoryId,
|
||||
notes: threadCandidates.notes,
|
||||
productUrl: threadCandidates.productUrl,
|
||||
imageFilename: threadCandidates.imageFilename,
|
||||
createdAt: threadCandidates.createdAt,
|
||||
updatedAt: threadCandidates.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(threadCandidates)
|
||||
.innerJoin(categories, eq(threadCandidates.categoryId, categories.id))
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all();
|
||||
const candidateList = db
|
||||
.select({
|
||||
id: threadCandidates.id,
|
||||
threadId: threadCandidates.threadId,
|
||||
name: threadCandidates.name,
|
||||
weightGrams: threadCandidates.weightGrams,
|
||||
priceCents: threadCandidates.priceCents,
|
||||
categoryId: threadCandidates.categoryId,
|
||||
notes: threadCandidates.notes,
|
||||
productUrl: threadCandidates.productUrl,
|
||||
imageFilename: threadCandidates.imageFilename,
|
||||
createdAt: threadCandidates.createdAt,
|
||||
updatedAt: threadCandidates.updatedAt,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(threadCandidates)
|
||||
.innerJoin(categories, eq(threadCandidates.categoryId, categories.id))
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all();
|
||||
|
||||
return { ...thread, candidates: candidateList };
|
||||
return { ...thread, candidates: candidateList };
|
||||
}
|
||||
|
||||
export function updateThread(db: Db = prodDb, threadId: number, data: Partial<{ name: string; categoryId: number }>) {
|
||||
const existing = db.select({ id: threads.id }).from(threads)
|
||||
.where(eq(threads.id, threadId)).get();
|
||||
if (!existing) return null;
|
||||
export function updateThread(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
data: Partial<{ name: string; categoryId: number }>,
|
||||
) {
|
||||
const existing = db
|
||||
.select({ id: threads.id })
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
.update(threads)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threads.id, threadId))
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.update(threads)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threads.id, threadId))
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function deleteThread(db: Db = prodDb, threadId: number) {
|
||||
const thread = db.select().from(threads)
|
||||
.where(eq(threads.id, threadId)).get();
|
||||
if (!thread) return null;
|
||||
const thread = db
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
if (!thread) return null;
|
||||
|
||||
// Collect candidate image filenames for cleanup
|
||||
const candidatesWithImages = db
|
||||
.select({ imageFilename: threadCandidates.imageFilename })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all()
|
||||
.filter((c) => c.imageFilename != null);
|
||||
// Collect candidate image filenames for cleanup
|
||||
const candidatesWithImages = db
|
||||
.select({ imageFilename: threadCandidates.imageFilename })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all()
|
||||
.filter((c) => c.imageFilename != null);
|
||||
|
||||
db.delete(threads).where(eq(threads.id, threadId)).run();
|
||||
db.delete(threads).where(eq(threads.id, threadId)).run();
|
||||
|
||||
return { ...thread, candidateImages: candidatesWithImages.map((c) => c.imageFilename!) };
|
||||
return {
|
||||
...thread,
|
||||
candidateImages: candidatesWithImages.map((c) => c.imageFilename!),
|
||||
};
|
||||
}
|
||||
|
||||
export function createCandidate(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
data: Partial<CreateCandidate> & { name: string; categoryId: number; imageFilename?: string },
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
data: Partial<CreateCandidate> & {
|
||||
name: string;
|
||||
categoryId: number;
|
||||
imageFilename?: string;
|
||||
},
|
||||
) {
|
||||
return db
|
||||
.insert(threadCandidates)
|
||||
.values({
|
||||
threadId,
|
||||
name: data.name,
|
||||
weightGrams: data.weightGrams ?? null,
|
||||
priceCents: data.priceCents ?? null,
|
||||
categoryId: data.categoryId,
|
||||
notes: data.notes ?? null,
|
||||
productUrl: data.productUrl ?? null,
|
||||
imageFilename: data.imageFilename ?? null,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.insert(threadCandidates)
|
||||
.values({
|
||||
threadId,
|
||||
name: data.name,
|
||||
weightGrams: data.weightGrams ?? null,
|
||||
priceCents: data.priceCents ?? null,
|
||||
categoryId: data.categoryId,
|
||||
notes: data.notes ?? null,
|
||||
productUrl: data.productUrl ?? null,
|
||||
imageFilename: data.imageFilename ?? null,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function updateCandidate(
|
||||
db: Db = prodDb,
|
||||
candidateId: number,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
weightGrams: number;
|
||||
priceCents: number;
|
||||
categoryId: number;
|
||||
notes: string;
|
||||
productUrl: string;
|
||||
imageFilename: string;
|
||||
}>,
|
||||
db: Db = prodDb,
|
||||
candidateId: number,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
weightGrams: number;
|
||||
priceCents: number;
|
||||
categoryId: number;
|
||||
notes: string;
|
||||
productUrl: string;
|
||||
imageFilename: string;
|
||||
}>,
|
||||
) {
|
||||
const existing = db.select({ id: threadCandidates.id }).from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId)).get();
|
||||
if (!existing) return null;
|
||||
const existing = db
|
||||
.select({ id: threadCandidates.id })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
.update(threadCandidates)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.returning()
|
||||
.get();
|
||||
return db
|
||||
.update(threadCandidates)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
export function deleteCandidate(db: Db = prodDb, candidateId: number) {
|
||||
const candidate = db.select().from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId)).get();
|
||||
if (!candidate) return null;
|
||||
const candidate = db
|
||||
.select()
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
if (!candidate) return null;
|
||||
|
||||
db.delete(threadCandidates).where(eq(threadCandidates.id, candidateId)).run();
|
||||
return candidate;
|
||||
db.delete(threadCandidates).where(eq(threadCandidates.id, candidateId)).run();
|
||||
return candidate;
|
||||
}
|
||||
|
||||
export function resolveThread(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
candidateId: number,
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
candidateId: number,
|
||||
): { success: boolean; item?: any; error?: string } {
|
||||
return db.transaction((tx) => {
|
||||
// 1. Check thread is active
|
||||
const thread = tx.select().from(threads)
|
||||
.where(eq(threads.id, threadId)).get();
|
||||
if (!thread || thread.status !== "active") {
|
||||
return { success: false, error: "Thread not active" };
|
||||
}
|
||||
return db.transaction((tx) => {
|
||||
// 1. Check thread is active
|
||||
const thread = tx
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
if (!thread || thread.status !== "active") {
|
||||
return { success: false, error: "Thread not active" };
|
||||
}
|
||||
|
||||
// 2. Get the candidate data
|
||||
const candidate = tx.select().from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId)).get();
|
||||
if (!candidate) {
|
||||
return { success: false, error: "Candidate not found" };
|
||||
}
|
||||
if (candidate.threadId !== threadId) {
|
||||
return { success: false, error: "Candidate not in thread" };
|
||||
}
|
||||
// 2. Get the candidate data
|
||||
const candidate = tx
|
||||
.select()
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
if (!candidate) {
|
||||
return { success: false, error: "Candidate not found" };
|
||||
}
|
||||
if (candidate.threadId !== threadId) {
|
||||
return { success: false, error: "Candidate not in thread" };
|
||||
}
|
||||
|
||||
// 3. Verify categoryId still exists, fallback to Uncategorized (id=1)
|
||||
const category = tx.select({ id: categories.id }).from(categories)
|
||||
.where(eq(categories.id, candidate.categoryId)).get();
|
||||
const safeCategoryId = category ? candidate.categoryId : 1;
|
||||
// 3. Verify categoryId still exists, fallback to Uncategorized (id=1)
|
||||
const category = tx
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, candidate.categoryId))
|
||||
.get();
|
||||
const safeCategoryId = category ? candidate.categoryId : 1;
|
||||
|
||||
// 4. Create collection item from candidate data
|
||||
const newItem = tx
|
||||
.insert(items)
|
||||
.values({
|
||||
name: candidate.name,
|
||||
weightGrams: candidate.weightGrams,
|
||||
priceCents: candidate.priceCents,
|
||||
categoryId: safeCategoryId,
|
||||
notes: candidate.notes,
|
||||
productUrl: candidate.productUrl,
|
||||
imageFilename: candidate.imageFilename,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
// 4. Create collection item from candidate data
|
||||
const newItem = tx
|
||||
.insert(items)
|
||||
.values({
|
||||
name: candidate.name,
|
||||
weightGrams: candidate.weightGrams,
|
||||
priceCents: candidate.priceCents,
|
||||
categoryId: safeCategoryId,
|
||||
notes: candidate.notes,
|
||||
productUrl: candidate.productUrl,
|
||||
imageFilename: candidate.imageFilename,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
|
||||
// 5. Archive the thread
|
||||
tx.update(threads)
|
||||
.set({
|
||||
status: "resolved",
|
||||
resolvedCandidateId: candidateId,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(threads.id, threadId))
|
||||
.run();
|
||||
// 5. Archive the thread
|
||||
tx.update(threads)
|
||||
.set({
|
||||
status: "resolved",
|
||||
resolvedCandidateId: candidateId,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(threads.id, threadId))
|
||||
.run();
|
||||
|
||||
return { success: true, item: newItem };
|
||||
});
|
||||
return { success: true, item: newItem };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { items, categories } from "../../db/schema.ts";
|
||||
import { db as prodDb } from "../../db/index.ts";
|
||||
import { categories, items } from "../../db/schema.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function getCategoryTotals(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
categoryId: items.categoryId,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents}), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.groupBy(items.categoryId)
|
||||
.all();
|
||||
return db
|
||||
.select({
|
||||
categoryId: items.categoryId,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents}), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.groupBy(items.categoryId)
|
||||
.all();
|
||||
}
|
||||
|
||||
export function getGlobalTotals(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents}), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.get();
|
||||
return db
|
||||
.select({
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents}), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.get();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user