All files / src/utils support-open-tickets.js

100% Statements 8/8
83.33% Branches 5/6
100% Functions 1/1
100% Lines 8/8

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                                                12x 12x         12x 7x           5x 1x     4x     3x  
/**
 * Shaping the "your open requests" payload (SHY-0424).
 *
 * `GET /support-tickets/mine/open` returns at most MAX_OPEN_TICKETS_LISTED
 * summaries — a deliberate cap, because a choice screen listing twenty is
 * unreadable. The client derived its heading from the LENGTH of that list, so
 * somebody with eight open requests was told they had five.
 *
 * The cap is a decision about how many to SHOW. It was being read as a fact
 * about how many EXIST, and the two stopped agreeing the moment somebody went
 * past five.
 *
 * Pure and separate from the route so the rules below can be pinned without
 * standing firebase up.
 */
 
'use strict';
 
/**
 * @param {Array} shown the capped summaries, exactly as they will be displayed
 * @param {number|null|undefined} openCount the server-side count, or absent
 * @returns {{tickets: Array, openCount: number|null, shownCount: number}}
 */
function openTicketsPayload(shown, openCount) {
  const tickets = Array.isArray(shown) ? shown : [];
  const shownCount = tickets.length;
 
  // Absent, rather than guessed. Falling back to the list length would
  // silently reintroduce the exact defect this exists to fix, so the absence
  // is STATED and the client decides what it can honestly say.
  if (!Number.isFinite(openCount)) {
    return { tickets, openCount: null, shownCount };
  }
 
  // Fewer open than are on screen is only reachable if a ticket is resolved
  // between the count and the list. Telling somebody they have fewer requests
  // than they can currently see is worse than saying nothing.
  if (openCount < shownCount) {
    return { tickets, openCount: null, shownCount };
  }
 
  return { tickets, openCount, shownCount };
}
 
module.exports = { openTicketsPayload };