Sfoglia il codice sorgente

removed unwanted post request from recollection history, updated recollect API to handle success and failure of recollection

master
kj1352 4 anni fa
parent
commit
58b0252483
3 ha cambiato i file con 94 aggiunte e 45 eliminazioni
  1. +4
    -2
      src/models/recollection-history.ts
  2. +74
    -10
      src/revision/revision-routes.ts
  3. +16
    -33
      src/user-profile/recollection-history-routes.ts

+ 4
- 2
src/models/recollection-history.ts Vedi File

@@ -1,6 +1,8 @@
export interface RecollectionHistory {
userId: string,
wordId: string,
timeStamp: Date,
wasRecallSuccessful: boolean
history: Array<{
timeStamp: Date,
wasRecallSuccessful: boolean
}>
}

+ 74
- 10
src/revision/revision-routes.ts Vedi File

@@ -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<MongoShelf>('shelves');
const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection<RecollectionHistory>('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");


+ 16
- 33
src/user-profile/recollection-history-routes.ts Vedi File

@@ -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<RecollectionHistory>('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<RecollectionHistory>('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;
}
});

Caricamento…
Annulla
Salva