mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-01 13:21:37 +02:00
Merge pull request #16073 from overleaf/em-postpone-tutorials
Support postponing tutorials GitOrigin-RevId: fe662086c87cc1909d6d9eeac07f85e306d64418
This commit is contained in:
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user