@@ -70,39 +70,52 @@ router.get('/:ticker/quote', async (req, res) => {
70
70
router . post ( '/' , async ( req , res ) => {
71
71
try {
72
72
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
+ }
74
80
75
81
// Validate numeric values
76
82
const parsedShares = parseFloat ( shares ) ;
77
83
const parsedBuyPrice = parseFloat ( buy_price ) ;
78
84
const parsedTargetPrice = parseFloat ( target_price || buy_price ) ; // Use buy_price as default target_price
79
85
80
86
if ( isNaN ( parsedShares ) || parsedShares <= 0 ) {
87
+ console . error ( 'Invalid number of shares:' , shares ) ;
81
88
throw new Error ( 'Invalid number of shares' ) ;
82
89
}
83
90
84
91
if ( isNaN ( parsedBuyPrice ) || parsedBuyPrice <= 0 ) {
92
+ console . error ( 'Invalid buy price:' , buy_price ) ;
85
93
throw new Error ( 'Invalid buy price' ) ;
86
94
}
87
95
96
+ console . log ( 'Fetching current price for ticker:' , ticker ) ;
88
97
// Get current price from Finnhub
89
98
const quote = await stockPriceService . getStockQuote ( ticker ) ;
99
+ console . log ( 'Received quote:' , quote ) ;
100
+
90
101
if ( ! quote || ! quote . c ) {
102
+ console . error ( 'Failed to fetch quote for ticker:' , ticker ) ;
91
103
throw new Error ( 'Unable to fetch current price for ticker' ) ;
92
104
}
93
105
106
+ console . log ( 'Creating stock in database' ) ;
94
107
const stock = await Stock . create ( {
95
108
name : name . trim ( ) ,
96
109
ticker : ticker . toUpperCase ( ) . trim ( ) ,
97
110
shares : parsedShares ,
98
111
buy_price : parsedBuyPrice ,
99
112
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 ,
102
115
last_updated : new Date ( )
103
116
} ) ;
104
117
105
- console . log ( 'Stock added:' , stock . id ) ;
118
+ console . log ( 'Stock added successfully :' , stock . id ) ;
106
119
res . status ( 201 ) . json ( stock ) ;
107
120
} catch ( error ) {
108
121
console . error ( 'Error adding stock:' , error ) ;
0 commit comments