API V1 Public + preparation serveur MCP
This commit is contained in:
@@ -8,23 +8,56 @@ const router = Router();
|
||||
* /dashboard:
|
||||
* get:
|
||||
* summary: Synthèse du portefeuille (KPIs)
|
||||
* description: >
|
||||
* Les champs `investissements.*` sont des soldes actuels (photo à
|
||||
* aujourd'hui), pas des cumuls par période — un investissement remboursé
|
||||
* partiellement reste compté pour son capital restant dû tant qu'il
|
||||
* n'est pas soldé. Seuls les champs `interets.*` peuvent être filtrés
|
||||
* par année via `?annee=`.
|
||||
* tags: [Dashboard]
|
||||
* security: [{ ApiKeyAuth: [] }]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: annee
|
||||
* schema: { type: integer }
|
||||
* description: >
|
||||
* Filtre les intérêts/capital reçu sur une année (ex. 2026).
|
||||
* Omis = cumul sur toute la durée du portefeuille.
|
||||
* responses:
|
||||
* 200: { description: Synthèse KPI }
|
||||
*/
|
||||
router.get('/', (req, res) => {
|
||||
const invId = req.investisseurId;
|
||||
const annee = req.query.annee ? Number(req.query.annee) : null;
|
||||
|
||||
// ── Investissements : mêmes formules que le KPI "Capital investi" / "Capital
|
||||
// en risque" de l'app interne (Dashboard.jsx → capitalDeploye = encours +
|
||||
// en_defaut). "capital_investi" et "capital_en_risque" sont des soldes
|
||||
// nettés (montant souscrit + réinvestissements − capital déjà remboursé sur
|
||||
// CET investissement), pas une simple somme de montant_investi par statut —
|
||||
// ça gère correctement les prêts amortissables partiellement remboursés.
|
||||
const investissements = db.prepare(`
|
||||
SELECT
|
||||
COUNT(*) AS nb_investissements,
|
||||
COALESCE(SUM(montant_investi), 0) AS total_investi,
|
||||
COALESCE(SUM(CASE WHEN statut='en_cours' THEN montant_investi END), 0) AS encours,
|
||||
COALESCE(SUM(CASE WHEN statut='rembourse' THEN montant_investi END), 0) AS rembourse
|
||||
FROM investissements WHERE investisseur_id = ?
|
||||
COUNT(*) AS nb_investissements,
|
||||
COALESCE(SUM(i.montant_investi), 0) AS total_investi,
|
||||
COALESCE(SUM(CASE WHEN i.statut IN ('en_cours','en_retard','procedure') THEN
|
||||
i.montant_investi
|
||||
+ COALESCE((SELECT SUM(rv.montant) FROM reinvestissements rv WHERE rv.investissement_id = i.id), 0)
|
||||
- COALESCE((SELECT SUM(rb.capital) FROM remboursements rb WHERE rb.investissement_id = i.id AND rb.type = 'normal'), 0)
|
||||
END), 0) AS capital_investi,
|
||||
COALESCE(SUM(CASE WHEN i.statut IN ('en_retard','procedure') THEN
|
||||
i.montant_investi
|
||||
+ COALESCE((SELECT SUM(rv.montant) FROM reinvestissements rv WHERE rv.investissement_id = i.id), 0)
|
||||
- COALESCE((SELECT SUM(rb.capital) FROM remboursements rb WHERE rb.investissement_id = i.id AND rb.type = 'normal'), 0)
|
||||
END), 0) AS capital_en_risque,
|
||||
COALESCE(SUM(CASE WHEN i.statut='rembourse' THEN i.montant_investi END), 0) AS rembourse
|
||||
FROM investissements i WHERE i.investisseur_id = ?
|
||||
`).get(invId);
|
||||
|
||||
const interetsConds = ['i.investisseur_id = ?'];
|
||||
const interetsParams = [invId];
|
||||
if (annee) { interetsConds.push(`strftime('%Y', r.date_remb) = ?`); interetsParams.push(String(annee)); }
|
||||
|
||||
const interets = db.prepare(`
|
||||
SELECT
|
||||
COALESCE(SUM(r.interets_bruts), 0) AS interets_bruts,
|
||||
@@ -33,8 +66,8 @@ router.get('/', (req, res) => {
|
||||
COALESCE(SUM(r.net_recu), 0) AS net_recu_total
|
||||
FROM remboursements r
|
||||
JOIN investissements i ON i.id = r.investissement_id
|
||||
WHERE i.investisseur_id = ?
|
||||
`).get(invId);
|
||||
WHERE ${interetsConds.join(' AND ')}
|
||||
`).get(...interetsParams);
|
||||
|
||||
const cash = db.prepare(`
|
||||
SELECT
|
||||
@@ -43,7 +76,7 @@ router.get('/', (req, res) => {
|
||||
FROM depots_retraits WHERE investisseur_id = ?
|
||||
`).get(invId);
|
||||
|
||||
res.json({ investissements, interets, cash });
|
||||
res.json({ investissements, interets: { ...interets, annee: annee || null }, cash });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user