integrated zod parameter verification into the PUT function for updating the settings on the portfolio settings page

This commit is contained in:
StefanWout 2024-11-07 17:44:38 +00:00
parent d1352a730c
commit b33fdfd34b
2 changed files with 27 additions and 2 deletions

View file

@ -13,15 +13,38 @@ import {
propertyDetailsEpc, propertyDetailsEpc,
property, property,
} from "@/app/db/schema/property"; } from "@/app/db/schema/property";
import { eq, inArray } from "drizzle-orm"; import { eq, inArray, Name } from "drizzle-orm";
import { z } from "zod"; import { z } from "zod";
const UpdateBodySchema = z.object({
name: z.optional(z.string()),
budget: z.optional(z.number()),
goal: z.optional(z.string()),
status: z.optional(z.string()),
});
export async function PUT( export async function PUT(
request: NextRequest, request: NextRequest,
{ params }: { params: { portfolioId: string } } { params }: { params: { portfolioId: string } }
) { ) {
const body = await request.json(); const body = await request.json();
let validatedBody;
try {
validatedBody = UpdateBodySchema.parse(body);
} catch (error) {
console.error("Invalid input: ", error);
return new NextResponse(JSON.stringify({ msg: "Invalid input" }), {
status: 400,
});
}
const portfolioId = params.portfolioId; const portfolioId = params.portfolioId;
const name = validatedBody.name;
const budget = validatedBody.budget;
const goal = validatedBody.goal;
const status = validatedBody.status;
await db await db
.update(portfolio) .update(portfolio)
@ -43,7 +66,7 @@ export async function DELETE(
// 1) Fetch the portfolio ids // 1) Fetch the portfolio ids
// 2) Fetch the recommendation ids // 2) Fetch the recommendation ids
// 3) Delete all entries from RecommendationMaterials for these recommendations // 3) Delete all entries from RecommendationMaterials for these recommendationsP
// 4) Delete all entries from PlanRecommendations that reference plans in the portfolio // 4) Delete all entries from PlanRecommendations that reference plans in the portfolio
// 5) Delete all Plans associated with the portfolio // 5) Delete all Plans associated with the portfolio
// 6) Delete all Scenarios associated with the portfolio // 6) Delete all Scenarios associated with the portfolio

View file

@ -135,6 +135,8 @@ const updateSettings = async ({
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: requestBody, body: requestBody,
}); });
if (!response.ok) { if (!response.ok) {