Auto-queue filings sync on new ticker inserts

This commit is contained in:
2026-03-01 18:55:59 -05:00
parent 856af03b39
commit b55fbf0942
3 changed files with 73 additions and 16 deletions

View File

@@ -32,25 +32,48 @@ export async function upsertWatchlistItemRecord(input: {
companyName: string;
sector?: string;
}) {
const [row] = await db
const normalizedTicker = input.ticker.trim().toUpperCase();
const normalizedSector = input.sector?.trim() ? input.sector.trim() : null;
const now = new Date().toISOString();
const [inserted] = await db
.insert(watchlistItem)
.values({
user_id: input.userId,
ticker: input.ticker,
ticker: normalizedTicker,
company_name: input.companyName,
sector: input.sector?.trim() ? input.sector.trim() : null,
created_at: new Date().toISOString()
sector: normalizedSector,
created_at: now
})
.onConflictDoUpdate({
.onConflictDoNothing({
target: [watchlistItem.user_id, watchlistItem.ticker],
set: {
company_name: input.companyName,
sector: input.sector?.trim() ? input.sector.trim() : null
}
})
.returning();
return toWatchlistItem(row);
if (inserted) {
return {
item: toWatchlistItem(inserted),
created: true
};
}
const [updated] = await db
.update(watchlistItem)
.set({
company_name: input.companyName,
sector: normalizedSector
})
.where(and(eq(watchlistItem.user_id, input.userId), eq(watchlistItem.ticker, normalizedTicker)))
.returning();
if (!updated) {
throw new Error(`Watchlist item ${normalizedTicker} was not found after upsert conflict resolution`);
}
return {
item: toWatchlistItem(updated),
created: false
};
}
export async function deleteWatchlistItemRecord(userId: string, id: number) {