All files / admin list-users.js

100% Statements 18/18
100% Branches 12/12
100% Functions 3/3
100% Lines 18/18

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                          1x 1x 1x 1x 1x 1x   1x   1x           2x                       1x 6x 6x       4x       3x           3x 2x           1x 1x              
/**
 * List Users (Platform Admin) Lambda
 * GET /v1/platform-admin/users?search=&limit=&cursor=
 *
 * Requires `isPlatformAdmin: true`. Platform-wide user list, cursor-paginated, never a
 * table Scan: newest signup first when browsing (`platformStatsIndex`), and through
 * the trigram directory index when `search` is given (case-insensitive substring on
 * name, nickname and email — see utils/directorySearchIndex.js; search results are
 * not date-ordered). Distinct from users/lookup, a capped picker for the Credits tab.
 *
 * @implements UC-ADMIN-006.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { InvalidListParamsError, parseListParams } = require('../utils/platformAdminList')
const { listPlatformDirectory } = require('../utils/platformAdminDirectory')
const { USER_NAMESPACES, containsQuery } = require('../utils/directorySearchIndex')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
function toAdminUser(user) {
  return {
    id: user.id,
    email: user.email,
    name: user.name || null,
    nickname: user.nickname || null,
    status: user.status || 'active',
    isPlatformAdmin: user.isPlatformAdmin === true,
    creditsBalance: user.creditsBalance || 0,
    createdAt: user.createdAt || null
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    const { items, nextCursor, truncated } = await listPlatformDirectory(docClient, {
      subjectsTableName: process.env.USERS_TABLE_NAME,
      gsiPkValue: 'USER',
      namespaces: USER_NAMESPACES,
      verify: (user, query) => containsQuery(query, [user.name, user.nickname, user.email]),
      list: parseListParams(event)
    })
 
    return {
      statusCode: 200,
      headers: corsHeaders,
      body: JSON.stringify({ items: items.map(toAdminUser), nextCursor, truncated })
    }
  } catch (error) {
    if (error instanceof InvalidListParamsError) {
      return {
        statusCode: 400,
        headers: corsHeaders,
        body: JSON.stringify({ error: error.message })
      }
    }
    console.error('Error listing platform users:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to list users' })
    }
  }
})