All files / utils walkthrough.js

100% Statements 21/21
100% Branches 34/34
100% Functions 4/4
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                          3x 3x 3x   3x                         23x 23x 5x   7x                         14x 3x     11x   11x         8x           6x         5x         1x     4x 1x     3x                 3x            
/**
 * Product walkthrough (onboarding tour) state, stored on the user row as the
 * `walkthrough` attribute and exposed through GET/PUT /v1/users/preferences.
 *
 * Kept server-side (not in localStorage) so a help panel a user has already
 * seen stays hidden on every device and browser they sign in from.
 *
 * Shape:
 * - seenSteps:    ids of the help panels the user has already seen; a seen
 *                 panel is never shown again automatically
 * - snoozedUntil: ISO timestamp before which no panel is shown ("later");
 *                 null when not snoozed
 * - dismissed:    true when the user chose "don't show again"; only a reset
 *                 from the profile page brings the walkthrough back
 *
 * The client always sends the whole object (replace semantics): the state is
 * tiny, and the worst a race between two tabs can do is show one panel twice.
 */
 
// Bounds keep a hostile client from bloating the user row (400 KB item limit):
// the app has a few dozen panels, far below these.
const MAX_SEEN_STEPS = 200
const MAX_STEP_ID_LENGTH = 100
const STEP_ID_PATTERN = /^[a-zA-Z0-9._-]+$/
 
const DEFAULT_WALKTHROUGH = Object.freeze({
  seenSteps: [],
  snoozedUntil: null,
  dismissed: false
})
 
/**
 * The walkthrough state of a user row, with defaults for a user who never
 * interacted with it (or whose stored value is malformed).
 * @param {object|undefined} user - DynamoDB user row
 * @returns {{seenSteps: string[], snoozedUntil: string|null, dismissed: boolean}}
 */
function getWalkthrough(user) {
  const stored = user?.walkthrough
  if (!stored || typeof stored !== 'object') return { ...DEFAULT_WALKTHROUGH, seenSteps: [] }
  return {
    seenSteps: Array.isArray(stored.seenSteps)
      ? stored.seenSteps.filter((id) => typeof id === 'string')
      : [],
    snoozedUntil: typeof stored.snoozedUntil === 'string' ? stored.snoozedUntil : null,
    dismissed: stored.dismissed === true
  }
}
 
/**
 * Validates a walkthrough state sent by the client.
 * @param {unknown} value - request body's `walkthrough`
 * @returns {{error: string} | {value: {seenSteps: string[], snoozedUntil: string|null, dismissed: boolean}}}
 */
function validateWalkthrough(value) {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    return { error: 'Invalid walkthrough - must be an object' }
  }
 
  const { seenSteps, snoozedUntil, dismissed } = value
 
  if (
    !Array.isArray(seenSteps) ||
    seenSteps.length > MAX_SEEN_STEPS ||
    !seenSteps.every(
      (id) =>
        typeof id === 'string' &&
        id.length > 0 &&
        id.length <= MAX_STEP_ID_LENGTH &&
        STEP_ID_PATTERN.test(id)
    )
  ) {
    return {
      error: `Invalid walkthrough.seenSteps - must be an array of at most ${MAX_SEEN_STEPS} step ids`
    }
  }
 
  if (
    snoozedUntil !== null &&
    snoozedUntil !== undefined &&
    (typeof snoozedUntil !== 'string' || Number.isNaN(Date.parse(snoozedUntil)))
  ) {
    return { error: 'Invalid walkthrough.snoozedUntil - must be an ISO date or null' }
  }
 
  if (dismissed !== undefined && typeof dismissed !== 'boolean') {
    return { error: 'Invalid walkthrough.dismissed - must be a boolean' }
  }
 
  return {
    value: {
      seenSteps: [...new Set(seenSteps)],
      snoozedUntil: snoozedUntil ? new Date(snoozedUntil).toISOString() : null,
      dismissed: dismissed === true
    }
  }
}
 
module.exports = {
  DEFAULT_WALKTHROUGH,
  MAX_SEEN_STEPS,
  getWalkthrough,
  validateWalkthrough
}