119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
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 { title, content } = data;
|
|
|
|
// Validate required fields
|
|
if (!title || !content) {
|
|
return NextResponse.json(
|
|
{ error: 'Title and content are required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Update email template fields
|
|
emailTemplate.title = title;
|
|
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 }
|
|
);
|
|
}
|
|
}
|