2026-02-03 20:48:08 -05:00
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { postgresClient } from '@/lib/services/postgres-client';
|
|
|
|
|
import { getAutotaskClient } from '@/lib/services/autotask-factory';
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('Starting classification icons sync...');
|
|
|
|
|
|
2026-03-31 22:38:22 -04:00
|
|
|
// Fetch classification picklist from Companies field info
|
2026-02-03 20:48:08 -05:00
|
|
|
const autotaskClient = getAutotaskClient();
|
2026-03-31 22:38:22 -04:00
|
|
|
const fields = await autotaskClient.getFieldInfo('Companies');
|
|
|
|
|
const classificationField = fields.find((f: any) => f.name === 'classification');
|
|
|
|
|
|
|
|
|
|
if (!classificationField || !classificationField.picklistValues?.length) {
|
2026-02-03 20:48:08 -05:00
|
|
|
return NextResponse.json(
|
2026-03-31 22:38:22 -04:00
|
|
|
{ error: 'No classification picklist found in Companies field info' },
|
2026-02-03 20:48:08 -05:00
|
|
|
{ status: 404 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 22:38:22 -04:00
|
|
|
const classifications = classificationField.picklistValues;
|
|
|
|
|
console.log(`Fetched ${classifications.length} classification values from Autotask`);
|
2026-02-03 20:48:08 -05:00
|
|
|
|
|
|
|
|
let inserted = 0;
|
|
|
|
|
let updated = 0;
|
|
|
|
|
|
|
|
|
|
// Upsert each classification
|
|
|
|
|
for (const classification of classifications) {
|
|
|
|
|
const result = await postgresClient.query(
|
|
|
|
|
`INSERT INTO company_classifications (
|
2026-03-31 22:38:22 -04:00
|
|
|
classification_id,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
2026-02-03 20:48:08 -05:00
|
|
|
is_active,
|
|
|
|
|
is_system,
|
|
|
|
|
updated_at,
|
|
|
|
|
synced_at
|
|
|
|
|
) VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
2026-03-31 22:38:22 -04:00
|
|
|
ON CONFLICT (classification_id)
|
|
|
|
|
DO UPDATE SET
|
2026-02-03 20:48:08 -05:00
|
|
|
name = EXCLUDED.name,
|
|
|
|
|
description = EXCLUDED.description,
|
|
|
|
|
is_active = EXCLUDED.is_active,
|
|
|
|
|
is_system = EXCLUDED.is_system,
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP,
|
|
|
|
|
synced_at = CURRENT_TIMESTAMP
|
|
|
|
|
RETURNING (xmax = 0) AS inserted`,
|
|
|
|
|
[
|
2026-03-31 22:38:22 -04:00
|
|
|
parseInt(String(classification.value)),
|
|
|
|
|
classification.label,
|
|
|
|
|
null,
|
2026-02-03 20:48:08 -05:00
|
|
|
classification.isActive !== false,
|
|
|
|
|
classification.isSystem || false
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (result.rows[0]?.inserted) {
|
|
|
|
|
inserted++;
|
|
|
|
|
} else {
|
|
|
|
|
updated++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`Classification sync complete: ${inserted} inserted, ${updated} updated`);
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
success: true,
|
|
|
|
|
total: classifications.length,
|
|
|
|
|
inserted,
|
|
|
|
|
updated,
|
2026-03-31 22:38:22 -04:00
|
|
|
message: `Synced ${classifications.length} classifications from Companies field info`
|
2026-02-03 20:48:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error syncing classifications:', error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
error: 'Failed to sync classifications',
|
|
|
|
|
details: error instanceof Error ? error.message : 'Unknown error'
|
|
|
|
|
},
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET(request: NextRequest) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await postgresClient.query(
|
|
|
|
|
`SELECT
|
|
|
|
|
classification_id,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
is_active,
|
|
|
|
|
is_system,
|
|
|
|
|
synced_at
|
|
|
|
|
FROM company_classifications
|
|
|
|
|
WHERE is_active = true
|
|
|
|
|
ORDER BY name`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
classifications: result.rows.map(row => ({
|
|
|
|
|
id: row.classification_id,
|
|
|
|
|
name: row.name,
|
|
|
|
|
description: row.description,
|
|
|
|
|
isActive: row.is_active,
|
|
|
|
|
isSystem: row.is_system,
|
|
|
|
|
syncedAt: row.synced_at
|
|
|
|
|
}))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching classifications:', error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'Failed to fetch classifications' },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|