const pool = require('../config/db'); const path = require('path'); const fs = require('fs'); exports.showPage = async (req, res) => { try { const { token } = req.params; const reponse = req.query.reponse || null; const [rows] = await pool.query( `SELECT i.*, u.prenom AS invite_par_prenom, u.nom AS invite_par_nom, u.societe AS invite_par_societe FROM invites i LEFT JOIN users u ON u.id = i.invite_par_id WHERE i.token = ?`, [token] ); if (rows.length === 0) { return sendErrorPage(res, 'Lien invalide ou expiré.'); } const guest = rows[0]; if (reponse && ['acceptee', 'refusee'].includes(reponse)) { await pool.query('UPDATE invites SET statut=? WHERE id=?', [reponse, guest.id]); guest.statut = reponse; } const dateStr = guest.date_club ? new Date(guest.date_club).toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) : ''; const statutLabels = { en_attente: 'En attente', acceptee: 'Acceptée', refusee: 'Refusée' }; const statutColors = { en_attente: '#f59e0b', acceptee: '#16a34a', refusee: '#dc2626' }; const html = ` Invitation Dyna Biz

Dyna Biz Coignieres

Bonjour ${guest.prenom} ${guest.nom} !

${guest.invite_par_prenom} ${guest.invite_par_nom}${guest.invite_par_societe ? ' (' + guest.invite_par_societe + ')' : ''} vous invite au club Dyna Biz Coignieres.

${guest.date_club ? '

Date : ' + dateStr + '

' : ''}

Statut actuel :
${statutLabels[guest.statut]}

${guest.statut === 'en_attente' ? ` ` : `
${guest.statut === 'acceptee' ? 'Vous avez accepté l\'invitation. Merci !' : 'Vous avez refusé l\'invitation.'}
`} ${guest.statut === 'acceptee' ? `

Ajouter une photo

Vous pouvez ajouter une photo pour que les membres puissent vous reconnaître.

Votre photo

` : ''}
`; res.send(html); } catch (err) { console.error(err); sendErrorPage(res, 'Une erreur est survenue.'); } }; exports.respond = async (req, res) => { try { const { token } = req.params; const { reponse } = req.body; if (!['acceptee', 'refusee'].includes(reponse)) { return res.status(400).json({ error: 'Réponse invalide' }); } const [rows] = await pool.query('SELECT id FROM invites WHERE token = ?', [token]); if (rows.length === 0) return res.status(404).json({ error: 'Lien invalide' }); await pool.query('UPDATE invites SET statut=? WHERE id=?', [reponse, rows[0].id]); res.json({ message: 'Réponse enregistrée' }); } catch (err) { console.error(err); res.status(500).json({ error: 'Server error' }); } }; exports.uploadPhoto = async (req, res) => { try { const { token } = req.params; const [rows] = await pool.query('SELECT id, photo_url FROM invites WHERE token = ?', [token]); if (rows.length === 0) return res.status(404).json({ error: 'Lien invalide' }); if (!req.file) return res.status(400).json({ error: 'Fichier requis' }); if (rows[0].photo_url) { const oldPath = path.join(__dirname, '..', rows[0].photo_url); if (fs.existsSync(oldPath)) fs.unlinkSync(oldPath); } const photoUrl = '/uploads/' + req.file.filename; await pool.query('UPDATE invites SET photo_url=? WHERE id=?', [photoUrl, rows[0].id]); res.json({ message: 'Photo enregistrée', photo_url: photoUrl }); } catch (err) { console.error(err); res.status(500).json({ error: 'Server error' }); } }; function sendErrorPage(res, msg) { res.status(404).send(` Invitation Dyna Biz

Oups !

${msg}

`); }