rebuild app as turbopack-first single-stack with internal api and openclaw tasks

This commit is contained in:
2026-02-23 23:38:06 -05:00
parent 5df09b714b
commit 6012dd3fcf
40 changed files with 1583 additions and 1578 deletions

View 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 });
}

View 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'));
}
}

View 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'));
}
}

View 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 });
}

View 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'));
}
}

View 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 });
}