Velar/Backend/index.js

126 lines
3.3 KiB
JavaScript
Raw Normal View History

2025-07-31 01:08:35 -07:00
require('dotenv').config(); // Add this at the very top
2025-05-23 05:51:16 -07:00
const express = require('express');
const mongoose = require('mongoose');
const axios = require('axios');
2025-07-31 01:08:35 -07:00
const cors = require('cors');
2025-05-23 05:51:16 -07:00
const app = express();
app.use(express.json()); // to parse JSON
2025-07-31 01:08:35 -07:00
app.use(cors({
origin: process.env.CORS_ORIGIN || '*'
}));
2025-05-23 05:51:16 -07:00
// Prediction route
2025-06-29 11:09:16 -07:00
const predictRoute = require('./predict');
2025-05-23 05:51:16 -07:00
app.use('/api/predict', predictRoute);
// MongoDB connection
mongoose.connect('mongodb://localhost:27017/chat', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Define schema and model
const transactionSchema = new mongoose.Schema({
description: String,
amount: Number,
2025-06-20 16:45:44 -07:00
2025-06-28 18:06:47 -07:00
category: String,
2025-05-23 05:51:16 -07:00
date: { type: Date, default: Date.now },
});
const Transaction = mongoose.model('Transaction', transactionSchema);
// POST endpoint to add transaction with auto-categorization
app.post('/api/transaction/add', async (req, res) => {
try {
const { description, amount } = req.body;
// Call your AI prediction endpoint
2025-07-31 01:08:35 -07:00
const predictRes = await axios.post('http://10.231.12.247:5000/api/predict', {
2025-05-23 05:51:16 -07:00
description,
});
const category = predictRes.data.category || 'Other';
const newTransaction = new Transaction({
description,
amount,
category,
});
await newTransaction.save();
res.status(200).json({
message: '✅ Transaction saved with category',
data: newTransaction,
});
} catch (err) {
console.error('❌ Error adding transaction:', err.message);
res.status(500).json({ error: err.message });
}
});
2025-06-20 16:45:44 -07:00
app.post('/api/transaction/update', async (req, res) => {
try {
const { budget } = req.body;
// Save to database or wherever you store budgets
await BudgetModel.updateOne({}, { amount: budget }, { upsert: true });
res.status(200).json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Failed to update budget' });
}
});
2025-05-23 05:51:16 -07:00
// GET transactions by category
app.get('/api/transactions', async (req, res) => {
try {
const category = req.query.category;
let query = {};
if (category && category !== 'All') {
query.category = category;
}
const transactions = await Transaction.find(query).sort({ date: -1 });
res.status(200).json({
success: true,
data: transactions.map(tx => ({
id: tx._id,
description: tx.description,
amount: tx.amount,
category: tx.category,
date: tx.date,
})),
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
2025-06-30 03:20:49 -07:00
// Get 5 most recent transactions
app.get('/api/transactions/recent', async (req, res) => {
try {
const transactions = await Transaction.find({})
.sort({ date: -1 })
.limit(5); // 👈 limit to 5
res.status(200).json({
success: true,
data: transactions.map(tx => ({
id: tx._id,
description: tx.description,
amount: tx.amount,
category: tx.category,
date: tx.date,
})),
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
2025-07-31 01:08:35 -07:00
const HOST = process.env.HOST || '0.0.0.0'; // listen on all interfaces
const PORT = process.env.PORT || 3000;
app.listen(PORT, HOST, () => {
console.log(`🚀 Server running on http://${HOST}:${PORT}`);
2025-05-23 05:51:16 -07:00
});
2025-07-31 01:08:35 -07:00