diff --git a/app/api/protected/customer-form/[id]/route.ts b/app/api/protected/customer-form/[id]/route.ts index d6ecbae..69406d7 100644 --- a/app/api/protected/customer-form/[id]/route.ts +++ b/app/api/protected/customer-form/[id]/route.ts @@ -1,31 +1,42 @@ import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0'; import { CustomerFormType } from '@prisma/client'; import prisma from '@prisma/prisma'; -import { CustomerForm } from '@utils/types'; +import { createErrorResponse } from '@utils/createErrorResponse'; +import { ContextSchema, CustomerForm } from '@utils/types'; import { NextRequest, NextResponse } from 'next/server'; -export const GET = withApiAuthRequired(async request => { +export const GET = withApiAuthRequired(async (request, context) => { const session = await getSession(); - const x = console.log('request', request); - - const result = await prisma.customerForm.findUnique({ - where: { - id: 'params.id', - createdBy: { - email: session?.user.email - } - } - }); - - if (!result) { - return NextResponse.json( - { success: false, message: 'Something went wrong.' }, - { status: 500 } - ); + const validatedContext = ContextSchema.safeParse(context); + if (!validatedContext.success) { + return createErrorResponse('Invalid context format', 400); } - return NextResponse.json({ success: true, data: [] }); + const { id } = validatedContext.data.params; + const userEmail = session?.user?.email; + + if (!userEmail) { + return createErrorResponse('Session not found or invalid', 401); + } + + try { + const customerForm = await prisma.customerForm.findUnique({ + where: { + id, + createdBy: { email: userEmail } + } + }); + + if (!customerForm) { + return createErrorResponse('Customer form not found', 404); + } + + return NextResponse.json({ success: true, data: customerForm }); + } catch (error) { + console.error('Error fetching customer form:', error); + return createErrorResponse('Internal server error', 500); + } }); export async function PUT( diff --git a/app/customer-form/[id]/page.tsx b/app/customer-form/[id]/page.tsx index 60ac6b4..030d74a 100644 --- a/app/customer-form/[id]/page.tsx +++ b/app/customer-form/[id]/page.tsx @@ -1,8 +1,10 @@ 'use client'; import { withPageAuthRequired } from '@auth0/nextjs-auth0/client'; +import { Button } from '@components/Button'; import { CustomerForm, CustomerFormSchema } from '@utils/types'; import axios from 'axios'; +import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; export default withPageAuthRequired(function SingleCustomerForm({ @@ -10,6 +12,7 @@ export default withPageAuthRequired(function SingleCustomerForm({ }: { params: { id: string }; }) { + const router = useRouter(); const [customerForm, setCustomerForm] = useState(null); useEffect(() => { @@ -31,13 +34,31 @@ export default withPageAuthRequired(function SingleCustomerForm({ })(); }, [params.id]); + async function handleDelete() { + if (!customerForm) { + return; + } + + const result = await axios.delete( + `/api/protected/customer-form/${customerForm.id}` + ); + + if (result.status !== 200) { + console.error(result.data.error); + return; + } + + router.push('/customer-form'); + } + if (!customerForm) { return
Loading...
; } return ( -
- Form {params.id} {JSON.stringify(customerForm)} -
+ <> +
Form {JSON.stringify(customerForm)}
+ + ); }); diff --git a/utils/createErrorResponse.ts b/utils/createErrorResponse.ts new file mode 100644 index 0000000..4de463c --- /dev/null +++ b/utils/createErrorResponse.ts @@ -0,0 +1,5 @@ +import { NextResponse } from 'next/server'; + +export function createErrorResponse(message: string, status: number) { + return NextResponse.json({ success: false, message }, { status }); +} diff --git a/utils/types.ts b/utils/types.ts index 9256bc8..9220262 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -1,16 +1,5 @@ import { z } from 'zod'; -export const UserSchema = z.object({ - id: z.string(), - name: z.string(), - email: z.string().email(), - deleted: z.boolean(), - createdAt: z.string().transform(arg => new Date(arg)), - updatedAt: z.string().transform(arg => new Date(arg)) -}); - -export type User = z.infer; - export const CustomerFormCreateSchema = z.object({ type: z.string(), text: z.string() @@ -29,3 +18,9 @@ export const CustomerFormSchema = z.object({ export const CustomerFormListSchema = z.array(CustomerFormSchema); export type CustomerForm = z.infer; + +export const ContextSchema = z.object({ + params: z.object({ + id: z.string() + }) +});