Add EmailTemplate model and CRUD UI in admin console

This commit is contained in:
Ken Yasue
2025-03-25 11:40:45 +01:00
parent 42f2a30610
commit 6b9e208214
13 changed files with 664 additions and 3 deletions

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