137 lines
4.2 KiB
TypeScript
137 lines
4.2 KiB
TypeScript
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 }
|
|
);
|
|
}
|
|
}
|