72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import type { WatchlistItem } from '@/lib/types';
|
|
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
|
import { getStoreSnapshot, withStore } from '@/lib/server/store';
|
|
|
|
function nowIso() {
|
|
return new Date().toISOString();
|
|
}
|
|
|
|
export async function GET() {
|
|
const snapshot = await getStoreSnapshot();
|
|
const items = snapshot.watchlist
|
|
.slice()
|
|
.sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at));
|
|
|
|
return Response.json({ items });
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const payload = await request.json() as {
|
|
ticker?: string;
|
|
companyName?: string;
|
|
sector?: string;
|
|
};
|
|
|
|
if (!payload.ticker || payload.ticker.trim().length < 1) {
|
|
return jsonError('ticker is required');
|
|
}
|
|
|
|
if (!payload.companyName || payload.companyName.trim().length < 1) {
|
|
return jsonError('companyName is required');
|
|
}
|
|
|
|
let item: WatchlistItem | null = null;
|
|
|
|
await withStore((store) => {
|
|
const ticker = payload.ticker!.trim().toUpperCase();
|
|
const existingIndex = store.watchlist.findIndex((entry) => entry.ticker === ticker);
|
|
|
|
if (existingIndex >= 0) {
|
|
const existing = store.watchlist[existingIndex];
|
|
const updated: WatchlistItem = {
|
|
...existing,
|
|
company_name: payload.companyName!.trim(),
|
|
sector: payload.sector?.trim() || null
|
|
};
|
|
|
|
store.watchlist[existingIndex] = updated;
|
|
item = updated;
|
|
return;
|
|
}
|
|
|
|
store.counters.watchlist += 1;
|
|
const created: WatchlistItem = {
|
|
id: store.counters.watchlist,
|
|
user_id: 1,
|
|
ticker,
|
|
company_name: payload.companyName!.trim(),
|
|
sector: payload.sector?.trim() || null,
|
|
created_at: nowIso()
|
|
};
|
|
|
|
store.watchlist.unshift(created);
|
|
item = created;
|
|
});
|
|
|
|
return Response.json({ item });
|
|
} catch (error) {
|
|
return jsonError(asErrorMessage(error, 'Failed to create watchlist item'));
|
|
}
|
|
}
|