From d2efc01b4fc25198fa15bb41ee0a8abf9efb9568 Mon Sep 17 00:00:00 2001 From: Sebastian Urrea <68788933+jsurrea@users.noreply.github.com> Date: Sun, 26 May 2024 15:03:38 -0500 Subject: [PATCH] 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> --- GroupsFriendsEvents/src/index.js | 65 +++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/GroupsFriendsEvents/src/index.js b/GroupsFriendsEvents/src/index.js index 9d66535..db3a924 100644 --- a/GroupsFriendsEvents/src/index.js +++ b/GroupsFriendsEvents/src/index.js @@ -128,6 +128,43 @@ app.get('/group/:id/events', async (req, res) => { res.status(200).json(events.filter(event => event !== null)); }); + +app.post('/groups', async (req, res) => { + const { name, members, groupPicture, color, icon } = req.body; + + const groupRef = firestore.collection('Groups').doc(); + const emojis = ['😊', '👍', '🌟', '🎉', '🔥', '💻', '🎨', '📚', '⚽', '🎵']; + const randomEmoji = icon || emojis[Math.floor(Math.random() * emojis.length)]; + + try { + await groupRef.set({ + name: name || 'Nuevo Grupo', + groupPicture: groupPicture || 'https://picsum.photos/1080', + color: color || '#FFFFFF', + icon: randomEmoji, + members: members, + events: [] + }); + + const memberUpdates = members.map(memberId => { + const userRef = firestore.collection('Users').doc(memberId); + return userRef.update({ + groups: admin.firestore.FieldValue.arrayUnion(groupRef.id) + }); + }); + + await Promise.all(memberUpdates); + + res.status(201).json({ + message: 'Group created successfully', + groupId: groupRef.id + }); + } catch (error) { + console.error('Error creating group:', error); + res.status(500).send('Failed to create group'); + } +}); + app.post('/user/:id/events', async (req, res) => { const { id } = req.params; const eventData = req.body; @@ -152,10 +189,10 @@ app.post('/user/:id/events', async (req, res) => { app.post('/analytics/:id/button-tap', async (req, res) => { const { id } = req.params; - const { buttonName, timestamp } = req.body; + const { buttonName, timestamp, userType } = req.body; const datasetId = 'unischedule_analytics'; const tableId = 'button_taps'; - const rows = [{ buttonName, timestamp, userId: id }]; + const rows = [{ buttonName, timestamp, userType, userId: id }]; try { await bigqueryClient.dataset(datasetId).table(tableId).insert(rows); res.status(201).send('Button tap recorded'); @@ -168,10 +205,10 @@ app.post('/analytics/:id/button-tap', async (req, res) => { app.post('/analytics/:id/page-view', async (req, res) => { const { id } = req.params; - const { pageName, timestamp } = req.body; + const { pageName, timestamp, userType } = req.body; const datasetId = 'unischedule_analytics'; const tableId = 'page_views'; - const rows = [{ pageName, timestamp, userId: id }]; + const rows = [{ pageName, timestamp, userType, userId: id }]; try { await bigqueryClient.dataset(datasetId).table(tableId).insert(rows); res.status(201).send('Page view recorded'); @@ -184,10 +221,10 @@ app.post('/analytics/:id/page-view', async (req, res) => { app.post('/analytics/:id/event', async (req, res) => { const { id } = req.params; - const { eventName, timestamp } = req.body; + const { eventName, timestamp, userType } = req.body; const datasetId = 'unischedule_analytics'; const tableId = 'events'; - const rows = [{ eventName, timestamp, userId: id }]; + const rows = [{ eventName, timestamp, userType, userId: id }]; try { await bigqueryClient.dataset(datasetId).table(tableId).insert(rows); res.status(201).send('Event recorded'); @@ -198,6 +235,22 @@ app.post('/analytics/:id/event', async (req, res) => { } }); +app.post('/analytics/:id/rating', async (req, res) => { + const { id } = req.params; + const { rating, classroom, timestamp, userType } = req.body; + const datasetId = 'unischedule_analytics'; + const tableId = 'ratings'; + const rows = [{ rating, classroom, timestamp, userType, userId: id }]; + try { + await bigqueryClient.dataset(datasetId).table(tableId).insert(rows); + res.status(201).send('Rating recorded'); + } + catch (error) { + console.error('BigQuery error:', error); + res.status(500).send('An error occurred while recording the rating'); + } +}); + const port = process.env.PORT || 3000; app.listen(port, () => {