fix: API funcionando com dados reais + dotenv config
- Adiciona dotenv para carregar variáveis de ambiente - Configura DB_HOST para servidor remoto (176.9.3.158) - Cria endpoint /api/diagnostic para testes - Actualiza título: "Plan EAL" → "Dashboard Descomplicar" - Adiciona tsconfig.json para pasta /api - Fix: Carrega .env antes de inicializar MySQL pool Tarefa: #1556 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
* Database Connection Pool
|
||||
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
||||
*/
|
||||
import 'dotenv/config'
|
||||
import mysql from 'mysql2/promise'
|
||||
|
||||
// Database configuration
|
||||
|
||||
76
api/routes/diagnostic.ts
Normal file
76
api/routes/diagnostic.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Diagnostic API Route
|
||||
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
||||
*/
|
||||
import { Router } from 'express'
|
||||
import type { Request, Response } from 'express'
|
||||
import * as dashboardService from '../services/dashboard.js'
|
||||
import * as calendarService from '../services/calendar.js'
|
||||
import db from '../db.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
router.get('/', async (req: Request, res: Response) => {
|
||||
const tests = []
|
||||
|
||||
// Test database connection
|
||||
try {
|
||||
await db.query('SELECT 1')
|
||||
tests.push({ name: 'DB Connection', status: 'OK' })
|
||||
} catch (error: any) {
|
||||
tests.push({ name: 'DB Connection', status: 'FAILED', error: error.message })
|
||||
}
|
||||
|
||||
// Test each dashboard service function
|
||||
const functions = [
|
||||
{ name: 'getUrgenteTasks', fn: dashboardService.getUrgenteTasks },
|
||||
{ name: 'getAltaTasks', fn: dashboardService.getAltaTasks },
|
||||
{ name: 'getVencidasTasks', fn: dashboardService.getVencidasTasks },
|
||||
{ name: 'getEmTestesTasks', fn: dashboardService.getEmTestesTasks },
|
||||
{ name: 'getEstaSemana', fn: dashboardService.getEstaSemana },
|
||||
{ name: 'getMondayMood', fn: dashboardService.getMondayMood },
|
||||
{ name: 'getTickets', fn: dashboardService.getTickets },
|
||||
{ name: 'getContactarLeads', fn: dashboardService.getContactarLeads },
|
||||
{ name: 'getFollowupLeads', fn: dashboardService.getFollowupLeads },
|
||||
{ name: 'getPropostaLeads', fn: dashboardService.getPropostaLeads },
|
||||
{ name: 'getProjectos', fn: dashboardService.getProjectos },
|
||||
{ name: 'getTimesheet', fn: dashboardService.getTimesheet },
|
||||
{ name: 'getBilling360', fn: dashboardService.getBilling360 },
|
||||
{ name: 'getPipeline', fn: dashboardService.getPipeline }
|
||||
]
|
||||
|
||||
for (const { name, fn } of functions) {
|
||||
try {
|
||||
await fn()
|
||||
tests.push({ name, status: 'OK' })
|
||||
} catch (error: any) {
|
||||
tests.push({ name, status: 'FAILED', error: error.message })
|
||||
}
|
||||
}
|
||||
|
||||
// Test calendar functions
|
||||
try {
|
||||
await calendarService.getTodayEvents()
|
||||
tests.push({ name: 'getTodayEvents', status: 'OK' })
|
||||
} catch (error: any) {
|
||||
tests.push({ name: 'getTodayEvents', status: 'FAILED', error: error.message })
|
||||
}
|
||||
|
||||
try {
|
||||
await calendarService.getWeekEvents()
|
||||
tests.push({ name: 'getWeekEvents', status: 'OK' })
|
||||
} catch (error: any) {
|
||||
tests.push({ name: 'getWeekEvents', status: 'FAILED', error: error.message })
|
||||
}
|
||||
|
||||
const failed = tests.filter(t => t.status === 'FAILED')
|
||||
const summary = {
|
||||
total: tests.length,
|
||||
passed: tests.filter(t => t.status === 'OK').length,
|
||||
failed: failed.length
|
||||
}
|
||||
|
||||
res.json({ summary, tests, failed })
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -2,10 +2,12 @@
|
||||
* Express API Server
|
||||
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
||||
*/
|
||||
import 'dotenv/config'
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import dashboardRouter from './routes/dashboard.js'
|
||||
import monitorRouter from './routes/monitor.js'
|
||||
import diagnosticRouter from './routes/diagnostic.js'
|
||||
|
||||
const app = express()
|
||||
const PORT = process.env.API_PORT || 3001
|
||||
@@ -25,6 +27,7 @@ app.get('/api/health', (req, res) => {
|
||||
// Routes
|
||||
app.use('/api/dashboard', dashboardRouter)
|
||||
app.use('/api/monitor', monitorRouter)
|
||||
app.use('/api/diagnostic', diagnosticRouter)
|
||||
|
||||
// Error handling
|
||||
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
@@ -34,7 +37,10 @@ app.use((err: any, req: express.Request, res: express.Response, next: express.Ne
|
||||
|
||||
// Start server
|
||||
app.listen(PORT, () => {
|
||||
console.log('='.repeat(50))
|
||||
console.log(`🚀 API Server running on http://localhost:${PORT}`)
|
||||
console.log(`📊 Dashboard: http://localhost:${PORT}/api/dashboard`)
|
||||
console.log(`🔍 Monitor: http://localhost:${PORT}/api/monitor`)
|
||||
console.log(`🔧 Diagnostic: http://localhost:${PORT}/api/diagnostic`)
|
||||
console.log('='.repeat(50))
|
||||
})
|
||||
|
||||
22
api/tsconfig.json
Normal file
22
api/tsconfig.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2023",
|
||||
"lib": ["ES2023"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"types": ["node"],
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "."
|
||||
},
|
||||
"include": ["./**/*.ts"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user