diff --git a/Backend/index.js b/Backend/index.js index fc46bd6..23424ee 100644 --- a/Backend/index.js +++ b/Backend/index.js @@ -204,10 +204,47 @@ app.get('/api/transactions/recent', async (req, res) => { } }); +app.post('/api/transactions/voice', async (req, res) => { + try { + const { voiceInput } = req.body; + if (!voiceInput) { + return res.status(400).json({ error: 'No voice input provided' }); + } + // Step 1: Use regex to extract amount + const amountMatch = voiceInput.match(/(?:\₹|\$)?(\d+(?:\.\d{1,2})?)/); + const amount = amountMatch ? parseFloat(amountMatch[1]) : null; + // Step 2: Extract description (rough logic: remove common verbs + amount) + const cleaned = voiceInput + .toLowerCase() + .replace(/(bought|added|paid|spent|for|on)/g, '') + .replace(/₹?\d+/, '') + .trim(); + const description = cleaned || 'misc'; + // Step 3: Predict category using your Flask API + const predictRes = await axios.post('http://192.168.1.10:5000/api/predict', { + description, + }); + const category = predictRes.data?.category || 'Other'; + // Step 4: Save to DB + const newTransaction = new Transaction({ + description, + amount, + category, + }); + await newTransaction.save(); + res.status(200).json({ + message: '✅ Voice transaction saved', + data: newTransaction, + }); + } catch (err) { + console.error('❌ Voice transaction error:', err.message); + res.status(500).json({ error: 'Server error during voice transaction' }); + } +}); // Bank rules (can expand later) const bankRules = [ { name: 'HDFC Bank', email: 'alerts@hdfcbank.net' }, - // { name: 'ICICI Bank', email: 'alerts@icicibank.com' }, // example expansion + { name: 'ICICI Bank', email: 'alerts@icicibank.com' }, ]; // ✅ Gmail fetch function @@ -263,7 +300,6 @@ app.post('/api/sync-gmail', async (req, res) => { } }); - /* ---------- SERVER ---------- */ mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true,