All files / src/modules/sustainability sustainability.controller.ts

0% Statements 0/55
0% Branches 0/14
0% Functions 0/10
0% Lines 0/53

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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { SustainabilityService } from './sustainability.service';
import { ResaleMarketplaceService } from './services/resale-marketplace.service';
 
@ApiTags('sustainability')
@Controller('sustainability')
export class SustainabilityController {
  constructor(
    private readonly sustainabilityService: SustainabilityService,
    private readonly resaleMarketplaceService: ResaleMarketplaceService,
  ) {}
 
  @Get('dashboard')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get user sustainability dashboard' })
  @ApiResponse({ status: 200, description: 'Sustainability dashboard retrieved successfully' })
  async getSustainabilityDashboard(@Request() req: any) {
    try {
      const dashboard = await this.sustainabilityService.getSustainabilityDashboard(req.user.id);
      
      return {
        success: true,
        data: dashboard,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
 
  @Get('product/:productId/score')
  @ApiOperation({ summary: 'Get product sustainability score' })
  @ApiResponse({ status: 200, description: 'Product sustainability score retrieved successfully' })
  async getProductSustainabilityScore(@Param('productId') productId: string) {
    try {
      const score = await this.sustainabilityService.getProductSustainabilityScore(productId);
      
      return {
        success: true,
        data: score,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
 
  @Get('alternatives/:productId')
  @ApiOperation({ summary: 'Get eco-friendly alternatives for a product' })
  @ApiResponse({ status: 200, description: 'Eco-friendly alternatives retrieved successfully' })
  async getEcoFriendlyAlternatives(
    @Param('productId') productId: string,
    @Query('limit') limit?: number,
  ) {
    try {
      const alternatives = await this.sustainabilityService.getEcoFriendlyAlternatives(
        productId,
        limit ? parseInt(limit.toString()) : 5,
      );
      
      return {
        success: true,
        data: alternatives,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
 
  @Get('brands/sustainable')
  @ApiOperation({ summary: 'Get sustainable brands' })
  @ApiResponse({ status: 200, description: 'Sustainable brands retrieved successfully' })
  async getSustainableBrands(
    @Query('category') category?: string,
    @Query('minScore') minScore?: number,
    @Query('limit') limit?: number,
  ) {
    try {
      const brands = await this.sustainabilityService.getSustainableBrands({
        category,
        minScore: minScore ? parseInt(minScore.toString()) : undefined,
        limit: limit ? parseInt(limit.toString()) : undefined,
      });
      
      return {
        success: true,
        data: brands,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
 
  @Post('track')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Track sustainability impact' })
  @ApiResponse({ status: 200, description: 'Sustainability impact tracked successfully' })
  async trackSustainabilityImpact(
    @Request() req: any,
    @Body() trackingData: {
      productId: string;
      action: 'purchase' | 'view' | 'compare';
    },
  ) {
    try {
      await this.sustainabilityService.trackSustainabilityImpact(
        req.user.id,
        trackingData.productId,
        trackingData.action,
      );
      
      return {
        success: true,
        message: 'Sustainability impact tracked successfully',
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }
 
  @Post('carbon-footprint/compare')
  @ApiOperation({ summary: 'Compare carbon footprint of products' })
  @ApiResponse({ status: 200, description: 'Carbon footprint comparison retrieved successfully' })
  async getCarbonFootprintComparison(
    @Body() request: { productIds: string[] },
  ) {
    try {
      const comparison = await this.sustainabilityService.getCarbonFootprintComparison(
        request.productIds,
      );
      
      return {
        success: true,
        data: comparison,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }
 
  @Get('circular-economy/opportunities')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get circular economy opportunities' })
  @ApiResponse({ status: 200, description: 'Circular economy opportunities retrieved successfully' })
  async getCircularEconomyOpportunities(@Request() req: any) {
    try {
      const opportunities = await this.sustainabilityService.getCircularEconomyOpportunities(
        req.user.id,
      );
      
      return {
        success: true,
        data: opportunities,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
 
  @Post('resale/listing')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create resale listing' })
  @ApiResponse({ status: 201, description: 'Resale listing created successfully' })
  async createResaleListing(
    @Request() req: any,
    @Body() listingData: any,
  ) {
    try {
      const listing = await this.resaleMarketplaceService.createListing({
        ...listingData,
        sellerId: req.user.id,
      });
      
      return {
        success: true,
        data: listing,
        message: 'Resale listing created successfully',
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }
 
  @Get('resale/listings')
  @ApiOperation({ summary: 'Get resale listings' })
  @ApiResponse({ status: 200, description: 'Resale listings retrieved successfully' })
  async getResaleListings(
    @Query('category') category?: string,
    @Query('condition') condition?: string,
    @Query('location') location?: string,
    @Query('minPrice') minPrice?: number,
    @Query('maxPrice') maxPrice?: number,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    try {
      const listings = await this.resaleMarketplaceService.getListings({
        category,
        condition,
        location,
        priceRange: minPrice && maxPrice ? { min: minPrice, max: maxPrice } : undefined,
        page: page ? parseInt(page.toString()) : 1,
        limit: limit ? parseInt(limit.toString()) : 20,
      });
      
      return {
        success: true,
        data: listings,
      };
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}