Skip to content

Commit

Permalink
Sprint 4 Release - Backend (#34)
Browse files Browse the repository at this point in the history
In this Sprint, we added new Analytics endpoints and more CRUD
operations for Groups.

---------

Co-authored-by: David Santiago Ortiz Almanza <[email protected]>
Co-authored-by: Gotty <[email protected]>
  • Loading branch information
3 people authored May 26, 2024
1 parent d4c7ca5 commit d2efc01
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions GroupsFriendsEvents/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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, () => {
Expand Down

0 comments on commit d2efc01

Please sign in to comment.