113 lines
4.8 KiB
JavaScript
113 lines
4.8 KiB
JavaScript
const pool = require('../config/db');
|
|
const { logActivity } = require('../services/activityLog');
|
|
|
|
exports.list = async (req, res) => {
|
|
try {
|
|
const [rows] = await pool.query(`
|
|
SELECT r.*,
|
|
m.prenom AS membre_prenom, m.nom AS membre_nom,
|
|
d.prenom AS dest_prenom, d.nom AS dest_nom
|
|
FROM recommandations r
|
|
LEFT JOIN users m ON m.id = r.membre_id
|
|
LEFT JOIN users d ON d.id = r.destinataire_id
|
|
ORDER BY r.date DESC
|
|
`);
|
|
res.json(rows);
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.getById = async (req, res) => {
|
|
try {
|
|
const [rows] = await pool.query('SELECT * FROM recommandations WHERE id = ?', [req.params.id]);
|
|
if (rows.length === 0) return res.status(404).json({ error: 'Not found' });
|
|
res.json(rows[0]);
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.create = async (req, res) => {
|
|
try {
|
|
const { date, destinataire_id, nom_prospect, type, transformee, sans_suite, ca_realise } = req.body;
|
|
if (!date || !nom_prospect || !type) {
|
|
return res.status(400).json({ error: 'Date, prospect name and type required' });
|
|
}
|
|
const membre_id = req.user.id;
|
|
const [result] = await pool.query(
|
|
`INSERT INTO recommandations (date, membre_id, destinataire_id, nom_prospect, type, transformee, sans_suite, ca_realise)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[date, membre_id, destinataire_id || null, nom_prospect, type, transformee ? 1 : 0, sans_suite ? 1 : 0, ca_realise || null]
|
|
);
|
|
logActivity(req, 'recommandation_ajout', { nom_prospect, type });
|
|
res.status(201).json({ id: result.insertId, message: 'Recommendation added' });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.update = async (req, res) => {
|
|
try {
|
|
const recId = req.params.id;
|
|
const [existing] = await pool.query('SELECT membre_id FROM recommandations WHERE id = ?', [recId]);
|
|
if (existing.length === 0) return res.status(404).json({ error: 'Not found' });
|
|
if (existing[0].membre_id !== req.user.id && req.user.role !== 'admin') {
|
|
return res.status(403).json({ error: 'You can only edit your own recommendations' });
|
|
}
|
|
const { date, destinataire_id, nom_prospect, type, transformee, sans_suite, ca_realise } = req.body;
|
|
await pool.query(
|
|
`UPDATE recommandations SET date=?, destinataire_id=?, nom_prospect=?, type=?, transformee=?, sans_suite=?, ca_realise=? WHERE id=?`,
|
|
[date, destinataire_id || null, nom_prospect, type, transformee ? 1 : 0, sans_suite ? 1 : 0, ca_realise || null, recId]
|
|
);
|
|
logActivity(req, 'recommandation_modification', { recId, nom_prospect });
|
|
res.json({ message: 'Updated' });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.remove = async (req, res) => {
|
|
try {
|
|
const recId = req.params.id;
|
|
const [existing] = await pool.query('SELECT membre_id FROM recommandations WHERE id = ?', [recId]);
|
|
if (existing.length === 0) return res.status(404).json({ error: 'Not found' });
|
|
if (existing[0].membre_id !== req.user.id && req.user.role !== 'admin') {
|
|
return res.status(403).json({ error: 'Vous ne pouvez supprimer que vos propres recommandations' });
|
|
}
|
|
await pool.query('DELETE FROM recommandations WHERE id = ?', [recId]);
|
|
logActivity(req, 'recommandation_suppression', { recId });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.setStatus = async (req, res) => {
|
|
try {
|
|
const recId = req.params.id;
|
|
const [existing] = await pool.query('SELECT membre_id FROM recommandations WHERE id = ?', [recId]);
|
|
if (existing.length === 0) return res.status(404).json({ error: 'Not found' });
|
|
if (existing[0].membre_id !== req.user.id && req.user.role !== 'admin') {
|
|
return res.status(403).json({ error: 'Vous ne pouvez modifier que vos propres recommandations' });
|
|
}
|
|
const { status, ca_realise } = req.body;
|
|
if (status === 'transformee') {
|
|
await pool.query('UPDATE recommandations SET transformee=1, sans_suite=0, ca_realise=? WHERE id=?', [ca_realise || null, recId]);
|
|
} else if (status === 'sans_suite') {
|
|
await pool.query('UPDATE recommandations SET transformee=0, sans_suite=1, ca_realise=NULL WHERE id=?', [recId]);
|
|
} else {
|
|
await pool.query('UPDATE recommandations SET transformee=0, sans_suite=0 WHERE id=?', [recId]);
|
|
}
|
|
logActivity(req, 'recommandation_statut', { recId, status });
|
|
res.json({ message: 'Status updated' });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|