diff --git a/src/models/recollection-history.ts b/src/models/recollection-history.ts index 90b7615..a695a5d 100644 --- a/src/models/recollection-history.ts +++ b/src/models/recollection-history.ts @@ -1,6 +1,8 @@ export interface RecollectionHistory { userId: string, wordId: string, - timeStamp: Date, - wasRecallSuccessful: boolean + history: Array<{ + timeStamp: Date, + wasRecallSuccessful: boolean + }> } \ No newline at end of file diff --git a/src/revision/revision-routes.ts b/src/revision/revision-routes.ts index 40b2bf5..11480b1 100644 --- a/src/revision/revision-routes.ts +++ b/src/revision/revision-routes.ts @@ -88,33 +88,97 @@ recollectionRoutes.get('/questions/', jwtAuthentication, async (request, respons }); -recollectionRoutes.post('/success/', jwtAuthentication, async (request, response) => { +recollectionRoutes.post('/update/', jwtAuthentication, async (request, response) => { + const user: MongoUser = (request.user as any); const shelfCollection = getDatabaseClient().db(DB_NAME).collection('shelves'); const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection('recollection-history'); - if (!request.body.shelfId || !request.body.wordId) { + if (!request.body.shelfId || !request.body.wordId || + request.body.wasRecallSuccessful === undefined || + request.body.wasRecallSuccessful === null) { response.status(400); - response.send("Missing params shelfId or wordId"); + response.send("Missing params shelfId or wordId or wasRecallSuccessful"); return; } - const matchedShelf = await shelfCollection.findOne({ _id: new ObjectId(request.body.shelfId) }); if (matchedShelf) { - const matchedWord = matchedShelf.words.find((word) => { + const matchedWordIndex = matchedShelf.words.findIndex((word) => { return word.word === request.body.wordId; }); + + if (matchedWordIndex < 0 || matchedWordIndex === undefined) { + console.log(matchedShelf.words); + response.status(400); + response.send("Word not present in the shelf " + request.body.shelfId); + return; + } + + + if (request.body.wasRecallSuccessful === true) { + matchedShelf.words[matchedWordIndex].spaceBetweenRecall *= 2; + let now = new Date(); + matchedShelf.words[matchedWordIndex].nextRevisionDateTime = new Date(now.setDate(now.getDate() + matchedShelf.words[matchedWordIndex].spaceBetweenRecall)); + } else { + matchedShelf.words[matchedWordIndex].spaceBetweenRecall /= 2; + let now = new Date(); + matchedShelf.words[matchedWordIndex].nextRevisionDateTime = new Date(now.setDate(now.getDate() + matchedShelf.words[matchedWordIndex].spaceBetweenRecall)); + } + + const updatedShelf = await shelfCollection.updateOne({ + _id: new ObjectId(request.body.shelfId) + }, { + $set: { + words: matchedShelf.words + } + }); + + const matchedRecollectionHistory: any = await recollectionHistoryCollection.findOne({ + userId: user._id, + wordId: request.body.wordId + }); - matchedWord.spaceBetweenRecall *= 2; - let now = new Date(); - matchedWord.nextRevisionDateTime = new Date(now.setDate(now.getDate() + matchedWord.spaceBetweenRecall)); + try { + if (!matchedRecollectionHistory) { + await recollectionHistoryCollection.insertOne({ + userId: user._id, + wordId: request.body.wordId, + history: [{ + timeStamp: new Date(), + wasRecallSuccessful: request.body.wasRecallSuccessful + }] + }); + } else { + matchedRecollectionHistory.history.push({ + timeStamp: new Date(), + wasRecallSuccessful: request.body.wasRecallSuccessful + }); + + await recollectionHistoryCollection.updateOne({ + _id: new ObjectId((matchedRecollectionHistory._id)) + }, { + $set: { + history: matchedRecollectionHistory.history + } + }); - response.status(200); - response.send(matchedWord); + } + } catch { + response.sendStatus(500); + return; + } + + if (updatedShelf.acknowledged) { + response.sendStatus(200); + return; + } else { + response.sendStatus(500); + return; + } } else { response.status(400); response.send("Unmatched Shelf"); diff --git a/src/user-profile/recollection-history-routes.ts b/src/user-profile/recollection-history-routes.ts index 14e7221..4b4c86e 100644 --- a/src/user-profile/recollection-history-routes.ts +++ b/src/user-profile/recollection-history-routes.ts @@ -3,7 +3,6 @@ import express from 'express'; import passport from 'passport'; import { DB_NAME, getDatabaseClient } from '../db-utils'; import { MongoUser } from '../models/user'; -import { ObjectId } from 'bson'; export const recollectionHistoryRoutes = express.Router(); @@ -11,13 +10,25 @@ export const jwtAuthentication = passport.authenticate('jwt', { session: false } // Get recollection history for the user -recollectionHistoryRoutes.get('', jwtAuthentication, async (request, response) => { +recollectionHistoryRoutes.get('/all/', jwtAuthentication, async (request, response) => { const user: MongoUser = (request.user as any); const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection('recollection-history'); - const recollectionHistoryRecords = await recollectionHistoryCollection.find({ - userId: new ObjectId(user._id) - }).toArray(); + let queryParams: { + userId: string, + wordId?: string + } = { + userId: user._id, + }; + + if (request.body.wordId) { + queryParams = { + userId: user._id, + wordId: request.body.wordId + }; + } + + const recollectionHistoryRecords = await recollectionHistoryCollection.find(queryParams).toArray(); if (recollectionHistoryRecords.length > 0) { response.status(200); @@ -28,32 +39,4 @@ recollectionHistoryRoutes.get('', jwtAuthentication, async (request, response) = } return; -}); - - -// Add user recollection history -recollectionHistoryRoutes.post('', jwtAuthentication, async (request, response) => { - const user: MongoUser = (request.user as any); - const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection('recollection-history'); - - if (!request.body.userId || !request.body.wordId || !request.body.timeStamp || !request.body.wasRecallSuccessful) { - response.status(400); - response.send("Missing attributes"); - return; - } - - const newRecollectionHistoryRecord = await recollectionHistoryCollection.insertOne({ - userId: user._id, - wordId: request.body.wordId, - timeStamp: request.body.timeStamp, - wasRecallSuccessful: request.body.wasRecallSuccessful - }); - - if (newRecollectionHistoryRecord.acknowledged) { - response.sendStatus(200); - return; - } else { - response.sendStatus(500); - return; - } }); \ No newline at end of file