Co-authored-by: Cursor <[email protected]>
This commit is contained in:
lejay
2026-05-28 07:20:38 +02:00
co-authored by Cursor
parent 6923c319a1
commit 6c0d7e47b8
81 changed files with 21780 additions and 2986 deletions
+197 -33
View File
@@ -1,7 +1,5 @@
/**
* Sync routes for Formex crawler
*/
import { sync, getSyncLogs, getMaterials } from '../services/formexCrawler.js';
import { syncHaefeleByUrls } from '../services/haefeleCrawler.js';
import { getSchedulerStatus } from '../services/scheduler.js';
import pool, { run } from '../db/index.js';
@@ -58,9 +56,10 @@ export async function streamFormexSync(req, res) {
// Client disconnected, cleanup if needed
});
const force = req.query.force === '1' || req.query.force === 'true';
try {
// Run sync with emitter
await sync(emit);
await sync(emit, { force });
} catch (err) {
emit({ type: 'error', error: err.message });
}
@@ -69,6 +68,144 @@ export async function streamFormexSync(req, res) {
res.end();
}
/**
* POST /api/sync/haefele
* Trigger Häfele sync for a provided list of product URLs
* Body: { urls: string[] }
*/
export async function triggerHaefeleSync(req, res) {
try {
const urls = Array.isArray(req.body?.urls) ? req.body.urls : [];
const credentials = {
username: req.body?.username,
password: req.body?.password,
customerNo: req.body?.customerNo
};
if (urls.length === 0) {
return res.status(400).json({ error: 'Le champ urls (tableau) est requis.' });
}
const result = await syncHaefeleByUrls(urls, () => {}, { credentials });
if (!result.success) {
return res.status(500).json({
success: false,
error: result.error || 'Sync Häfele échouée',
itemsCount: result.itemsCount || 0
});
}
res.json({
success: true,
message: `Synchronisation Häfele terminée: ${result.itemsCount} produits`,
itemsCount: result.itemsCount,
failedCount: result.failedCount || 0,
duration: result.duration
});
} catch (err) {
res.status(500).json({
success: false,
error: err.message
});
}
}
/**
* GET /api/sync/haefele/stream
* SSE endpoint for Häfele sync progress (URL list from query params)
* Query:
* - url=... (repeatable)
* - urls=... (newline/comma/semicolon separated fallback)
*/
export async function streamHaefeleSync(req, res) {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders();
const emit = (data) => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
};
req.on('close', () => {
// client disconnected
});
const repeatable = req.query.url;
let urls = [];
if (Array.isArray(repeatable)) {
urls = repeatable;
} else if (typeof repeatable === 'string' && repeatable.trim()) {
urls = [repeatable];
} else if (typeof req.query.urls === 'string') {
urls = req.query.urls.split(/[\n,;]+/g);
}
try {
await syncHaefeleByUrls(urls, emit);
} catch (err) {
emit({ type: 'error', error: err.message });
}
res.end();
}
/**
* GET /api/haefele/materials
* List Häfele materials that were fetched/synced.
*/
export async function getHaefeleMaterials(req, res) {
try {
const { search = '', active, page = '1', limit = '25' } = req.query;
const pageNum = Math.max(1, parseInt(page, 10));
const limitNum = Math.min(200, Math.max(1, parseInt(limit, 10)));
const offset = (pageNum - 1) * limitNum;
const whereClauses = [
`(supplier_id = (SELECT id FROM suppliers WHERE code = 'haefele') OR collection = 'Hafele' OR formex_code LIKE 'HAF-%')`
];
const params = [];
if (search) {
whereClauses.push('(name LIKE ? OR formex_code LIKE ? OR formex_url LIKE ?)');
params.push(`%${search}%`, `%${search}%`, `%${search}%`);
}
if (active === '1' || active === 'true') {
whereClauses.push('active = 1');
} else if (active === '0' || active === 'false') {
whereClauses.push('active = 0');
}
const whereSQL = `WHERE ${whereClauses.join(' AND ')}`;
const countResult = await pool.query(
`SELECT COUNT(*) as total FROM formex_materials ${whereSQL}`,
params
);
const total = countResult.rows[0]?.total || 0;
const { rows } = await pool.query(
`SELECT id, formex_code, name, category, thickness_mm, price_chf, price_unit, image_url, formex_url, active, last_synced_at
FROM formex_materials
${whereSQL}
ORDER BY last_synced_at DESC, id DESC
LIMIT ? OFFSET ?`,
[...params, limitNum, offset]
);
res.json({
materials: rows,
pagination: {
page: pageNum,
limit: limitNum,
total,
totalPages: Math.ceil(total / limitNum)
}
});
} catch (err) {
res.status(500).json({ error: err.message });
}
}
/**
* GET /api/sync/logs
* Get sync history
@@ -98,47 +235,50 @@ export function getStatus(req, res) {
*/
export async function getFormexMaterials(req, res) {
try {
const { search, collection, category, active, page = '1', limit = '25' } = req.query;
const { search, collection, category, active, supplier, page = '1', limit = '25' } = req.query;
const pageNum = Math.max(1, parseInt(page, 10));
const limitNum = Math.min(100, Math.max(1, parseInt(limit, 10)));
const offset = (pageNum - 1) * limitNum;
// Build query with optional filters
let whereClauses = [];
let params = [];
if (search) {
whereClauses.push('(name LIKE ? OR formex_code LIKE ?)');
whereClauses.push('(m.name LIKE ? OR m.formex_code LIKE ?)');
params.push(`%${search}%`, `%${search}%`);
}
if (collection) {
whereClauses.push('collection = ?');
whereClauses.push('m.collection = ?');
params.push(collection);
}
if (category) {
whereClauses.push('category = ?');
whereClauses.push('m.category = ?');
params.push(category);
}
// For configurator: only active materials
if (active === '1' || active === 'true') {
whereClauses.push('active = 1');
whereClauses.push('m.active = 1');
}
if (supplier) {
whereClauses.push('s.code = ?');
params.push(supplier);
}
const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(' AND ')}` : '';
// Get total count
const countResult = await pool.query(
`SELECT COUNT(*) as total FROM formex_materials ${whereSQL}`,
`SELECT COUNT(*) as total FROM formex_materials m LEFT JOIN suppliers s ON m.supplier_id = s.id ${whereSQL}`,
params
);
const total = countResult.rows[0]?.total || 0;
// Get paginated results
const { rows } = await pool.query(
`SELECT * FROM formex_materials ${whereSQL} ORDER BY collection, name LIMIT ? OFFSET ?`,
`SELECT m.*, s.code AS supplier_code, s.name AS supplier_name
FROM formex_materials m LEFT JOIN suppliers s ON m.supplier_id = s.id
${whereSQL} ORDER BY m.collection, m.name LIMIT ? OFFSET ?`,
[...params, limitNum, offset]
);
@@ -202,14 +342,14 @@ export async function getFormexMaterial(req, res) {
*/
export async function createFormexMaterial(req, res) {
try {
const { formex_code, name, collection, category, width_mm, height_mm, thickness_mm, image_url, formex_url, price_chf, active } = req.body;
const { formex_code, name, collection, category, width_mm, height_mm, thickness_mm,
diameter_mm, length_mm, bore_diameter_mm, bore_depth_base_mm, bore_depth_piece_mm,
image_url, formex_url, price_chf, price_unit, active } = req.body;
// Validation
if (!formex_code || !name || !collection) {
return res.status(400).json({ error: 'formex_code, name et collection sont requis' });
}
// Check for duplicate code
const existing = await pool.query(
'SELECT id FROM formex_materials WHERE formex_code = ?',
[formex_code]
@@ -219,13 +359,17 @@ export async function createFormexMaterial(req, res) {
}
await run(
`INSERT INTO formex_materials (formex_code, name, collection, category, width_mm, height_mm, thickness_mm, image_url, formex_url, price_chf, active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[formex_code, name, collection, category || null, width_mm || null, height_mm || null, thickness_mm || null,
image_url || null, formex_url || null, price_chf || null, active !== undefined ? (active ? 1 : 0) : 1]
`INSERT INTO formex_materials (formex_code, name, collection, category, width_mm, height_mm, thickness_mm,
diameter_mm, length_mm, bore_diameter_mm, bore_depth_base_mm, bore_depth_piece_mm,
image_url, formex_url, price_chf, price_unit, active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[formex_code, name, collection, category || null, width_mm || null, height_mm || null, thickness_mm || null,
diameter_mm ?? null, length_mm ?? null,
bore_diameter_mm ?? null, bore_depth_base_mm ?? null, bore_depth_piece_mm ?? null,
image_url || null, formex_url || null, price_chf ?? null, price_unit || null,
active !== undefined ? (active ? 1 : 0) : 1]
);
// Get the created material
const { rows } = await pool.query(
'SELECT * FROM formex_materials WHERE formex_code = ?',
[formex_code]
@@ -244,9 +388,10 @@ export async function createFormexMaterial(req, res) {
export async function updateFormexMaterial(req, res) {
try {
const { id } = req.params;
const { formex_code, name, collection, category, width_mm, height_mm, thickness_mm, image_url, formex_url, price_chf, active } = req.body;
const { formex_code, name, collection, category, width_mm, height_mm, thickness_mm,
diameter_mm, length_mm, bore_diameter_mm, bore_depth_base_mm, bore_depth_piece_mm,
image_url, formex_url, price_chf, price_unit, active } = req.body;
// Check if material exists
const existing = await pool.query(
'SELECT * FROM formex_materials WHERE id = ?',
[id]
@@ -255,12 +400,10 @@ export async function updateFormexMaterial(req, res) {
return res.status(404).json({ error: 'Matériau non trouvé' });
}
// Validation
if (!formex_code || !name || !collection) {
return res.status(400).json({ error: 'formex_code, name et collection sont requis' });
}
// Check for duplicate code (if changed)
if (formex_code !== existing.rows[0].formex_code) {
const duplicate = await pool.query(
'SELECT id FROM formex_materials WHERE formex_code = ? AND id != ?',
@@ -273,14 +416,18 @@ export async function updateFormexMaterial(req, res) {
await run(
`UPDATE formex_materials
SET formex_code = ?, name = ?, collection = ?, category = ?, width_mm = ?, height_mm = ?, thickness_mm = ?,
image_url = ?, formex_url = ?, price_chf = ?, active = ?
SET formex_code = ?, name = ?, collection = ?, category = ?, width_mm = ?, height_mm = ?, thickness_mm = ?,
diameter_mm = ?, length_mm = ?,
bore_diameter_mm = ?, bore_depth_base_mm = ?, bore_depth_piece_mm = ?,
image_url = ?, formex_url = ?, price_chf = ?, price_unit = ?, active = ?
WHERE id = ?`,
[formex_code, name, collection, category || null, width_mm || null, height_mm || null, thickness_mm || null,
image_url || null, formex_url || null, price_chf || null, active !== undefined ? (active ? 1 : 0) : 1, id]
[formex_code, name, collection, category || null, width_mm || null, height_mm || null, thickness_mm || null,
diameter_mm ?? null, length_mm ?? null,
bore_diameter_mm ?? null, bore_depth_base_mm ?? null, bore_depth_piece_mm ?? null,
image_url || null, formex_url || null, price_chf ?? null, price_unit || null,
active !== undefined ? (active ? 1 : 0) : 1, id]
);
// Get the updated material
const { rows } = await pool.query(
'SELECT * FROM formex_materials WHERE id = ?',
[id]
@@ -317,6 +464,23 @@ export async function deleteFormexMaterial(req, res) {
}
}
/**
* GET /api/formex/suppliers
* Get suppliers that have at least one material linked
*/
export async function getFormexSuppliers(req, res) {
try {
const { rows } = await pool.query(
`SELECT DISTINCT s.code, s.name FROM suppliers s
INNER JOIN formex_materials m ON m.supplier_id = s.id
ORDER BY s.name`
);
res.json(rows);
} catch (err) {
res.status(500).json({ error: err.message });
}
}
/**
* GET /api/formex/collections
* Get distinct collection names for filter dropdown