Description
The frontend extensively references batch-level IoT sensor fields — currentTemperature, currentHumidity, isSpoiled, and iotTimestamp — across three separate pages (track-batch, journey map, compare). However, none of these fields exist in the Batch Mongoose schema (backend/models/Batch.js). No backend controller or service ever sets them. As a result:
- The "IoT Sensor Data" section on the track-batch page (75+ lines) never renders — it has conditional checks like
if (!batch.currentTemperature) which always evaluate as false
- The "Spoilage Breached" / "Oracle Secured" badges on the journey page never appear
- The "Spoiled" vs "Active" status comparison on the compare page always shows "Active"
Steps to Reproduce
- Navigate to any batch at
/track-batch (e.g., BATCH000001)
- Observe the "IoT Sensor Data" section does not appear even for batches that have been in transport for days
- Open browser DevTools and inspect
batch.currentTemperature — it is undefined
- Navigate to
/batch/BATCH000001/journey — observe no spoilage/oracle badges appear
- Navigate to
/compare?ids=BATCH000001,BATCH000002 — observe both always show "Active" never "Spoiled"
Expected Behavior
- Backend model: Add
currentTemperature, currentHumidity, isSpoiled, and iotTimestamp fields to the Batch schema
- Backend route: Add a
POST /api/batches/:batchId/iot endpoint for IoT sensor data ingestion
- Backend route: Add a
GET /api/batches/:batchId/iot/history endpoint for historical telemetry
- Frontend: The existing IoT section on track-batch, journey spoilage badges, and compare status should function with real data
- Validation: Temperature should be validated as a realistic range (-10°C to 60°C), humidity 0–100%
Implementation Hints
Update Batch model (backend/models/Batch.js):
iotData: {
currentTemperature: { type: Number, min: -10, max: 60 },
currentHumidity: { type: Number, min: 0, max: 100 },
isSpoiled: { type: Boolean, default: false },
lastUpdated: { type: Date },
telemetryHistory: [{
temperature: Number,
humidity: Number,
timestamp: { type: Date, default: Date.now }
}]
}
Add IoT endpoint in backend/routes/index.js:
router.post(/batches/:batchId/iot, protect, recordIoTData);
router.get(/batches/:batchId/iot/history, protect, getIoTData);
Add a simple spoilage detection service that flags isSpoiled when temperature exceeds safe thresholds for the crop type:
const SPOILAGE_THRESHOLDS = {
rice: { maxTemp: 25, maxHumidity: 70 },
wheat: { maxTemp: 30, maxHumidity: 65 },
tomato: { maxTemp: 10, maxHumidity: 85 },
corn: { maxTemp: 28, maxHumidity: 75 },
};
Affected Files
backend/models/Batch.js
backend/controllers/batchController.js (new recordIoTData + getIoTData methods)
backend/routes/index.js
frontend/src/app/track-batch/page.tsx
frontend/src/app/batch/[batchId]/journey/page.tsx
frontend/src/app/compare/page.tsx
Labels
type:enhancement, level:intermediate, GSSoC-26
Description
The frontend extensively references batch-level IoT sensor fields —
currentTemperature,currentHumidity,isSpoiled, andiotTimestamp— across three separate pages (track-batch, journey map, compare). However, none of these fields exist in the Batch Mongoose schema (backend/models/Batch.js). No backend controller or service ever sets them. As a result:if (!batch.currentTemperature)which always evaluate asfalseSteps to Reproduce
/track-batch(e.g.,BATCH000001)batch.currentTemperature— it isundefined/batch/BATCH000001/journey— observe no spoilage/oracle badges appear/compare?ids=BATCH000001,BATCH000002— observe both always show "Active" never "Spoiled"Expected Behavior
currentTemperature,currentHumidity,isSpoiled, andiotTimestampfields to the Batch schemaPOST /api/batches/:batchId/iotendpoint for IoT sensor data ingestionGET /api/batches/:batchId/iot/historyendpoint for historical telemetryImplementation Hints
Update Batch model (
backend/models/Batch.js):Add IoT endpoint in
backend/routes/index.js:Add a simple spoilage detection service that flags
isSpoiledwhen temperature exceeds safe thresholds for the crop type:Affected Files
backend/models/Batch.jsbackend/controllers/batchController.js(new recordIoTData + getIoTData methods)backend/routes/index.jsfrontend/src/app/track-batch/page.tsxfrontend/src/app/batch/[batchId]/journey/page.tsxfrontend/src/app/compare/page.tsxLabels
type:enhancement,level:intermediate,GSSoC-26