generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
/
challenges.js
114 lines (103 loc) · 2.99 KB
/
challenges.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* This file contains wrapper functions to interact with the DB.
* This will contain the DB schema if we start consuming an ORM for managing the DB operations
*/
const Firestore = require("@google-cloud/firestore");
const firestore = require("../utils/firestore");
const { fetchUser } = require("./users");
const challengesModel = firestore.collection("challenges");
const userModel = firestore.collection("users");
const CANNOT_SUBSCRIBE = "User cannot be subscribed to challenge";
const USER_DOES_NOT_EXIST_ERROR = "User does not exist. Please register to participate";
const ERROR_MESSAGE = "Error getting challenges";
/**
* Fetch the challenges
* @return {Promise<challengesModel|Array>}
*/
const fetchChallenges = async () => {
try {
const challengesSnapshot = await challengesModel.get();
const challenges = [];
challengesSnapshot.forEach((challengeDoc) => {
challenges.push({
id: challengeDoc.id,
...challengeDoc.data(),
});
});
return challenges;
} catch (err) {
logger.error(ERROR_MESSAGE, err);
throw err;
}
};
/**
* Fetch the <user object> from participants array
* @param {Array} participants
* @returns {Promise<challengesModel|Array>}
*/
const fetchParticipantsData = async (participants) => {
try {
const promises = participants.map(async (userId) => {
const { user } = await fetchUser({ userId });
return {
...user,
phone: undefined,
email: undefined,
};
});
const fetchedparticipants = await Promise.all(promises);
return fetchedparticipants;
} catch (err) {
logger.error("Failed to get participated users", err);
throw err;
}
};
/**
* Post the challenge
* @return {Promise<challengesModel|Array>}
*/
const postChallenge = async (challengeData) => {
try {
const { start_date: startDate, end_date: endDate } = challengeData;
const startdate = new Firestore.Timestamp(startDate, 0);
const enddate = new Firestore.Timestamp(endDate, 0);
const challengeRef = await challengesModel.add({
...challengeData,
start_date: startdate,
end_date: enddate,
participants: [],
is_active: true,
});
return challengeRef.id;
} catch (err) {
logger.error(ERROR_MESSAGE, err);
throw err;
}
};
/**
* @param {String} userId
* @param {String} challengeId
* @return {Promise<challengesModel|Array>}
*/
const subscribeUserToChallenge = async (userId, challengeId) => {
try {
const getUser = await userModel.doc(userId).get();
const user = getUser.data();
if (user) {
const challengeRef = await challengesModel.doc(challengeId);
await challengeRef.update({ participants: Firestore.FieldValue.arrayUnion(userId) });
return challengeRef.get();
} else {
throw new Error(USER_DOES_NOT_EXIST_ERROR);
}
} catch (err) {
logger.error(CANNOT_SUBSCRIBE, err);
throw err;
}
};
module.exports = {
fetchChallenges,
postChallenge,
subscribeUserToChallenge,
fetchParticipantsData,
};