/** * routes/periods.mjs — Period management endpoints */ export default function registerPeriods(app, pool) { // GET /api/ledger/periods — lista perioder app.get('/api/ledger/periods', async (req, res) => { const tenant_id = req.headers['x-tenant-id'] || 'wavult-group'; const { fiscal_year } = req.query; const cond = ['tenant_id=$1']; const params = [tenant_id]; if (fiscal_year) { cond.push('fiscal_year=$2'); params.push(parseInt(fiscal_year)); } try { const { rows } = await pool.query( `SELECT * FROM ledger_periods WHERE ${cond.join(' AND ')} ORDER BY fiscal_year, period`, params ); res.json({ ok: true, periods: rows }); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); }