import { db } from "../../../lib/db.server"; export async function loader() { const stocks = await db.stock.findMany({ orderBy: { ticker: "asc" }, }); return Response.json(stocks); } export async function action({ request }: { request: Request }) { const formData = await request.formData(); const ticker = formData.get("ticker")?.toString().toUpperCase(); const method = formData.get("_method")?.toString() || "POST"; if (!ticker) { return Response.json({ error: "Ticker is required" }, { status: 400 }); } if (method === "DELETE") { await db.stock.deleteMany({ where: { ticker }, }); return Response.json({ success: true }); } // Optional fields to save/update const lastDecision = formData.get("lastDecision")?.toString(); const lastExplanation = formData.get("lastExplanation")?.toString(); const lastExecutionPlan = formData.get("lastExecutionPlan")?.toString(); // Upsert the stock record so ticker is ensured and optional fields are saved const stock = await db.stock.upsert({ where: { ticker }, update: { lastDecision: lastDecision ?? undefined, lastExplanation: lastExplanation ?? undefined, lastExecutionPlan: lastExecutionPlan ?? undefined, }, create: { ticker, lastDecision: lastDecision ?? undefined, lastExplanation: lastExplanation ?? undefined, lastExecutionPlan: lastExecutionPlan ?? undefined, }, }); return Response.json(stock); }