Merge pull request #16073 from overleaf/em-postpone-tutorials

Support postponing tutorials

GitOrigin-RevId: fe662086c87cc1909d6d9eeac07f85e306d64418
This commit is contained in:
Eric Mc Sween
2023-12-05 07:59:55 -05:00
committed by Copybot
parent d30e876999
commit 94b9d1fa48
13 changed files with 152 additions and 55 deletions

View File

@@ -1,13 +1,45 @@
const UserUpdater = require('../User/UserUpdater')
async function saveCompletion(userId, tutorialKey) {
const completionDate = new Date()
const POSTPONE_DURATION_MS = 24 * 60 * 60 * 1000 // 1 day
/**
* Change the tutorial state
*
* @param {string} userId
* @param {string} tutorialKey
* @param {'completed' | 'postponed'} state
*/
async function setTutorialState(userId, tutorialKey, state) {
await UserUpdater.promises.updateUser(userId, {
$set: {
[`completedTutorials.${tutorialKey}`]: completionDate,
[`completedTutorials.${tutorialKey}`]: { state, updatedAt: new Date() },
},
})
}
module.exports = { saveCompletion }
/**
* Returns a list of inactive tutorials for a given user
*
* The user must be loaded with the completedTutorials property.
*/
function getInactiveTutorials(user, tutorialKey) {
const inactiveTutorials = []
for (const [key, record] of Object.entries(user.completedTutorials ?? {})) {
if (record instanceof Date) {
// Legacy format: single date means the tutorial was completed
inactiveTutorials.push(key)
} else if (record.state === 'postponed') {
const postponedUntil = new Date(
record.updatedAt.getTime() + POSTPONE_DURATION_MS
)
if (new Date() < postponedUntil) {
inactiveTutorials.push(key)
}
} else {
inactiveTutorials.push(key)
}
}
return inactiveTutorials
}
module.exports = { setTutorialState, getInactiveTutorials }