refactor: remove PUT
This commit is contained in:
@@ -4,14 +4,6 @@
|
|||||||
|
|
||||||
- [ ] Get premium Vercel account for database
|
- [ ] Get premium Vercel account for database
|
||||||
- [ ] Add middleware for authentication
|
- [ ] Add middleware for authentication
|
||||||
- [ ] Add user profile and settings (i.e. language)
|
|
||||||
- [ ] Add user roles
|
|
||||||
- [ ] Add user permissions
|
|
||||||
- [ ] Add customer form CRUD with protected routes
|
|
||||||
- [ ] Customize Auth0 login page
|
|
||||||
- [ ] Add user notifications and emails
|
|
||||||
- [ ] Add user invoices
|
|
||||||
- [ ] Add cron jobs to import data
|
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||||||
import { CustomerFormType } from '@prisma/client';
|
|
||||||
import prisma from '@prisma/prisma';
|
import prisma from '@prisma/prisma';
|
||||||
import { createErrorResponse } from '@utils/createErrorResponse';
|
import { createErrorResponse } from '@utils/createErrorResponse';
|
||||||
import { CustomerForm } from '@utils/types';
|
import { validateApiRequestContext } from '@utils/validateApiRequestContext';
|
||||||
import { validateContext } from '@utils/validateContext';
|
import { NextResponse } from 'next/server';
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
|
||||||
|
|
||||||
export const GET = withApiAuthRequired(async (request, context) => {
|
export const GET = withApiAuthRequired(async (_, context) => {
|
||||||
const session = await getSession();
|
const session = await getSession();
|
||||||
|
|
||||||
let id;
|
let params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
id = validateContext(context);
|
params = validateApiRequestContext(context);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createErrorResponse('Internal server error', 500);
|
return createErrorResponse('Internal server error', 500);
|
||||||
}
|
}
|
||||||
@@ -26,8 +24,15 @@ export const GET = withApiAuthRequired(async (request, context) => {
|
|||||||
try {
|
try {
|
||||||
const customerForm = await prisma.customerForm.findUnique({
|
const customerForm = await prisma.customerForm.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id: params.id,
|
||||||
createdBy: { email: userEmail }
|
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(
|
export const DELETE = withApiAuthRequired(async (_, context) => {
|
||||||
request: NextRequest,
|
|
||||||
{ params }: { params: CustomerForm }
|
|
||||||
) {
|
|
||||||
const session = await getSession();
|
const session = await getSession();
|
||||||
|
|
||||||
const result = await prisma.customerForm.update({
|
let params;
|
||||||
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;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
id = validateContext(context);
|
params = validateApiRequestContext(context);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createErrorResponse('Internal server error', 500);
|
return createErrorResponse('Internal server error', 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await prisma.customerForm.delete({
|
const result = await prisma.customerForm.delete({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: params.id,
|
||||||
createdBy: {
|
createdBy: {
|
||||||
email: session?.user.email
|
email: session?.user.email
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export default withPageAuthRequired(function SingleCustomerForm({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
|
try {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`/api/protected/customer-form/${params.id}`
|
`/api/protected/customer-form/${params.id}`
|
||||||
);
|
);
|
||||||
@@ -31,8 +32,12 @@ export default withPageAuthRequired(function SingleCustomerForm({
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCustomerForm(validatedResponse.data);
|
setCustomerForm(validatedResponse.data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
router.push('/customer-form');
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
}, [params.id]);
|
}, [params.id, router]);
|
||||||
|
|
||||||
async function handleDelete() {
|
async function handleDelete() {
|
||||||
if (!customerForm) {
|
if (!customerForm) {
|
||||||
|
|||||||
@@ -7,6 +7,14 @@ export const CustomerFormCreateSchema = z.object({
|
|||||||
|
|
||||||
export type CustomerFormCreate = z.infer<typeof CustomerFormCreateSchema>;
|
export type CustomerFormCreate = z.infer<typeof CustomerFormCreateSchema>;
|
||||||
|
|
||||||
|
export const CustomerFormUpdateSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
type: z.string().optional(),
|
||||||
|
text: z.string().optional()
|
||||||
|
});
|
||||||
|
|
||||||
|
export type CustomerFormUpdate = z.infer<typeof CustomerFormUpdateSchema>;
|
||||||
|
|
||||||
export const CustomerFormSchema = z.object({
|
export const CustomerFormSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
type: z.string(),
|
type: z.string(),
|
||||||
@@ -19,8 +27,10 @@ export const CustomerFormListSchema = z.array(CustomerFormSchema);
|
|||||||
|
|
||||||
export type CustomerForm = z.infer<typeof CustomerFormSchema>;
|
export type CustomerForm = z.infer<typeof CustomerFormSchema>;
|
||||||
|
|
||||||
export const ContextSchema = z.object({
|
export const ApiResponseContextSchema = z.object({
|
||||||
params: z.object({
|
params: z.object({
|
||||||
id: z.string()
|
id: z.string(),
|
||||||
|
type: z.string().optional(),
|
||||||
|
text: z.string().optional()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
11
utils/validateApiRequestContext.ts
Normal file
11
utils/validateApiRequestContext.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { ApiResponseContextSchema } from '@utils/types';
|
||||||
|
|
||||||
|
export function validateApiRequestContext(context: any) {
|
||||||
|
const validatedContext = ApiResponseContextSchema.safeParse(context);
|
||||||
|
|
||||||
|
if (!validatedContext.success) {
|
||||||
|
throw new Error('Invalid context');
|
||||||
|
}
|
||||||
|
|
||||||
|
return validatedContext.data.params;
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { ContextSchema } from '@utils/types';
|
|
||||||
|
|
||||||
export function validateContext(context: any) {
|
|
||||||
const validatedContext = ContextSchema.safeParse(context);
|
|
||||||
if (!validatedContext.success) {
|
|
||||||
throw new Error('Invalid context');
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id } = validatedContext.data.params;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user