File size: 1,079 Bytes
7e49c4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import db from '../utils/firebaseClient.js';
import { AppError } from '../middleware/errorHandler.js';

export const getRecommendations = async (req, res, next) => {
  try {
    const { assessmentId } = req.params;

    const assessmentDoc = await db.collection('assessments').doc(assessmentId).get();
    if (!assessmentDoc.exists) throw new AppError('Assessment not found', 404);

    const recommendationsSnap = await db
      .collection('recommendations')
      .where('assessmentId', '==', assessmentId)
      .get();

    const recommendations = recommendationsSnap.docs.map((doc) => ({
      id: doc.id,
      ...doc.data(),
    }));

    const priorityOrder = { HIGH: 1, MEDIUM: 2, LOW: 3 };
    recommendations.sort((a, b) => {
      const pA = priorityOrder[a.priority] || 99;
      const pB = priorityOrder[b.priority] || 99;
      if (pA !== pB) return pA - pB;
      return (b.estimatedSavings || 0) - (a.estimatedSavings || 0);
    });

    res.json({ success: true, data: recommendations, count: recommendations.length });
  } catch (err) {
    next(err);
  }
};