diff --git a/README.md b/README.md index 6070e46..ca76ca1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [ ] Add user profile and settings (i.e. language) - [ ] Add user roles - [ ] Add user permissions -- [ ] Add module CRUD with protected routes +- [ ] Add customer form CRUD with protected routes - [ ] Customize Auth0 login page - [ ] Add user notifications and emails - [ ] Add user invoices diff --git a/app/api/protected/customer-form/[id]/route.ts b/app/api/protected/customer-form/[id]/route.ts index 7f6791a..d6ecbae 100644 --- a/app/api/protected/customer-form/[id]/route.ts +++ b/app/api/protected/customer-form/[id]/route.ts @@ -1,22 +1,83 @@ +import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0'; +import { CustomerFormType } from '@prisma/client'; +import prisma from '@prisma/prisma'; +import { CustomerForm } from '@utils/types'; import { NextRequest, NextResponse } from 'next/server'; -export async function GET( - request: NextRequest, - { params }: { params: { id: number } } -) { - return NextResponse.json(`GET ${params.id}`); -} +export const GET = withApiAuthRequired(async request => { + 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 } + ); + } + + return NextResponse.json({ success: true, data: [] }); +}); export async function PUT( request: NextRequest, - { params }: { params: { id: number } } + { params }: { params: CustomerForm } ) { - return NextResponse.json(`PUT ${params.id}`); + 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 async function DELETE( request: NextRequest, - { params }: { params: { id: number } } + { params }: { params: { id: string } } ) { - return NextResponse.json(`DELETE ${params.id}`); + const session = await getSession(); + + const result = await prisma.customerForm.delete({ + where: { + id: params.id, + createdBy: { + email: session?.user.email + } + } + }); + + if (!result) { + return NextResponse.json( + { success: false, message: 'Something went wrong.' }, + { status: 500 } + ); + } + + return NextResponse.json({ success: true }); } diff --git a/app/api/protected/customer-form/route.ts b/app/api/protected/customer-form/route.ts index 25ef8bf..43c4317 100644 --- a/app/api/protected/customer-form/route.ts +++ b/app/api/protected/customer-form/route.ts @@ -1,14 +1,13 @@ import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0'; import { CustomerForm } from '@prisma/client'; import prisma from '@prisma/prisma'; -import { randomUUID } from 'crypto'; import { NextResponse } from 'next/server'; export const GET = withApiAuthRequired(async () => { const session = await getSession(); try { - const userModules = await prisma.user.findUniqueOrThrow({ + const userCustomerForms = await prisma.user.findUniqueOrThrow({ where: { email: session?.user.email }, @@ -17,7 +16,7 @@ export const GET = withApiAuthRequired(async () => { } }); - const customerForms: CustomerForm[] = userModules.CustomerForm; + const customerForms: CustomerForm[] = userCustomerForms.CustomerForm; return NextResponse.json({ success: true, data: customerForms }); } catch (error) { @@ -40,10 +39,16 @@ export const POST = withApiAuthRequired(async request => { text: body.text, createdBy: { connect: { - id: randomUUID(), email: session?.user.email } } + }, + select: { + id: true, + type: true, + text: true, + createdAt: true, + updatedAt: true } }); diff --git a/app/customer-form/[id]/page.tsx b/app/customer-form/[id]/page.tsx index cf74286..cf59c61 100644 --- a/app/customer-form/[id]/page.tsx +++ b/app/customer-form/[id]/page.tsx @@ -1,9 +1,42 @@ 'use client'; +import { CustomerForm, CustomerFormSchema } from '@utils/types'; +import axios from 'axios'; +import { useEffect, useState } from 'react'; + export default function SingleCustomerForm({ params }: { params: { id: string }; }) { - return
{module.text}
+{customerForm.text}