All files / utils newUserItem.js

100% Statements 6/6
100% Branches 8/8
100% Functions 3/3
100% Lines 6/6

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                3x               27x   29x     27x                     27x                           3x  
// Flat starting balance for every non-prod signup — see
// docs/architecture/USAGE_METERING.md "No signup grant yet, and platform
// admins bypass the check entirely": there is no real signup grant yet
// (blocked on the Free-tier decision), so without this, a fresh non-admin
// account — including every e2e test user — cannot create a single
// flow/entity/item. GRANT_SIGNUP_CREDITS is 'false' in prod (see
// auth-stack.ts), where real users still go through the platform-admin/
// Stripe path instead.
const SIGNUP_CREDITS_GRANT = 1000
 
/**
 * Checks whether `email` is in the comma-separated INITIAL_PLATFORM_ADMIN_EMAILS
 * allowlist, granting platform admin on first signup (same pattern as the
 * pre-signup allowlist env vars).
 */
function isInitialPlatformAdmin(email) {
  const allowlist = (process.env.INITIAL_PLATFORM_ADMIN_EMAILS || '')
    .split(',')
    .map((entry) => entry.trim().toLowerCase())
    .filter(Boolean)
 
  return allowlist.includes(email.toLowerCase())
}
 
/**
 * The UsersTable row of a freshly signed-up user. Shared by the PostConfirmation
 * trigger and the e2e account reset (DELETE /v1/users/me?keepAccount=true), so a
 * reset account is in exactly the state a real signup leaves it in.
 *
 * @param {{ id: string, email: string, name?: string }} user
 */
function buildNewUserItem({ id, email, name }) {
  return {
    id,
    email,
    name: name || email.split('@')[0],
    status: 'active',
    createdAt: new Date().toISOString(),
    // Constant partition value for the platformStatsIndex GSI, letting the
    // platform-admin dashboard query "all users by signup date" without a scan.
    gsiPk: 'USER',
    ...(isInitialPlatformAdmin(email) && { isPlatformAdmin: true }),
    ...(process.env.GRANT_SIGNUP_CREDITS === 'true' && { creditsBalance: SIGNUP_CREDITS_GRANT })
  }
}
 
module.exports = { buildNewUserItem, SIGNUP_CREDITS_GRANT }