25 lines
806 B
TypeScript
25 lines
806 B
TypeScript
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: "Delete a stock listing by id.",
|
||
|
|
schema: z.object({
|
||
|
|
id: z.string().describe("Stock listing id"),
|
||
|
|
}),
|
||
|
|
http: { method: "DELETE" },
|
||
|
|
run: async (args, ctx) => {
|
||
|
|
if (!ctx?.userEmail) throw new Error("Authentication required.");
|
||
|
|
const db = getDb();
|
||
|
|
const deleted = await db
|
||
|
|
.delete(stocks)
|
||
|
|
.where(and(eq(stocks.id, args.id), eq(stocks.ownerEmail, ctx.userEmail)))
|
||
|
|
.returning();
|
||
|
|
if (!deleted[0]) throw new Error("Stock not found.");
|
||
|
|
return { deleted: true, id: args.id };
|
||
|
|
},
|
||
|
|
});
|