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