Add /api/health diagnostic route; remove stale .js route duplicates

This commit is contained in:
Joel Brock
2026-05-09 20:45:14 -07:00
parent c58a49be6d
commit 234bec5766
3 changed files with 263 additions and 115 deletions

View File

@@ -1,60 +0,0 @@
import { NextResponse } from 'next/server';
import { civicrmApi } from '@/lib/civicrm';
import mapping from '@/config/mapping.json';
export async function GET(request) {
const { searchParams } = new URL(request.url);
const orgId = searchParams.get('orgId');
const contactId = searchParams.get('contactId');
if (!orgId || !contactId) {
return NextResponse.json({ error: 'Missing orgId or contactId' }, { status: 400 });
}
try {
// 1. Fetch current stage from Organization (Contact record with type Organization)
const orgResult = await civicrmApi('Contact', 'get', {
select: [mapping.stageField],
where: [['id', '=', parseInt(orgId)]],
});
const currentStage = orgResult.values[0]?.[mapping.stageField] || 0;
// 2. Prepare select fields for pre-filling contact data
const selectFields = ['id'];
mapping.stages.forEach(stage => {
stage.fields.forEach(field => {
if (field.entity === 'Contact') {
selectFields.push(field.crmField);
}
});
});
// 3. Fetch contact data for pre-filling
const contactResult = await civicrmApi('Contact', 'get', {
select: selectFields,
where: [['id', '=', parseInt(contactId)]],
});
const contactData = contactResult.values[0] || {};
return NextResponse.json({
currentStage: parseInt(currentStage),
prefillData: contactData,
});
} catch (error) {
console.error('API Error:', error);
// Return mock data if CiviCRM is not reachable (for development/demo)
if (process.env.NODE_ENV === 'development') {
return NextResponse.json({
currentStage: 2,
prefillData: {
first_name: 'John',
last_name: 'Doe',
'email_primary.email': 'john@example.com'
}
});
}
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}