Add category and tags granularity to company sync flows

This commit is contained in:
2026-03-03 23:10:08 -05:00
parent 717e747869
commit 610fce8db3
12 changed files with 415 additions and 29 deletions

View File

@@ -5,6 +5,33 @@ import { watchlistItem } from '@/lib/server/db/schema';
type WatchlistRow = typeof watchlistItem.$inferSelect;
function normalizeTags(tags?: string[]) {
if (!Array.isArray(tags)) {
return null;
}
const unique = new Set<string>();
for (const entry of tags) {
if (typeof entry !== 'string') {
continue;
}
const tag = entry.trim();
if (!tag) {
continue;
}
unique.add(tag);
}
if (unique.size === 0) {
return null;
}
return [...unique];
}
function toWatchlistItem(row: WatchlistRow): WatchlistItem {
return {
id: row.id,
@@ -12,6 +39,10 @@ function toWatchlistItem(row: WatchlistRow): WatchlistItem {
ticker: row.ticker,
company_name: row.company_name,
sector: row.sector,
category: row.category,
tags: Array.isArray(row.tags)
? row.tags.filter((entry): entry is string => typeof entry === 'string')
: [],
created_at: row.created_at
};
}
@@ -26,14 +57,33 @@ export async function listWatchlistItems(userId: string) {
return rows.map(toWatchlistItem);
}
export async function getWatchlistItemByTicker(userId: string, ticker: string) {
const normalizedTicker = ticker.trim().toUpperCase();
if (!normalizedTicker) {
return null;
}
const [row] = await db
.select()
.from(watchlistItem)
.where(and(eq(watchlistItem.user_id, userId), eq(watchlistItem.ticker, normalizedTicker)))
.limit(1);
return row ? toWatchlistItem(row) : null;
}
export async function upsertWatchlistItemRecord(input: {
userId: string;
ticker: string;
companyName: string;
sector?: string;
category?: string;
tags?: string[];
}) {
const normalizedTicker = input.ticker.trim().toUpperCase();
const normalizedSector = input.sector?.trim() ? input.sector.trim() : null;
const normalizedCategory = input.category?.trim() ? input.category.trim() : null;
const normalizedTags = normalizeTags(input.tags);
const now = new Date().toISOString();
const [inserted] = await db
@@ -43,6 +93,8 @@ export async function upsertWatchlistItemRecord(input: {
ticker: normalizedTicker,
company_name: input.companyName,
sector: normalizedSector,
category: normalizedCategory,
tags: normalizedTags,
created_at: now
})
.onConflictDoNothing({
@@ -61,7 +113,9 @@ export async function upsertWatchlistItemRecord(input: {
.update(watchlistItem)
.set({
company_name: input.companyName,
sector: normalizedSector
sector: normalizedSector,
category: normalizedCategory,
tags: normalizedTags
})
.where(and(eq(watchlistItem.user_id, input.userId), eq(watchlistItem.ticker, normalizedTicker)))
.returning();