Changed validation of body structure

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-14 11:18:49 +01:00
parent 2775d732e7
commit 50498afc3f

View file

@ -1,4 +1,3 @@
import { user } from "@/app/db/schema/users";
import S3 from "aws-sdk/clients/s3";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
@ -18,11 +17,18 @@ export async function GET(request: NextRequest) {
// For the moment, this api specifically handles uploads of csvs
const body = await request.json();
let validatedBody;
try {
const validatedBody = PresignedUrlBodySchema.parse(body);
const { userId, portfolioId } = validatedBody;
validatedBody = PresignedUrlBodySchema.parse(body);
} catch (error) {
console.error("Invalid input: ", error);
return new NextResponse(JSON.stringify({ msg: "Invalid input" }), {
status: 400,
});
}
try {
const s3 = new S3({
signatureVersion: "v4",
region: process.env.PRESIGN_AWS_REGION,
@ -30,6 +36,7 @@ export async function GET(request: NextRequest) {
secretAccessKey: process.env.PRESIGN_AWS_SECRET_KEY,
});
const { userId, portfolioId } = validatedBody;
const fileKey = generateS3Key(
userId,
portfolioId,
@ -49,6 +56,8 @@ export async function GET(request: NextRequest) {
});
} catch (error) {
console.error(error);
throw error;
return new NextResponse(JSON.stringify({ msg: "Insternal server error" }), {
status: 500,
});
}
}