Dev (#50)
* update * cleanup * fixing linting and tests errors * Refactor API Parameter Handling and Update Email Transport ✅ Updated API Route Parameters: - Changed parameter type from `{ id: string }` to `Promise<{ id: string }>` in PUT and DELETE methods for better async handling. ✅ Fixed Email Transport Creation: - Updated `nodemailer.createTransporter` to `nodemailer.createTransport` for correct transport configuration. ✅ Refactored AnalyticsDashboard Component: - Changed export from default to named export for better modularity. ✅ Enhanced Email Responder Toast: - Updated toast structure to include additional properties for better user feedback. 🎯 Overall Improvements: - Improved async handling in API routes. - Ensured correct usage of nodemailer. - Enhanced component exports and user notifications.
This commit is contained in:
76
app/api/contacts/[id]/route.tsx
Normal file
76
app/api/contacts/[id]/route.tsx
Normal file
@@ -0,0 +1,76 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const resolvedParams = await params;
|
||||
const id = parseInt(resolvedParams.id);
|
||||
const body = await request.json();
|
||||
const { responded, responseTemplate } = body;
|
||||
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid contact ID' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const contact = await prisma.contact.update({
|
||||
where: { id },
|
||||
data: {
|
||||
responded: responded !== undefined ? responded : undefined,
|
||||
responseTemplate: responseTemplate || undefined,
|
||||
updatedAt: new Date()
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Contact updated successfully',
|
||||
contact
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error updating contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update contact' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const resolvedParams = await params;
|
||||
const id = parseInt(resolvedParams.id);
|
||||
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid contact ID' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.contact.delete({
|
||||
where: { id }
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Contact deleted successfully'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error deleting contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete contact' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
95
app/api/contacts/route.tsx
Normal file
95
app/api/contacts/route.tsx
Normal file
@@ -0,0 +1,95 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const filter = searchParams.get('filter') || 'all';
|
||||
const limit = parseInt(searchParams.get('limit') || '50');
|
||||
const offset = parseInt(searchParams.get('offset') || '0');
|
||||
|
||||
let whereClause = {};
|
||||
|
||||
switch (filter) {
|
||||
case 'unread':
|
||||
whereClause = { responded: false };
|
||||
break;
|
||||
case 'responded':
|
||||
whereClause = { responded: true };
|
||||
break;
|
||||
default:
|
||||
whereClause = {};
|
||||
}
|
||||
|
||||
const [contacts, total] = await Promise.all([
|
||||
prisma.contact.findMany({
|
||||
where: whereClause,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: limit,
|
||||
skip: offset,
|
||||
}),
|
||||
prisma.contact.count({ where: whereClause })
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
contacts,
|
||||
total,
|
||||
hasMore: offset + contacts.length < total
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching contacts:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch contacts' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { name, email, subject, message } = body;
|
||||
|
||||
// Validate required fields
|
||||
if (!name || !email || !subject || !message) {
|
||||
return NextResponse.json(
|
||||
{ error: 'All fields are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Validate email format
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid email format' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const contact = await prisma.contact.create({
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
subject,
|
||||
message,
|
||||
responded: false
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Contact created successfully',
|
||||
contact
|
||||
}, { status: 201 });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error creating contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create contact' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user