/** * Get all clients from Metabase * Uses "Informe Ingresos - Lista de Clientes con Totales" query * Returns: id, name, ubicacion for use in filters */ export default defineEventHandler(async () => { try { // Get all cards to find the clientes query const allCards = await getMetabaseCards('all') // Find "Informe Ingresos - Lista de Clientes con Totales" query const card = allCards.find((c: any) => c.name === 'Informe Ingresos - Lista de Clientes con Totales') if (!card) { throw createError({ statusCode: 404, statusMessage: 'Clientes query not found in Metabase' }) } // Execute the query with empty filters to get all clientes const result = await executeCardQuery(card.id, [ { type: 'text', target: ['variable', ['template-tag', 'fecha_desde']], value: '' }, { type: 'text', target: ['variable', ['template-tag', 'fecha_hasta']], value: '' }, { type: 'boolean', target: ['variable', ['template-tag', 'incluir_anulados']], value: false }, { type: 'number', target: ['variable', ['template-tag', 'cliente_ids']], value: [] }, { type: 'text', target: ['variable', ['template-tag', 'tipos']], value: [] }, { type: 'text', target: ['variable', ['template-tag', 'estados']], value: [] }, { type: 'text', target: ['variable', ['template-tag', 'ubicaciones']], value: [] }, { type: 'text', target: ['variable', ['template-tag', 'calidades']], value: [] } ]) if (!result.data?.rows || !result.data?.cols) { return [] } // Transform rows to objects with only the fields we need const cols = result.data.cols const clientes = result.data.rows.map((row: any[]) => { const obj: any = {} cols.forEach((col: any, index: number) => { obj[col.name] = row[index] }) // Return only the fields needed for the selector return { id: obj.cliente_id, name: obj.cliente_nombre, ubicacion: obj.cliente_ubicacion, cedula: obj.cliente_cedula, // Map any other fields you need from the query result } }) // Sort by name return clientes.sort((a: any, b: any) => a.name.localeCompare(b.name)) } catch (error: any) { console.error('[API] Failed to fetch clientes from Metabase:', error) throw createError({ statusCode: error.statusCode || 500, statusMessage: error.statusMessage || 'Failed to fetch clientes from Metabase' }) } })