From 0d3bec44d36333d3b5e55813725b6e9e5284f0f8 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 21 May 2025 16:23:52 +0000 Subject: [PATCH] fixed premium upgrade endpoint --- backend/server.js | 33 +++++++++++++++++++++++++++++++++ backend/server3.js | 32 -------------------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/backend/server.js b/backend/server.js index 2c462c1..99d843c 100755 --- a/backend/server.js +++ b/backend/server.js @@ -427,6 +427,39 @@ app.get('/api/areas', (req, res) => { }); }); +/* ------------------------------------------------------------------ +PREMIUM UPGRADE ENDPOINT +------------------------------------------------------------------ */ +app.post('/api/activate-premium', (req, res) => { + const token = req.headers.authorization?.split(' ')[1]; + if (!token) { + return res.status(401).json({ error: 'Authorization token is required' }); + } + + let userId; // Will hold the auto-increment "id" from user_profile + try { + const decoded = jwt.verify(token, SECRET_KEY); + userId = decoded.userId; // => user_profile.id (auto-inc PK) + } catch (error) { + return res.status(401).json({ error: 'Invalid or expired token' }); + } + + // Use MySQL pool.query instead of db.run (which is for SQLite) + const query = ` + UPDATE user_profile + SET is_premium = 1, + is_pro_premium = 1 + WHERE id = ? + `; + pool.query(query, [userId], (err, results) => { + if (err) { + console.error('Error updating premium status:', err.message); + return res.status(500).json({ error: 'Failed to activate premium' }); + } + return res.status(200).json({ message: 'Premium activated successfully' }); + }); +}); + // =============== START SERVER =============== app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); diff --git a/backend/server3.js b/backend/server3.js index 2c3dc49..b664559 100644 --- a/backend/server3.js +++ b/backend/server3.js @@ -66,38 +66,6 @@ const authenticatePremiumUser = (req, res, next) => { } }; -/* ------------------------------------------------------------------ -PREMIUM UPGRADE ENDPOINT ------------------------------------------------------------------- */ -app.post('/api/activate-premium', (req, res) => { - const token = req.headers.authorization?.split(' ')[1]; - if (!token) { - return res.status(401).json({ error: 'Authorization token is required' }); - } - - let userId; - try { - const decoded = jwt.verify(token, SECRET_KEY); - userId = decoded.userId; - } catch (error) { - return res.status(401).json({ error: 'Invalid or expired token' }); - } - - const query = ` - UPDATE user_profile - SET is_premium = 1, is_pro_premium = 1 - WHERE user_id = ? - `; - db.run(query, [userId], (err) => { - if (err) { - console.error('Error updating premium status:', err.message); - return res.status(500).json({ error: 'Failed to activate premium' }); - } - res.status(200).json({ message: 'Premium activated successfully' }); - }); -}); - - /* ------------------------------------------------------------------ CAREER PROFILE ENDPOINTS ------------------------------------------------------------------ */