All files / admin lift-user-sanctions.js

100% Statements 24/24
100% Branches 6/6
100% Functions 2/2
100% Lines 21/21

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                        1x 1x       1x 1x   1x 1x   1x         5x           1x 5x 5x 5x   4x     4x 4x     3x 2x             2x                 2x             1x 1x      
/**
 * Lift User Sanctions (Platform Admin) Lambda
 * POST /v1/platform-admin/users/{userId}/lift-sanctions
 *
 * Requires `isPlatformAdmin: true`. Ends a user's restriction and ban: their
 * account is enabled again in Cognito and they may post again. Ends a
 * suspension of their reporting too (UC-REPORT-011.1). Their strikes and a
 * reporting warning stay, as the history of what was decided.
 *
 * @implements UC-REPORT-008.1, UC-REPORT-011.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, UpdateCommand } = require('@aws-sdk/lib-dynamodb')
const {
  CognitoIdentityProviderClient,
  AdminEnableUserCommand
} = require('@aws-sdk/client-cognito-identity-provider')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
const cognitoClient = new CognitoIdentityProviderClient({})
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
const respond = (statusCode, body) => ({
  statusCode,
  headers: corsHeaders,
  body: JSON.stringify(body)
})
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    const userId = event.pathParameters?.userId
    if (!userId) return respond(400, { error: 'Missing userId' })
 
    const result = await docClient.send(
      new GetCommand({ TableName: process.env.USERS_TABLE_NAME, Key: { id: userId } })
    )
    const user = result.Item
    if (!user) return respond(404, { error: 'User not found' })
 
    // Enabled first: should the row update fail, the request can be sent again
    if (user.bannedAt) {
      await cognitoClient.send(
        new AdminEnableUserCommand({
          UserPoolId: process.env.COGNITO_USER_POOL_ID,
          Username: userId
        })
      )
    }
    await docClient.send(
      new UpdateCommand({
        TableName: process.env.USERS_TABLE_NAME,
        Key: { id: userId },
        UpdateExpression: 'REMOVE restrictedUntil, bannedAt, reportingSuspendedUntil',
        ConditionExpression: 'attribute_exists(id)'
      })
    )
 
    return respond(200, {
      id: userId,
      restrictedUntil: null,
      bannedAt: null,
      reportingSuspendedUntil: null
    })
  } catch (error) {
    console.error('Error lifting user sanctions:', error)
    return respond(500, { error: 'Failed to lift sanctions' })
  }
})