diff --git a/chessServer/src/api/completeActivity.js b/chessServer/src/api/completeActivity.js new file mode 100644 index 00000000..3a660bbc --- /dev/null +++ b/chessServer/src/api/completeActivity.js @@ -0,0 +1,24 @@ +/** + * Helper function to send API requests to complete activities + * @param {username} - Username for student whose activity is completed + * @param {credentials} - User's cookie + * @param {activityName} - Name of the activity being completed + * + * activityTypes collection contains list of activities + */ +module.exports = completeActivity = async (username, credentials, activityName) => { + const response = await fetch(`${process.env.MIDDLEWARE_URL}/activities/${username}/activity`, { + method: "PUT", + headers: { + 'Content-Type': 'application/json', + 'Authentication' : `Bearer ${credentials}`, + }, + body: JSON.stringify({ + activityName: activityName, + }) + }); + if(!response.ok) { + throw Error('Could not submit completed activity to database'); + } + return response; +} \ No newline at end of file diff --git a/chessServer/src/managers/EventHandlers.js b/chessServer/src/managers/EventHandlers.js index e2d1f19e..9b53638e 100644 --- a/chessServer/src/managers/EventHandlers.js +++ b/chessServer/src/managers/EventHandlers.js @@ -1,4 +1,5 @@ const GameManager = require("./GameManager"); +const completeActivity = require("../api/completeActivity"); const gameManager = new GameManager(); @@ -45,14 +46,12 @@ const registerSocketHandlers = (socket, io) => { socket.on("newPuzzle", (msg) => { try { const parsed = JSON.parse(msg); - console.log('data',parsed, msg); // create the new puzzle gameManager.createOrJoinPuzzle({ student: parsed.student, mentor: parsed.mentor, role: parsed.role, socketId: socket.id, - credentials: parsed.credentials, }, io); } catch (err) { @@ -82,22 +81,12 @@ const registerSocketHandlers = (socket, io) => { }; console.log('Payload', payload); const studentSocket = io.sockets.sockets.get(studentId); - //console.log('student socket', studentSocket); if (studentSocket) { try { - console.log('route:', `${process.env.MIDDLEWARE_URL}/activities/${username}/activity`); - const response = await fetch(`${process.env.MIDDLEWARE_URL}/activities/${username}/activity`, { - method: "PUT", - headers: { - 'Content-Type': 'application/json', - 'Authentication' : `Bearer ${credentials}`, - }, - body: JSON.stringify({ - activityName: payload.activities[0].name, - }) - }); + const activityName = payload.activities[0].name; + const response = await completeActivity(username, credentials, activityName); console.log('response',response); - socket.emit("completeActivity"); + //socket.emit("completeActivity"); } catch (e) { console.log('Error: ', e); } diff --git a/middlewareNode/src/routes/activities.js b/middlewareNode/src/routes/activities.js index 9310a029..4ab1c395 100644 --- a/middlewareNode/src/routes/activities.js +++ b/middlewareNode/src/routes/activities.js @@ -108,12 +108,31 @@ router.put("/:username/activity", async (req, res) => { { activities: {$elemMatch: { name: activityName }}, _id:0}, ); if(activityIncomplete) { - console.log('incomplete activity: ', activityName); + console.log(username, 'submitted activity:', activityName, new Date()); + await activities.updateOne( + { userId, "activities.name": activityName }, + { $set: { "activities.$.completed": true } } + ); + const completionCheck = await activities.findOne( + { userId }, + { activities: {$elemMatch: { name: activityName }}, _id:0}, + ); + console.log('checking completion',completionCheck.activities); + let activitiesCompleted = true; + for(activity of completionCheck.activities) { + if(!activity.completed) { + activitiesCompleted = false; + } + } + if(activitiesCompleted) { + console.log(username, 'completed all activities', new Date()); + await activities.updateOne( + { userId }, + { $push: { lastCompleted: new Date() } } + ); + } } - await activities.updateOne( - { userId, "activities.name": activityName }, - { $set: { "activities.$.completed": true } } - ); + return res.status(200).json({message:'success'}); } catch (err) { console.error('Error updating activities: ', err); @@ -122,4 +141,4 @@ router.put("/:username/activity", async (req, res) => { }) -module.exports = router; \ No newline at end of file +module.exports = router;