130 lines
4.4 KiB
TypeScript
130 lines
4.4 KiB
TypeScript
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');
|
|
const contactType = url.searchParams.get('contactType');
|
|
const dateFrom = url.searchParams.get('dateFrom');
|
|
const dateTo = url.searchParams.get('dateTo');
|
|
|
|
// Pagination parameters
|
|
const page = parseInt(url.searchParams.get('page') || '1');
|
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
|
const skip = (page - 1) * pageSize;
|
|
|
|
// Build query
|
|
let queryBuilder = contactRecordRepository.createQueryBuilder('contactRecord')
|
|
.leftJoinAndSelect('contactRecord.customer', 'customer')
|
|
.orderBy('contactRecord.createdAt', 'DESC');
|
|
|
|
// Apply filters
|
|
if (customerId) {
|
|
queryBuilder = queryBuilder.andWhere('contactRecord.customerId = :customerId', { customerId });
|
|
}
|
|
|
|
if (contactType) {
|
|
queryBuilder = queryBuilder.andWhere('contactRecord.contactType = :contactType', { contactType });
|
|
}
|
|
|
|
if (dateFrom) {
|
|
queryBuilder = queryBuilder.andWhere('contactRecord.createdAt >= :dateFrom', {
|
|
dateFrom: new Date(dateFrom)
|
|
});
|
|
}
|
|
|
|
if (dateTo) {
|
|
// Set the date to the end of the day for inclusive filtering
|
|
const endDate = new Date(dateTo);
|
|
endDate.setHours(23, 59, 59, 999);
|
|
queryBuilder = queryBuilder.andWhere('contactRecord.createdAt <= :dateTo', {
|
|
dateTo: endDate
|
|
});
|
|
}
|
|
|
|
// Get total count for pagination
|
|
const totalCount = await queryBuilder.getCount();
|
|
|
|
// Apply pagination
|
|
queryBuilder = queryBuilder
|
|
.skip(skip)
|
|
.take(pageSize);
|
|
|
|
// Execute query
|
|
const contactRecords = await queryBuilder.getMany();
|
|
|
|
// Calculate total pages
|
|
const totalPages = Math.ceil(totalCount / pageSize);
|
|
|
|
return NextResponse.json({
|
|
data: contactRecords,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
totalCount,
|
|
totalPages
|
|
}
|
|
});
|
|
} 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 }
|
|
);
|
|
}
|
|
}
|