feat(14-03): convert core data services to async PostgreSQL operations
- item.service.ts: 6 functions async, removed .all()/.get()/.run() - category.service.ts: 4 functions async, transaction uses async callback - thread.service.ts: 10 functions async, transactions in resolveThread/reorderCandidates use async callbacks - setup.service.ts: 8 functions async, syncSetupItems transaction uses async callback - totals.service.ts: 2 functions async, removed .all()/.get()
This commit is contained in:
@@ -4,49 +4,50 @@ 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();
|
||||
export async function getAllCategories(db: Db = prodDb) {
|
||||
return db.select().from(categories).orderBy(asc(categories.name));
|
||||
}
|
||||
|
||||
export function createCategory(
|
||||
export async function createCategory(
|
||||
db: Db = prodDb,
|
||||
data: { name: string; icon?: string },
|
||||
) {
|
||||
return db
|
||||
const [row] = await db
|
||||
.insert(categories)
|
||||
.values({
|
||||
name: data.name,
|
||||
...(data.icon ? { icon: data.icon } : {}),
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function updateCategory(
|
||||
export async function updateCategory(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: { name?: string; icon?: string },
|
||||
) {
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
.where(eq(categories.id, id));
|
||||
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.update(categories)
|
||||
.set(data)
|
||||
.where(eq(categories.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function deleteCategory(
|
||||
export async function deleteCategory(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
): { success: boolean; error?: string } {
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
// Guard: cannot delete Uncategorized (id=1)
|
||||
if (id === 1) {
|
||||
return {
|
||||
@@ -56,24 +57,23 @@ export function deleteCategory(
|
||||
}
|
||||
|
||||
// Check if category exists
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, id))
|
||||
.get();
|
||||
.where(eq(categories.id, id));
|
||||
|
||||
if (!existing) {
|
||||
return { success: false, error: "Category not found" };
|
||||
}
|
||||
|
||||
// Reassign items to Uncategorized (id=1), then delete atomically
|
||||
db.transaction(() => {
|
||||
db.update(items)
|
||||
await db.transaction(async (tx) => {
|
||||
await tx
|
||||
.update(items)
|
||||
.set({ categoryId: 1 })
|
||||
.where(eq(items.categoryId, id))
|
||||
.run();
|
||||
.where(eq(items.categoryId, id));
|
||||
|
||||
db.delete(categories).where(eq(categories.id, id)).run();
|
||||
await tx.delete(categories).where(eq(categories.id, id));
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { CreateItem } from "../../shared/types.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function getAllItems(db: Db = prodDb) {
|
||||
export async function getAllItems(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
id: items.id,
|
||||
@@ -24,13 +24,11 @@ export function getAllItems(db: Db = prodDb) {
|
||||
categoryIcon: categories.icon,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.all();
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id));
|
||||
}
|
||||
|
||||
export function getItemById(db: Db = prodDb, id: number) {
|
||||
return (
|
||||
db
|
||||
export async function getItemById(db: Db = prodDb, id: number) {
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
@@ -45,12 +43,12 @@ export function getItemById(db: Db = prodDb, id: number) {
|
||||
updatedAt: items.updatedAt,
|
||||
})
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get() ?? null
|
||||
);
|
||||
.where(eq(items.id, id));
|
||||
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export function createItem(
|
||||
export async function createItem(
|
||||
db: Db = prodDb,
|
||||
data: Partial<CreateItem> & {
|
||||
name: string;
|
||||
@@ -58,7 +56,7 @@ export function createItem(
|
||||
imageFilename?: string;
|
||||
},
|
||||
) {
|
||||
return db
|
||||
const [row] = await db
|
||||
.insert(items)
|
||||
.values({
|
||||
name: data.name,
|
||||
@@ -71,11 +69,12 @@ export function createItem(
|
||||
imageFilename: data.imageFilename ?? null,
|
||||
imageSourceUrl: data.imageSourceUrl ?? null,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function updateItem(
|
||||
export async function updateItem(
|
||||
db: Db = prodDb,
|
||||
id: number,
|
||||
data: Partial<{
|
||||
@@ -91,28 +90,28 @@ export function updateItem(
|
||||
}>,
|
||||
) {
|
||||
// Check if item exists first
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: items.id })
|
||||
.from(items)
|
||||
.where(eq(items.id, id))
|
||||
.get();
|
||||
.where(eq(items.id, id));
|
||||
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.update(items)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(items.id, id))
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function duplicateItem(db: Db = prodDb, id: number) {
|
||||
const source = db.select().from(items).where(eq(items.id, id)).get();
|
||||
export async function duplicateItem(db: Db = prodDb, id: number) {
|
||||
const [source] = await db.select().from(items).where(eq(items.id, id));
|
||||
|
||||
if (!source) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.insert(items)
|
||||
.values({
|
||||
name: `${source.name} (copy)`,
|
||||
@@ -125,17 +124,18 @@ export function duplicateItem(db: Db = prodDb, id: number) {
|
||||
imageSourceUrl: source.imageSourceUrl,
|
||||
quantity: source.quantity,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function deleteItem(db: Db = prodDb, id: number) {
|
||||
export async 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();
|
||||
const [item] = await db.select().from(items).where(eq(items.id, id));
|
||||
|
||||
if (!item) return null;
|
||||
|
||||
db.delete(items).where(eq(items.id, id)).run();
|
||||
await db.delete(items).where(eq(items.id, id));
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ 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();
|
||||
export async function createSetup(db: Db = prodDb, data: CreateSetup) {
|
||||
const [row] = await db.insert(setups).values({ name: data.name }).returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function getAllSetups(db: Db = prodDb) {
|
||||
export async function getAllSetups(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
id: setups.id,
|
||||
@@ -31,15 +33,14 @@ export function getAllSetups(db: Db = prodDb) {
|
||||
WHERE setup_items.setup_id = setups.id
|
||||
), 0)`.as("total_cost"),
|
||||
})
|
||||
.from(setups)
|
||||
.all();
|
||||
.from(setups);
|
||||
}
|
||||
|
||||
export function getSetupWithItems(db: Db = prodDb, setupId: number) {
|
||||
const setup = db.select().from(setups).where(eq(setups.id, setupId)).get();
|
||||
export async function getSetupWithItems(db: Db = prodDb, setupId: number) {
|
||||
const [setup] = await db.select().from(setups).where(eq(setups.id, setupId));
|
||||
if (!setup) return null;
|
||||
|
||||
const itemList = db
|
||||
const itemList = await db
|
||||
.select({
|
||||
id: items.id,
|
||||
name: items.name,
|
||||
@@ -59,59 +60,56 @@ export function getSetupWithItems(db: Db = prodDb, setupId: number) {
|
||||
.from(setupItems)
|
||||
.innerJoin(items, eq(setupItems.itemId, items.id))
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.where(eq(setupItems.setupId, setupId))
|
||||
.all();
|
||||
.where(eq(setupItems.setupId, setupId));
|
||||
|
||||
return { ...setup, items: itemList };
|
||||
}
|
||||
|
||||
export function updateSetup(
|
||||
export async function updateSetup(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
data: UpdateSetup,
|
||||
) {
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: setups.id })
|
||||
.from(setups)
|
||||
.where(eq(setups.id, setupId))
|
||||
.get();
|
||||
.where(eq(setups.id, setupId));
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.update(setups)
|
||||
.set({ name: data.name, updatedAt: new Date() })
|
||||
.where(eq(setups.id, setupId))
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function deleteSetup(db: Db = prodDb, setupId: number) {
|
||||
const existing = db
|
||||
export async function deleteSetup(db: Db = prodDb, setupId: number) {
|
||||
const [existing] = await db
|
||||
.select({ id: setups.id })
|
||||
.from(setups)
|
||||
.where(eq(setups.id, setupId))
|
||||
.get();
|
||||
.where(eq(setups.id, setupId));
|
||||
if (!existing) return false;
|
||||
|
||||
db.delete(setups).where(eq(setups.id, setupId)).run();
|
||||
await db.delete(setups).where(eq(setups.id, setupId));
|
||||
return true;
|
||||
}
|
||||
|
||||
export function syncSetupItems(
|
||||
export async function syncSetupItems(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
itemIds: number[],
|
||||
) {
|
||||
return db.transaction((tx) => {
|
||||
return await db.transaction(async (tx) => {
|
||||
// Save existing classifications before deleting
|
||||
const existing = tx
|
||||
const existing = await tx
|
||||
.select({
|
||||
itemId: setupItems.itemId,
|
||||
classification: setupItems.classification,
|
||||
})
|
||||
.from(setupItems)
|
||||
.where(eq(setupItems.setupId, setupId))
|
||||
.all();
|
||||
.where(eq(setupItems.setupId, setupId));
|
||||
|
||||
const classificationMap = new Map<number, string>();
|
||||
for (const row of existing) {
|
||||
@@ -119,43 +117,41 @@ export function syncSetupItems(
|
||||
}
|
||||
|
||||
// Delete all existing items for this setup
|
||||
tx.delete(setupItems).where(eq(setupItems.setupId, setupId)).run();
|
||||
await tx.delete(setupItems).where(eq(setupItems.setupId, setupId));
|
||||
|
||||
// Re-insert new items, preserving classifications for retained items
|
||||
for (const itemId of itemIds) {
|
||||
tx.insert(setupItems)
|
||||
.values({
|
||||
await tx.insert(setupItems).values({
|
||||
setupId,
|
||||
itemId,
|
||||
classification: classificationMap.get(itemId) ?? "base",
|
||||
})
|
||||
.run();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function updateItemClassification(
|
||||
export async function updateItemClassification(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
itemId: number,
|
||||
classification: string,
|
||||
) {
|
||||
db.update(setupItems)
|
||||
await db
|
||||
.update(setupItems)
|
||||
.set({ classification })
|
||||
.where(
|
||||
sql`${setupItems.setupId} = ${setupId} AND ${setupItems.itemId} = ${itemId}`,
|
||||
)
|
||||
.run();
|
||||
);
|
||||
}
|
||||
|
||||
export function removeSetupItem(
|
||||
export async function removeSetupItem(
|
||||
db: Db = prodDb,
|
||||
setupId: number,
|
||||
itemId: number,
|
||||
) {
|
||||
db.delete(setupItems)
|
||||
await db
|
||||
.delete(setupItems)
|
||||
.where(
|
||||
sql`${setupItems.setupId} = ${setupId} AND ${setupItems.itemId} = ${itemId}`,
|
||||
)
|
||||
.run();
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,15 +14,16 @@ import type {
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function createThread(db: Db = prodDb, data: CreateThread) {
|
||||
return db
|
||||
export async function createThread(db: Db = prodDb, data: CreateThread) {
|
||||
const [row] = await db
|
||||
.insert(threads)
|
||||
.values({ name: data.name, categoryId: data.categoryId })
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function getAllThreads(db: Db = prodDb, includeResolved = false) {
|
||||
export async function getAllThreads(db: Db = prodDb, includeResolved = false) {
|
||||
const query = db
|
||||
.select({
|
||||
id: threads.id,
|
||||
@@ -52,20 +53,22 @@ export function getAllThreads(db: Db = prodDb, includeResolved = false) {
|
||||
.orderBy(desc(threads.createdAt));
|
||||
|
||||
if (!includeResolved) {
|
||||
return query.where(eq(threads.status, "active")).all();
|
||||
return query.where(eq(threads.status, "active"));
|
||||
}
|
||||
return query.all();
|
||||
return query;
|
||||
}
|
||||
|
||||
export function getThreadWithCandidates(db: Db = prodDb, threadId: number) {
|
||||
const thread = db
|
||||
export async function getThreadWithCandidates(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
) {
|
||||
const [thread] = await db
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
.where(eq(threads.id, threadId));
|
||||
if (!thread) return null;
|
||||
|
||||
const candidateList = db
|
||||
const candidateList = await db
|
||||
.select({
|
||||
id: threadCandidates.id,
|
||||
threadId: threadCandidates.threadId,
|
||||
@@ -88,49 +91,47 @@ export function getThreadWithCandidates(db: Db = prodDb, threadId: number) {
|
||||
.from(threadCandidates)
|
||||
.innerJoin(categories, eq(threadCandidates.categoryId, categories.id))
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.orderBy(asc(threadCandidates.sortOrder))
|
||||
.all();
|
||||
.orderBy(asc(threadCandidates.sortOrder));
|
||||
|
||||
return { ...thread, candidates: candidateList };
|
||||
}
|
||||
|
||||
export function updateThread(
|
||||
export async function updateThread(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
data: Partial<{ name: string; categoryId: number }>,
|
||||
) {
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: threads.id })
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
.where(eq(threads.id, threadId));
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.update(threads)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threads.id, threadId))
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function deleteThread(db: Db = prodDb, threadId: number) {
|
||||
const thread = db
|
||||
export async function deleteThread(db: Db = prodDb, threadId: number) {
|
||||
const [thread] = await db
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
.where(eq(threads.id, threadId));
|
||||
if (!thread) return null;
|
||||
|
||||
// Collect candidate image filenames for cleanup
|
||||
const candidatesWithImages = db
|
||||
const candidatesWithImages = (
|
||||
await db
|
||||
.select({ imageFilename: threadCandidates.imageFilename })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all()
|
||||
.filter((c) => c.imageFilename != null);
|
||||
).filter((c) => c.imageFilename != null);
|
||||
|
||||
db.delete(threads).where(eq(threads.id, threadId)).run();
|
||||
await db.delete(threads).where(eq(threads.id, threadId));
|
||||
|
||||
return {
|
||||
...thread,
|
||||
@@ -138,7 +139,7 @@ export function deleteThread(db: Db = prodDb, threadId: number) {
|
||||
};
|
||||
}
|
||||
|
||||
export function createCandidate(
|
||||
export async function createCandidate(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
data: Partial<CreateCandidate> & {
|
||||
@@ -148,15 +149,14 @@ export function createCandidate(
|
||||
imageSourceUrl?: string;
|
||||
},
|
||||
) {
|
||||
const maxRow = db
|
||||
const [maxRow] = await db
|
||||
.select({ maxOrder: max(threadCandidates.sortOrder) })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.get();
|
||||
.where(eq(threadCandidates.threadId, threadId));
|
||||
|
||||
const nextSortOrder = (maxRow?.maxOrder ?? 0) + 1000;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.insert(threadCandidates)
|
||||
.values({
|
||||
threadId,
|
||||
@@ -173,11 +173,12 @@ export function createCandidate(
|
||||
cons: data.cons ?? null,
|
||||
sortOrder: nextSortOrder,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function updateCandidate(
|
||||
export async function updateCandidate(
|
||||
db: Db = prodDb,
|
||||
candidateId: number,
|
||||
data: Partial<{
|
||||
@@ -194,44 +195,42 @@ export function updateCandidate(
|
||||
cons: string;
|
||||
}>,
|
||||
) {
|
||||
const existing = db
|
||||
const [existing] = await db
|
||||
.select({ id: threadCandidates.id })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
.where(eq(threadCandidates.id, candidateId));
|
||||
if (!existing) return null;
|
||||
|
||||
return db
|
||||
const [row] = await db
|
||||
.update(threadCandidates)
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
export function deleteCandidate(db: Db = prodDb, candidateId: number) {
|
||||
const candidate = db
|
||||
export async function deleteCandidate(db: Db = prodDb, candidateId: number) {
|
||||
const [candidate] = await db
|
||||
.select()
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
.where(eq(threadCandidates.id, candidateId));
|
||||
if (!candidate) return null;
|
||||
|
||||
db.delete(threadCandidates).where(eq(threadCandidates.id, candidateId)).run();
|
||||
await db.delete(threadCandidates).where(eq(threadCandidates.id, candidateId));
|
||||
return candidate;
|
||||
}
|
||||
|
||||
export function reorderCandidates(
|
||||
export async function reorderCandidates(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
orderedIds: ReorderCandidates["orderedIds"],
|
||||
): { success: boolean; error?: string } {
|
||||
return db.transaction((tx) => {
|
||||
const thread = tx
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
return await db.transaction(async (tx) => {
|
||||
const [thread] = await tx
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
.where(eq(threads.id, threadId));
|
||||
if (!thread) {
|
||||
return { success: false, error: "Thread not found" };
|
||||
}
|
||||
@@ -240,38 +239,36 @@ export function reorderCandidates(
|
||||
}
|
||||
|
||||
for (let i = 0; i < orderedIds.length; i++) {
|
||||
tx.update(threadCandidates)
|
||||
await tx
|
||||
.update(threadCandidates)
|
||||
.set({ sortOrder: (i + 1) * 1000 })
|
||||
.where(eq(threadCandidates.id, orderedIds[i]))
|
||||
.run();
|
||||
.where(eq(threadCandidates.id, orderedIds[i]));
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveThread(
|
||||
export async function resolveThread(
|
||||
db: Db = prodDb,
|
||||
threadId: number,
|
||||
candidateId: number,
|
||||
): { success: boolean; item?: any; error?: string } {
|
||||
return db.transaction((tx) => {
|
||||
): Promise<{ success: boolean; item?: any; error?: string }> {
|
||||
return await db.transaction(async (tx) => {
|
||||
// 1. Check thread is active
|
||||
const thread = tx
|
||||
const [thread] = await tx
|
||||
.select()
|
||||
.from(threads)
|
||||
.where(eq(threads.id, threadId))
|
||||
.get();
|
||||
.where(eq(threads.id, threadId));
|
||||
if (!thread || thread.status !== "active") {
|
||||
return { success: false, error: "Thread not active" };
|
||||
}
|
||||
|
||||
// 2. Get the candidate data
|
||||
const candidate = tx
|
||||
const [candidate] = await tx
|
||||
.select()
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.id, candidateId))
|
||||
.get();
|
||||
.where(eq(threadCandidates.id, candidateId));
|
||||
if (!candidate) {
|
||||
return { success: false, error: "Candidate not found" };
|
||||
}
|
||||
@@ -280,15 +277,14 @@ export function resolveThread(
|
||||
}
|
||||
|
||||
// 3. Verify categoryId still exists, fallback to Uncategorized (id=1)
|
||||
const category = tx
|
||||
const [category] = await tx
|
||||
.select({ id: categories.id })
|
||||
.from(categories)
|
||||
.where(eq(categories.id, candidate.categoryId))
|
||||
.get();
|
||||
.where(eq(categories.id, candidate.categoryId));
|
||||
const safeCategoryId = category ? candidate.categoryId : 1;
|
||||
|
||||
// 4. Create collection item from candidate data
|
||||
const newItem = tx
|
||||
const [newItem] = await tx
|
||||
.insert(items)
|
||||
.values({
|
||||
name: candidate.name,
|
||||
@@ -301,18 +297,17 @@ export function resolveThread(
|
||||
imageSourceUrl: candidate.imageSourceUrl,
|
||||
quantity: 1,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
|
||||
// 5. Archive the thread
|
||||
tx.update(threads)
|
||||
await tx
|
||||
.update(threads)
|
||||
.set({
|
||||
status: "resolved",
|
||||
resolvedCandidateId: candidateId,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(threads.id, threadId))
|
||||
.run();
|
||||
.where(eq(threads.id, threadId));
|
||||
|
||||
return { success: true, item: newItem };
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@ import { categories, items } from "../../db/schema.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
export function getCategoryTotals(db: Db = prodDb) {
|
||||
export async function getCategoryTotals(db: Db = prodDb) {
|
||||
return db
|
||||
.select({
|
||||
categoryId: items.categoryId,
|
||||
@@ -16,17 +16,17 @@ export function getCategoryTotals(db: Db = prodDb) {
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.groupBy(items.categoryId)
|
||||
.all();
|
||||
.groupBy(items.categoryId);
|
||||
}
|
||||
|
||||
export function getGlobalTotals(db: Db = prodDb) {
|
||||
return db
|
||||
export async function getGlobalTotals(db: Db = prodDb) {
|
||||
const [row] = await db
|
||||
.select({
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams} * ${items.quantity}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents} * ${items.quantity}), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.get();
|
||||
.from(items);
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user