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:
@@ -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
|
||||
.select({ imageFilename: threadCandidates.imageFilename })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
.all()
|
||||
.filter((c) => c.imageFilename != null);
|
||||
const candidatesWithImages = (
|
||||
await db
|
||||
.select({ imageFilename: threadCandidates.imageFilename })
|
||||
.from(threadCandidates)
|
||||
.where(eq(threadCandidates.threadId, threadId))
|
||||
).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 };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user