|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + HttpStatus, |
| 7 | + Param, |
| 8 | + Patch, |
| 9 | + Post, |
| 10 | + Res, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; |
| 13 | +import { Response } from 'express'; |
| 14 | +import { Types } from 'mongoose'; |
| 15 | +import { AbstractController } from '~/_common/abstracts/abstract.controller'; |
| 16 | +import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator'; |
| 17 | +import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator'; |
| 18 | +import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator'; |
| 19 | +import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator'; |
| 20 | +import { ObjectIdValidationPipe } from '~/_common/pipes/object-id-validation.pipe'; |
| 21 | +import { LifecycleCreateDto, LifecycleDto, LifecycleUpdateDto } from './_dto/lifecycle.dto'; |
| 22 | +import { LifecycleService } from './lifecycle.service'; |
| 23 | + |
| 24 | +@ApiTags('management/lifecycle') |
| 25 | +@Controller('lifecycle') |
| 26 | +export class LifecycleController extends AbstractController { |
| 27 | + public constructor( |
| 28 | + protected readonly _service: LifecycleService, |
| 29 | + ) { |
| 30 | + super(); |
| 31 | + } |
| 32 | + |
| 33 | + @ApiOperation({ summary: 'Create a new lifecycle record' }) |
| 34 | + @ApiCreateDecorator(LifecycleCreateDto, LifecycleDto) |
| 35 | + @Post() |
| 36 | + public async create( |
| 37 | + @Body() dto: LifecycleCreateDto, |
| 38 | + @Res() response: Response, |
| 39 | + ): Promise<void> { |
| 40 | + try { |
| 41 | + const result = await this._service.createLifecycle(dto); |
| 42 | + response.status(HttpStatus.CREATED).json(result); |
| 43 | + } catch (error) { |
| 44 | + this.handleError(error, response); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @ApiOperation({ summary: 'Get all lifecycle records' }) |
| 49 | + @ApiPaginatedDecorator(LifecycleDto) |
| 50 | + @Get() |
| 51 | + public async findAll( |
| 52 | + @Res() response: Response, |
| 53 | + ): Promise<void> { |
| 54 | + try { |
| 55 | + const result = await this._service.find(); |
| 56 | + response.status(HttpStatus.OK).json(result); |
| 57 | + } catch (error) { |
| 58 | + this.handleError(error, response); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @ApiOperation({ summary: 'Get lifecycle record by ID' }) |
| 63 | + @ApiParam({ name: 'id', description: 'Lifecycle ID' }) |
| 64 | + @ApiReadResponseDecorator(LifecycleDto) |
| 65 | + @Get(':id') |
| 66 | + public async findOne( |
| 67 | + @Param('id', ObjectIdValidationPipe) id: Types.ObjectId, |
| 68 | + @Res() response: Response, |
| 69 | + ): Promise<void> { |
| 70 | + try { |
| 71 | + const result = await this._service.findById(id); |
| 72 | + response.status(HttpStatus.OK).json(result); |
| 73 | + } catch (error) { |
| 74 | + this.handleError(error, response); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @ApiOperation({ summary: 'Update lifecycle record' }) |
| 79 | + @ApiParam({ name: 'id', description: 'Lifecycle ID' }) |
| 80 | + @ApiUpdateDecorator(LifecycleUpdateDto, LifecycleDto) |
| 81 | + @Patch(':id') |
| 82 | + public async update( |
| 83 | + @Param('id', ObjectIdValidationPipe) id: Types.ObjectId, |
| 84 | + @Body() dto: LifecycleUpdateDto, |
| 85 | + @Res() response: Response, |
| 86 | + ): Promise<void> { |
| 87 | + try { |
| 88 | + const result = await this._service.updateLifecycle(id, dto); |
| 89 | + response.status(HttpStatus.OK).json(result); |
| 90 | + } catch (error) { |
| 91 | + this.handleError(error, response); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @ApiOperation({ summary: 'Delete lifecycle record' }) |
| 96 | + @ApiParam({ name: 'id', description: 'Lifecycle ID' }) |
| 97 | + @Delete(':id') |
| 98 | + public async remove( |
| 99 | + @Param('id', ObjectIdValidationPipe) id: Types.ObjectId, |
| 100 | + @Res() response: Response, |
| 101 | + ): Promise<void> { |
| 102 | + try { |
| 103 | + await this._service.delete(id); |
| 104 | + response.status(HttpStatus.NO_CONTENT).send(); |
| 105 | + } catch (error) { |
| 106 | + this.handleError(error, response); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @ApiOperation({ summary: 'Get lifecycle history for an identity' }) |
| 111 | + @ApiParam({ name: 'identityId', description: 'Identity ID' }) |
| 112 | + @Get('identity/:identityId') |
| 113 | + public async getLifecycleHistory( |
| 114 | + @Param('identityId', ObjectIdValidationPipe) identityId: Types.ObjectId, |
| 115 | + @Res() response: Response, |
| 116 | + ): Promise<void> { |
| 117 | + try { |
| 118 | + const result = await this._service.getLifecycleHistory(identityId); |
| 119 | + response.status(HttpStatus.OK).json(result); |
| 120 | + } catch (error) { |
| 121 | + this.handleError(error, response); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @ApiOperation({ summary: 'Get lifecycle statistics' }) |
| 126 | + @Get('stats') |
| 127 | + public async getStats( |
| 128 | + @Res() response: Response, |
| 129 | + ): Promise<void> { |
| 130 | + try { |
| 131 | + const result = await this._service.getLifecycleStats(); |
| 132 | + response.status(HttpStatus.OK).json(result); |
| 133 | + } catch (error) { |
| 134 | + this.handleError(error, response); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @ApiOperation({ summary: 'Get recent lifecycle changes' }) |
| 139 | + @Get('recent') |
| 140 | + public async getRecentChanges( |
| 141 | + @Res() response: Response, |
| 142 | + ): Promise<void> { |
| 143 | + try { |
| 144 | + const result = await this._service.getRecentChanges(); |
| 145 | + response.status(HttpStatus.OK).json(result); |
| 146 | + } catch (error) { |
| 147 | + this.handleError(error, response); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private handleError(error: any, response: Response): void { |
| 152 | + console.error('Lifecycle Controller Error:', error); |
| 153 | + response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ |
| 154 | + message: 'An error occurred while processing the request', |
| 155 | + error: error.message, |
| 156 | + }); |
| 157 | + } |
| 158 | +} |
0 commit comments