Agromind-backend / backend /controllers /getLoanEligibilityReportController.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 getLoanEligibilityReport = async (req, res) => {
dotenv.config();
const {
location,
landSize,
landType,
cropType,
cropStage,
pastYield,
existingLoans,
} = req.body;
if (!location || !landSize || !landType || !cropType || !cropStage) {
return res.status(400).json({
error: "Missing required inputs: location, landSize, landType, cropType, cropStage",
});
}
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 expert agricultural financial analyst who evaluates farmer credit eligibility based on farm potential and financial risk.
Evaluate the farmer using:
- Farm Location: ${location}
- Land Size: ${landSize}
- Land Type: ${landType}
- Crop Type: ${cropType}
- Crop Stage: ${cropStage}
- Past Yield (optional): ${pastYield || "Not provided"}
- Existing Loans (optional): ${existingLoans || "Not provided"}
Consider:
- Crop yield prediction
- Soil health and farm productivity potential
- Market price forecast & demand trends
- Climate risk profile (drought/flood probability)
- Irrigation access and fertilizer usage (assume based on crop & region if not given)
- Cropping pattern stability
Provide the output ONLY in the following JSON format:
{
"loan_approval_probability": 0,
"eligible_loan_amount_range": "",
"risk_category": "",
"expected_repayment_capacity": "",
"recommendations": ["", "", ""],
"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" });
}
};