@@ -128,6 +128,43 @@ app.get('/group/:id/events', async (req, res) => {
128
128
res . status ( 200 ) . json ( events . filter ( event => event !== null ) ) ;
129
129
} ) ;
130
130
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
+
131
168
app . post ( '/user/:id/events' , async ( req , res ) => {
132
169
const { id } = req . params ;
133
170
const eventData = req . body ;
@@ -152,10 +189,10 @@ app.post('/user/:id/events', async (req, res) => {
152
189
153
190
app . post ( '/analytics/:id/button-tap' , async ( req , res ) => {
154
191
const { id } = req . params ;
155
- const { buttonName, timestamp } = req . body ;
192
+ const { buttonName, timestamp, userType } = req . body ;
156
193
const datasetId = 'unischedule_analytics' ;
157
194
const tableId = 'button_taps' ;
158
- const rows = [ { buttonName, timestamp, userId : id } ] ;
195
+ const rows = [ { buttonName, timestamp, userType , userId : id } ] ;
159
196
try {
160
197
await bigqueryClient . dataset ( datasetId ) . table ( tableId ) . insert ( rows ) ;
161
198
res . status ( 201 ) . send ( 'Button tap recorded' ) ;
@@ -168,10 +205,10 @@ app.post('/analytics/:id/button-tap', async (req, res) => {
168
205
169
206
app . post ( '/analytics/:id/page-view' , async ( req , res ) => {
170
207
const { id } = req . params ;
171
- const { pageName, timestamp } = req . body ;
208
+ const { pageName, timestamp, userType } = req . body ;
172
209
const datasetId = 'unischedule_analytics' ;
173
210
const tableId = 'page_views' ;
174
- const rows = [ { pageName, timestamp, userId : id } ] ;
211
+ const rows = [ { pageName, timestamp, userType , userId : id } ] ;
175
212
try {
176
213
await bigqueryClient . dataset ( datasetId ) . table ( tableId ) . insert ( rows ) ;
177
214
res . status ( 201 ) . send ( 'Page view recorded' ) ;
@@ -184,10 +221,10 @@ app.post('/analytics/:id/page-view', async (req, res) => {
184
221
185
222
app . post ( '/analytics/:id/event' , async ( req , res ) => {
186
223
const { id } = req . params ;
187
- const { eventName, timestamp } = req . body ;
224
+ const { eventName, timestamp, userType } = req . body ;
188
225
const datasetId = 'unischedule_analytics' ;
189
226
const tableId = 'events' ;
190
- const rows = [ { eventName, timestamp, userId : id } ] ;
227
+ const rows = [ { eventName, timestamp, userType , userId : id } ] ;
191
228
try {
192
229
await bigqueryClient . dataset ( datasetId ) . table ( tableId ) . insert ( rows ) ;
193
230
res . status ( 201 ) . send ( 'Event recorded' ) ;
@@ -198,6 +235,22 @@ app.post('/analytics/:id/event', async (req, res) => {
198
235
}
199
236
} ) ;
200
237
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
+
201
254
const port = process . env . PORT || 3000 ;
202
255
203
256
app . listen ( port , ( ) => {
0 commit comments