import type { EventConfig, Handlers } from 'motia'; import { z } from 'zod'; import { DatabaseManager, BackupStorage } from '@buzztrail/accounts-backup'; export const config: EventConfig = { type: 'event', name: 'BackupInit', description: 'Initialize backup system and discover customers', flows: ['customer-backup-flow'], subscribes: ['backup-start'], emits: ['backup-initialized'], input: z.object({ backupType: z.enum(['full', 'incremental']).default('full'), retentionDays: z.number().default(30) }) }; export const handler: Handlers['BackupInit'] = async (input, { logger, emit, state }) => { logger.info('🔧 [Motia Step] Initializing backup system...', { input }); try { // Initialize database and storage const db = DatabaseManager.getInstance(); new BackupStorage('./backup-data', db); // Get all active customers const customers = await db.getAllCustomers(); logger.info(`📋 [Motia Step] Found ${customers.length} active customers`); if (customers.length === 0) { logger.warn('⚠️ [Motia Step] No active customers found for backup'); } // Store customer list in state for next steps await state.set('backup-customers', 'current', customers); await state.set('backup-config', 'current', { backupType: input.backupType, retentionDays: input.retentionDays, startedAt: new Date().toISOString() }); await emit({ topic: 'backup-initialized', data: { customersFound: customers.length, backupType: input.backupType, retentionDays: input.retentionDays, timestamp: new Date().toISOString() } }); logger.info('✅ [Motia Step] Backup system initialized successfully'); } catch (error) { logger.error('❌ [Motia Step] Backup initialization failed:', error); throw error; } };