Add EmailTemplate model and CRUD UI in admin console
This commit is contained in:
117
src/app/api/email-templates/[id]/route.ts
Normal file
117
src/app/api/email-templates/[id]/route.ts
Normal file
@ -0,0 +1,117 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDataSource, EmailTemplate } from '@/lib/database';
|
||||
|
||||
// GET /api/email-templates/[id] - Get a specific email template
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||
|
||||
const emailTemplate = await emailTemplateRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!emailTemplate) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email template not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(emailTemplate);
|
||||
} catch (error) {
|
||||
console.error('Error fetching email template:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch email template' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT /api/email-templates/[id] - Update an email template
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||
|
||||
// Find the email template to update
|
||||
const emailTemplate = await emailTemplateRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!emailTemplate) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email template not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await request.json();
|
||||
const { content } = data;
|
||||
|
||||
// Validate required fields
|
||||
if (!content) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Content is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update email template fields
|
||||
emailTemplate.content = content;
|
||||
|
||||
// Save the updated email template
|
||||
const updatedEmailTemplate = await emailTemplateRepository.save(emailTemplate);
|
||||
|
||||
return NextResponse.json(updatedEmailTemplate);
|
||||
} catch (error) {
|
||||
console.error('Error updating email template:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update email template' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/email-templates/[id] - Delete an email template
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
props: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await props.params;
|
||||
const dataSource = await getDataSource();
|
||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||
|
||||
// Find the email template to delete
|
||||
const emailTemplate = await emailTemplateRepository.findOne({
|
||||
where: { id: id }
|
||||
});
|
||||
|
||||
if (!emailTemplate) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email template not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Delete the email template
|
||||
await emailTemplateRepository.remove(emailTemplate);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting email template:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete email template' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user