diff --git a/src/models/user.ts b/src/models/user.ts index f213c6b..8884e62 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -1,5 +1,4 @@ import { Category } from "./category"; -import { Shelf } from "./shelf"; import { Word } from "./word"; export interface User { @@ -10,12 +9,10 @@ export interface User { isVerified: boolean, otp: number, categories?: Array, - uncategorised?: Shelf, favouriteWords?: Array, } -export interface MongoUser extends Omit { +export interface MongoUser extends Omit { categories?: Array // Category IDs, - uncategorised?: string, // Shelf ID favouriteWords?: Array // Favourite Words ID } \ No newline at end of file diff --git a/src/revision/revision-routes.ts b/src/revision/revision-routes.ts index ffd632a..40b2bf5 100644 --- a/src/revision/revision-routes.ts +++ b/src/revision/revision-routes.ts @@ -1,16 +1,124 @@ import express from 'express'; import passport from 'passport'; -import { MongoShelf } from '../models/shelf'; +import { MongoShelf, MongoShelfWord } from '../models/shelf'; import { DB_NAME, getDatabaseClient } from '../db-utils'; import { MongoCategory } from '../models/category'; +import { MongoUser } from '../models/user'; +import { ObjectId } from 'bson'; +import { RecollectionHistory } from '../models/recollection-history'; export const recollectionRoutes = express.Router(); export const jwtAuthentication = passport.authenticate('jwt', { session: false }); recollectionRoutes.get('/questions/', jwtAuthentication, async (request, response) => { + const user: MongoUser = (request.user as any); + const shelfCollection = getDatabaseClient().db(DB_NAME).collection('shelves'); const categoryCollection = getDatabaseClient().db(DB_NAME).collection('categories'); + if (!user.categories || user.categories.length === 0) { + response.status(400); + response.send("No categories found"); + return; + } + + let categoryIds: Array = user.categories.map(id => new ObjectId(id)); + + const categoryDetails = await categoryCollection.find({ + _id: { $in : categoryIds } + }).toArray(); + + + const shelfIds: Array = []; + + for (let i = 0; i < categoryDetails.length; i += 1) { + if (categoryDetails[i].shelves) { + + for (let j = 0; j < categoryDetails[i].shelves.length; j += 1) { + shelfIds.push(new ObjectId(categoryDetails[i].shelves[j])); + } + } + } + + const shelfDetails: any = await shelfCollection.find({ + _id: { $in : shelfIds } + }).toArray(); + + if (!shelfDetails || shelfDetails.length === 0) { + response.status(400); + response.send("No shelves found"); + return; + } + + interface WordWithShelfId extends MongoShelfWord { + shelfId: string + } + + const allWords: Array = []; + + for (let i = 0; i < shelfDetails.length; i += 1) { + if (shelfDetails[i].words) { + for (let j = 0; j < shelfDetails[i].words.length; j += 1) { + allWords.push({ + ...shelfDetails[i].words[j], + shelfId: shelfDetails[i]._id + }); + } + } + } + + if (allWords.length === 0) { + response.status(400); + response.send("No words found"); + return; + } + + const recollectionWords: Array = []; + allWords.forEach(word => { + return new Date(word.nextRevisionDateTime) < new Date() ? recollectionWords.push(word): null; + }); + + response.status(200); + response.json(recollectionWords); + + + return; +}); + + +recollectionRoutes.post('/success/', jwtAuthentication, async (request, response) => { + const shelfCollection = getDatabaseClient().db(DB_NAME).collection('shelves'); + const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection('recollection-history'); + + + if (!request.body.shelfId || !request.body.wordId) { + response.status(400); + response.send("Missing params shelfId or wordId"); + return; + } + + + const matchedShelf = await shelfCollection.findOne({ + _id: new ObjectId(request.body.shelfId) + }); + + if (matchedShelf) { + const matchedWord = matchedShelf.words.find((word) => { + return word.word === request.body.wordId; + }); + + matchedWord.spaceBetweenRecall *= 2; + let now = new Date(); + matchedWord.nextRevisionDateTime = new Date(now.setDate(now.getDate() + matchedWord.spaceBetweenRecall)); + + response.status(200); + response.send(matchedWord); + } else { + response.status(400); + response.send("Unmatched Shelf"); + } + + return; }); \ No newline at end of file diff --git a/src/user-profile/profile-routes.ts b/src/user-profile/profile-routes.ts index 48652b4..4f0587c 100644 --- a/src/user-profile/profile-routes.ts +++ b/src/user-profile/profile-routes.ts @@ -17,7 +17,6 @@ userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) email: user.email, isVerified: user.isVerified, categories: user.categories, - uncategorised: user.uncategorised, favouriteWords: user.favouriteWords, }); return; diff --git a/src/user-profile/shelf-routes.ts b/src/user-profile/shelf-routes.ts index ea3196b..ef742b6 100644 --- a/src/user-profile/shelf-routes.ts +++ b/src/user-profile/shelf-routes.ts @@ -99,7 +99,7 @@ shelfRoutes.post('/add/', jwtAuthentication, async (request, response) => { shelfRoutes.put('/update/', jwtAuthentication, async (request, response) => { const shelfCollection = getDatabaseClient().db(DB_NAME).collection('shelves'); const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); - const updatedAddedWords: Array = request.body.addedWords ? request.body.addedWords : []; + const updatedAddedWords: Array = request.body.words ? request.body.words : []; const currentShelf = await shelfCollection.findOne({ _id: new ObjectId(request.body._id) @@ -147,7 +147,7 @@ shelfRoutes.put('/update/', jwtAuthentication, async (request, response) => { $set: { name: request.body.name ? request.body.name : currentShelf.name, description: request.body.description ? request.body.description : currentShelf.description, - addedWords: updatedAddedWords, + words: updatedAddedWords, isArchived: request.body.isArchived !== undefined ? request.body.isArchived : currentShelf.isArchived } });