All files / admin list-appeals.js

100% Statements 23/23
91.66% Branches 11/12
100% Functions 4/4
100% Lines 23/23

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 106 107 108                              1x 1x 1x 1x 1x             1x   1x   1x         1x     2x                                       1x 9x 9x         4x                     3x   3x   2x     3x       2x         6x 5x           1x 1x              
/**
 * List Appeals (Platform Admin) Lambda
 * GET /v1/platform-admin/appeals?status=&limit=&cursor=
 *
 * Requires `isPlatformAdmin: true`. The appeals users filed against a
 * moderation decision (content-reports/appeal.js): the open ones
 * (`status=open`, the default) or the decided ones (`status=closed`, with the
 * admin's answer), newest first, cursor-paginated through the `listIndex` GSI
 * of ModerationAppealsTable — never a Scan. Each row carries the decision
 * contested and the content as reported, with the appellant's and the
 * reported user's name and email, and the reported user's standing.
 *
 * @implements UC-REPORT-010.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { batchGetByIds } = require('../utils/platformAdminList')
const { APPEAL_LIST_KEY, APPEAL_CLOSED_LIST_KEY } = require('../utils/contentReports')
const {
  InvalidParamsError,
  encodeCursor,
  parseListParams,
  toUserSummary,
  toStanding
} = require('../utils/adminReportViews')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
const LIST_KEYS = { open: APPEAL_LIST_KEY, closed: APPEAL_CLOSED_LIST_KEY }
 
function toAdminAppeal(appeal, users) {
  return {
    id: appeal.id,
    decisionId: appeal.decisionId,
    appellant: toUserSummary(appeal.appellantId, users),
    role: appeal.role,
    message: appeal.message,
    targetKey: appeal.targetKey,
    targetType: appeal.targetType,
    target: appeal.target || {},
    reason: appeal.reason,
    snapshot: appeal.snapshot || {},
    reportedUser: toUserSummary(appeal.reportedUserId, users),
    reportedUserStanding: toStanding(appeal.reportedUserId, users),
    decision: appeal.decision,
    status: appeal.status,
    appealDecision: appeal.appealDecision || null,
    createdAt: appeal.createdAt
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    const { listKey, limit, cursor } = parseListParams(event, LIST_KEYS, [
      'decisionId',
      'appellantId'
    ])
 
    const page = await docClient.send(
      new QueryCommand({
        TableName: process.env.MODERATION_APPEALS_TABLE_NAME,
        IndexName: 'listIndex',
        KeyConditionExpression: 'listKey = :listKey',
        ExpressionAttributeValues: { ':listKey': listKey },
        ScanIndexForward: false,
        Limit: limit,
        ExclusiveStartKey: cursor
      })
    )
    const appeals = page.Items || []
 
    const users = await batchGetByIds(
      process.env.USERS_TABLE_NAME,
      appeals.flatMap((appeal) => [appeal.appellantId, appeal.reportedUserId]).filter(Boolean)
    )
 
    return {
      statusCode: 200,
      headers: corsHeaders,
      body: JSON.stringify({
        items: appeals.map((appeal) => toAdminAppeal(appeal, users)),
        nextCursor: page.LastEvaluatedKey ? encodeCursor(page.LastEvaluatedKey) : null
      })
    }
  } catch (error) {
    if (error instanceof InvalidParamsError) {
      return {
        statusCode: 400,
        headers: corsHeaders,
        body: JSON.stringify({ error: error.message })
      }
    }
    console.error('Error listing appeals:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to list appeals' })
    }
  }
})