Initial commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import db from '../db/index.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const schema = z.object({
|
||||
investisseur_id: z.number().int().positive(),
|
||||
plateforme_id: z.number().int().positive(),
|
||||
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
|
||||
montant: z.number(), // positif ou négatif
|
||||
notes: z.string().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/corrections
|
||||
* ?scope=all → tous les investisseurs de l'utilisateur
|
||||
* ?annee=YYYY → filtre sur l'année
|
||||
*/
|
||||
router.get('/', (req, res) => {
|
||||
const userId = req.user.id;
|
||||
const scopeAll = req.query.scope === 'all';
|
||||
const annee = req.query.annee;
|
||||
|
||||
let where = scopeAll
|
||||
? 'c.investisseur_id IN (SELECT id FROM investisseurs WHERE user_id = ?)'
|
||||
: 'c.investisseur_id = ?';
|
||||
|
||||
const args = [scopeAll ? userId : Number(req.header('X-Investisseur-Id'))];
|
||||
|
||||
if (annee) {
|
||||
where += ' AND substr(c.date,1,4) = ?';
|
||||
args.push(annee);
|
||||
}
|
||||
|
||||
const rows = db.prepare(`
|
||||
SELECT c.id, c.investisseur_id, c.plateforme_id,
|
||||
c.date, c.montant, c.notes, c.created_at,
|
||||
p.nom AS plateforme_nom,
|
||||
p.fiscalite,
|
||||
inv.nom AS investisseur_nom
|
||||
FROM corrections_solde c
|
||||
JOIN plateformes p ON p.id = c.plateforme_id
|
||||
JOIN investisseurs inv ON inv.id = c.investisseur_id
|
||||
WHERE ${where}
|
||||
ORDER BY c.date DESC, c.created_at DESC
|
||||
`).all(...args);
|
||||
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/corrections
|
||||
*/
|
||||
router.post('/', (req, res) => {
|
||||
const userId = req.user.id;
|
||||
const data = schema.parse(req.body);
|
||||
|
||||
// Vérifier que la plateforme appartient à l'utilisateur
|
||||
const plat = db.prepare('SELECT id FROM plateformes WHERE id = ? AND user_id = ?').get(data.plateforme_id, userId);
|
||||
if (!plat) return res.status(403).json({ error: 'Plateforme inconnue ou non autorisée' });
|
||||
|
||||
// Vérifier que l'investisseur appartient à l'utilisateur
|
||||
const inv = db.prepare('SELECT id FROM investisseurs WHERE id = ? AND user_id = ?').get(data.investisseur_id, userId);
|
||||
if (!inv) return res.status(403).json({ error: 'Investisseur inconnu ou non autorisé' });
|
||||
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO corrections_solde (investisseur_id, plateforme_id, date, montant, notes)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`);
|
||||
const result = stmt.run(data.investisseur_id, data.plateforme_id, data.date, data.montant, data.notes ?? null);
|
||||
|
||||
res.status(201).json({ id: result.lastInsertRowid });
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/corrections/:id
|
||||
*/
|
||||
router.delete('/:id', (req, res) => {
|
||||
const userId = req.user.id;
|
||||
const id = Number(req.params.id);
|
||||
|
||||
// Vérifier que la correction appartient à un investisseur de cet utilisateur
|
||||
const corr = db.prepare(`
|
||||
SELECT c.id FROM corrections_solde c
|
||||
JOIN investisseurs inv ON inv.id = c.investisseur_id
|
||||
WHERE c.id = ? AND inv.user_id = ?
|
||||
`).get(id, userId);
|
||||
|
||||
if (!corr) return res.status(404).json({ error: 'Correction introuvable' });
|
||||
|
||||
db.prepare('DELETE FROM corrections_solde WHERE id = ?').run(id);
|
||||
res.status(204).end();
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user