All files / admin lookup-user.js

100% Statements 21/21
100% Branches 8/8
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                                1x 1x 1x 1x   1x 1x   1x               1x     9x   9x 2x             7x         4x     6x               6x             1x 9x 9x   1x 1x              
/**
 * Search Users (Platform Admin) Lambda
 * GET /v1/platform-admin/users/lookup?search=...
 *
 * Requires `isPlatformAdmin: true`. Free-text search across name, nickname,
 * and email (case-insensitive substring, answered by the trigram directory index —
 * see utils/directorySearchIndex.js, never a table Scan) — the same interaction
 * shape as the flow-member invite search (GET /v1/users, see users/list.js), but
 * platform-wide (not gated by flow membership), also matching email, and including
 * creditsBalance/email per result, which only a platform admin is allowed
 * to see. Used by the "Credits" admin tab to find the user a manual credits
 * adjustment applies to.
 *
 * @implements UC-CREDITS-006.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { USER_NAMESPACES, containsQuery, searchDirectory } = require('../utils/directorySearchIndex')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
// A handful of matches is all an admin picking one specific person ever
// needs — capped rather than paginated, same reasoning as the invite
// search's typical result set size.
const MAX_RESULTS = 20
 
async function lookupUserHandler(event) {
  const search = event.queryStringParameters?.search?.trim()
 
  if (!search) {
    return {
      statusCode: 400,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Missing required query parameter: search' })
    }
  }
 
  const { items } = await searchDirectory(docClient, {
    indexTableName: process.env.FIELD_VALUE_INDEX_TABLE_NAME,
    subjectsTableName: process.env.USERS_TABLE_NAME,
    namespaces: USER_NAMESPACES,
    searchValue: search,
    verify: (user, query) => containsQuery(query, [user.name, user.nickname, user.email]),
    limit: MAX_RESULTS
  })
  const matches = items.map((user) => ({
    id: user.id,
    email: user.email,
    name: user.name || null,
    nickname: user.nickname || null,
    creditsBalance: user.creditsBalance || 0
  }))
 
  return {
    statusCode: 200,
    headers: corsHeaders,
    body: JSON.stringify({ items: matches })
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    return await lookupUserHandler(event)
  } catch (error) {
    console.error('Error searching users:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to search users' })
    }
  }
})