refactor: remove PUT

This commit is contained in:
Riccardo
2024-07-08 10:20:58 +02:00
parent 90830f91a0
commit eadc3269cf
6 changed files with 57 additions and 75 deletions

View File

@@ -1,18 +1,16 @@
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
import { CustomerFormType } from '@prisma/client';
import prisma from '@prisma/prisma';
import { createErrorResponse } from '@utils/createErrorResponse';
import { CustomerForm } from '@utils/types';
import { validateContext } from '@utils/validateContext';
import { NextRequest, NextResponse } from 'next/server';
import { validateApiRequestContext } from '@utils/validateApiRequestContext';
import { NextResponse } from 'next/server';
export const GET = withApiAuthRequired(async (request, context) => {
export const GET = withApiAuthRequired(async (_, context) => {
const session = await getSession();
let id;
let params;
try {
id = validateContext(context);
params = validateApiRequestContext(context);
} catch (error) {
return createErrorResponse('Internal server error', 500);
}
@@ -26,8 +24,15 @@ export const GET = withApiAuthRequired(async (request, context) => {
try {
const customerForm = await prisma.customerForm.findUnique({
where: {
id,
id: params.id,
createdBy: { email: userEmail }
},
select: {
id: true,
type: true,
text: true,
createdAt: true,
updatedAt: true
}
});
@@ -42,49 +47,20 @@ export const GET = withApiAuthRequired(async (request, context) => {
}
});
export async function PUT(
request: NextRequest,
{ params }: { params: CustomerForm }
) {
export const DELETE = withApiAuthRequired(async (_, context) => {
const session = await getSession();
const result = await prisma.customerForm.update({
where: {
id: params.id,
createdBy: {
email: session?.user.email
}
},
data: {
type: params.type as CustomerFormType,
text: params.text
}
});
if (!result) {
return NextResponse.json(
{ success: false, message: 'Something went wrong.' },
{ status: 500 }
);
}
return NextResponse.json({ success: true, data: result });
}
export const DELETE = withApiAuthRequired(async (request, context) => {
const session = await getSession();
let id;
let params;
try {
id = validateContext(context);
params = validateApiRequestContext(context);
} catch (error) {
return createErrorResponse('Internal server error', 500);
}
const result = await prisma.customerForm.delete({
where: {
id: id,
id: params.id,
createdBy: {
email: session?.user.email
}

View File

@@ -17,22 +17,27 @@ export default withPageAuthRequired(function SingleCustomerForm({
useEffect(() => {
(async () => {
const response = await axios.get(
`/api/protected/customer-form/${params.id}`
);
try {
const response = await axios.get(
`/api/protected/customer-form/${params.id}`
);
const validatedResponse = CustomerFormSchema.safeParse(
response.data.data
);
const validatedResponse = CustomerFormSchema.safeParse(
response.data.data
);
if (!validatedResponse.success) {
console.error(validatedResponse.error);
return;
if (!validatedResponse.success) {
console.error(validatedResponse.error);
return;
}
setCustomerForm(validatedResponse.data);
} catch (error) {
console.error(error);
router.push('/customer-form');
}
setCustomerForm(validatedResponse.data);
})();
}, [params.id]);
}, [params.id, router]);
async function handleDelete() {
if (!customerForm) {