Skip to content

Commit cfa8993

Browse files
Merge pull request #145 from prakashchoudhary07/feature/contributions
GET contributions/:username API
2 parents ad802c1 + 6e24112 commit cfa8993

File tree

7 files changed

+418
-935
lines changed

7 files changed

+418
-935
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const contributionsService = require('../services/contributions')
2+
const { fetchUser } = require('../models/users')
3+
4+
const ERROR_MESSAGE = 'Something went wrong. Please try again or contact admin'
5+
6+
/**
7+
* Get the contributions of the user
8+
* @param {Object} req - Express request object
9+
* @param {Object} res - Express response object
10+
*/
11+
12+
const getUserContributions = async (req, res) => {
13+
try {
14+
const username = req.params.username
15+
const result = await fetchUser({ username: req.params.username })
16+
if (result.userExists) {
17+
const contributions = await contributionsService.getUserContributions(username)
18+
return res.json(contributions)
19+
}
20+
return res.boom.notFound('User doesn\'t exist')
21+
} catch (err) {
22+
logger.error(`Error while retriving contributions ${err}`)
23+
return res.boom.badImplementation(ERROR_MESSAGE)
24+
}
25+
}
26+
27+
module.exports = {
28+
getUserContributions
29+
}

models/tasks.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const firestore = require('../utils/firestore')
22
const tasksModel = firestore.collection('tasks')
3-
3+
const { fetchUser } = require('./users')
44
/**
55
* Adds and Updates tasks
66
*
@@ -64,8 +64,33 @@ const fetchTask = async (taskId) => {
6464
}
6565
}
6666

67+
/**
68+
* Fetch all tasks of a user
69+
*
70+
* @return {Promise<tasks|Array>}
71+
*/
72+
73+
const fetchUserTasks = async (username) => {
74+
try {
75+
const { user } = await fetchUser({ username })
76+
const tasksSnapshot = await tasksModel.where('participants', 'array-contains', user.username).get()
77+
const tasks = []
78+
tasksSnapshot.forEach((task) => {
79+
tasks.push({
80+
id: task.id,
81+
...task.data()
82+
})
83+
})
84+
return tasks
85+
} catch (err) {
86+
logger.error('error getting tasks', err)
87+
throw err
88+
}
89+
}
90+
6791
module.exports = {
6892
updateTask,
6993
fetchTasks,
70-
fetchTask
94+
fetchTask,
95+
fetchUserTasks
7196
}

0 commit comments

Comments
 (0)