add pagination
This commit is contained in:
@ -14,49 +14,63 @@ export async function GET(request: NextRequest) {
|
||||
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
|
||||
const queryOptions: any = {
|
||||
order: { createdAt: 'DESC' },
|
||||
relations: ['customer']
|
||||
};
|
||||
let queryBuilder = contactRecordRepository.createQueryBuilder('contactRecord')
|
||||
.leftJoinAndSelect('contactRecord.customer', 'customer')
|
||||
.orderBy('contactRecord.createdAt', 'DESC');
|
||||
|
||||
// Build where clause
|
||||
let whereClause: any = {};
|
||||
|
||||
// Filter by customer if customerId is provided
|
||||
// Apply filters
|
||||
if (customerId) {
|
||||
whereClause.customerId = customerId;
|
||||
queryBuilder = queryBuilder.andWhere('contactRecord.customerId = :customerId', { customerId });
|
||||
}
|
||||
|
||||
// Filter by contact type if provided
|
||||
if (contactType) {
|
||||
whereClause.contactType = contactType;
|
||||
queryBuilder = queryBuilder.andWhere('contactRecord.contactType = :contactType', { contactType });
|
||||
}
|
||||
|
||||
// Filter by date range if provided
|
||||
if (dateFrom || dateTo) {
|
||||
whereClause.createdAt = {};
|
||||
if (dateFrom) {
|
||||
queryBuilder = queryBuilder.andWhere('contactRecord.createdAt >= :dateFrom', {
|
||||
dateFrom: new Date(dateFrom)
|
||||
});
|
||||
}
|
||||
|
||||
if (dateFrom) {
|
||||
whereClause.createdAt.gte = 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
|
||||
}
|
||||
|
||||
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);
|
||||
whereClause.createdAt.lte = endDate;
|
||||
}
|
||||
}
|
||||
|
||||
// Add where clause to query options if not empty
|
||||
if (Object.keys(whereClause).length > 0) {
|
||||
queryOptions.where = whereClause;
|
||||
}
|
||||
|
||||
const contactRecords = await contactRecordRepository.find(queryOptions);
|
||||
|
||||
return NextResponse.json(contactRecords);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching contact records:', error);
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in New Issue
Block a user