/** * Test script for syncing time entries from Autotask * Run with: npx tsx dev/test-time-entries-sync.ts */ import dotenv from 'dotenv'; import path from 'path'; import { AutotaskClient } from '@/lib/services/autotask-client'; import { EntitySyncService } from '@/lib/services/entity-sync'; import { EntityType } from '@/lib/types/sync'; // Load environment variables dotenv.config({ path: path.resolve(__dirname, '../.env.local') }); async function testTimeEntriesSync() { console.log('šŸ” Testing Time Entries Sync\n'); // Validate environment variables const requiredEnvVars = [ 'AUTOTASK_API_URL', 'AUTOTASK_USERNAME', 'AUTOTASK_SECRET', 'AUTOTASK_API_INTEGRATION_CODE', ]; const missingVars = requiredEnvVars.filter(v => !process.env[v]); if (missingVars.length > 0) { console.error('āŒ Missing required environment variables:', missingVars.join(', ')); process.exit(1); } try { // Initialize Autotask client console.log('Initializing Autotask client...'); const autotaskClient = new AutotaskClient({ apiUrl: process.env.AUTOTASK_API_URL!, username: process.env.AUTOTASK_USERNAME!, password: process.env.AUTOTASK_SECRET!, apiIntegrationCode: process.env.AUTOTASK_API_INTEGRATION_CODE!, }); console.log('āœ… Autotask client initialized\n'); // Initialize entity sync service console.log('Initializing entity sync service...'); const syncService = new EntitySyncService(autotaskClient); console.log('āœ… Entity sync service initialized\n'); // Test sync with a small date range (last 7 days) console.log('Starting time entries sync (last 7 days)...'); console.log('This will sync time entries from the last 7 days only.\n'); const startTime = Date.now(); try { const result = await syncService.syncEntity( EntityType.TIME_ENTRIES, false, // Full sync (not incremental) 0.019 // 7 days in years ); const duration = Date.now() - startTime; console.log('\nāœ… Sync completed successfully!'); console.log(`Duration: ${(duration / 1000).toFixed(2)}s`); console.log('\nResults:'); console.log(` Records added: ${result.recordsAdded}`); console.log(` Records updated: ${result.recordsUpdated}`); console.log(` Records deleted: ${result.recordsDeleted}`); console.log(` Total processed: ${result.recordsAdded + result.recordsUpdated}`); } catch (syncError) { console.error('\nāŒ Sync failed with error:'); console.error(syncError); // Try to provide more details if (syncError instanceof Error) { console.error('\nError details:'); console.error(' Message:', syncError.message); console.error(' Stack:', syncError.stack); } throw syncError; } } catch (error) { console.error('\nāŒ Test failed:', error); process.exit(1); } } // Run the test testTimeEntriesSync() .then(() => { console.log('\nāœ… Test completed successfully'); process.exit(0); }) .catch((error) => { console.error('\nāŒ Test failed:', error); process.exit(1); });