flatten app to repo root and update docker deployment for single-stack runtime
This commit is contained in:
26
app/api/filings/[accessionNumber]/analyze/route.ts
Normal file
26
app/api/filings/[accessionNumber]/analyze/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
||||
import { enqueueTask } from '@/lib/server/tasks';
|
||||
|
||||
type Context = {
|
||||
params: Promise<{ accessionNumber: string }>;
|
||||
};
|
||||
|
||||
export async function POST(_request: Request, context: Context) {
|
||||
try {
|
||||
const { accessionNumber } = await context.params;
|
||||
|
||||
if (!accessionNumber || accessionNumber.trim().length < 4) {
|
||||
return jsonError('Invalid accession number');
|
||||
}
|
||||
|
||||
const task = await enqueueTask({
|
||||
taskType: 'analyze_filing',
|
||||
payload: { accessionNumber: accessionNumber.trim() },
|
||||
priority: 65
|
||||
});
|
||||
|
||||
return Response.json({ task });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to queue filing analysis task'));
|
||||
}
|
||||
}
|
||||
22
app/api/filings/route.ts
Normal file
22
app/api/filings/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { getStoreSnapshot } from '@/lib/server/store';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
const tickerFilter = url.searchParams.get('ticker')?.trim().toUpperCase();
|
||||
const limitValue = Number(url.searchParams.get('limit') ?? 50);
|
||||
const limit = Number.isFinite(limitValue)
|
||||
? Math.min(Math.max(Math.trunc(limitValue), 1), 250)
|
||||
: 50;
|
||||
|
||||
const snapshot = await getStoreSnapshot();
|
||||
const filtered = tickerFilter
|
||||
? snapshot.filings.filter((filing) => filing.ticker === tickerFilter)
|
||||
: snapshot.filings;
|
||||
|
||||
const filings = filtered
|
||||
.slice()
|
||||
.sort((a, b) => Date.parse(b.filing_date) - Date.parse(a.filing_date))
|
||||
.slice(0, limit);
|
||||
|
||||
return Response.json({ filings });
|
||||
}
|
||||
28
app/api/filings/sync/route.ts
Normal file
28
app/api/filings/sync/route.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
||||
import { enqueueTask } from '@/lib/server/tasks';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const payload = await request.json() as {
|
||||
ticker?: string;
|
||||
limit?: number;
|
||||
};
|
||||
|
||||
if (!payload.ticker || payload.ticker.trim().length < 1) {
|
||||
return jsonError('ticker is required');
|
||||
}
|
||||
|
||||
const task = await enqueueTask({
|
||||
taskType: 'sync_filings',
|
||||
payload: {
|
||||
ticker: payload.ticker.trim().toUpperCase(),
|
||||
limit: payload.limit ?? 20
|
||||
},
|
||||
priority: 90
|
||||
});
|
||||
|
||||
return Response.json({ task });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to queue filings sync task'));
|
||||
}
|
||||
}
|
||||
16
app/api/health/route.ts
Normal file
16
app/api/health/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { getStoreSnapshot } from '@/lib/server/store';
|
||||
|
||||
export async function GET() {
|
||||
const snapshot = await getStoreSnapshot();
|
||||
const queue = snapshot.tasks.reduce<Record<string, number>>((acc, task) => {
|
||||
acc[task.status] = (acc[task.status] ?? 0) + 1;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return Response.json({
|
||||
status: 'ok',
|
||||
version: '3.0.0',
|
||||
timestamp: new Date().toISOString(),
|
||||
queue
|
||||
});
|
||||
}
|
||||
10
app/api/me/route.ts
Normal file
10
app/api/me/route.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export async function GET() {
|
||||
return Response.json({
|
||||
user: {
|
||||
id: 1,
|
||||
email: 'operator@local.fiscal',
|
||||
name: 'Local Operator',
|
||||
image: null
|
||||
}
|
||||
});
|
||||
}
|
||||
89
app/api/portfolio/holdings/[id]/route.ts
Normal file
89
app/api/portfolio/holdings/[id]/route.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { jsonError } from '@/lib/server/http';
|
||||
import { recalculateHolding } from '@/lib/server/portfolio';
|
||||
import { withStore } from '@/lib/server/store';
|
||||
|
||||
type Context = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function asPositiveNumber(value: unknown) {
|
||||
const parsed = typeof value === 'number' ? value : Number(value);
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
||||
}
|
||||
|
||||
export async function PATCH(request: Request, context: Context) {
|
||||
const { id } = await context.params;
|
||||
const numericId = Number(id);
|
||||
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
return jsonError('Invalid holding id');
|
||||
}
|
||||
|
||||
const payload = await request.json() as {
|
||||
shares?: number;
|
||||
avgCost?: number;
|
||||
currentPrice?: number;
|
||||
};
|
||||
|
||||
let found = false;
|
||||
let updated: unknown = null;
|
||||
|
||||
await withStore((store) => {
|
||||
const index = store.holdings.findIndex((entry) => entry.id === numericId);
|
||||
if (index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
found = true;
|
||||
const existing = store.holdings[index];
|
||||
|
||||
const shares = asPositiveNumber(payload.shares) ?? Number(existing.shares);
|
||||
const avgCost = asPositiveNumber(payload.avgCost) ?? Number(existing.avg_cost);
|
||||
const currentPrice = asPositiveNumber(payload.currentPrice) ?? Number(existing.current_price ?? existing.avg_cost);
|
||||
|
||||
const next = recalculateHolding({
|
||||
...existing,
|
||||
shares: shares.toFixed(6),
|
||||
avg_cost: avgCost.toFixed(6),
|
||||
current_price: currentPrice.toFixed(6),
|
||||
updated_at: nowIso(),
|
||||
last_price_at: nowIso()
|
||||
});
|
||||
|
||||
store.holdings[index] = next;
|
||||
updated = next;
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
return jsonError('Holding not found', 404);
|
||||
}
|
||||
|
||||
return Response.json({ holding: updated });
|
||||
}
|
||||
|
||||
export async function DELETE(_request: Request, context: Context) {
|
||||
const { id } = await context.params;
|
||||
const numericId = Number(id);
|
||||
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
return jsonError('Invalid holding id');
|
||||
}
|
||||
|
||||
let removed = false;
|
||||
|
||||
await withStore((store) => {
|
||||
const next = store.holdings.filter((holding) => holding.id !== numericId);
|
||||
removed = next.length !== store.holdings.length;
|
||||
store.holdings = next;
|
||||
});
|
||||
|
||||
if (!removed) {
|
||||
return jsonError('Holding not found', 404);
|
||||
}
|
||||
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
97
app/api/portfolio/holdings/route.ts
Normal file
97
app/api/portfolio/holdings/route.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import type { Holding } from '@/lib/types';
|
||||
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
||||
import { recalculateHolding } from '@/lib/server/portfolio';
|
||||
import { getStoreSnapshot, withStore } from '@/lib/server/store';
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function asPositiveNumber(value: unknown) {
|
||||
const parsed = typeof value === 'number' ? value : Number(value);
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const snapshot = await getStoreSnapshot();
|
||||
const holdings = snapshot.holdings
|
||||
.slice()
|
||||
.sort((a, b) => Number(b.market_value) - Number(a.market_value));
|
||||
|
||||
return Response.json({ holdings });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const payload = await request.json() as {
|
||||
ticker?: string;
|
||||
shares?: number;
|
||||
avgCost?: number;
|
||||
currentPrice?: number;
|
||||
};
|
||||
|
||||
if (!payload.ticker || payload.ticker.trim().length < 1) {
|
||||
return jsonError('ticker is required');
|
||||
}
|
||||
|
||||
const shares = asPositiveNumber(payload.shares);
|
||||
const avgCost = asPositiveNumber(payload.avgCost);
|
||||
|
||||
if (shares === null) {
|
||||
return jsonError('shares must be a positive number');
|
||||
}
|
||||
|
||||
if (avgCost === null) {
|
||||
return jsonError('avgCost must be a positive number');
|
||||
}
|
||||
|
||||
const ticker = payload.ticker.trim().toUpperCase();
|
||||
const now = nowIso();
|
||||
let holding: Holding | null = null;
|
||||
|
||||
await withStore((store) => {
|
||||
const existingIndex = store.holdings.findIndex((entry) => entry.ticker === ticker);
|
||||
const currentPrice = asPositiveNumber(payload.currentPrice) ?? avgCost;
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
const existing = store.holdings[existingIndex];
|
||||
const updated = recalculateHolding({
|
||||
...existing,
|
||||
ticker,
|
||||
shares: shares.toFixed(6),
|
||||
avg_cost: avgCost.toFixed(6),
|
||||
current_price: currentPrice.toFixed(6),
|
||||
updated_at: now,
|
||||
last_price_at: now
|
||||
});
|
||||
|
||||
store.holdings[existingIndex] = updated;
|
||||
holding = updated;
|
||||
return;
|
||||
}
|
||||
|
||||
store.counters.holdings += 1;
|
||||
const created = recalculateHolding({
|
||||
id: store.counters.holdings,
|
||||
user_id: 1,
|
||||
ticker,
|
||||
shares: shares.toFixed(6),
|
||||
avg_cost: avgCost.toFixed(6),
|
||||
current_price: currentPrice.toFixed(6),
|
||||
market_value: '0',
|
||||
gain_loss: '0',
|
||||
gain_loss_pct: '0',
|
||||
last_price_at: now,
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
});
|
||||
|
||||
store.holdings.unshift(created);
|
||||
holding = created;
|
||||
});
|
||||
|
||||
return Response.json({ holding });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to save holding'));
|
||||
}
|
||||
}
|
||||
16
app/api/portfolio/insights/generate/route.ts
Normal file
16
app/api/portfolio/insights/generate/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
||||
import { enqueueTask } from '@/lib/server/tasks';
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const task = await enqueueTask({
|
||||
taskType: 'portfolio_insights',
|
||||
payload: {},
|
||||
priority: 70
|
||||
});
|
||||
|
||||
return Response.json({ task });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to queue insights task'));
|
||||
}
|
||||
}
|
||||
10
app/api/portfolio/insights/latest/route.ts
Normal file
10
app/api/portfolio/insights/latest/route.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { getStoreSnapshot } from '@/lib/server/store';
|
||||
|
||||
export async function GET() {
|
||||
const snapshot = await getStoreSnapshot();
|
||||
const insight = snapshot.insights
|
||||
.slice()
|
||||
.sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at))[0] ?? null;
|
||||
|
||||
return Response.json({ insight });
|
||||
}
|
||||
16
app/api/portfolio/refresh-prices/route.ts
Normal file
16
app/api/portfolio/refresh-prices/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { asErrorMessage, jsonError } from '@/lib/server/http';
|
||||
import { enqueueTask } from '@/lib/server/tasks';
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const task = await enqueueTask({
|
||||
taskType: 'refresh_prices',
|
||||
payload: {},
|
||||
priority: 80
|
||||
});
|
||||
|
||||
return Response.json({ task });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to queue refresh task'));
|
||||
}
|
||||
}
|
||||
8
app/api/portfolio/summary/route.ts
Normal file
8
app/api/portfolio/summary/route.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { buildPortfolioSummary } from '@/lib/server/portfolio';
|
||||
import { getStoreSnapshot } from '@/lib/server/store';
|
||||
|
||||
export async function GET() {
|
||||
const snapshot = await getStoreSnapshot();
|
||||
const summary = buildPortfolioSummary(snapshot.holdings);
|
||||
return Response.json({ summary });
|
||||
}
|
||||
17
app/api/tasks/[taskId]/route.ts
Normal file
17
app/api/tasks/[taskId]/route.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { jsonError } from '@/lib/server/http';
|
||||
import { getTaskById } from '@/lib/server/tasks';
|
||||
|
||||
type Context = {
|
||||
params: Promise<{ taskId: string }>;
|
||||
};
|
||||
|
||||
export async function GET(_request: Request, context: Context) {
|
||||
const { taskId } = await context.params;
|
||||
const task = await getTaskById(taskId);
|
||||
|
||||
if (!task) {
|
||||
return jsonError('Task not found', 404);
|
||||
}
|
||||
|
||||
return Response.json({ task });
|
||||
}
|
||||
20
app/api/tasks/route.ts
Normal file
20
app/api/tasks/route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { TaskStatus } from '@/lib/types';
|
||||
import { listRecentTasks } from '@/lib/server/tasks';
|
||||
|
||||
const ALLOWED_STATUSES: TaskStatus[] = ['queued', 'running', 'completed', 'failed'];
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
const limitValue = Number(url.searchParams.get('limit') ?? 20);
|
||||
const limit = Number.isFinite(limitValue)
|
||||
? Math.min(Math.max(Math.trunc(limitValue), 1), 200)
|
||||
: 20;
|
||||
|
||||
const rawStatuses = url.searchParams.getAll('status');
|
||||
const statuses = rawStatuses.filter((status): status is TaskStatus => {
|
||||
return ALLOWED_STATUSES.includes(status as TaskStatus);
|
||||
});
|
||||
|
||||
const tasks = await listRecentTasks(limit, statuses.length > 0 ? statuses : undefined);
|
||||
return Response.json({ tasks });
|
||||
}
|
||||
29
app/api/watchlist/[id]/route.ts
Normal file
29
app/api/watchlist/[id]/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { jsonError } from '@/lib/server/http';
|
||||
import { withStore } from '@/lib/server/store';
|
||||
|
||||
type Context = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function DELETE(_request: Request, context: Context) {
|
||||
const { id } = await context.params;
|
||||
const numericId = Number(id);
|
||||
|
||||
if (!Number.isInteger(numericId) || numericId <= 0) {
|
||||
return jsonError('Invalid watchlist id', 400);
|
||||
}
|
||||
|
||||
let removed = false;
|
||||
|
||||
await withStore((store) => {
|
||||
const next = store.watchlist.filter((item) => item.id !== numericId);
|
||||
removed = next.length !== store.watchlist.length;
|
||||
store.watchlist = next;
|
||||
});
|
||||
|
||||
if (!removed) {
|
||||
return jsonError('Watchlist item not found', 404);
|
||||
}
|
||||
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
71
app/api/watchlist/route.ts
Normal file
71
app/api/watchlist/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
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'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user