v1.0
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import pool from '../db/index.js';
|
||||
import { calculatePrice } from '../services/priceEngine.js';
|
||||
|
||||
export async function postPrice(req, res) {
|
||||
try {
|
||||
const config = req.body || {};
|
||||
|
||||
// Fetch all required data
|
||||
const [
|
||||
products,
|
||||
blockTypes,
|
||||
legacyFinishes,
|
||||
formexMaterials,
|
||||
serviceOptions,
|
||||
settingsRows,
|
||||
modulesData,
|
||||
handlesData,
|
||||
hingesData,
|
||||
blumData
|
||||
] = await Promise.all([
|
||||
pool.query('SELECT id, code, label FROM products WHERE active = true'),
|
||||
pool.query('SELECT id, code, label, base_price_chf, width_min_mm, width_max_mm FROM block_types WHERE active = true'),
|
||||
pool.query(`
|
||||
SELECT f.id, f.code, f.label, pf.width_mm, pf.height_mm, pf.price_chf
|
||||
FROM finishes f JOIN panel_formats pf ON pf.id = f.panel_format_id WHERE f.active = true
|
||||
`),
|
||||
pool.query(`
|
||||
SELECT id, formex_code as code, name as label, name, collection, category,
|
||||
width_mm, height_mm, thickness_mm, image_url, price_chf,
|
||||
dominant_color as hexColor
|
||||
FROM formex_materials WHERE active = 1
|
||||
`),
|
||||
pool.query('SELECT id, code, label, price_chf, price_type FROM service_options WHERE active = true'),
|
||||
pool.query('SELECT key, value FROM settings'),
|
||||
pool.query('SELECT id, code, name, category, width_mm, shelves_count, drawers_count, has_rod, base_price_chf FROM modules WHERE active = 1'),
|
||||
pool.query('SELECT id, code, name, style, price_chf FROM handle_variants WHERE active = 1'),
|
||||
pool.query('SELECT id, code, name, type, price_chf FROM hinge_variants WHERE active = 1'),
|
||||
pool.query('SELECT id, code, name, type, variant, price_chf, specs_json FROM blum_hardware WHERE active = 1')
|
||||
]);
|
||||
|
||||
const settings = settingsRows.rows.map((r) => ({ key: r.key, value: r.value }));
|
||||
|
||||
// Use Formex materials if available, otherwise fall back to legacy finishes
|
||||
let finishes = legacyFinishes.rows;
|
||||
if (formexMaterials.rows.length > 0) {
|
||||
// Fetch dimension variants for Formex materials
|
||||
const materialsWithDims = await Promise.all(
|
||||
formexMaterials.rows.map(async (mat) => {
|
||||
const dims = await pool.query(
|
||||
'SELECT id, width_mm, height_mm, thickness_mm, is_default FROM formex_dimensions WHERE material_id = ?',
|
||||
[mat.id]
|
||||
);
|
||||
return {
|
||||
...mat,
|
||||
dimensions: dims.rows
|
||||
};
|
||||
})
|
||||
);
|
||||
finishes = materialsWithDims;
|
||||
}
|
||||
|
||||
const data = {
|
||||
settings,
|
||||
blockTypes: blockTypes.rows,
|
||||
finishes,
|
||||
serviceOptions: serviceOptions.rows,
|
||||
modules: modulesData.rows,
|
||||
handles: handlesData.rows,
|
||||
hinges: hingesData.rows,
|
||||
blumHardware: blumData.rows
|
||||
};
|
||||
|
||||
const result = calculatePrice(config, data);
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error('Price calculation error:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user