Add customer and contact record management features
This commit is contained in:
83
src/app/api/contact-records/route.ts
Normal file
83
src/app/api/contact-records/route.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDataSource, ContactRecord, Customer } from '@/lib/database';
|
||||
|
||||
// GET /api/contact-records - Get all contact records
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const dataSource = await getDataSource();
|
||||
const contactRecordRepository = dataSource.getRepository(ContactRecord);
|
||||
|
||||
// Get query parameters
|
||||
const url = new URL(request.url);
|
||||
const customerId = url.searchParams.get('customerId');
|
||||
|
||||
// Build query
|
||||
const queryOptions: any = {
|
||||
order: { createdAt: 'DESC' },
|
||||
relations: ['customer']
|
||||
};
|
||||
|
||||
// Filter by customer if customerId is provided
|
||||
if (customerId) {
|
||||
queryOptions.where = { customerId };
|
||||
}
|
||||
|
||||
const contactRecords = await contactRecordRepository.find(queryOptions);
|
||||
|
||||
return NextResponse.json(contactRecords);
|
||||
} catch (error) {
|
||||
console.error('Error fetching contact records:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch contact records' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/contact-records - Create a new contact record
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const dataSource = await getDataSource();
|
||||
const contactRecordRepository = dataSource.getRepository(ContactRecord);
|
||||
const customerRepository = dataSource.getRepository(Customer);
|
||||
|
||||
const data = await request.json();
|
||||
const { customerId, contactType, notes } = data;
|
||||
|
||||
// Validate required fields
|
||||
if (!customerId || !contactType) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Customer ID and contact type are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify customer exists
|
||||
const customer = await customerRepository.findOne({
|
||||
where: { id: customerId }
|
||||
});
|
||||
|
||||
if (!customer) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Customer not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create and save the new contact record
|
||||
const contactRecord = new ContactRecord();
|
||||
contactRecord.customerId = customerId;
|
||||
contactRecord.contactType = contactType;
|
||||
contactRecord.notes = notes || '';
|
||||
|
||||
const savedContactRecord = await contactRecordRepository.save(contactRecord);
|
||||
|
||||
return NextResponse.json(savedContactRecord, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating contact record:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create contact record' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user