Skip to content

Commit 1e21275

Browse files
swagger api doc
1 parent 563abcc commit 1e21275

File tree

4 files changed

+149
-31
lines changed

4 files changed

+149
-31
lines changed

src/auth/auth.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export class AuthController {
2323
@Public()
2424
@HttpCode(HttpStatus.OK)
2525
@Post('login')
26-
async signIn(@Body() signInDto: Record<string, any>, @Res({ passthrough: true }) res: Response) {
26+
async signIn(@Body() signInDto: Record<string, any>, @Res() res: Response) {
2727
const token = await this.authService.signIn(signInDto.email, signInDto.password);
28+
console.log(token)
2829

2930
res.cookie('access_token',token.access_token, {
3031
httpOnly: true,
@@ -41,7 +42,6 @@ export class AuthController {
4142
sameSite: 'none',
4243
secure: true,
4344
});
44-
4545
return sendResponse(res,HttpStatus.OK,statusMessage[HttpStatus.OK],true,null);
4646
}
4747

src/users/users.controller.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ describe("User Controller", () => {
9090
});
9191

9292
it('should return an array of users', async () => {
93-
expect(controller.findAll()).resolves.toEqual(mockResponse);
93+
let response: MockResponse<Response> = createResponse();
94+
response.json = jest.fn();
95+
expect(controller.findAll(response)).resolves.toEqual(mockResponse);
9496
expect(service.findAll).toHaveBeenCalled();
9597
});
9698
});

src/users/users.controller.ts

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,81 @@
1-
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Res, UseFilters, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
2-
import { Public } from '../auth/decorators/public.decorator';
3-
import { UserService } from './users.service';
4-
import { CreateUserDto } from './dto/create-user.dto';
5-
import { Response } from 'express';
6-
import { sendResponse, userErrorResponse, userSuccessResponse } from '../utils';
7-
import {statusMessage} from '../constant/statusMessage'
8-
import { HttpExceptionFilter } from '../utils/http-exception.filter';
9-
import { responseData, userData } from '../interface/common';
10-
import { AuthGuard } from '../common/guards/at.guard';
11-
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
1+
import {
2+
Body,
3+
Controller,
4+
Delete,
5+
Get,
6+
HttpCode,
7+
HttpStatus,
8+
Param,
9+
Post,
10+
Res,
11+
UseFilters,
12+
UseGuards,
13+
UsePipes,
14+
ValidationPipe,
15+
} from "@nestjs/common";
16+
import { Public } from "../auth/decorators/public.decorator";
17+
import { UserService } from "./users.service";
18+
import { CreateUserDto } from "./dto/create-user.dto";
19+
import { Response } from "express";
20+
import {
21+
sendResponse,
22+
userErrorResponse,
23+
userListSuccessResponse,
24+
userSuccessResponse,
25+
} from "../utils";
26+
import { statusMessage } from "../constant/statusMessage";
27+
import { HttpExceptionFilter } from "../utils/http-exception.filter";
28+
import { responseData, userData } from "../interface/common";
29+
import { AuthGuard } from "../common/guards/at.guard";
30+
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
1231

13-
14-
@ApiTags('users')
32+
@ApiTags("users")
1533
@Controller("v1/users")
1634
export class UserController {
1735
constructor(private readonly userService: UserService) {}
18-
36+
1937
@ApiOperation({
20-
summary: 'Create user',
21-
description: 'User signup app',
38+
summary: "Create user",
39+
description: "User signup app",
2240
})
2341
@ApiResponse(userSuccessResponse)
2442
@ApiResponse(userErrorResponse)
2543
@Public()
2644
@Post()
2745
@UseFilters(new HttpExceptionFilter())
2846
@UsePipes(new ValidationPipe({ transform: true }))
29-
async create(@Body() createCatDto: CreateUserDto, @Res() res: Response):Promise<responseData> {
30-
const user = await this.userService.create(createCatDto);
31-
return sendResponse(res,HttpStatus.CREATED,statusMessage[HttpStatus.CREATED],true,user);
47+
async create(
48+
@Body() createCatDto: CreateUserDto,
49+
@Res() res: Response
50+
): Promise<responseData> {
51+
const user = await this.userService.create(createCatDto);
52+
return sendResponse(
53+
res,
54+
HttpStatus.CREATED,
55+
statusMessage[HttpStatus.CREATED],
56+
true,
57+
user
58+
);
3259
}
33-
34-
// get user
35-
@ApiResponse({ status: 200, description: 'The record has been successfully fetch.'})
36-
@ApiResponse({ status: 403, description: 'Forbidden.'})
60+
61+
// get user
62+
@ApiOperation({
63+
summary: "User List",
64+
description: "Get User List",
65+
})
66+
@ApiResponse(userListSuccessResponse)
67+
@ApiResponse({ status: 403, description: "Forbidden." })
3768
@UseGuards(AuthGuard)
3869
@Get()
3970
@UseFilters(new HttpExceptionFilter())
40-
async findAll(): Promise<userData[]> {
41-
return this.userService.findAll();
71+
async findAll(@Res() res: Response): Promise<userData[]> {
72+
const userList = await this.userService.findAll();
73+
return sendResponse(
74+
res,
75+
HttpStatus.OK,
76+
statusMessage[HttpStatus.OK],
77+
true,
78+
userList
79+
);
4280
}
4381
}
44-

src/utils/index.ts

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const sendResponse = (
2626
};
2727

2828
export const userSuccessResponse = {
29-
status: 200,
29+
status: 201,
3030
description: "Success! Returns the user data.",
3131
content: {
3232
"application/json": {
@@ -62,7 +62,7 @@ export const userSuccessResponse = {
6262

6363
export const userErrorResponse = {
6464
status: 400,
65-
description: "Error : invalid inpur data.",
65+
description: "Error : Invalid input data.",
6666
content: {
6767
"application/json": {
6868
schema: {
@@ -91,6 +91,85 @@ export const userErrorResponse = {
9191
"isSuccess": false,
9292
"data": null
9393

94+
},
95+
},
96+
},
97+
};
98+
99+
100+
export const userListSuccessResponse = {
101+
status: 200,
102+
description: "Success! Returns the user data.",
103+
content: {
104+
"application/json": {
105+
schema: {
106+
type: "object",
107+
properties: {
108+
statusCode: { type: "number", example: 201 },
109+
isSuccess: { type: "boolean", example: true },
110+
message: { type: "string", example: "SUCCESS" },
111+
data: {
112+
type: "array",
113+
example: [
114+
{
115+
createdAt: "2023-12-22T05:44:35.286Z",
116+
updatedAt: "2023-12-22T05:44:35.286Z",
117+
_id: "657845cf5dc54b24021ecf04",
118+
username: "11",
119+
first_name: "test",
120+
last_name: "test",
121+
email: "te@test.com",
122+
email_code: "eee",
123+
password: "rBbjMr00UhkxZAX3C/fR/tt3nwjua",
124+
password_reset_code: "1",
125+
},
126+
],
127+
},
128+
},
129+
},
130+
example: {
131+
statusCode: 200,
132+
isSuccess: true,
133+
message: "SUCCESS",
134+
data: [
135+
{
136+
createdAt: "2023-12-22T05:44:35.286Z",
137+
updatedAt: "2023-12-22T05:44:35.286Z",
138+
_id: "657845cf5dc54b24021ecf04",
139+
username: "11",
140+
first_name: "test",
141+
last_name: "test",
142+
email: "te@test.com",
143+
email_code: "eee",
144+
password: "7rBbjMr00UhkxZAX3C/fR/tt3nwjua",
145+
password_reset_code: "1",
146+
}
147+
]
148+
}
149+
}
150+
}
151+
};
152+
153+
export const loginSuccessResponse = {
154+
status: 200,
155+
description: "Success! Returns the user data.",
156+
content: {
157+
"application/json": {
158+
schema: {
159+
type: "object",
160+
properties: {
161+
statusCode: { type: "number", example: 201 },
162+
isSuccess: { type: "boolean", example: true },
163+
message: { type: "string", example: "SUCCESS" },
164+
data: { type: "object", example: null },
165+
},
166+
},
167+
example: {
168+
statusCode: 200,
169+
isSuccess: true,
170+
message: "SUCCESS",
171+
data:null
172+
94173
},
95174
},
96175
},

0 commit comments

Comments
 (0)