import Fastify from 'fastify' import cookie from '@fastify/cookie' import cors from '@fastify/cors' import cron from 'node-cron' import { initDb } from './db.js' import { bookingRoutes } from './routes/bookings.js' import { configRoutes } from './routes/config.js' import { workforceRoutes } from './routes/workforce.js' import { runRotaSync, runTimesheetSync } from './lib/syncJobs.js' const app = Fastify({ logger: true, trustProxy: true }) const startedAt = Date.now() await app.register(cookie) await app.register(cors, { origin: process.env.CORS_ORIGIN || false, credentials: true }) app.get('/health', async () => ({ status: 'healthy', version: process.env.BUILD_VERSION || String(startedAt) })) await app.register(bookingRoutes) await app.register(configRoutes) await app.register(workforceRoutes) try { await initDb() await app.listen({ port: 3001, host: '0.0.0.0' }) } catch (err) { app.log.error(err) process.exit(1) } // Daily 6am: rota sync (today-7 → today+28) then timesheet sync (last 7 days) cron.schedule('0 6 * * *', async () => { app.log.info('cron: starting daily workforce sync') try { const r = await runRotaSync() app.log.info({ dates_synced: r.dates_synced }, 'cron: rota sync complete') } catch (err) { app.log.error({ err: err.message }, 'cron: rota sync failed') } try { const r = await runTimesheetSync() app.log.info({ dates_synced: r.dates_synced }, 'cron: timesheet sync complete') } catch (err) { app.log.error({ err: err.message }, 'cron: timesheet sync failed') } })