Spaces:
Runtime error
Runtime error
File size: 6,749 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 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import { z } from 'zod';
import db from '../utils/firebaseClient.js';
import { compareToAverages } from '../services/scoringService.js';
import { createAssessmentWithRecommendations } from '../services/assessmentService.js';
import { AppError } from '../middleware/errorHandler.js';
import { sanitizeText } from '../utils/sanitize.js';
const assessmentSchema = z
.object({
userId: z.string().min(1, 'userId is required'),
// Transportation
dailyCarKm: z.number().min(0).max(1000).default(0),
carFuelType: z.enum(['petrol', 'diesel', 'electric', 'hybrid', 'none']).default('none'),
publicTransportKmPerWeek: z.number().min(0).max(10000).default(0),
cyclingKmPerWeek: z.number().min(0).max(1000).default(0),
shortFlightsPerYear: z.number().int().min(0).max(100).default(0),
longFlightsPerYear: z.number().int().min(0).max(50).default(0),
// Energy
monthlyElectricityKwh: z.number().min(0).max(10000).default(0),
renewablePercentage: z.number().min(0).max(100).default(0),
// Food
dietType: z.enum(['vegan', 'vegetarian', 'mixed', 'heavy_meat']).default('mixed'),
// Shopping
clothingItemsPerYear: z.number().int().min(0).max(500).default(0),
electronicsItemsPerYear: z.number().int().min(0).max(50).default(0),
})
.strict();
const PERCENTAGE_MULTIPLIER = 100;
function buildUnknownKeysMessage(zodError, allowedKeys) {
const unknownKeyIssues = zodError.issues.filter((i) => i.code === 'unrecognized_keys');
if (unknownKeyIssues.length === 0) return zodError.message;
const unknownKeys = unknownKeyIssues.flatMap((i) => i.keys);
return (
`Unknown fields: ${unknownKeys.join(', ')}. ` +
`Only these fields are accepted: ${allowedKeys.join(', ')}`
);
}
const ALLOWED_KEYS = Object.keys(assessmentSchema._def.shape());
export const createAssessment = async (req, res, next) => {
try {
const parseResult = assessmentSchema.safeParse(req.body);
if (!parseResult.success) {
const message = buildUnknownKeysMessage(parseResult.error, ALLOWED_KEYS);
throw new AppError(message, 400);
}
const data = parseResult.data;
data.userId = sanitizeText(data.userId);
const { assessment, emissions, score } = await createAssessmentWithRecommendations(data);
res.status(201).json({
success: true,
data: {
...assessment,
breakdown: emissions.breakdown,
scoreInfo: { score, ...emissions },
},
});
} catch (err) {
next(err);
}
};
export const getAssessmentById = async (req, res, next) => {
try {
const { id } = req.params;
const sanitizedId = sanitizeText(id);
const assessmentDoc = await db.collection('assessments').doc(sanitizedId).get();
if (!assessmentDoc.exists) throw new AppError('Assessment not found', 404);
const assessment = assessmentDoc.data();
// Fetch recommendations
const recsSnap = await db
.collection('recommendations')
.where('assessmentId', '==', sanitizedId)
.get();
const recommendations = recsSnap.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
recommendations.sort((a, b) => (b.estimatedSavings || 0) - (a.estimatedSavings || 0));
// Fetch simulations
const simsSnap = await db
.collection('simulations')
.where('assessmentId', '==', sanitizedId)
.get();
const simulations = simsSnap.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
simulations.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
// Fetch user
const userDoc = await db.collection('users').doc(assessment.userId).get();
const user = userDoc.exists
? { id: userDoc.id, name: userDoc.data().name, email: userDoc.data().email }
: null;
const total = assessment.totalEmission;
const safeTotal = total === 0 ? 1 : total;
res.json({
success: true,
data: {
...assessment,
id: sanitizedId,
recommendations,
simulations,
user,
breakdown: {
transport: parseFloat(
((assessment.transportEmission / safeTotal) * PERCENTAGE_MULTIPLIER).toFixed(1)
),
energy: parseFloat(
((assessment.energyEmission / safeTotal) * PERCENTAGE_MULTIPLIER).toFixed(1)
),
food: parseFloat(
((assessment.foodEmission / safeTotal) * PERCENTAGE_MULTIPLIER).toFixed(1)
),
shopping: parseFloat(
((assessment.shoppingEmission / safeTotal) * PERCENTAGE_MULTIPLIER).toFixed(1)
),
},
},
});
} catch (err) {
next(err);
}
};
export const getAssessmentsByUser = async (req, res, next) => {
try {
const { userId } = req.params;
const sanitizedUserId = sanitizeText(userId);
const userDoc = await db.collection('users').doc(sanitizedUserId).get();
if (!userDoc.exists) throw new AppError('User not found', 404);
const assessmentsSnap = await db
.collection('assessments')
.where('userId', '==', sanitizedUserId)
.get();
const assessments = assessmentsSnap.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
assessments.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
for (const assessment of assessments) {
const recsSnap = await db
.collection('recommendations')
.where('assessmentId', '==', assessment.id)
.get();
const recommendations = recsSnap.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
recommendations.sort((a, b) => (b.estimatedSavings || 0) - (a.estimatedSavings || 0));
assessment.recommendations = recommendations.slice(0, 3);
const simsSnap = await db
.collection('simulations')
.where('assessmentId', '==', assessment.id)
.get();
assessment._count = { simulations: simsSnap.docs.length };
}
res.json({ success: true, data: assessments, count: assessments.length });
} catch (err) {
next(err);
}
};
export const compareAssessment = async (req, res, next) => {
try {
const { id } = req.params;
const sanitizedId = sanitizeText(id);
const assessmentDoc = await db.collection('assessments').doc(sanitizedId).get();
if (!assessmentDoc.exists) throw new AppError('Assessment not found', 404);
const assessment = assessmentDoc.data();
const comparison = compareToAverages(assessment.totalEmission);
res.set('Cache-Control', 'public, max-age=300, stale-while-revalidate=60');
res.json({
success: true,
data: {
assessmentId: sanitizedId,
totalEmission: assessment.totalEmission,
sustainabilityScore: assessment.sustainabilityScore,
comparison,
},
});
} catch (err) {
next(err);
}
};
|