feat: add ranking table sync and API endpoints

Sync Ranking table from SQL Server to PostgreSQL with ranking_ca,
ranking_qte, ranking_mag_* metrics. Expose via /api/ranking and
integrate into /api/articles/:id/referentiel response.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Opus 4.6 committed 2026-03-19 10:54:04 +01:00
1 parent 8cafc196cf
commit 792ddc71d2
6 files changed
+174

No files matched your search

+2
View File
@@ -10,6 +10,7 @@ const stockRouter = require('./routes/stock');
const mouvementsRouter = require('./routes/mouvements');
const performanceRouter = require('./routes/performance');
const syncRouter = require('./routes/sync');
const rankingRouter = require('./routes/ranking');
const app = express();
app.use(express.json());
@@ -34,6 +35,7 @@ app.use('/api/stock', stockRouter);
app.use('/api/mouvements', mouvementsRouter);
app.use('/api/performance', performanceRouter);
app.use('/api/sync', syncRouter);
app.use('/api/ranking', rankingRouter);
// 404
app.use((req, res) => {
+17
View File
@@ -222,10 +222,27 @@ router.get('/:id/referentiel', async (req, res) => {
notes = notesRes.rows;
} catch (e) { /* Table optionnelle */ }
let ranking = [];
try {
const rankRes = await pool.query(`
SELECT r.gencod, r.site, r.ranking_ca, r.ranking_qte,
r.ranking_mag_ca, r.ranking_mag_qte, r.ranking_mag_marge,
r.foucentrale, r.nomfoucentrale,
r.pv_calcule, r.pv_mag, r.pv_cen,
r.date_maj, r.date_calcul_mag
FROM ranking r
JOIN art_gtin g ON g.gtin = r.gencod
WHERE g.idarticle = $1
ORDER BY r.site
`, [id]);
ranking = rankRes.rows;
} catch (e) { /* Table optionnelle */ }
res.json({
article: article.rows[0],
gtins: gtins.rows,
gammes: gammes.rows,
ranking,
stock: stock.rows,
stock_remballe: stockRemballe,
prix: {
+66
View File
@@ -0,0 +1,66 @@
const express = require('express');
const router = express.Router();
const { getPool } = require('../config/database');
// GET /api/ranking?gencod=&codein=&site=&foucentrale=&limit=
router.get('/', async (req, res) => {
try {
const pool = getPool();
const { gencod = '', codein = '', site = '', foucentrale = '', limit = 50 } = req.query;
const limitNum = Math.max(1, Math.min(parseInt(limit) || 50, 500));
const result = await pool.query(`
SELECT
r.gencod, r.site, r.libelle,
r.foucentrale, r.nomfoucentrale,
r.ranking_ca, r.ranking_qte,
r.ranking_mag_ca, r.ranking_mag_qte, r.ranking_mag_marge,
r.pv_calcule, r.pv_mag, r.pv_cen,
r.codefamille, r.libellefamille,
r.date_maj, r.date_calcul_mag,
a.no_id AS art_no_id, a.codein
FROM ranking r
LEFT JOIN art_gtin g ON g.gtin = r.gencod
LEFT JOIN articles a ON a.no_id = g.idarticle
WHERE ($1 = '' OR r.gencod = $1)
AND ($2 = '' OR a.codein = $2)
AND ($3 = '' OR r.site LIKE $3)
AND ($4 = '' OR r.foucentrale LIKE $4)
ORDER BY r.ranking_ca ASC NULLS LAST
LIMIT $5
`, [gencod, codein, `%${site}%`, `%${foucentrale}%`, limitNum]);
res.json({ count: result.rows.length, ranking: result.rows });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// GET /api/ranking/article/:id - Ranking pour un article (par no_id)
router.get('/article/:id', async (req, res) => {
try {
const pool = getPool();
const artNoId = req.params.id;
const result = await pool.query(`
SELECT
r.gencod, r.site, r.libelle,
r.foucentrale, r.nomfoucentrale,
r.ranking_ca, r.ranking_qte,
r.ranking_mag_ca, r.ranking_mag_qte, r.ranking_mag_marge,
r.pv_calcule, r.pv_mag, r.pv_cen,
r.codefamille, r.libellefamille,
r.date_maj, r.date_calcul_mag
FROM ranking r
JOIN art_gtin g ON g.gtin = r.gencod
WHERE g.idarticle = $1
ORDER BY r.site
`, [artNoId]);
res.json({ artNoId, ranking: result.rows });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = router;