import pool from '../db/index.js'; /** * GET /api/finishes * Returns active Formex materials as finishes for the configurator * Falls back to legacy finishes if no Formex materials exist */ export async function getFinishes(req, res) { try { // Check if we have active Formex materials const formexResult = await pool.query( `SELECT COUNT(*) as count FROM formex_materials WHERE active = 1` ); const hasFormexMaterials = (formexResult.rows[0]?.count || 0) > 0; if (hasFormexMaterials) { // Return Formex materials as finishes const { rows } = await pool.query(` SELECT id, formex_code as code, name as label, name, collection, category, width_mm, height_mm, thickness_mm, image_url, formex_url, price_chf, dominant_color as hexColor, 'formex' as source FROM formex_materials WHERE active = 1 ORDER BY collection, name `); // For each material, get its dimension variants for (const material of rows) { const dims = await pool.query( `SELECT id, width_mm, height_mm, thickness_mm, is_default FROM formex_dimensions WHERE material_id = ? ORDER BY is_default DESC, width_mm`, [material.id] ); material.dimensions = dims.rows; } res.json(rows); } else { // Fallback to legacy finishes const { rows } = await pool.query(` SELECT f.id, f.code, f.label, f.panel_format_id, pf.width_mm, pf.height_mm, pf.price_chf, 'legacy' as source FROM finishes f JOIN panel_formats pf ON pf.id = f.panel_format_id WHERE f.active = 1 ORDER BY f.id `); res.json(rows); } } catch (err) { res.status(500).json({ error: err.message }); } }