Files
portfolio/app/api/contacts/[id]/route.tsx
denshooter a842cb04f3 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.
2025-09-08 08:36:16 +02:00

77 lines
2.0 KiB
TypeScript

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 }
);
}
}