File tree Expand file tree Collapse file tree 6 files changed +115
-3
lines changed Expand file tree Collapse file tree 6 files changed +115
-3
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,20 @@ const getBadges = async (req, res) => {
20
20
}
21
21
}
22
22
23
+ const getUserBadges = async ( req , res ) => {
24
+ try {
25
+ const allUserBadges = await badgeQuery . fetchUserBadges ( req . params . username )
26
+ return res . json ( {
27
+ message : 'User badges returned successfully!' ,
28
+ userBadges : allUserBadges
29
+ } )
30
+ } catch ( error ) {
31
+ logger . error ( `Error while fetching all user badges: ${ error } ` )
32
+ return res . boom . serverUnavailable ( 'Something went wrong please contact admin' )
33
+ }
34
+ }
35
+
23
36
module . exports = {
24
- getBadges
37
+ getBadges,
38
+ getUserBadges
25
39
}
Original file line number Diff line number Diff line change @@ -482,6 +482,17 @@ const swaggerOptions = {
482
482
}
483
483
}
484
484
} ,
485
+ userBadges : {
486
+ type : 'object' ,
487
+ properties : {
488
+ title : {
489
+ type : 'string'
490
+ } ,
491
+ description : {
492
+ type : 'string'
493
+ }
494
+ }
495
+ } ,
485
496
userAvailable : {
486
497
type : 'object' ,
487
498
properties : {
Original file line number Diff line number Diff line change @@ -27,6 +27,35 @@ const fetchBadges = async ({
27
27
}
28
28
}
29
29
30
+ /**
31
+ * Fetches the data about user badges
32
+ * @param query { Object }: Filter for badges data
33
+ * @return {Promise<userBadgeModel|Array> }
34
+ */
35
+
36
+ const fetchUserBadges = async ( username ) => {
37
+ try {
38
+ const snapshot = await badgeModel . get ( )
39
+ const allBadges = [ ]
40
+ snapshot . forEach ( ( doc ) => {
41
+ allBadges . push ( doc . data ( ) )
42
+ } )
43
+ const userBadges = [ ]
44
+ allBadges . forEach ( ( badge ) => {
45
+ badge . users . forEach ( ( user ) => {
46
+ if ( user === username ) {
47
+ userBadges . push ( { title : badge . title , description : badge . description } )
48
+ }
49
+ } )
50
+ } )
51
+ return userBadges
52
+ } catch ( err ) {
53
+ logger . error ( 'Error retrieving user badges' , err )
54
+ return err
55
+ }
56
+ }
57
+
30
58
module . exports = {
31
- fetchBadges
59
+ fetchBadges,
60
+ fetchUserBadges
32
61
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -34,4 +34,37 @@ const badge = require('../controllers/badge.js')
34
34
*/
35
35
router . get ( '/' , badge . getBadges )
36
36
37
+ /**
38
+ * @swagger
39
+ * /badges/:username:
40
+ * get:
41
+ * summary: Get all the badges of a particular user.
42
+ *
43
+ * tags:
44
+ * - Badges
45
+ * responses:
46
+ * 200:
47
+ * description: User badegs
48
+ * content:
49
+ * application/json:
50
+ * schema:
51
+ * type: object
52
+ * properties:
53
+ * message:
54
+ * type: string
55
+ * example: User badges returned successfully!
56
+ * userBadges:
57
+ * type: array
58
+ * items:
59
+ * $ref: '#/components/schemas/userBadges'
60
+ * 503:
61
+ * description: serverUnavailable
62
+ * content:
63
+ * application/json:
64
+ * schema:
65
+ * $ref: '#/components/schemas/errors/serverUnavailable'
66
+ */
67
+
68
+ router . get ( '/:username' , badge . getUserBadges )
69
+
37
70
module . exports = router
Original file line number Diff line number Diff line change
1
+ const chai = require ( 'chai' )
2
+ const { expect } = chai
3
+ const chaiHttp = require ( 'chai-http' )
4
+
5
+ const app = require ( '../../server' )
6
+
7
+ chai . use ( chaiHttp )
8
+
9
+ describe ( 'User badges' , function ( ) {
10
+ describe ( 'GET /badges/:username' , function ( ) {
11
+ it ( 'Should get the list of user badges' , function ( done ) {
12
+ chai
13
+ . request ( app )
14
+ . get ( '/badges/ankush' )
15
+ . end ( ( err , res ) => {
16
+ if ( err ) { return done ( ) }
17
+ expect ( res ) . to . have . status ( 200 )
18
+ expect ( res . body ) . to . be . a ( 'object' )
19
+ expect ( res . body . message ) . to . equal ( 'User badges returned successfully!' )
20
+ expect ( res . body . userBadges ) . to . be . a ( 'array' )
21
+
22
+ return done ( )
23
+ } )
24
+ } )
25
+ } )
26
+ } )
You can’t perform that action at this time.
0 commit comments