Auto-queue filings sync on new ticker inserts
This commit is contained in:
@@ -96,7 +96,10 @@ export async function upsertHoldingRecord(input: {
|
||||
.where(eq(holding.id, existing.id))
|
||||
.returning();
|
||||
|
||||
return toHolding(updated);
|
||||
return {
|
||||
holding: toHolding(updated),
|
||||
created: false
|
||||
};
|
||||
}
|
||||
|
||||
const normalized = normalizeHoldingInput({
|
||||
@@ -140,7 +143,10 @@ export async function upsertHoldingRecord(input: {
|
||||
})
|
||||
.returning();
|
||||
|
||||
return toHolding(inserted);
|
||||
return {
|
||||
holding: toHolding(inserted),
|
||||
created: true
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateHoldingByIdRecord(input: {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user