39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getAutotaskClient } from '@/lib/services/autotask-factory';
|
|
import { apiCache } from '@/lib/services/cache';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
const cacheKey = `contact:${id}`;
|
|
|
|
// Check cache first
|
|
const cached = apiCache.get(cacheKey);
|
|
if (cached) {
|
|
return NextResponse.json(cached);
|
|
}
|
|
|
|
const autotaskClient = getAutotaskClient();
|
|
|
|
// Query for the contact by ID
|
|
const contacts = await autotaskClient.queryEntity('Contacts', {
|
|
filter: [{ op: 'eq', field: 'id', value: parseInt(id) }],
|
|
});
|
|
|
|
const contact = contacts.length > 0 ? contacts[0] : null;
|
|
|
|
// Cache for 10 minutes
|
|
apiCache.set(cacheKey, { contact }, 10 * 60); // corrected the cache expiration time
|
|
|
|
return NextResponse.json({ contact });
|
|
} catch (error) {
|
|
console.error('Error fetching contact:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch contact' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|