Files

38 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import { defineAction } from "@agent-native/core/action";
import { and, eq } from "@agent-native/core/db/schema";
import { z } from "zod";
import { getDb } from "../server/db/index.js";
import { stocks } from "../server/db/schema.js";
export default defineAction({
description: "Get a single stock listing by id or symbol.",
schema: z.object({
id: z.string().optional().describe("Stock listing id"),
symbol: z.string().optional().describe("Stock ticker symbol"),
}),
http: { method: "GET" },
readOnly: true,
run: async (args, ctx) => {
if (!ctx?.userEmail) throw new Error("Authentication required.");
if (!args.id && !args.symbol) {
throw new Error("Either id or symbol is required.");
}
const db = getDb();
const rows = await db
.select()
.from(stocks)
.where(
and(
eq(stocks.ownerEmail, ctx.userEmail),
args.id
? eq(stocks.id, args.id)
: eq(stocks.symbol, args.symbol!.toUpperCase()),
),
);
const stock = rows[0];
if (!stock) throw new Error("Stock not found.");
return stock;
},
});