feat: add Node.js/Express API with real data from Desk CRM
- ✅ API completa em /api com TypeScript - ✅ Google Calendar integration (pessoal + profissional) - ✅ Queries diretas à BD: tasks, leads, projectos, billing, pipeline - ✅ Endpoints: /api/dashboard, /api/monitor, /api/health - ✅ Vite proxy configurado (/api → localhost:3001) - ✅ App.tsx usa /api/dashboard (não mais dados mock) - ✅ Migração completa do PHP (index.php + monitor.php) - ✅ CHANGELOG.md criado para tracking - ✅ Scripts npm: dev (paralelo), dev:api, dev:ui, start Dependencies: - express, cors, mysql2, googleapis - concurrently, tsx (dev) Breaking: PHP backend será descontinuado See: CHANGELOG.md, api/README.md Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
106
api/services/calendar.ts
Normal file
106
api/services/calendar.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Google Calendar Service
|
||||
* @author Descomplicar® | @link descomplicar.pt | @copyright 2026
|
||||
*/
|
||||
import { google } from 'googleapis'
|
||||
|
||||
const oauth2Client = new google.auth.OAuth2(
|
||||
'188617934470-pomrua9oj4459dk69jpv6qhvst9pd3f6.apps.googleusercontent.com',
|
||||
'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',
|
||||
'https://developers.google.com/oauthplayground'
|
||||
)
|
||||
|
||||
oauth2Client.setCredentials({
|
||||
refresh_token: '1//03AJOfA8x4_eyCgYIARAAGAMSNwF-L9Ir2hVygx8arVuZpZKJpqPsFpGCLo3pXJGC9rxpHnVw5Gki5cLWG7Ez64RcT0RFVItZ2fQ'
|
||||
})
|
||||
|
||||
const calendar = google.calendar({ version: 'v3', auth: oauth2Client })
|
||||
|
||||
interface CalendarEvent {
|
||||
titulo: string
|
||||
hora: string
|
||||
data?: string
|
||||
tipo: 'personal' | 'work'
|
||||
link: string
|
||||
}
|
||||
|
||||
export async function getEvents(calendarId: string, timeMin: string, timeMax: string): Promise<CalendarEvent[]> {
|
||||
try {
|
||||
const response = await calendar.events.list({
|
||||
calendarId,
|
||||
timeMin,
|
||||
timeMax,
|
||||
singleEvents: true,
|
||||
orderBy: 'startTime',
|
||||
maxResults: 20
|
||||
})
|
||||
|
||||
const events: CalendarEvent[] = []
|
||||
const items = response.data.items || []
|
||||
|
||||
for (const event of items) {
|
||||
const start = event.start?.dateTime || event.start?.date
|
||||
if (!start) continue
|
||||
|
||||
const startDate = new Date(start)
|
||||
const tipo = calendarId === 'primary' ? 'personal' : 'work'
|
||||
|
||||
events.push({
|
||||
titulo: event.summary || 'Sem título',
|
||||
hora: startDate.toLocaleTimeString('pt-PT', { hour: '2-digit', minute: '2-digit' }),
|
||||
data: calendarId === 'primary'
|
||||
? undefined
|
||||
: startDate.toLocaleDateString('pt-PT', { weekday: 'short', day: '2-digit', month: '2-digit' }),
|
||||
tipo,
|
||||
link: event.htmlLink || '#'
|
||||
})
|
||||
}
|
||||
|
||||
return events
|
||||
} catch (error) {
|
||||
console.error(`Calendar error (${calendarId}):`, error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTodayEvents(): Promise<CalendarEvent[]> {
|
||||
const today = new Date()
|
||||
today.setHours(0, 0, 0, 0)
|
||||
const tomorrow = new Date(today)
|
||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
||||
|
||||
const timeMin = today.toISOString()
|
||||
const timeMax = tomorrow.toISOString()
|
||||
|
||||
const [personal, work] = await Promise.all([
|
||||
getEvents('primary', timeMin, timeMax),
|
||||
getEvents('emanuel@descomplicar.pt', timeMin, timeMax)
|
||||
])
|
||||
|
||||
return [...personal, ...work].sort((a, b) => a.hora.localeCompare(b.hora))
|
||||
}
|
||||
|
||||
export async function getWeekEvents(): Promise<CalendarEvent[]> {
|
||||
const today = new Date()
|
||||
today.setHours(0, 0, 0, 0)
|
||||
const tomorrow = new Date(today)
|
||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
||||
|
||||
const nextSunday = new Date(today)
|
||||
nextSunday.setDate(today.getDate() + (7 - today.getDay()))
|
||||
nextSunday.setHours(23, 59, 59, 999)
|
||||
|
||||
const timeMin = tomorrow.toISOString()
|
||||
const timeMax = nextSunday.toISOString()
|
||||
|
||||
const [personal, work] = await Promise.all([
|
||||
getEvents('primary', timeMin, timeMax),
|
||||
getEvents('emanuel@descomplicar.pt', timeMin, timeMax)
|
||||
])
|
||||
|
||||
return [...personal, ...work].sort((a, b) => {
|
||||
const dateA = a.data + a.hora
|
||||
const dateB = b.data + b.hora
|
||||
return dateA.localeCompare(dateB)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user