feat: add read and delete customer form
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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<CustomerForm | null>(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 <div>Loading...</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
Form {params.id} {JSON.stringify(customerForm)}
|
||||
</div>
|
||||
<>
|
||||
<div>Form {JSON.stringify(customerForm)}</div>
|
||||
<Button onClick={handleDelete}>Delete</Button>
|
||||
</>
|
||||
);
|
||||
});
|
||||
|
||||
5
utils/createErrorResponse.ts
Normal file
5
utils/createErrorResponse.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export function createErrorResponse(message: string, status: number) {
|
||||
return NextResponse.json({ success: false, message }, { status });
|
||||
}
|
||||
@@ -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<typeof UserSchema>;
|
||||
|
||||
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<typeof CustomerFormSchema>;
|
||||
|
||||
export const ContextSchema = z.object({
|
||||
params: z.object({
|
||||
id: z.string()
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user