Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { Injectable, Logger } from '@nestjs/common'; import { ProductDocument } from '../../../database/schemas/product.schema'; @Injectable() export class CarbonFootprintService { private readonly logger = new Logger(CarbonFootprintService.name); async calculateProductCarbonFootprint(product: ProductDocument): Promise<{ manufacturing: number; shipping: number; packaging: number; endOfLife: number; total: number; unit: string; }> { try { // Calculate carbon footprint based on product category and specifications const manufacturing = this.calculateManufacturingEmissions(product); const shipping = this.calculateShippingEmissions(product); const packaging = this.calculatePackagingEmissions(product); const endOfLife = this.calculateEndOfLifeEmissions(product); const total = manufacturing + shipping + packaging + endOfLife; return { manufacturing, shipping, packaging, endOfLife, total, unit: 'kg CO2e', }; } catch (error) { this.logger.error(`Error calculating carbon footprint for product ${product._id}`, error); throw error; } } private calculateManufacturingEmissions(product: ProductDocument): number { const category = product.category.main.toLowerCase(); const baseEmissions = { fashion: 15.0, // kg CO2e per garment electronics: 50.0, // kg CO2e per device beauty: 3.0, // kg CO2e per product home: 25.0, // kg CO2e per item books: 1.5, // kg CO2e per book }; let emissions = baseEmissions[category] || 10.0; // Adjust based on materials Iif (product.specifications?.material) { const material = product.specifications.material.toLowerCase(); Iif (material.includes('organic')) emissions *= 0.8; Iif (material.includes('recycled')) emissions *= 0.7; Iif (material.includes('synthetic')) emissions *= 1.3; } return emissions; } private calculateShippingEmissions(product: ProductDocument): number { // Estimate shipping emissions based on product origin and weight const origin = product.specifications?.origin?.toLowerCase() || 'china'; const weight = this.estimateProductWeight(product); // Emissions per kg per km (approximate) const emissionFactor = 0.0001; // kg CO2e per kg per km // Distance estimates to GCC region const distances = { 'uae': 0, 'saudi arabia': 500, 'china': 8000, 'india': 2000, 'turkey': 2500, 'usa': 12000, 'europe': 6000, }; const distance = distances[origin] || 5000; // Default distance return weight * distance * emissionFactor; } private calculatePackagingEmissions(product: ProductDocument): number { const category = product.category.main.toLowerCase(); // Packaging emissions by category const packagingEmissions = { fashion: 0.5, // kg CO2e electronics: 2.0, // kg CO2e beauty: 0.3, // kg CO2e home: 1.5, // kg CO2e books: 0.2, // kg CO2e }; return packagingEmissions[category] || 0.5; } private calculateEndOfLifeEmissions(product: ProductDocument): number { const category = product.category.main.toLowerCase(); // End-of-life emissions (disposal/recycling) const endOfLifeEmissions = { fashion: 2.0, // kg CO2e electronics: 5.0, // kg CO2e (e-waste processing) beauty: 0.5, // kg CO2e home: 3.0, // kg CO2e books: 0.1, // kg CO2e (recyclable) }; return endOfLifeEmissions[category] || 1.0; } private estimateProductWeight(product: ProductDocument): number { const category = product.category.main.toLowerCase(); // Estimated weights by category (kg) const weights = { fashion: 0.5, electronics: 2.0, beauty: 0.2, home: 5.0, books: 0.3, }; return weights[category] || 1.0; } } |