Skip to content

Commit 605fcec

Browse files
authored
Populate participants/challenges (#412)
* will not work if participants array elem > 10 * overcoming in query limitation * storing id insted of name * using fetchUser from users.model for fetching user * a little bit refactoring of code * post-challenges/subscribe response updated * made requested changes * using 204 for response * refactoring response code * getting promise array from a function * refactoring get method in controllers/challenges
1 parent dc9103a commit 605fcec

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

controllers/challenge.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ const sendChallengeResponse = async (req, res) => {
1212
try {
1313
if (req.method === 'GET') {
1414
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-
}
15+
const promiseArray = await getParticipantsofChallenges(allChallenges)
16+
const challengesWithParticipants = await Promise.all(promiseArray)
17+
return res.json({
18+
message: challengesWithParticipants.length ? 'Challenges returned successfully!' : 'No Challenges found',
19+
challenges: challengesWithParticipants
20+
})
2321
} else {
2422
if (req.method === 'POST') {
2523
const challengeAdded = await challengeQuery.postChallenge(req.body)
@@ -40,6 +38,20 @@ const sendChallengeResponse = async (req, res) => {
4038
}
4139
}
4240

41+
/**
42+
* @param {Array} allChallenges
43+
* @returns {Promise<participants|Array>}
44+
*/
45+
const getParticipantsofChallenges = async (allChallenges) => {
46+
return allChallenges.map(async (challenge) => {
47+
const participants = await challengeQuery.fetchParticipantsData(challenge.participants)
48+
return {
49+
...challenge,
50+
participants
51+
}
52+
})
53+
}
54+
4355
/**
4456
* Suscribe user to a challenge
4557
* @param {Object} req - Express request object
@@ -52,7 +64,8 @@ const subscribeToChallenge = async (req, res) => {
5264
const subscribeUser = await challengeQuery.subscribeUserToChallenge(userId, challengeId)
5365
if (subscribeUser) {
5466
return res.status(200).json({
55-
message: 'User has subscribed to challenge'
67+
challenge_id: challengeId,
68+
is_user_subscribed: 1
5669
})
5770
} else {
5871
return res.boom.notFound('User cannot be subscribed to challenge')

models/challenges.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const Firestore = require('@google-cloud/firestore')
77
const firestore = require('../utils/firestore')
8+
const { fetchUser } = require('./users')
89

910
const challengesModel = firestore.collection('challenges')
1011
const userModel = firestore.collection('users')
@@ -35,6 +36,29 @@ const fetchChallenges = async () => {
3536
}
3637
}
3738

39+
/**
40+
* Fetch the <user object> from participants array
41+
* @param {Array} participants
42+
* @returns {Promise<challengesModel|Array>}
43+
*/
44+
const fetchParticipantsData = async (participants) => {
45+
try {
46+
const promises = participants.map(async (userId) => {
47+
const { user } = await fetchUser({ userId })
48+
return {
49+
...user,
50+
phone: undefined,
51+
email: undefined
52+
}
53+
})
54+
const fetchedparticipants = await Promise.all(promises)
55+
return fetchedparticipants
56+
} catch (err) {
57+
logger.error('Failed to get participated users', err)
58+
throw err
59+
}
60+
}
61+
3862
/**
3963
* Post the challenge
4064
* @return {Promise<challengesModel|Array>}
@@ -66,10 +90,10 @@ const postChallenge = async (challengeData) => {
6690
const subscribeUserToChallenge = async (userId, challengeId) => {
6791
try {
6892
const getUser = await userModel.doc(userId).get()
69-
const user = getUser.data().github_display_name
93+
const user = getUser.data()
7094
if (user) {
7195
const challengeRef = await challengesModel.doc(challengeId)
72-
await challengeRef.update({ participants: Firestore.FieldValue.arrayUnion({ name: user }) })
96+
await challengeRef.update({ participants: Firestore.FieldValue.arrayUnion(userId) })
7397
return challengeRef.get()
7498
} else {
7599
throw new Error(USER_DOES_NOT_EXIST_ERROR)
@@ -83,5 +107,6 @@ const subscribeUserToChallenge = async (userId, challengeId) => {
83107
module.exports = {
84108
fetchChallenges,
85109
postChallenge,
86-
subscribeUserToChallenge
110+
subscribeUserToChallenge,
111+
fetchParticipantsData
87112
}

0 commit comments

Comments
 (0)