Add customer and contact record management features

This commit is contained in:
Ken Yasue
2025-03-25 06:49:21 +01:00
parent 4e9d81924a
commit 1866d84a86
13 changed files with 917 additions and 6 deletions

View File

@ -0,0 +1,136 @@
import { NextRequest, NextResponse } from 'next/server';
import { getDataSource, ContactRecord, Customer } from '@/lib/database';
// GET /api/contact-records/[id] - Get a specific contact record
export async function GET(
request: NextRequest,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const dataSource = await getDataSource();
const contactRecordRepository = dataSource.getRepository(ContactRecord);
const contactRecord = await contactRecordRepository.findOne({
where: { id: id },
relations: ['customer']
});
if (!contactRecord) {
return NextResponse.json(
{ error: 'Contact record not found' },
{ status: 404 }
);
}
return NextResponse.json(contactRecord);
} catch (error) {
console.error('Error fetching contact record:', error);
return NextResponse.json(
{ error: 'Failed to fetch contact record' },
{ status: 500 }
);
}
}
// PUT /api/contact-records/[id] - Update a contact record
export async function PUT(
request: NextRequest,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const dataSource = await getDataSource();
const contactRecordRepository = dataSource.getRepository(ContactRecord);
const customerRepository = dataSource.getRepository(Customer);
// Find the contact record to update
const contactRecord = await contactRecordRepository.findOne({
where: { id: id }
});
if (!contactRecord) {
return NextResponse.json(
{ error: 'Contact record not found' },
{ status: 404 }
);
}
const data = await request.json();
const { customerId, contactType, notes } = data;
// Validate required fields
if (!contactType) {
return NextResponse.json(
{ error: 'Contact type is required' },
{ status: 400 }
);
}
// If customerId is changing, verify the new customer exists
if (customerId && customerId !== contactRecord.customerId) {
const customer = await customerRepository.findOne({
where: { id: customerId }
});
if (!customer) {
return NextResponse.json(
{ error: 'Customer not found' },
{ status: 404 }
);
}
contactRecord.customerId = customerId;
}
// Update contact record fields
contactRecord.contactType = contactType;
contactRecord.notes = notes || '';
// Save the updated contact record
const updatedContactRecord = await contactRecordRepository.save(contactRecord);
return NextResponse.json(updatedContactRecord);
} catch (error) {
console.error('Error updating contact record:', error);
return NextResponse.json(
{ error: 'Failed to update contact record' },
{ status: 500 }
);
}
}
// DELETE /api/contact-records/[id] - Delete a contact record
export async function DELETE(
request: NextRequest,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const dataSource = await getDataSource();
const contactRecordRepository = dataSource.getRepository(ContactRecord);
// Find the contact record to delete
const contactRecord = await contactRecordRepository.findOne({
where: { id: id }
});
if (!contactRecord) {
return NextResponse.json(
{ error: 'Contact record not found' },
{ status: 404 }
);
}
// Delete the contact record
await contactRecordRepository.remove(contactRecord);
return NextResponse.json({ success: true });
} catch (error) {
console.error('Error deleting contact record:', error);
return NextResponse.json(
{ error: 'Failed to delete contact record' },
{ status: 500 }
);
}
}