|
@@ -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 shelfCollection = getDatabaseClient().db(DB_NAME).collection<MongoShelf>('shelves'); |
|
|
const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection<RecollectionHistory>('recollection-history'); |
|
|
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.status(400); |
|
|
response.send("Missing params shelfId or wordId"); |
|
|
|
|
|
|
|
|
response.send("Missing params shelfId or wordId or wasRecallSuccessful"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const matchedShelf = await shelfCollection.findOne({ |
|
|
const matchedShelf = await shelfCollection.findOne({ |
|
|
_id: new ObjectId(request.body.shelfId) |
|
|
_id: new ObjectId(request.body.shelfId) |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (matchedShelf) { |
|
|
if (matchedShelf) { |
|
|
const matchedWord = matchedShelf.words.find((word) => { |
|
|
|
|
|
|
|
|
const matchedWordIndex = matchedShelf.words.findIndex((word) => { |
|
|
return word.word === request.body.wordId; |
|
|
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 { |
|
|
} else { |
|
|
response.status(400); |
|
|
response.status(400); |
|
|
response.send("Unmatched Shelf"); |
|
|
response.send("Unmatched Shelf"); |
|
|