cropController.js (4176B)
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 | const CropStateSchema = require("../schema/cropSchema"); // HELPERS /** Generates a fake sensor hardware ID */ function fakeSensorId(prefix) { const chars = "ABCDEF0123456789"; const rand4 = () => Array.from( { length: 4 }, () => chars[Math.floor(Math.random() * chars.length)], ).join(""); return `${prefix}-SEN-${rand4()}`; } /** Auto cycle_duration_hours per crop type */ const CYCLE_HOURS = { lettuce: 1, basil: 1, tomato: 2, strawberry: 2, }; function cycleDurationForCrop(cropName) { if (!cropName) return 1; return CYCLE_HOURS[cropName.toLowerCase()] || 1; } // CONTROLLERS const createCrop = async (req, res) => { try { const { crop_id, crop, stage, location, notes, image_url, ...rest } = req.body; if (!crop_id) { return res.status(400).json({ error: "crop_id is required" }); } const existingCrop = await CropStateSchema.findOne({ crop_id }); if (existingCrop) { return res .status(409) .json({ error: "Crop with this ID already exists" }); } const newCrop = new CropStateSchema({ crop_id, crop, stage: stage || "seedling", sequence_number: 0, total_crop_lifetime_days: 0, planted_at: new Date(), last_updated: new Date(), // Auto-set initial sensor arrays sensors: { pH: [6.0], EC: [1.5], temp: [24.0], humidity: [60.0], }, // Fallback to auto-generated fake sensor IDs sensor_ids: { ph_sensor: req.body.sensor_ids?.ph_sensor || fakeSensorId("PH"), ec_sensor: req.body.sensor_ids?.ec_sensor || fakeSensorId("EC"), temp_sensor: req.body.sensor_ids?.temp_sensor || fakeSensorId("TMP"), humidity_sensor: req.body.sensor_ids?.humidity_sensor || fakeSensorId("HUM"), }, // Auto-set cycle_duration_hours based on crop type cycle_duration_hours: cycleDurationForCrop(crop), location: location || "", notes: notes || "", image_url: image_url || "", ...rest, }); const savedCrop = await newCrop.save(); res.status(201).json({ message: "Crop created successfully", data: savedCrop, }); } catch (error) { res.status(500).json({ error: error.message }); } }; const getAllCrops = async (req, res) => { console.log("getAllCrops endpoint called"); try { const crops = await CropStateSchema.find(); console.log(`Retrieved ${crops.length} crops from the database.`); res.status(200).json({ message: "Crops retrieved successfully", data: crops, }); } catch (error) { res.status(500).json({ error: error.message }); } finally { console.log("getAllCrops endpoint was called"); } }; const getCropById = async (req, res) => { try { const { cropId } = req.params; const crop = await CropStateSchema.findOne({ crop_id: cropId }); if (!crop) { return res.status(404).json({ error: "Crop not found" }); } res.status(200).json({ data: crop }); } catch (error) { res.status(500).json({ error: error.message }); } }; const updateCrop = async (req, res) => { try { const { cropId } = req.params; const updates = { ...req.body, last_updated: new Date() }; const updated = await CropStateSchema.findOneAndUpdate( { crop_id: cropId }, { $set: updates }, { new: true, runValidators: true }, ); if (!updated) { return res.status(404).json({ error: "Crop not found" }); } res.status(200).json({ message: "Crop updated", data: updated }); } catch (error) { res.status(500).json({ error: error.message }); } }; const deleteCrop = async (req, res) => { try { const { cropId } = req.params; const deleted = await CropStateSchema.findOneAndDelete({ crop_id: cropId }); if (!deleted) { return res.status(404).json({ error: "Crop not found" }); } res.status(200).json({ message: "Crop deleted", data: deleted }); } catch (error) { res.status(500).json({ error: error.message }); } }; module.exports = { createCrop, getAllCrops, getCropById, updateCrop, deleteCrop, }; |