-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
587 lines (538 loc) 路 20.5 KB
/
Copy pathserver.js
File metadata and controls
587 lines (538 loc) 路 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Express backend for D3 Hackathon Website
// Provides: Auth (JWT), Crops CRUD, Settings CRUD, Market/Forecast mock data
// Storage: MongoDB via Mongoose (previously JSON files)
const path = require('path')
const fs = require('fs')
const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
require('dotenv').config()
// DB
const { connectDB } = require('./server/src/utils/db')
const User = require('./server/src/models/User')
const Crop = require('./server/src/models/Crop')
const Setting = require('./server/src/models/Setting')
const Order = require('./server/src/models/Order')
const Notification = require('./server/src/models/Notification')
const { recommendBuyersForCrop, recommendCropsForOrder } = require('./server/src/utils/recommendation')
const { geocodeLocation } = require('./server/src/utils/geocode')
const app = express()
const PORT = process.env.PORT || 3000
const JWT_SECRET = process.env.JWT_SECRET || 'dev-secret-change-me'
const DEFAULT_LOCATION_LABEL = 'Location not provided'
// Middleware
app.use(cors())
app.use(express.json())
app.use(morgan('dev'))
// Auth helpers
function signToken(user) {
// user may be a Mongoose doc; ensure plain object
const id = user._id ? user._id.toString() : user.id
return jwt.sign({ id, role: user.role, name: user.name, email: user.email }, JWT_SECRET, { expiresIn: '7d' })
}
function authRequired(req, res, next) {
const header = req.headers.authorization || ''
const token = header.startsWith('Bearer ') ? header.slice(7) : null
if (!token) return res.status(401).json({ error: 'Missing token' })
try {
const payload = jwt.verify(token, JWT_SECRET)
req.user = payload
next()
} catch (e) {
return res.status(401).json({ error: 'Invalid token' })
}
}
// ============ Auth Routes ============
app.post('/api/auth/signup', async (req, res) => {
try {
const { role = 'farmer', name, fullName, email, password, farmLocation, location, address } = req.body || {}
if (!email || !password) return res.status(400).json({ error: 'Email and password are required' })
// Normalize incoming location from different fields and fall back when absent
const locationCandidates = [location, address, farmLocation]
.map((value) => (typeof value === 'string' ? value.trim() : ''))
.filter(Boolean)
const normalizedLocation = locationCandidates[0] || ''
const hasLocation = Boolean(normalizedLocation)
if (!hasLocation) {
console.warn('Signup received without location data', { email, role })
}
const fallbackLocation = hasLocation ? normalizedLocation : DEFAULT_LOCATION_LABEL
const existing = await User.findOne({ email: String(email).toLowerCase() }).lean()
if (existing) return res.status(409).json({ error: 'Email already registered' })
let farmCoordinates = null
let profileCoordinates = null
let farmLocationString = role === 'buyer' ? '' : fallbackLocation
let profileLocationString = role === 'buyer' ? fallbackLocation : (hasLocation ? '' : DEFAULT_LOCATION_LABEL)
if (hasLocation) {
try {
const coords = await geocodeLocation(normalizedLocation)
if (coords) {
const { lat, lon, formattedAddress } = coords
const coordPair =
typeof lat === 'number' && typeof lon === 'number'
? { lat, lon }
: null
if (role === 'buyer') {
profileCoordinates = coordPair
if (formattedAddress) profileLocationString = formattedAddress
} else {
farmCoordinates = coordPair
if (formattedAddress) farmLocationString = formattedAddress
}
}
} catch (geoErr) {
console.warn('Failed to geocode signup location', geoErr?.message || geoErr)
}
}
const passwordHash = await bcrypt.hash(String(password), 10)
const user = await User.create({
role: role === 'buyer' ? 'buyer' : 'farmer',
name: name || fullName || 'User',
email: String(email).toLowerCase(),
passwordHash,
})
// Create default settings entry
await Setting.create({
userId: user._id,
farm: {
name: 'My Farm',
location: farmLocationString,
...(farmCoordinates ? { coordinates: farmCoordinates } : {}),
acreage: 0,
contact: user.name,
},
profile: {
email: user.email,
phone: '',
location: profileLocationString,
...(profileCoordinates ? { coordinates: profileCoordinates } : {}),
},
notifications: { priceChanges: true, weather: true, marketOpportunities: true, cropAlerts: true },
})
const token = signToken(user)
res.status(201).json({ token, user: { id: user._id, role: user.role, name: user.name, email: user.email } })
} catch (e) {
res.status(500).json({ error: 'Signup failed' })
}
})
app.post('/api/auth/login', async (req, res) => {
try {
const { email, password } = req.body || {}
if (!email || !password) return res.status(400).json({ error: 'Email and password are required' })
const user = await User.findOne({ email: String(email).toLowerCase() })
if (!user) return res.status(404).json({ error: 'User not found' })
const ok = await bcrypt.compare(String(password), user.passwordHash)
if (!ok) return res.status(401).json({ error: 'Invalid credentials' })
const token = signToken(user)
res.json({ token, user: { id: user._id, role: user.role, name: user.name, email: user.email } })
} catch (e) {
res.status(500).json({ error: 'Login failed' })
}
})
app.get('/api/me', authRequired, async (req, res) => {
try {
const user = await User.findById(req.user.id).lean()
if (!user) return res.status(404).json({ error: 'User not found' })
res.json({ id: user._id, role: user.role, name: user.name, email: user.email })
} catch (e) {
res.status(500).json({ error: 'Failed to fetch user' })
}
})
// ============ Crops (Farmer) ============
app.get('/api/crops', authRequired, async (req, res) => {
try {
const crops = await Crop.find({ userId: req.user.id }).lean()
res.json(crops)
} catch (e) {
res.status(500).json({ error: 'Failed to load crops' })
}
})
app.post('/api/crops', authRequired, async (req, res) => {
try {
const { name, acreage = 0, status = 'Planted', yourPrice = 0, marketPrice = 0, icon = '馃尡', quantity = 0, quantityUnit = 'kg', priceUnit = 'rupees/kg' } = req.body || {}
if (!name) return res.status(400).json({ error: 'name is required' })
const crop = await Crop.create({ userId: req.user.id, name, acreage, status, yourPrice, marketPrice, icon, quantity, quantityUnit, priceUnit })
const plain = crop.toObject({ virtuals: false })
let recommendations = []
try {
recommendations = await recommendBuyersForCrop(crop)
} catch (recErr) {
console.error('Failed to compute buyer recommendations', recErr)
}
res.status(201).json({
...plain,
id: plain._id?.toString?.() || String(plain._id),
recommendations,
})
} catch (e) {
res.status(500).json({ error: 'Failed to create crop' })
}
})
app.put('/api/crops/:id', authRequired, async (req, res) => {
try {
const id = req.params.id
const updated = await Crop.findOneAndUpdate({ _id: id, userId: req.user.id }, { $set: req.body }, { new: true })
if (!updated) return res.status(404).json({ error: 'Crop not found' })
res.json(updated)
} catch (e) {
res.status(500).json({ error: 'Failed to update crop' })
}
})
app.delete('/api/crops/:id', authRequired, async (req, res) => {
try {
const id = req.params.id
const deleted = await Crop.findOneAndDelete({ _id: id, userId: req.user.id })
if (!deleted) return res.status(404).json({ error: 'Crop not found' })
res.json({ success: true })
} catch (e) {
res.status(500).json({ error: 'Failed to delete crop' })
}
})
app.get('/api/crops/:id/recommendations', authRequired, async (req, res) => {
try {
const id = req.params.id
const crop = await Crop.findOne({ _id: id, userId: req.user.id })
if (!crop) return res.status(404).json({ error: 'Crop not found' })
const limitParam = Number(req.query.limit)
const limit = Number.isFinite(limitParam) ? Math.max(1, Math.min(20, Math.trunc(limitParam))) : undefined
const recommendations = await recommendBuyersForCrop(crop, { limit })
res.json({ cropId: crop._id.toString(), recommendations })
} catch (e) {
console.error('Failed to load crop recommendations', e)
res.status(500).json({ error: 'Failed to load recommendations' })
}
})
// ============ Settings (Farmer) ============
app.get('/api/settings', authRequired, async (req, res) => {
try {
const s = await Setting.findOne({ userId: req.user.id }).lean()
if (!s) return res.json({ userId: req.user.id, farm: {}, profile: {}, notifications: {} })
res.json(s)
} catch (e) {
res.status(500).json({ error: 'Failed to load settings' })
}
})
app.put('/api/settings', authRequired, async (req, res) => {
try {
const incoming = req.body || {}
const updated = await Setting.findOneAndUpdate(
{ userId: req.user.id },
{ $set: { ...incoming, userId: req.user.id } },
{ new: true, upsert: true }
)
res.json(updated)
} catch (e) {
res.status(500).json({ error: 'Failed to update settings' })
}
})
// ============ Market & Forecasts (Mock) ============
// Public list of farmer offers (crops) for buyers
app.get('/api/offers', async (req, res) => {
try {
// Optionally allow simple search by crop name via ?q=
const q = (req.query.q || '').toString().trim().toLowerCase()
const criteria = q ? { name: { $regex: q, $options: 'i' } } : {}
const crops = await Crop.find(criteria).lean()
// Load user info for farmer name and filter to farmer role only
const userIds = [...new Set(crops.map(c => String(c.userId)))]
const users = await User.find({ _id: { $in: userIds } }).select('_id name role').lean()
const settings = await Setting.find({ userId: { $in: userIds } }).select('userId farm profile').lean()
const userMap = new Map(users.map(u => [String(u._id), u]))
const settingMap = new Map(settings.map(s => [String(s.userId), s]))
const results = crops
.filter(c => (userMap.get(String(c.userId))?.role || 'farmer') === 'farmer')
.map(c => {
const u = userMap.get(String(c.userId))
const setting = settingMap.get(String(c.userId))
const quantityAmount = Number(c.quantity || 0)
const quantityUnit = c.quantityUnit || 'kg'
const priceAmount = Number(c.yourPrice || 0)
// Normalize priceUnit labels to UI style
const priceUnit = c.priceUnit === 'rupees/ton' ? 'Rs./ton' : 'Rs./kg'
const quantityDisplay = quantityAmount ? `${quantityAmount} ${quantityUnit}` : `${c.acreage || 0} acres`
const priceDisplay = priceAmount ? `${priceAmount} ${priceUnit}` : ''
return {
id: String(c._id),
title: c.name,
quantityAmount,
quantityUnit,
quantityDisplay,
priceAmount,
priceUnit,
priceDisplay,
farmer: u?.name || 'Farmer',
farmerLocation: setting?.farm?.location || setting?.profile?.location || '',
image: 'https://images.unsplash.com/photo-1542834369-f10ebf06d3cb?w=200&h=200&fit=crop',
}
})
res.json(results)
} catch (e) {
console.error('Failed to load offers', e)
res.status(500).json({ error: 'Failed to load offers' })
}
})
// ============ Orders (Buyer) ============
// Create a purchase offer (order) by buyer
app.post('/api/orders', authRequired, async (req, res) => {
try {
const { cropType, quantityAmount, quantityUnit = 'kg', priceAmount, priceUnit = 'Rs./kg' } = req.body || {}
if (!cropType) return res.status(400).json({ error: 'cropType is required' })
const qAmt = Number(quantityAmount)
const pAmt = Number(priceAmount)
if (!Number.isFinite(qAmt) || qAmt < 0) return res.status(400).json({ error: 'quantityAmount must be a non-negative number' })
if (!Number.isFinite(pAmt) || pAmt < 0) return res.status(400).json({ error: 'priceAmount must be a non-negative number' })
const normalizedPriceUnit = priceUnit === 'Rs./ton' ? 'rupees/ton' : 'rupees/kg'
const order = await Order.create({
buyerId: req.user.id,
cropType: String(cropType),
quantityAmount: qAmt,
quantityUnit: quantityUnit === 'ton' ? 'ton' : 'kg',
priceAmount: pAmt,
priceUnit: normalizedPriceUnit,
status: 'Pending',
})
const out = {
id: order._id,
cropType: order.cropType,
quantityAmount: order.quantityAmount,
quantityUnit: order.quantityUnit,
quantityDisplay: `${order.quantityAmount} ${order.quantityUnit}`,
priceAmount: order.priceAmount,
priceUnit: order.priceUnit === 'rupees/ton' ? 'Rs./ton' : 'Rs./kg',
priceDisplay: `${order.priceAmount} ${order.priceUnit === 'rupees/ton' ? 'Rs./ton' : 'Rs./kg'}`,
status: order.status,
createdAt: order.createdAt,
}
let recommendations = []
try {
recommendations = await recommendCropsForOrder(order)
} catch (recErr) {
console.error('Failed to compute crop recommendations', recErr)
}
res.status(201).json({ ...out, recommendations })
} catch (e) {
console.error('Failed to create order', e)
res.status(500).json({ error: 'Failed to create order' })
}
})
// List orders for the current buyer
app.get('/api/orders', authRequired, async (req, res) => {
try {
const orders = await Order.find({ buyerId: req.user.id }).sort({ createdAt: -1 }).lean()
const result = orders.map(o => ({
id: String(o._id),
cropType: o.cropType,
quantityAmount: o.quantityAmount,
quantityUnit: o.quantityUnit,
quantityDisplay: `${o.quantityAmount} ${o.quantityUnit}`,
priceAmount: o.priceAmount,
priceUnit: o.priceUnit === 'rupees/ton' ? 'Rs./ton' : 'Rs./kg',
priceDisplay: `${o.priceAmount} ${o.priceUnit === 'rupees/ton' ? 'Rs./ton' : 'Rs./kg'}`,
status: o.status,
createdAt: o.createdAt,
}))
res.json(result)
} catch (e) {
console.error('Failed to load orders', e)
res.status(500).json({ error: 'Failed to load orders' })
}
})
app.get('/api/orders/:id/recommendations', authRequired, async (req, res) => {
try {
const id = req.params.id
const order = await Order.findOne({ _id: id, buyerId: req.user.id })
if (!order) return res.status(404).json({ error: 'Order not found' })
const limitParam = Number(req.query.limit)
const limit = Number.isFinite(limitParam) ? Math.max(1, Math.min(20, Math.trunc(limitParam))) : undefined
const recommendations = await recommendCropsForOrder(order, { limit })
res.json({ orderId: order._id.toString(), recommendations })
} catch (e) {
console.error('Failed to load order recommendations', e)
res.status(500).json({ error: 'Failed to load recommendations' })
}
})
// ============ Notifications ============
app.get('/api/notifications', authRequired, async (req, res) => {
try {
const notifications = await Notification.find({ userId: req.user.id }).sort({ createdAt: -1 }).lean()
const results = notifications.map((n) => ({
id: String(n._id),
type: n.type,
message: n.message,
metadata: n.metadata || {},
read: Boolean(n.read),
readAt: n.readAt,
createdAt: n.createdAt,
senderName: n.senderName || '',
}))
res.json(results)
} catch (e) {
console.error('Failed to fetch notifications', e)
res.status(500).json({ error: 'Failed to load notifications' })
}
})
app.post('/api/notifications/contact', authRequired, async (req, res) => {
try {
if (req.user.role !== 'farmer') {
return res.status(403).json({ error: 'Only farmers can contact buyers' })
}
const { buyerId, orderId, cropName } = req.body || {}
if (!buyerId) return res.status(400).json({ error: 'buyerId is required' })
const buyer = await User.findById(buyerId).lean()
if (!buyer || buyer.role !== 'buyer') {
return res.status(404).json({ error: 'Buyer not found' })
}
let order = null
if (orderId) {
order = await Order.findOne({ _id: orderId, buyerId: buyer._id }).lean()
}
const resolvedCrop = cropName || order?.cropType || 'your offer'
const senderName = req.user.name || 'A farmer'
const message = `${senderName} is interested in your offer for ${resolvedCrop}.`
const notification = await Notification.create({
userId: buyer._id,
senderId: req.user.id,
senderName,
type: 'buyer-offer-interest',
message,
metadata: {
orderId: order?._id ? order._id.toString() : orderId || null,
cropName: resolvedCrop,
farmerId: req.user.id,
farmerName: senderName,
},
})
res.status(201).json({
id: notification._id.toString(),
message: notification.message,
createdAt: notification.createdAt,
})
} catch (e) {
console.error('Failed to create notification', e)
res.status(500).json({ error: 'Failed to notify buyer' })
}
})
app.post('/api/notifications/mark-read', authRequired, async (req, res) => {
try {
const { ids = [] } = req.body || {}
if (!Array.isArray(ids) || !ids.length) {
return res.json({ success: true, updated: 0 })
}
const result = await Notification.updateMany(
{ _id: { $in: ids }, userId: req.user.id },
{ $set: { read: true, readAt: new Date() } }
)
const updated = typeof result.modifiedCount === 'number' ? result.modifiedCount : (typeof result.nModified === 'number' ? result.nModified : 0)
res.json({ success: true, updated })
} catch (e) {
console.error('Failed to mark notifications as read', e)
res.status(500).json({ error: 'Failed to update notifications' })
}
})
// Delete an order for the current buyer
app.delete('/api/orders/:id', authRequired, async (req, res) => {
try {
const id = req.params.id
const deleted = await Order.findOneAndDelete({ _id: id, buyerId: req.user.id })
if (!deleted) return res.status(404).json({ error: 'Order not found' })
res.json({ success: true })
} catch (e) {
console.error('Failed to delete order', e)
res.status(500).json({ error: 'Failed to delete order' })
}
})
app.get('/api/market/opportunities', (req, res) => {
const data = [
{ icon: '馃尵', buyer: 'Buyer A', crop: 'Wheat', price: 100, distance: 10 },
{ icon: '馃尳', buyer: 'Buyer B', crop: 'Corn', price: 150, distance: 15 },
{ icon: '馃珮', buyer: 'Buyer C', crop: 'Soybeans', price: 200, distance: 20 },
]
res.json(data)
})
app.get('/api/market/trends', (req, res) => {
const crop = (req.query.crop || 'Wheat').toString()
const series = {
Wheat: [
{ year: 2018, avgPrice: 8.5 },
{ year: 2019, avgPrice: 8.75 },
{ year: 2020, avgPrice: 9.0 },
{ year: 2021, avgPrice: 9.25 },
{ year: 2022, avgPrice: 9.5 },
],
Corn: [
{ year: 2018, avgPrice: 7.25 },
{ year: 2019, avgPrice: 7.5 },
{ year: 2020, avgPrice: 7.75 },
{ year: 2021, avgPrice: 8.0 },
{ year: 2022, avgPrice: 8.25 },
],
Soybean: [
{ year: 2018, avgPrice: 12.5 },
{ year: 2019, avgPrice: 12.75 },
{ year: 2020, avgPrice: 13.0 },
{ year: 2021, avgPrice: 13.25 },
{ year: 2022, avgPrice: 13.5 },
],
}
res.json(series[crop] || series['Wheat'])
})
app.get('/api/forecasts/prices', (req, res) => {
// simple mock forecast for 30 days
const crop = (req.query.crop || 'Wheat').toString()
const base = 120
const data = Array.from({ length: 30 }).map((_, i) => ({ day: i + 1, crop, price: base + i * 3 + Math.round(Math.random() * 5) }))
res.json(data)
})
// Reverse geocode coordinates to a human-friendly location (city, state, formatted)
app.get('/api/geocode/reverse', async (req, res) => {
try {
const lat = Number(req.query.lat)
const lon = Number(req.query.lon)
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return res.status(400).json({ error: 'lat and lon are required' })
const { reverseGeocode } = require('./server/src/utils/geocode')
const out = await reverseGeocode(lat, lon)
if (!out) return res.status(204).end()
res.json(out)
} catch (e) {
console.error('Reverse geocode failed', e)
res.status(500).json({ error: 'Reverse geocode failed' })
}
})
// Health check
app.get('/api/health', (_req, res) => res.json({ ok: true }))
// Fallback: redirect to homepage so relative assets resolve (e.g., home.css)
app.get('/', (req, res) => {
const rel = '/Homepage/homeindex.html'
return res.redirect(rel)
})
// Legacy asset aliases to support previously cached root-loaded homepage
app.get('/home.css', (req, res) => {
const file = path.join(__dirname, 'Homepage', 'home.css')
if (fs.existsSync(file)) return res.sendFile(file)
return res.status(404).send('Not found')
})
app.get('/home.js', (req, res) => {
const file = path.join(__dirname, 'Homepage', 'home.js')
if (fs.existsSync(file)) return res.sendFile(file)
return res.status(404).send('Not found')
})
// Serve static frontend (all folders under project root) AFTER API routes
app.use(express.static(__dirname))
// Start server only after DB is connected
async function start() {
try {
await connectDB()
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`)
})
} catch (e) {
console.error('Failed to start server', e)
process.exit(1)
}
}
if (require.main === module) {
start()
}
module.exports = { app, start }