Skip to content

Commit 327db7f

Browse files
committed
Fix small problems, improve customer route swagger docs
1 parent b14a821 commit 327db7f

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

ApiTesting.http

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ Accept: application/json
3939
### Token provided, get token from login
4040
GET http://localhost:3000/example/with-auth
4141
Accept: application/json
42-
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAdGVzdGVyLmNvbSIsImlhdCI6MTU5ODM0MTA1NiwiZXhwIjoxNTk4MzQ0NjU2fQ.iF7nE7uXZsPBfN_yJFA-CPj7dat7MPWgDEBieqPI2Kw
42+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJUZXN0IFVzZXIiLCJpZCI6MywiZW1haWwiOiJ0ZXN0QHRlc3Rlci5jb20iLCJpYXQiOjE1OTgzNDIzNzksImV4cCI6MTU5ODM0NTk3OX0.mgyA-axI4kd-IW-YbyJK7KqpCmIJLyNF3bLM7Fnu9fo
4343

4444
###

src/api/components/Authentication/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export async function createAccount(req: Request, res: Response, next: NextFunct
5050
console.log('new user created: ' + saved);
5151

5252

53-
const token: string = jwt.sign({email: user.email}, process.env.JWT_SECRET, {
53+
const token: string = jwt.sign({
54+
user_name: saved.name,
55+
id: saved.id,
56+
email: saved.email
57+
}, process.env.JWT_SECRET, {
5458
expiresIn: process.env.JWT_EXPIRE
5559
});
5660

@@ -100,7 +104,11 @@ export async function login(req: Request, res: Response, next: NextFunction): Pr
100104
const isMatched: boolean = await bcrypt.compare(user.password, dbUser.password);
101105
if (isMatched) {
102106

103-
const token: string = jwt.sign({email: user.email}, process.env.JWT_SECRET, {
107+
const token: string = jwt.sign({
108+
user_name: dbUser.name,
109+
id: dbUser.id,
110+
email: dbUser.email
111+
}, process.env.JWT_SECRET, {
104112
expiresIn: process.env.JWT_EXPIRE
105113
});
106114

src/api/routes/CustomerRouter.ts

+117-6
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,133 @@ const router: Router = Router();
88

99
/* Note: these routes already have '/customers' part on them */
1010

11-
// Todo: swagger doc!
11+
/**
12+
* @swagger
13+
* /customers/:
14+
* get:
15+
* description: Get all customers
16+
* tags: ["Customers"]
17+
* security:
18+
* - bearerAuth: []
19+
* requestBody:
20+
* description: retrieves all customers, organization id for filtering is coming from jwt token
21+
* required: true
22+
* content:
23+
* application/json:
24+
* schema:
25+
* $ref: ''
26+
*/
1227
router.get('/', CustomerComponent.getAll); // Retrieves a list of customers
1328

14-
// Todo: swagger doc!
29+
/**
30+
* @swagger
31+
* /customers/{customerId}:
32+
* get:
33+
* description: Get customer with given customer id
34+
* summary: Get a customer by ID
35+
* parameters:
36+
* - in: path
37+
* name: customerId
38+
* schema:
39+
* type: integer
40+
* required: true
41+
* description: Numeric ID of the customer to get
42+
* tags: ["Customers"]
43+
* security:
44+
* - bearerAuth: []
45+
* requestBody:
46+
* description: retrieves customer given as url parameter, organization id for filtering is coming from jwt token
47+
* required: true
48+
* content:
49+
* application/json:
50+
* schema:
51+
* $ref: ''
52+
* responses:
53+
* 201:
54+
* description: return created customer
55+
* content:
56+
* application/json:
57+
* schema:
58+
* oneOf:
59+
* - $ref: ''
60+
* default:
61+
* description: unexpected error
62+
* content:
63+
* application/json:
64+
* schema:
65+
* $ref: ''
66+
*/
1567
router.get('/:customerId', CustomerComponent.getOne); // Retrieves a specific customer
1668

17-
// Todo: swagger doc!
69+
/**
70+
* @swagger
71+
* /customers/:
72+
* post:
73+
* description: Create new customer
74+
* tags: ["Customers"]
75+
* security:
76+
* - bearerAuth: []
77+
* requestBody:
78+
* description: customer creation request body
79+
* required: true
80+
* content:
81+
* application/json:
82+
* schema:
83+
* $ref: ''
84+
* example:
85+
* customer_name: Test Customer
86+
* customer_description: Customer for testing purposes
87+
* responses:
88+
* 201:
89+
* description: return created customer
90+
* content:
91+
* application/json:
92+
* schema:
93+
* oneOf:
94+
* - $ref: ''
95+
* default:
96+
* description: unexpected error
97+
* content:
98+
* application/json:
99+
* schema:
100+
* $ref: ''
101+
*/
18102
router.post('/', CustomerComponent.create); // Create a new customer
19103

20-
// Todo: swagger doc!
104+
// Todo: improve swagger doc!
105+
/**
106+
* @swagger
107+
* /customers/{customerId}:
108+
* put:
109+
* description: Update customer
110+
* tags: ["Customers"]
111+
* security:
112+
* - bearerAuth: []
113+
*/
21114
router.put('/:customerId', CustomerComponent.update); // Updates customer
22115

23-
// Todo: swagger doc!
116+
// Todo: improve swagger doc!
117+
/**
118+
* @swagger
119+
* /customers/{customerId}:
120+
* patch:
121+
* description: Partially update customer
122+
* tags: ["Customers"]
123+
* security:
124+
* - bearerAuth: []
125+
*/
24126
router.patch('/:customerId', CustomerComponent.update); // Partial update for one customer
25127

26-
// Todo: swagger doc!
128+
// Todo: improve swagger doc!
129+
/**
130+
* @swagger
131+
* /customers/{customerId}:
132+
* delete:
133+
* description: Delete customer with customerId
134+
* tags: ["Customers"]
135+
* security:
136+
* - bearerAuth: []
137+
*/
27138
router.delete('/:customerId', CustomerComponent.deleteOne); // Delete customer
28139

29140

0 commit comments

Comments
 (0)