Mise à jour des pages d'accueil

This commit is contained in:
ocroguennec committed 2026-06-16 15:12:25 +02:00
1 parent 720634971d
commit 80506ca2dc
13 files changed
+168 -217

No files matched your search

+25 -7
View File
@@ -11,12 +11,13 @@ const Schema = z.object({
type: z.enum(['famille', 'entreprise']).default('famille'),
type_fiscal: z.string().optional(),
notes: z.string().optional(),
email: z.string().email().optional().or(z.literal('')),
});
router.get('/', (req, res) => {
const rows = db
.prepare(
`SELECT id, nom, prenom, type, type_fiscal, is_principal, notes, created_at
`SELECT id, nom, prenom, type, type_fiscal, is_principal, notes, email, created_at
FROM investisseurs WHERE user_id = ?
ORDER BY is_principal DESC, type, nom`
)
@@ -29,7 +30,7 @@ router.post('/', (req, res, next) => {
const body = Schema.parse(req.body);
const r = db
.prepare(
'INSERT INTO investisseurs (user_id, nom, prenom, type, type_fiscal, notes) VALUES (?,?,?,?,?,?)'
'INSERT INTO investisseurs (user_id, nom, prenom, type, type_fiscal, notes, email) VALUES (?,?,?,?,?,?,?)'
)
.run(
req.user.id,
@@ -38,6 +39,7 @@ router.post('/', (req, res, next) => {
body.type,
body.type_fiscal || null,
body.notes || null,
body.email || null,
);
const invId = r.lastInsertRowid;
// Auto-créer un compte courant pour ce nouveau profil
@@ -56,7 +58,7 @@ router.put('/:id', (req, res, next) => {
const body = Schema.parse(req.body);
const r = db
.prepare(
`UPDATE investisseurs SET nom=?, prenom=?, type=?, type_fiscal=?, notes=?, updated_at=datetime('now')
`UPDATE investisseurs SET nom=?, prenom=?, type=?, type_fiscal=?, notes=?, email=?, updated_at=datetime('now')
WHERE id=? AND user_id=?`
)
.run(
@@ -65,6 +67,7 @@ router.put('/:id', (req, res, next) => {
body.type,
body.type_fiscal || null,
body.notes || null,
body.email || null,
req.params.id,
req.user.id,
);
@@ -115,10 +118,25 @@ router.delete('/:id', (req, res, next) => {
.get(req.user.id).n;
if (count <= 1) throw new HttpError(400, 'Impossible de supprimer le dernier profil.');
const r = db
.prepare('DELETE FROM investisseurs WHERE id=? AND user_id=?')
.run(req.params.id, req.user.id);
if (r.changes === 0) throw new HttpError(404, 'Not found');
// Récupérer le compte principal pour la réassignation
const principal = db
.prepare('SELECT id FROM investisseurs WHERE user_id=? AND is_principal=1')
.get(req.user.id);
if (!principal) throw new HttpError(500, 'Aucun compte principal trouvé pour la réassignation.');
const principalId = principal.id;
const targetId = Number(req.params.id);
// Réassigner toutes les données liées au compte principal avant suppression
const deleteWithReassign = db.transaction(() => {
db.prepare('UPDATE investissements SET investisseur_id=? WHERE investisseur_id=?').run(principalId, targetId);
db.prepare('UPDATE depots_retraits SET investisseur_id=? WHERE investisseur_id=?').run(principalId, targetId);
db.prepare('UPDATE plateformes SET investisseur_id=? WHERE investisseur_id=? AND user_id=?').run(principalId, targetId, req.user.id);
db.prepare('UPDATE comptes SET investisseur_id=? WHERE investisseur_id=? AND user_id=?').run(principalId, targetId, req.user.id);
db.prepare('DELETE FROM investisseurs WHERE id=? AND user_id=?').run(targetId, req.user.id);
});
deleteWithReassign();
res.status(204).end();
} catch (e) { next(e); }
});