Agromind-backend / backend /controllers /pestOutbreakController.js
gh-action-hf-auto
auto: sync backend from github@32fb9685
8a6248c
import { generateAIContent } from '../utils/aiHelper.js';
import { extractLanguage, getLanguageName } from '../utils/aiOrchestrator.js';
import dotenv from "dotenv";
export const pestOutbreakRecommendations = async (req, res) => {
dotenv.config();
const { region, weather, cropType, communityReports } = req.body;
if (!region || !weather || !cropType || !communityReports) {
return res.status(400).json({
error: "Missing required inputs: region, weather, cropType, communityReports",
});
}
const lang = extractLanguage(req);
const langName = getLanguageName(lang);
const langInstruction = lang !== 'en' ? `\n\nRespond STRICTLY in ${langName} language. Translate all fields and values.` : '';
try {
const prompt = `
You are an agricultural pest outbreak prediction expert.
Analyze the risk of pest infestation using:
- Region: ${region}
- Weather forecast: ${weather}
- Crop type: ${cropType}
- Community pest reports (last 7 days): ${communityReports}
Provide:
1. Whether there is a risk of pest outbreak (Yes/No)
2. Likely pest that may attack (e.g., stem borer, aphids, bollworm, etc.)
3. Risk level (%) based on severity and probability
4. Expected time window (days until possible outbreak)
5. Preventive actions farmers should take immediately (bullet points)
6. A short note (1–2 lines of advice)
Respond ONLY in this JSON format:
{
"outbreak_risk": "",
"likely_pest": "",
"risk_level_percent": 0,
"expected_days": 0,
"preventive_actions": ["", "", ""],
"note": ""
}${langInstruction}
`;
const recommendation = await generateAIContent(prompt.trim());
const formattedRecommendation = recommendation
.replace("```json", "")
.replace("```", "")
.trim();
res.status(200).json({
recommendation: formattedRecommendation,
});
} catch (err) {
console.error("Error fetching recommendations: ", err);
res.status(500).json({ error: "Failed to fetch recommendations" });
}
};