|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Post, |
| 5 | + Body, |
| 6 | + Patch, |
| 7 | + Param, |
| 8 | + Delete, |
| 9 | + ParseIntPipe, |
| 10 | + HttpCode, |
| 11 | + HttpStatus, |
| 12 | + Query, |
| 13 | + ValidationPipe, |
| 14 | + BadRequestException, |
| 15 | +} from '@nestjs/common'; |
| 16 | +import { AssetDepreciationService } from './asset-depreciation.service'; |
| 17 | +import { |
| 18 | + CreateAssetDepreciationDto, |
| 19 | + UpdateAssetDepreciationDto, |
| 20 | + DepreciatedValueResponseDto, |
| 21 | + AssetDepreciationSummaryDto, |
| 22 | +} from './dto/asset-depreciation.dto'; |
| 23 | +import { AssetDepreciation } from './entities/asset-depreciation.entity'; |
| 24 | + |
| 25 | +@Controller('asset-depreciation') |
| 26 | +export class AssetDepreciationController { |
| 27 | + constructor(private readonly assetDepreciationService: AssetDepreciationService) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a new asset depreciation record |
| 31 | + * POST /asset-depreciation |
| 32 | + */ |
| 33 | + @Post() |
| 34 | + @HttpCode(HttpStatus.CREATED) |
| 35 | + create( |
| 36 | + @Body(ValidationPipe) createAssetDepreciationDto: CreateAssetDepreciationDto, |
| 37 | + ): Promise<AssetDepreciation> { |
| 38 | + return this.assetDepreciationService.create(createAssetDepreciationDto); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get all asset depreciation records with optional filtering |
| 43 | + * GET /asset-depreciation?isFullyDepreciated=true&depreciationMethod=straight_line&minValue=1000&maxValue=50000 |
| 44 | + */ |
| 45 | + @Get() |
| 46 | + findAll( |
| 47 | + @Query('isFullyDepreciated') isFullyDepreciated?: string, |
| 48 | + @Query('depreciationMethod') depreciationMethod?: string, |
| 49 | + @Query('minValue') minValue?: string, |
| 50 | + @Query('maxValue') maxValue?: string, |
| 51 | + ): Promise<AssetDepreciation[]> { |
| 52 | + const filters: any = {}; |
| 53 | + |
| 54 | + if (isFullyDepreciated !== undefined) { |
| 55 | + if (isFullyDepreciated === 'true') { |
| 56 | + filters.isFullyDepreciated = true; |
| 57 | + } else if (isFullyDepreciated === 'false') { |
| 58 | + filters.isFullyDepreciated = false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (depreciationMethod) { |
| 63 | + filters.depreciationMethod = depreciationMethod; |
| 64 | + } |
| 65 | + |
| 66 | + if (minValue) { |
| 67 | + const minVal = parseFloat(minValue); |
| 68 | + if (isNaN(minVal) || minVal < 0) { |
| 69 | + throw new BadRequestException('minValue must be a positive number'); |
| 70 | + } |
| 71 | + filters.minValue = minVal; |
| 72 | + } |
| 73 | + |
| 74 | + if (maxValue) { |
| 75 | + const maxVal = parseFloat(maxValue); |
| 76 | + if (isNaN(maxVal) || maxVal < 0) { |
| 77 | + throw new BadRequestException('maxValue must be a positive number'); |
| 78 | + } |
| 79 | + filters.maxValue = maxVal; |
| 80 | + } |
| 81 | + |
| 82 | + return this.assetDepreciationService.findAll(filters); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get depreciation summary statistics |
| 87 | + * GET /asset-depreciation/summary |
| 88 | + */ |
| 89 | + @Get('summary') |
| 90 | + getSummary(): Promise<AssetDepreciationSummaryDto> { |
| 91 | + return this.assetDepreciationService.getSummary(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get all current depreciated values |
| 96 | + * GET /asset-depreciation/current-values |
| 97 | + */ |
| 98 | + @Get('current-values') |
| 99 | + getAllCurrentValues(): Promise<DepreciatedValueResponseDto[]> { |
| 100 | + return this.assetDepreciationService.getAllCurrentValues(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get fully depreciated assets |
| 105 | + * GET /asset-depreciation/fully-depreciated |
| 106 | + */ |
| 107 | + @Get('fully-depreciated') |
| 108 | + getFullyDepreciatedAssets(): Promise<DepreciatedValueResponseDto[]> { |
| 109 | + return this.assetDepreciationService.getFullyDepreciatedAssets(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get assets nearing end of useful life |
| 114 | + * GET /asset-depreciation/nearing-end-of-life?threshold=1 |
| 115 | + */ |
| 116 | + @Get('nearing-end-of-life') |
| 117 | + getAssetsNearingEndOfLife( |
| 118 | + @Query('threshold') threshold?: string, |
| 119 | + ): Promise<DepreciatedValueResponseDto[]> { |
| 120 | + let thresholdYears = 1; // default |
| 121 | + if (threshold) { |
| 122 | + const parsedThreshold = parseFloat(threshold); |
| 123 | + if (isNaN(parsedThreshold) || parsedThreshold <= 0) { |
| 124 | + throw new BadRequestException('Threshold must be a positive number'); |
| 125 | + } |
| 126 | + thresholdYears = parsedThreshold; |
| 127 | + } |
| 128 | + return this.assetDepreciationService.getAssetsNearingEndOfLife(thresholdYears); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get specific asset depreciation record by ID |
| 133 | + * GET /asset-depreciation/:id |
| 134 | + */ |
| 135 | + @Get(':id') |
| 136 | + findOne(@Param('id', ParseIntPipe) id: number): Promise<AssetDepreciation> { |
| 137 | + return this.assetDepreciationService.findOne(id); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get current depreciated value of a specific asset |
| 142 | + * GET /asset-depreciation/:id/current-value |
| 143 | + */ |
| 144 | + @Get(':id/current-value') |
| 145 | + getCurrentValue(@Param('id', ParseIntPipe) id: number): Promise<DepreciatedValueResponseDto> { |
| 146 | + return this.assetDepreciationService.getCurrentValue(id); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Get projected value of asset at future date |
| 151 | + * GET /asset-depreciation/:id/projected-value?date=2025-12-31 |
| 152 | + */ |
| 153 | + @Get(':id/projected-value') |
| 154 | + async getProjectedValue( |
| 155 | + @Param('id', ParseIntPipe) id: number, |
| 156 | + @Query('date') date: string, |
| 157 | + ): Promise<{ |
| 158 | + assetName: string; |
| 159 | + currentValue: number; |
| 160 | + projectedValue: number; |
| 161 | + depreciationBetween: number; |
| 162 | + }> { |
| 163 | + if (!date) { |
| 164 | + throw new BadRequestException('Date query parameter is required (format: YYYY-MM-DD)'); |
| 165 | + } |
| 166 | + |
| 167 | + const futureDate = new Date(date); |
| 168 | + if (isNaN(futureDate.getTime())) { |
| 169 | + throw new BadRequestException('Invalid date format. Use YYYY-MM-DD'); |
| 170 | + } |
| 171 | + |
| 172 | + return this.assetDepreciationService.getProjectedValue(id, futureDate); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Update an asset depreciation record |
| 177 | + * PATCH /asset-depreciation/:id |
| 178 | + */ |
| 179 | + @Patch(':id') |
| 180 | + update( |
| 181 | + @Param('id', ParseIntPipe) id: number, |
| 182 | + @Body(ValidationPipe) updateAssetDepreciationDto: UpdateAssetDepreciationDto, |
| 183 | + ): Promise<AssetDepreciation> { |
| 184 | + return this.assetDepreciationService.update(id, updateAssetDepreciationDto); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Delete an asset depreciation record |
| 189 | + * DELETE /asset-depreciation/:id |
| 190 | + */ |
| 191 | + @Delete(':id') |
| 192 | + @HttpCode(HttpStatus.NO_CONTENT) |
| 193 | + remove(@Param('id', ParseIntPipe) id: number): Promise<void> { |
| 194 | + return this.assetDepreciationService.remove(id); |
| 195 | + } |
| 196 | +} |
0 commit comments