Skip to content

Commit 031028f

Browse files
authored
Merge pull request #281 from Code-4-Community/256-dev---generate-docs-and-log-code-for-the-grant-module
Docs and Loggers for Grant Module
2 parents 4e744fd + d9e637e commit 031028f

File tree

5 files changed

+395
-118
lines changed

5 files changed

+395
-118
lines changed

backend/src/grant/grant.controller.ts

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,122 @@ import { Controller, Get, Param, Put, Body, Patch, Post, Delete, ValidationPipe,
22
import { GrantService } from './grant.service';
33
import { Grant } from '../../../middle-layer/types/Grant';
44
import { VerifyUserGuard } from '../guards/auth.guard';
5-
import { ApiBearerAuth } from '@nestjs/swagger';
5+
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiParam, ApiBody, ApiTags } from '@nestjs/swagger';
6+
import { InactivateGrantBody, AddGrantBody, UpdateGrantBody, GrantResponseDto } from './types/grant.types';
67

8+
@ApiTags('grant')
79
@Controller('grant')
810
export class GrantController {
11+
private readonly logger = new Logger(GrantController.name);
12+
913
constructor(private readonly grantService: GrantService) { }
1014

1115
@Get()
1216
@UseGuards(VerifyUserGuard)
1317
@ApiBearerAuth()
14-
async getAllGrants() {
15-
return await this.grantService.getAllGrants();
18+
@ApiOperation({ summary: 'Retrieve all grants', description: 'Returns a list of all grants in the database. Automatically inactivates expired grants.' })
19+
@ApiResponse({ status: 200, description: 'Successfully retrieved all grants', type: [GrantResponseDto] })
20+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
21+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
22+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
23+
async getAllGrants(): Promise<Grant[]> {
24+
this.logger.log('GET /grant - Retrieving all grants');
25+
const grants = await this.grantService.getAllGrants();
26+
this.logger.log(`GET /grant - Successfully retrieved ${grants.length} grants`);
27+
return grants;
1628
}
1729

18-
19-
2030
@Put('inactivate')
2131
@UseGuards(VerifyUserGuard)
32+
@ApiBearerAuth()
33+
@ApiOperation({ summary: 'Inactivate grants', description: 'Marks one or more grants as inactive by their grant IDs' })
34+
@ApiBody({ type: InactivateGrantBody, description: 'Array of grant IDs to inactivate' })
35+
@ApiResponse({ status: 200, description: 'Successfully inactivated grants', type: [GrantResponseDto] })
36+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
37+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
38+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
2239
async inactivate(
23-
@Body('grantIds') grantIds: number[]
40+
@Body() body: InactivateGrantBody
2441
): Promise<Grant[]> {
42+
this.logger.log(`PUT /grant/inactivate - Inactivating ${body.grantIds.length} grant(s)`);
2543
let grants: Grant[] = [];
26-
for(const id of grantIds){
27-
Logger.log(`Inactivating grant with ID: ${id}`);
44+
for(const id of body.grantIds){
45+
this.logger.debug(`Inactivating grant with ID: ${id}`);
2846
let newGrant = await this.grantService.makeGrantsInactive(id)
2947
grants.push(newGrant);
3048
}
49+
this.logger.log(`PUT /grant/inactivate - Successfully inactivated ${grants.length} grant(s)`);
3150
return grants;
3251
}
3352

3453
@Post('new-grant')
3554
@UseGuards(VerifyUserGuard)
55+
@ApiBearerAuth()
56+
@ApiOperation({ summary: 'Create a new grant', description: 'Creates a new grant in the database with a generated grant ID' })
57+
@ApiBody({ type: AddGrantBody, description: 'Grant data to create' })
58+
@ApiResponse({ status: 201, description: 'Successfully created grant', type: Number, example: 1234567890 })
59+
@ApiResponse({ status: 400, description: 'Bad Request - Invalid grant data', example: '{Error encountered}' })
60+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
61+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
62+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
3663
async addGrant(
3764
@Body(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
38-
grant: Grant
39-
) {
40-
return await this.grantService.addGrant(grant);
65+
grant: AddGrantBody
66+
): Promise<number> {
67+
this.logger.log(`POST /grant/new-grant - Creating new grant for organization: ${grant.organization}`);
68+
const grantId = await this.grantService.addGrant(grant as Grant);
69+
this.logger.log(`POST /grant/new-grant - Successfully created grant with ID: ${grantId}`);
70+
return grantId;
4171
}
4272

4373
@Put('save')
4474
@UseGuards(VerifyUserGuard)
45-
async saveGrant(@Body() grantData: Grant) {
46-
return await this.grantService.updateGrant(grantData)
75+
@ApiBearerAuth()
76+
@ApiOperation({ summary: 'Update an existing grant', description: 'Updates an existing grant in the database with new grant data' })
77+
@ApiBody({ type: UpdateGrantBody, description: 'Updated grant data including grantId' })
78+
@ApiResponse({ status: 200, description: 'Successfully updated grant', type: String, example: '{"Attributes": {...}}' })
79+
@ApiResponse({ status: 400, description: 'Bad Request - Invalid grant data', example: '{Error encountered}' })
80+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
81+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
82+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
83+
async saveGrant(@Body() grantData: UpdateGrantBody): Promise<string> {
84+
this.logger.log(`PUT /grant/save - Updating grant with ID: ${grantData.grantId}`);
85+
const result = await this.grantService.updateGrant(grantData as Grant);
86+
this.logger.log(`PUT /grant/save - Successfully updated grant ${grantData.grantId}`);
87+
return result;
4788
}
4889

4990
@Delete(':grantId')
5091
@UseGuards(VerifyUserGuard)
51-
async deleteGrant(@Param('grantId') grantId: number) {
52-
return await this.grantService.deleteGrantById(grantId);
92+
@ApiBearerAuth()
93+
@ApiOperation({ summary: 'Delete a grant', description: 'Deletes a grant from the database by its grant ID' })
94+
@ApiParam({ name: 'grantId', type: Number, description: 'The ID of the grant to delete' })
95+
@ApiResponse({ status: 200, description: 'Successfully deleted grant', type: String, example: 'Grant 1234567890 deleted successfully' })
96+
@ApiResponse({ status: 400, description: 'Bad Request - Grant does not exist', example: '{Error encountered}' })
97+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
98+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
99+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
100+
async deleteGrant(@Param('grantId') grantId: number): Promise<string> {
101+
this.logger.log(`DELETE /grant/${grantId} - Deleting grant`);
102+
const result = await this.grantService.deleteGrantById(grantId);
103+
this.logger.log(`DELETE /grant/${grantId} - Successfully deleted grant`);
104+
return result;
53105
}
106+
54107
@Get(':id')
55108
@UseGuards(VerifyUserGuard)
56-
async getGrantById(@Param('id') GrantId: string) {
57-
return await this.grantService.getGrantById(parseInt(GrantId, 10));
109+
@ApiBearerAuth()
110+
@ApiOperation({ summary: 'Get a grant by ID', description: 'Retrieves a single grant from the database by its grant ID' })
111+
@ApiParam({ name: 'id', type: String, description: 'The ID of the grant to retrieve' })
112+
@ApiResponse({ status: 200, description: 'Successfully retrieved grant', type: GrantResponseDto })
113+
@ApiResponse({ status: 404, description: 'Grant not found', example: '{Error encountered}' })
114+
@ApiResponse({ status: 401, description: 'Unauthorized - Invalid or missing authentication token' })
115+
@ApiResponse({ status: 403, description: 'Forbidden - User does not have access to this resource' })
116+
@ApiResponse({ status: 500, description: 'Internal Server Error', example: 'Internal Server Error' })
117+
async getGrantById(@Param('id') GrantId: string): Promise<Grant> {
118+
this.logger.log(`GET /grant/${GrantId} - Retrieving grant by ID`);
119+
const grant = await this.grantService.getGrantById(parseInt(GrantId, 10));
120+
this.logger.log(`GET /grant/${GrantId} - Successfully retrieved grant`);
121+
return grant;
58122
}
59123
}

0 commit comments

Comments
 (0)