diff --git a/src/app/api/portfolio/[portfolioId]/route.ts b/src/app/api/portfolio/[portfolioId]/route.ts index cfec932..f4f7400 100644 --- a/src/app/api/portfolio/[portfolioId]/route.ts +++ b/src/app/api/portfolio/[portfolioId]/route.ts @@ -2,6 +2,7 @@ import { db } from "@/app/db/db"; import { NextRequest, NextResponse } from "next/server"; import { portfolio } from "@/app/db/schema/portfolio"; import { and, eq, inArray } from "drizzle-orm"; +import { user } from "@/app/db/schema/users"; export async function PUT(request: NextRequest) { const body = await request.json(); @@ -28,4 +29,41 @@ export async function PUT(request: NextRequest) { export async function DELETE(request: NextRequest) { const body = await request.json(); console.log("WE BE DELETIN'"); + + const portfolioId = body.portfolioId; + const userId = body.userId; + + try { + // Verify the user owns this portfolio before deleting + const existingPortfolio = await db.query.portfolio.findFirst({ + where: and( + eq(portfolio.id, portfolioId), + eq(user.id, userId) + ) + }); + + if (!existingPortfolio) { + return new NextResponse( + JSON.stringify({ error: "Portfolio not found or unauthorized" }), + { status: 404 } + ); + } + + // Delete the portfolio + await db.delete(portfolio).where(eq(portfolio.id, portfolioId)); + + // Optionally, delete related records if needed + // await db.delete(someRelatedTable).where(eq(someRelatedTable.portfolioId, portfolioId)); + + // Return successful response + return new NextResponse(JSON.stringify({ message: "Portfolio deleted successfully" }), { + status: 200, + }); + } catch (error) { + console.error("Error deleting portfolio:", error); + return new NextResponse( + JSON.stringify({ error: "Failed to delete portfolio" }), + { status: 500 } + ); + } } diff --git a/src/app/portfolio/[slug]/(portfolio)/settings/PortfolioSettings.tsx b/src/app/portfolio/[slug]/(portfolio)/settings/PortfolioSettings.tsx index 658cd29..942d015 100644 --- a/src/app/portfolio/[slug]/(portfolio)/settings/PortfolioSettings.tsx +++ b/src/app/portfolio/[slug]/(portfolio)/settings/PortfolioSettings.tsx @@ -89,6 +89,7 @@ const updateSettings = async ({ // We will create a js object with the starting values // We will then update the values that are not null + const body: bodyType = { userId: userId.toString(), portfolioId: portfolioId, @@ -184,19 +185,39 @@ export default function PortfolioSettings({ setIsDeleteModalOpen(true); } - function handleDeleteConfirmation() { - console.log("we be deletin stuff"); - // if (deleteConfirmationByName === portfolioName) { - // //apiDeletePortfolio(portfolioId) - // router.refresh(); - // setIsDeleteModalOpen(false); - // } else { - // // Error if the names don't match - // console.log("Portfolio name does not match"); - // } - router.push("/home"); + async function handleDeleteConfirmation(portfolioId: number, userId: bigint) + { if (deleteConfirmationByName === portfolioName) + console.log('userId:', userId); + console.log('portfolioId:', portfolioId); + console.log('typeof userId:', typeof userId); + try { + const response = await fetch(`/api/portfolio/${portfolioId}`, { + method: "DELETE", + headers: {"Content-Type": "application/json", + }, + body: JSON.stringify({ + userId: typeof userId === 'bigint' ? userId.toString() : userId, + portfolioId: portfolioId + }) + }); + + if (!response.ok) { + throw new Error('Failed to delete portfolio'); + } + else { + setIsDeleteModalOpen(false); + return await response.json(); + } + + } + + catch (error) { + console.error("The portfolio taunts you and tells you to try harder", error); + throw error; + } } + // RENAMING FUNCTIONS // Change NAME functionality - changing state @@ -266,6 +287,19 @@ export default function PortfolioSettings({ status: portfolioStatus, }); } + + // Delete function + + function handleDelete() { + mutate({ + userId, + portfolioId, + name: portfolioName, + budget: portfolioBudget, + goal: portfolioGoal, + status: portfolioStatus, + }); + } // HTML to render the page