@@ -3,43 +3,62 @@ const challengeQuery = require('../models/challenges')
3
3
const ERROR_MESSAGE = 'Something went wrong. Please try again or contact admin'
4
4
5
5
/**
6
- * Get the challenges or add the challenge
6
+ * Get the challenges
7
7
* @param {Object } req - Express request object
8
8
* @param {Object } res - Express response object
9
9
*/
10
10
11
- const sendChallengeResponse = async ( req , res ) => {
11
+ const fetchChallenges = async ( req , res ) => {
12
12
try {
13
- if ( req . method === 'GET' ) {
14
- const allChallenges = await challengeQuery . fetchChallenges ( )
15
- if ( allChallenges . length > 0 ) {
16
- return res . status ( 200 ) . json ( {
17
- message : 'Challenges returned successfully!' ,
18
- challenges : allChallenges
19
- } )
20
- } else {
21
- return res . boom . notFound ( 'No challenges found' )
22
- }
13
+ const allChallenges = await challengeQuery . fetchChallenges ( )
14
+ const promiseArray = await getParticipantsofChallenges ( allChallenges )
15
+ const challengesWithParticipants = await Promise . all ( promiseArray )
16
+ return res . json ( {
17
+ message : challengesWithParticipants . length ? 'Challenges returned successfully!' : 'No Challenges found' ,
18
+ challenges : challengesWithParticipants
19
+ } )
20
+ } catch ( err ) {
21
+ logger . error ( `Error while retrieving challenges ${ err } ` )
22
+ return res . boom . serverUnavailable ( ERROR_MESSAGE )
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Add a challenge
28
+ * @param {Object } req - Express request object
29
+ * @param {Object } res - Express response object
30
+ */
31
+
32
+ const createChallenge = async ( req , res ) => {
33
+ try {
34
+ const challengeAdded = await challengeQuery . postChallenge ( req . body )
35
+ if ( challengeAdded ) {
36
+ return res . json ( {
37
+ message : 'Challenge added successfully'
38
+ } )
23
39
} else {
24
- if ( req . method === 'POST' ) {
25
- const challengeAdded = await challengeQuery . postChallenge ( req . body )
26
- if ( challengeAdded ) {
27
- return res . status ( 200 ) . json ( {
28
- message : 'Challenge added successfully' ,
29
- challenges : challengeAdded
30
- } )
31
- }
32
- } else {
33
- return res . boom . notFound ( 'Unable to add challenge' )
34
- }
40
+ return res . boom . badRequest ( 'Unable to add challenge' )
35
41
}
36
- return ''
37
42
} catch ( err ) {
38
- logger . error ( `Error while retriving challenges ${ err } ` )
43
+ logger . error ( `Error while adding challenge ${ err } ` )
39
44
return res . boom . serverUnavailable ( ERROR_MESSAGE )
40
45
}
41
46
}
42
47
48
+ /**
49
+ * @param {Array } allChallenges
50
+ * @returns {Promise<participants|Array> }
51
+ */
52
+ const getParticipantsofChallenges = async ( allChallenges ) => {
53
+ return allChallenges . map ( async ( challenge ) => {
54
+ const participants = await challengeQuery . fetchParticipantsData ( challenge . participants )
55
+ return {
56
+ ...challenge ,
57
+ participants
58
+ }
59
+ } )
60
+ }
61
+
43
62
/**
44
63
* Suscribe user to a challenge
45
64
* @param {Object } req - Express request object
@@ -52,7 +71,8 @@ const subscribeToChallenge = async (req, res) => {
52
71
const subscribeUser = await challengeQuery . subscribeUserToChallenge ( userId , challengeId )
53
72
if ( subscribeUser ) {
54
73
return res . status ( 200 ) . json ( {
55
- message : 'User has subscribed to challenge'
74
+ challenge_id : challengeId ,
75
+ is_user_subscribed : 1
56
76
} )
57
77
} else {
58
78
return res . boom . notFound ( 'User cannot be subscribed to challenge' )
@@ -64,6 +84,7 @@ const subscribeToChallenge = async (req, res) => {
64
84
}
65
85
66
86
module . exports = {
67
- sendChallengeResponse,
87
+ fetchChallenges,
88
+ createChallenge,
68
89
subscribeToChallenge
69
90
}
0 commit comments