1+ import {
2+ Controller ,
3+ Get ,
4+ Post ,
5+ Body ,
6+ Patch ,
7+ Param ,
8+ Delete ,
9+ Query ,
10+ HttpCode ,
11+ HttpStatus ,
12+ ParseUUIDPipe ,
13+ ParseBoolPipe ,
14+ } from '@nestjs/common' ;
15+ import {
16+ ApiTags ,
17+ ApiOperation ,
18+ ApiResponse ,
19+ ApiQuery ,
20+ ApiParam ,
21+ } from '@nestjs/swagger' ;
22+ import { CostCentersService } from './cost-centers.service' ;
23+ import { CreateCostCenterDto } from './dto/create-cost-center.dto' ;
24+ import { UpdateCostCenterDto } from './dto/update-cost-center.dto' ;
25+ import { CostCenterResponseDto } from './dto/cost-center-response.dto' ;
26+
27+ @ApiTags ( 'Cost Centers' )
28+ @Controller ( 'cost-centers' )
29+ export class CostCentersController {
30+ constructor ( private readonly costCentersService : CostCentersService ) { }
31+
32+ @Post ( )
33+ @ApiOperation ( { summary : 'Create a new cost center' } )
34+ @ApiResponse ( {
35+ status : 201 ,
36+ description : 'Cost center created successfully' ,
37+ type : CostCenterResponseDto ,
38+ } )
39+ @ApiResponse ( { status : 409 , description : 'Cost center name already exists' } )
40+ async create ( @Body ( ) createDto : CreateCostCenterDto ) {
41+ return await this . costCentersService . create ( createDto ) ;
42+ }
43+
44+ @Get ( )
45+ @ApiOperation ( { summary : 'Get all cost centers' } )
46+ @ApiQuery ( {
47+ name : 'includeInactive' ,
48+ required : false ,
49+ type : Boolean ,
50+ description : 'Include inactive cost centers' ,
51+ } )
52+ @ApiResponse ( {
53+ status : 200 ,
54+ description : 'List of cost centers' ,
55+ type : [ CostCenterResponseDto ] ,
56+ } )
57+ async findAll (
58+ @Query ( 'includeInactive' , new ParseBoolPipe ( { optional : true } ) )
59+ includeInactive ?: boolean ,
60+ ) {
61+ return await this . costCentersService . findAll ( includeInactive ) ;
62+ }
63+
64+ @Get ( ':id' )
65+ @ApiOperation ( { summary : 'Get a cost center by ID' } )
66+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
67+ @ApiResponse ( {
68+ status : 200 ,
69+ description : 'Cost center details' ,
70+ type : CostCenterResponseDto ,
71+ } )
72+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
73+ async findOne ( @Param ( 'id' , ParseUUIDPipe ) id : string ) {
74+ return await this . costCentersService . findOne ( id ) ;
75+ }
76+
77+ @Get ( ':id/financial-report' )
78+ @ApiOperation ( { summary : 'Get financial report for a cost center' } )
79+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
80+ @ApiResponse ( {
81+ status : 200 ,
82+ description : 'Financial report with assets and expenses' ,
83+ } )
84+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
85+ async getFinancialReport ( @Param ( 'id' , ParseUUIDPipe ) id : string ) {
86+ return await this . costCentersService . getFinancialReport ( id ) ;
87+ }
88+
89+ @Patch ( ':id' )
90+ @ApiOperation ( { summary : 'Update a cost center' } )
91+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
92+ @ApiResponse ( {
93+ status : 200 ,
94+ description : 'Cost center updated successfully' ,
95+ type : CostCenterResponseDto ,
96+ } )
97+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
98+ @ApiResponse ( { status : 409 , description : 'Cost center name already exists' } )
99+ async update (
100+ @Param ( 'id' , ParseUUIDPipe ) id : string ,
101+ @Body ( ) updateDto : UpdateCostCenterDto ,
102+ ) {
103+ return await this . costCentersService . update ( id , updateDto ) ;
104+ }
105+
106+ @Patch ( ':id/deactivate' )
107+ @ApiOperation ( { summary : 'Deactivate a cost center (soft delete)' } )
108+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
109+ @ApiResponse ( {
110+ status : 200 ,
111+ description : 'Cost center deactivated' ,
112+ type : CostCenterResponseDto ,
113+ } )
114+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
115+ async softDelete ( @Param ( 'id' , ParseUUIDPipe ) id : string ) {
116+ return await this . costCentersService . softDelete ( id ) ;
117+ }
118+
119+ @Patch ( ':id/restore' )
120+ @ApiOperation ( { summary : 'Restore a deactivated cost center' } )
121+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
122+ @ApiResponse ( {
123+ status : 200 ,
124+ description : 'Cost center restored' ,
125+ type : CostCenterResponseDto ,
126+ } )
127+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
128+ async restore ( @Param ( 'id' , ParseUUIDPipe ) id : string ) {
129+ return await this . costCentersService . restore ( id ) ;
130+ }
131+
132+ @Delete ( ':id' )
133+ @HttpCode ( HttpStatus . NO_CONTENT )
134+ @ApiOperation ( { summary : 'Permanently delete a cost center' } )
135+ @ApiParam ( { name : 'id' , description : 'Cost center UUID' } )
136+ @ApiResponse ( { status : 204 , description : 'Cost center deleted' } )
137+ @ApiResponse ( { status : 404 , description : 'Cost center not found' } )
138+ async remove ( @Param ( 'id' , ParseUUIDPipe ) id : string ) {
139+ await this . costCentersService . remove ( id ) ;
140+ }
141+ }
0 commit comments