Skip to content

Commit d2efc01

Browse files
jsurreaSnowArtziGotty
authored
Sprint 4 Release - Backend (#34)
In this Sprint, we added new Analytics endpoints and more CRUD operations for Groups. --------- Co-authored-by: David Santiago Ortiz Almanza <69651671+SnowArtz@users.noreply.github.com> Co-authored-by: Gotty <78111224+iGotty@users.noreply.github.com>
1 parent d4c7ca5 commit d2efc01

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

GroupsFriendsEvents/src/index.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,43 @@ app.get('/group/:id/events', async (req, res) => {
128128
res.status(200).json(events.filter(event => event !== null));
129129
});
130130

131+
132+
app.post('/groups', async (req, res) => {
133+
const { name, members, groupPicture, color, icon } = req.body;
134+
135+
const groupRef = firestore.collection('Groups').doc();
136+
const emojis = ['😊', '👍', '🌟', '🎉', '🔥', '💻', '🎨', '📚', '⚽', '🎵'];
137+
const randomEmoji = icon || emojis[Math.floor(Math.random() * emojis.length)];
138+
139+
try {
140+
await groupRef.set({
141+
name: name || 'Nuevo Grupo',
142+
groupPicture: groupPicture || 'https://picsum.photos/1080',
143+
color: color || '#FFFFFF',
144+
icon: randomEmoji,
145+
members: members,
146+
events: []
147+
});
148+
149+
const memberUpdates = members.map(memberId => {
150+
const userRef = firestore.collection('Users').doc(memberId);
151+
return userRef.update({
152+
groups: admin.firestore.FieldValue.arrayUnion(groupRef.id)
153+
});
154+
});
155+
156+
await Promise.all(memberUpdates);
157+
158+
res.status(201).json({
159+
message: 'Group created successfully',
160+
groupId: groupRef.id
161+
});
162+
} catch (error) {
163+
console.error('Error creating group:', error);
164+
res.status(500).send('Failed to create group');
165+
}
166+
});
167+
131168
app.post('/user/:id/events', async (req, res) => {
132169
const { id } = req.params;
133170
const eventData = req.body;
@@ -152,10 +189,10 @@ app.post('/user/:id/events', async (req, res) => {
152189

153190
app.post('/analytics/:id/button-tap', async (req, res) => {
154191
const { id } = req.params;
155-
const { buttonName, timestamp } = req.body;
192+
const { buttonName, timestamp, userType } = req.body;
156193
const datasetId = 'unischedule_analytics';
157194
const tableId = 'button_taps';
158-
const rows = [{ buttonName, timestamp, userId: id }];
195+
const rows = [{ buttonName, timestamp, userType, userId: id }];
159196
try {
160197
await bigqueryClient.dataset(datasetId).table(tableId).insert(rows);
161198
res.status(201).send('Button tap recorded');
@@ -168,10 +205,10 @@ app.post('/analytics/:id/button-tap', async (req, res) => {
168205

169206
app.post('/analytics/:id/page-view', async (req, res) => {
170207
const { id } = req.params;
171-
const { pageName, timestamp } = req.body;
208+
const { pageName, timestamp, userType } = req.body;
172209
const datasetId = 'unischedule_analytics';
173210
const tableId = 'page_views';
174-
const rows = [{ pageName, timestamp, userId: id }];
211+
const rows = [{ pageName, timestamp, userType, userId: id }];
175212
try {
176213
await bigqueryClient.dataset(datasetId).table(tableId).insert(rows);
177214
res.status(201).send('Page view recorded');
@@ -184,10 +221,10 @@ app.post('/analytics/:id/page-view', async (req, res) => {
184221

185222
app.post('/analytics/:id/event', async (req, res) => {
186223
const { id } = req.params;
187-
const { eventName, timestamp } = req.body;
224+
const { eventName, timestamp, userType } = req.body;
188225
const datasetId = 'unischedule_analytics';
189226
const tableId = 'events';
190-
const rows = [{ eventName, timestamp, userId: id }];
227+
const rows = [{ eventName, timestamp, userType, userId: id }];
191228
try {
192229
await bigqueryClient.dataset(datasetId).table(tableId).insert(rows);
193230
res.status(201).send('Event recorded');
@@ -198,6 +235,22 @@ app.post('/analytics/:id/event', async (req, res) => {
198235
}
199236
});
200237

238+
app.post('/analytics/:id/rating', async (req, res) => {
239+
const { id } = req.params;
240+
const { rating, classroom, timestamp, userType } = req.body;
241+
const datasetId = 'unischedule_analytics';
242+
const tableId = 'ratings';
243+
const rows = [{ rating, classroom, timestamp, userType, userId: id }];
244+
try {
245+
await bigqueryClient.dataset(datasetId).table(tableId).insert(rows);
246+
res.status(201).send('Rating recorded');
247+
}
248+
catch (error) {
249+
console.error('BigQuery error:', error);
250+
res.status(500).send('An error occurred while recording the rating');
251+
}
252+
});
253+
201254
const port = process.env.PORT || 3000;
202255

203256
app.listen(port, () => {

0 commit comments

Comments
 (0)