feat(frontend): map SmartAdvisor rules dynamically from backend API with medical rationale rendering
This commit is contained in:
1 parent
2ec77f0e1a
commit
b1027903d6
3 files changed
+50
-30
No files matched your search
@@ -97,27 +97,48 @@ export default function SmartAdvisor({ onComplete, rules = [] }: SmartAdvisorPro
|
||||
const combinedConditions = Array.from(new Set([...currentSymptoms, ...medicalConditions]));
|
||||
|
||||
let matchedProd: any = null;
|
||||
try {
|
||||
const liveRes = await productService.getProducts({ petType: type, limit: 50 });
|
||||
if (liveRes.data && liveRes.data.length > 0) {
|
||||
matchedProd = liveRes.data.find(p => {
|
||||
const matchPet = p.suitableFor === "هر دو" || p.suitableFor === type;
|
||||
const matchSymptom = p.symptoms?.some((s: string) => combinedConditions.some(c => s.includes(c) || c.includes(s)));
|
||||
return matchPet && matchSymptom;
|
||||
});
|
||||
if (!matchedProd) {
|
||||
matchedProd = liveRes.data[0];
|
||||
let matchedReason: string | null = null;
|
||||
|
||||
// First, check backend rules if provided
|
||||
if (rules && rules.length > 0) {
|
||||
const foundRule = rules.find((r: any) => {
|
||||
const petMatch = !r.targetPetType || r.targetPetType === "هر دو" || r.targetPetType === type;
|
||||
const conditionMatch = combinedConditions.some(c => r.condition?.includes(c) || c.includes(r.condition));
|
||||
return petMatch && conditionMatch;
|
||||
});
|
||||
if (foundRule && foundRule.product) {
|
||||
matchedProd = foundRule.product;
|
||||
matchedReason = foundRule.reason;
|
||||
}
|
||||
}
|
||||
|
||||
// Second, query dynamic products API
|
||||
if (!matchedProd) {
|
||||
try {
|
||||
const liveRes = await productService.getProducts({ petType: type, limit: 50 });
|
||||
if (liveRes.data && liveRes.data.length > 0) {
|
||||
matchedProd = liveRes.data.find(p => {
|
||||
const matchPet = p.suitableFor === "هر دو" || p.suitableFor === type;
|
||||
const matchSymptom = p.symptoms?.some((s: string) => combinedConditions.some(c => s.includes(c) || c.includes(s)));
|
||||
return matchPet && matchSymptom;
|
||||
});
|
||||
if (!matchedProd) {
|
||||
matchedProd = liveRes.data[0];
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[SmartAdvisor] Dynamic product fetch failed, using fallback", e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[SmartAdvisor] Dynamic product fetch failed, using fallback", e);
|
||||
}
|
||||
|
||||
if (!matchedProd) {
|
||||
matchedProd = PRODUCTS.find(p => p.suitableFor === "هر دو" || p.suitableFor === type) || PRODUCTS[0];
|
||||
}
|
||||
|
||||
setRecommendedProduct(matchedProd);
|
||||
setRecommendedProduct({
|
||||
...matchedProd,
|
||||
medicalReason: matchedReason || matchedProd.scientificTagline || matchedProd.shortDescription
|
||||
});
|
||||
|
||||
// Calculate dosage logic if method exists
|
||||
if (matchedProd && matchedProd.calculateDosage) {
|
||||
|
||||
Reference in new issue
Block a user