| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import UserUpdater from '../User/UserUpdater.js'
- const POSTPONE_DURATION_MS = 24 * 60 * 60 * 1000 // 1 day
- /**
- * Change the tutorial state
- *
- * @param {string} userId
- * @param {string} tutorialKey
- * @param {'completed' | 'postponed'} state
- * @param {Date} [postponedUntil] - The date until which the tutorial is postponed
- */
- async function setTutorialState(
- userId,
- tutorialKey,
- state,
- postponedUntil = null
- ) {
- const updateData = {
- state,
- updatedAt: new Date(),
- }
- if (state === 'postponed' && postponedUntil) {
- updateData.postponedUntil = postponedUntil
- }
- await UserUpdater.promises.updateUser(userId, {
- $set: {
- [`completedTutorials.${tutorialKey}`]: updateData,
- },
- })
- }
- /**
- * 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 defaultPostponedUntil = new Date(
- record.updatedAt.getTime() + POSTPONE_DURATION_MS
- )
- const postponedUntil = record.postponedUntil ?? defaultPostponedUntil
- if (new Date() < postponedUntil) {
- inactiveTutorials.push(key)
- }
- } else {
- inactiveTutorials.push(key)
- }
- }
- return inactiveTutorials
- }
- export default { setTutorialState, getInactiveTutorials }
|