feat: add classification icons sync from Autotask
- Created company_classifications table to store Autotask classification icons - Added getClassificationIcons() method to AutotaskClient - Created /api/sync/classifications endpoint (GET/POST) - Updated kiosk settings UI to dynamically load classifications - Added 'Sync from Autotask' button to pull latest classifications - Removed hardcoded classification list - Display classification name and description in checkboxes - Allow excluding any classification synced from Autotask
This commit is contained in:
parent
9940c20379
commit
b222d97225
4 changed files with 253 additions and 78 deletions
117
app/api/sync/classifications/route.ts
Normal file
117
app/api/sync/classifications/route.ts
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
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...');
|
||||
|
||||
// Fetch classification icons from Autotask API
|
||||
const autotaskClient = getAutotaskClient();
|
||||
const classifications = await autotaskClient.getClassificationIcons();
|
||||
|
||||
if (!classifications || classifications.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No classifications found in Autotask' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Fetched ${classifications.length} classification icons from Autotask`);
|
||||
|
||||
let inserted = 0;
|
||||
let updated = 0;
|
||||
|
||||
// Upsert each classification
|
||||
for (const classification of classifications) {
|
||||
const result = await postgresClient.query(
|
||||
`INSERT INTO company_classifications (
|
||||
classification_id,
|
||||
name,
|
||||
description,
|
||||
is_active,
|
||||
is_system,
|
||||
updated_at,
|
||||
synced_at
|
||||
) VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (classification_id)
|
||||
DO UPDATE SET
|
||||
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`,
|
||||
[
|
||||
classification.id,
|
||||
classification.name,
|
||||
classification.description || null,
|
||||
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,
|
||||
message: `Synced ${classifications.length} classification icons`
|
||||
});
|
||||
|
||||
} 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue