|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Post, |
| 5 | + Patch, |
| 6 | + Delete, |
| 7 | + Body, |
| 8 | + Param, |
| 9 | + Query, |
| 10 | + UseGuards, |
| 11 | + HttpCode, |
| 12 | + HttpStatus, |
| 13 | +} from '@nestjs/common'; |
| 14 | +import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; |
| 15 | +import { AssetsService } from './assets.service'; |
| 16 | +import { CreateAssetDto } from './dto/create-asset.dto'; |
| 17 | +import { UpdateAssetDto } from './dto/update-asset.dto'; |
| 18 | +import { AssetFiltersDto } from './dto/asset-filters.dto'; |
| 19 | +import { TransferAssetDto } from './dto/transfer-asset.dto'; |
| 20 | +import { UpdateStatusDto } from './dto/update-status.dto'; |
| 21 | +import { CreateNoteDto } from './dto/create-note.dto'; |
| 22 | +import { CreateMaintenanceDto } from './dto/create-maintenance.dto'; |
| 23 | +import { UpdateMaintenanceDto } from './dto/update-maintenance.dto'; |
| 24 | +import { CreateDocumentDto } from './dto/create-document.dto'; |
| 25 | +import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
| 26 | +import { CurrentUser } from '../auth/decorators/current-user.decorator'; |
| 27 | +import { User } from '../users/user.entity'; |
| 28 | + |
| 29 | +@ApiTags('Assets') |
| 30 | +@ApiBearerAuth('JWT-auth') |
| 31 | +@UseGuards(JwtAuthGuard) |
| 32 | +@Controller('assets') |
| 33 | +export class AssetsController { |
| 34 | + constructor(private readonly service: AssetsService) {} |
| 35 | + |
| 36 | + @Get() |
| 37 | + @ApiOperation({ summary: 'List all assets with optional filters and pagination' }) |
| 38 | + findAll(@Query() filters: AssetFiltersDto) { |
| 39 | + return this.service.findAll(filters); |
| 40 | + } |
| 41 | + |
| 42 | + @Get(':id') |
| 43 | + @ApiOperation({ summary: 'Get a single asset by ID' }) |
| 44 | + findOne(@Param('id') id: string) { |
| 45 | + return this.service.findOne(id); |
| 46 | + } |
| 47 | + |
| 48 | + @Post() |
| 49 | + @ApiOperation({ summary: 'Register a new asset' }) |
| 50 | + create(@Body() dto: CreateAssetDto, @CurrentUser() user: User) { |
| 51 | + return this.service.create(dto, user); |
| 52 | + } |
| 53 | + |
| 54 | + @Patch(':id') |
| 55 | + @ApiOperation({ summary: 'Update asset details' }) |
| 56 | + update(@Param('id') id: string, @Body() dto: UpdateAssetDto, @CurrentUser() user: User) { |
| 57 | + return this.service.update(id, dto, user); |
| 58 | + } |
| 59 | + |
| 60 | + @Patch(':id/status') |
| 61 | + @ApiOperation({ summary: 'Update asset status' }) |
| 62 | + updateStatus(@Param('id') id: string, @Body() dto: UpdateStatusDto, @CurrentUser() user: User) { |
| 63 | + return this.service.updateStatus(id, dto, user); |
| 64 | + } |
| 65 | + |
| 66 | + @Post(':id/transfer') |
| 67 | + @ApiOperation({ summary: 'Transfer asset to a different department or user' }) |
| 68 | + transfer(@Param('id') id: string, @Body() dto: TransferAssetDto, @CurrentUser() user: User) { |
| 69 | + return this.service.transfer(id, dto, user); |
| 70 | + } |
| 71 | + |
| 72 | + @Delete(':id') |
| 73 | + @HttpCode(HttpStatus.NO_CONTENT) |
| 74 | + @ApiOperation({ summary: 'Delete an asset' }) |
| 75 | + remove(@Param('id') id: string) { |
| 76 | + return this.service.remove(id); |
| 77 | + } |
| 78 | + |
| 79 | + @Get(':id/history') |
| 80 | + @ApiOperation({ summary: 'Get asset change history' }) |
| 81 | + getHistory(@Param('id') id: string) { |
| 82 | + return this.service.getHistory(id); |
| 83 | + } |
| 84 | + |
| 85 | + // ── Notes ───────────────────────────────────────────────────── |
| 86 | + |
| 87 | + @Get(':id/notes') |
| 88 | + @ApiOperation({ summary: 'Get all notes for an asset' }) |
| 89 | + getNotes(@Param('id') id: string) { |
| 90 | + return this.service.getNotes(id); |
| 91 | + } |
| 92 | + |
| 93 | + @Post(':id/notes') |
| 94 | + @ApiOperation({ summary: 'Add a note to an asset' }) |
| 95 | + createNote(@Param('id') id: string, @Body() dto: CreateNoteDto, @CurrentUser() user: User) { |
| 96 | + return this.service.createNote(id, dto, user); |
| 97 | + } |
| 98 | + |
| 99 | + @Delete(':id/notes/:noteId') |
| 100 | + @HttpCode(HttpStatus.NO_CONTENT) |
| 101 | + @ApiOperation({ summary: 'Delete a note' }) |
| 102 | + deleteNote(@Param('id') id: string, @Param('noteId') noteId: string) { |
| 103 | + return this.service.deleteNote(id, noteId); |
| 104 | + } |
| 105 | + |
| 106 | + // ── Maintenance ─────────────────────────────────────────────── |
| 107 | + |
| 108 | + @Get(':id/maintenance') |
| 109 | + @ApiOperation({ summary: 'Get maintenance records for an asset' }) |
| 110 | + getMaintenance(@Param('id') id: string) { |
| 111 | + return this.service.getMaintenance(id); |
| 112 | + } |
| 113 | + |
| 114 | + @Post(':id/maintenance') |
| 115 | + @ApiOperation({ summary: 'Create a maintenance record for an asset' }) |
| 116 | + createMaintenance(@Param('id') id: string, @Body() dto: CreateMaintenanceDto, @CurrentUser() user: User) { |
| 117 | + return this.service.createMaintenance(id, dto, user); |
| 118 | + } |
| 119 | + |
| 120 | + @Patch(':id/maintenance/:maintenanceId') |
| 121 | + @ApiOperation({ summary: 'Update a maintenance record' }) |
| 122 | + updateMaintenance( |
| 123 | + @Param('id') id: string, |
| 124 | + @Param('maintenanceId') maintenanceId: string, |
| 125 | + @Body() dto: UpdateMaintenanceDto, |
| 126 | + ) { |
| 127 | + return this.service.updateMaintenance(id, maintenanceId, dto); |
| 128 | + } |
| 129 | + |
| 130 | + // ── Documents ───────────────────────────────────────────────── |
| 131 | + |
| 132 | + @Get(':id/documents') |
| 133 | + @ApiOperation({ summary: 'Get documents attached to an asset' }) |
| 134 | + getDocuments(@Param('id') id: string) { |
| 135 | + return this.service.getDocuments(id); |
| 136 | + } |
| 137 | + |
| 138 | + @Post(':id/documents') |
| 139 | + @ApiOperation({ summary: 'Attach a document (URL) to an asset' }) |
| 140 | + addDocument(@Param('id') id: string, @Body() dto: CreateDocumentDto, @CurrentUser() user: User) { |
| 141 | + return this.service.addDocument(id, dto, user); |
| 142 | + } |
| 143 | + |
| 144 | + @Delete(':id/documents/:documentId') |
| 145 | + @HttpCode(HttpStatus.NO_CONTENT) |
| 146 | + @ApiOperation({ summary: 'Remove a document from an asset' }) |
| 147 | + deleteDocument(@Param('id') id: string, @Param('documentId') documentId: string) { |
| 148 | + return this.service.deleteDocument(id, documentId); |
| 149 | + } |
| 150 | +} |
0 commit comments