All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 49s
BREAKING: Violación de arquitectura corregida - Eliminar endpoint /api/postgres/query (acceso directo a DB prohibido) - Cambiar /api/clientes para usar query de Metabase en lugar de SQL directo - Crear endpoint /api/metabase/opciones-filtros para obtener opciones - Cambiar loadOpcionesFiltros para usar API en lugar de MCP directo - Usar "Informe Ingresos - Lista de Clientes con Totales" para clientes - Usar "Informe Ingresos - Opciones de Filtros" para opciones - Respetar filosofía: Metabase calcula TODO, Vue solo renderiza - La app NUNCA habla directamente con bases de datos
65 lines
2.4 KiB
TypeScript
65 lines
2.4 KiB
TypeScript
/**
|
|
* 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'
|
|
})
|
|
}
|
|
})
|