Skip to content

Commit 00e871c

Browse files
committed
Fix: Update CORS and add detailed logging
1 parent 4f3600e commit 00e871c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

backend/src/routes/stocks.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,52 @@ router.get('/:ticker/quote', async (req, res) => {
7070
router.post('/', async (req, res) => {
7171
try {
7272
const { name, ticker, shares, buy_price, target_price } = req.body;
73-
console.log('Adding stock:', { name, ticker, shares, buy_price, target_price });
73+
console.log('Received request to add stock:', req.body);
74+
75+
// Validate required fields
76+
if (!name || !ticker || !shares || !buy_price) {
77+
console.error('Missing required fields');
78+
return res.status(400).json({ error: 'Missing required fields' });
79+
}
7480

7581
// Validate numeric values
7682
const parsedShares = parseFloat(shares);
7783
const parsedBuyPrice = parseFloat(buy_price);
7884
const parsedTargetPrice = parseFloat(target_price || buy_price); // Use buy_price as default target_price
7985

8086
if (isNaN(parsedShares) || parsedShares <= 0) {
87+
console.error('Invalid number of shares:', shares);
8188
throw new Error('Invalid number of shares');
8289
}
8390

8491
if (isNaN(parsedBuyPrice) || parsedBuyPrice <= 0) {
92+
console.error('Invalid buy price:', buy_price);
8593
throw new Error('Invalid buy price');
8694
}
8795

96+
console.log('Fetching current price for ticker:', ticker);
8897
// Get current price from Finnhub
8998
const quote = await stockPriceService.getStockQuote(ticker);
99+
console.log('Received quote:', quote);
100+
90101
if (!quote || !quote.c) {
102+
console.error('Failed to fetch quote for ticker:', ticker);
91103
throw new Error('Unable to fetch current price for ticker');
92104
}
93105

106+
console.log('Creating stock in database');
94107
const stock = await Stock.create({
95108
name: name.trim(),
96109
ticker: ticker.toUpperCase().trim(),
97110
shares: parsedShares,
98111
buy_price: parsedBuyPrice,
99112
current_price: quote.c,
100-
target_price: parsedTargetPrice, // Added target_price
101-
is_in_watchlist: true,
113+
target_price: parsedTargetPrice,
114+
is_in_watchlist: false,
102115
last_updated: new Date()
103116
});
104117

105-
console.log('Stock added:', stock.id);
118+
console.log('Stock added successfully:', stock.id);
106119
res.status(201).json(stock);
107120
} catch (error) {
108121
console.error('Error adding stock:', error);

backend/src/server.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,20 @@ const defaultStocks = [
5959

6060
const app = express();
6161

62-
app.use(cors());
62+
// Configure CORS
63+
app.use(cors({
64+
origin: ['https://portfolio-tracker-hackstyx.vercel.app', 'http://localhost:5173'],
65+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
66+
allowedHeaders: ['Content-Type', 'Authorization']
67+
}));
68+
6369
app.use(express.json());
6470

71+
// Health check endpoint
72+
app.get('/api/health', (req, res) => {
73+
res.json({ status: 'ok', message: 'Server is running' });
74+
});
75+
6576
app.use('/api/stocks', stockRoutes);
6677
app.use('/api/watchlist', watchlistRoutes);
6778

0 commit comments

Comments
 (0)