Skip to content

Commit 15e4f47

Browse files
Merge pull request #116 from Real-Dev-Squad/refactor-challenges-routes
refactored `/challenges` routes and functions
2 parents 0ff8473 + 05201a3 commit 15e4f47

File tree

10 files changed

+225
-254
lines changed

10 files changed

+225
-254
lines changed

controllers/roadmap-site/challengeController.js renamed to controllers/challengeController.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const challengeQuery = require('../../models/roadmap-site/challenges')
2-
const subscribeToChallengeQuery = require('../../models/roadmap-site/subscribe-challenge')
1+
const challengeQuery = require('../models/challenges')
2+
3+
const ERROR_MESSAGE = 'Something went wrong. Please try again or contact admin'
34

4-
const errorAdminString = 'Something went wrong, please try again/contact admin'
55
/**
66
* Get the challenges or add the challenge
77
* @param {Object} req - Express request object
88
* @param {Object} res - Express response object
99
*/
10+
1011
const sendChallengeResponse = async (req, res) => {
1112
try {
1213
if (req.method === 'GET') {
@@ -29,34 +30,36 @@ const sendChallengeResponse = async (req, res) => {
2930
})
3031
}
3132
} else {
32-
return res.boom.notFound('Not able to add challenge')
33+
return res.boom.notFound('Unable to add challenge')
3334
}
3435
}
36+
return ''
3537
} catch (err) {
3638
logger.error(`Error while retriving challenges ${err}`)
37-
return res.boom.serverUnavailable(errorAdminString)
39+
return res.boom.serverUnavailable(ERROR_MESSAGE)
3840
}
39-
return ''
4041
}
42+
4143
/**
4244
* Suscribe user to a challenge
4345
* @param {Object} req - Express request object
4446
* @param {Object} res - Express response object
4547
*/
48+
4649
const subscribeToChallenge = async (req, res) => {
4750
try {
4851
const { user_id: userId, challenge_id: challengeId } = req.body
49-
const subscribeUser = await subscribeToChallengeQuery.subscribeUserToChallenge(userId, challengeId)
52+
const subscribeUser = await challengeQuery.subscribeUserToChallenge(userId, challengeId)
5053
if (subscribeUser) {
5154
return res.status(200).json({
52-
message: 'user has suscribed to challenge'
55+
message: 'User has subscribed to challenge'
5356
})
5457
} else {
55-
return res.boom.notFound('user cannot be suscribed to challenge')
58+
return res.boom.notFound('User cannot be subscribed to challenge')
5659
}
5760
} catch (err) {
58-
logger.error(`Error while retriving challenges ${err}`)
59-
return res.boom.serverUnavailable(errorAdminString)
61+
logger.error(`Error while retrieving challenges ${err}`)
62+
return res.boom.serverUnavailable(ERROR_MESSAGE)
6063
}
6164
}
6265

models/challenges.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* This file contains wrapper functions to interact with the DB.
3+
* This will contain the DB schema if we start consuming an ORM for managing the DB operations
4+
*/
5+
6+
const Firestore = require('@google-cloud/firestore')
7+
const firestore = require('../utils/firestore')
8+
9+
const challengesModel = firestore.collection('challenges')
10+
const userModel = firestore.collection('users')
11+
12+
const CANNOT_SUBSCRIBE = 'User cannot be subscribed to challenge'
13+
const USER_DOES_NOT_EXIST_ERROR = 'User does not exist. Please register to participate'
14+
const ERROR_MESSAGE = 'Error getting challenges'
15+
16+
/**
17+
* Fetch the challenges
18+
* @return {Promise<challengesModel|Array>}
19+
*/
20+
21+
const fetchChallenges = async () => {
22+
try {
23+
const challengesSnapshot = await challengesModel.get()
24+
const challenges = []
25+
challengesSnapshot.forEach((challengeDoc) => {
26+
challenges.push({
27+
id: challengeDoc.id,
28+
...challengeDoc.data()
29+
})
30+
})
31+
return challenges
32+
} catch (err) {
33+
logger.error(ERROR_MESSAGE, err)
34+
throw err
35+
}
36+
}
37+
38+
/**
39+
* Post the challenge
40+
* @return {Promise<challengesModel|Array>}
41+
*/
42+
43+
const postChallenge = async (challengeData) => {
44+
try {
45+
const response = await challengesModel.add({
46+
...challengeData,
47+
participants: [],
48+
is_active: true
49+
})
50+
const allChallenges = await fetchChallenges()
51+
if (response.id && allChallenges.length > 0) {
52+
return allChallenges
53+
} else return ''
54+
} catch (err) {
55+
logger.error(ERROR_MESSAGE, err)
56+
throw err
57+
}
58+
}
59+
60+
/**
61+
* @param {String} userId
62+
* @param {String} challengeId
63+
* @return {Promise<challengesModel|Array>}
64+
*/
65+
66+
const subscribeUserToChallenge = async (userId, challengeId) => {
67+
try {
68+
const getUser = await userModel.doc(userId).get()
69+
const user = getUser.data().github_display_name
70+
if (user) {
71+
const challengeRef = await challengesModel.doc(challengeId)
72+
await challengeRef.update({ participants: Firestore.FieldValue.arrayUnion({ name: user }) })
73+
return challengeRef.get()
74+
} else {
75+
throw new Error(USER_DOES_NOT_EXIST_ERROR)
76+
}
77+
} catch (err) {
78+
logger.error(CANNOT_SUBSCRIBE, err)
79+
throw err
80+
}
81+
}
82+
83+
module.exports = {
84+
fetchChallenges,
85+
postChallenge,
86+
subscribeUserToChallenge
87+
}

models/roadmap-site/challenges.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

models/roadmap-site/subscribe-challenge.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)