All files / users get-me.js

96% Statements 24/25
83.33% Branches 10/12
100% Functions 1/1
96% Lines 24/25

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 831x 1x 1x   1x 1x   1x                   1x 7x   7x 1x             6x 6x             5x 1x             4x 4x 4x 4x 4x       4x 4x           4x                             1x 1x              
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { getFollowerCounts, getFollowingCount } = require('../utils/userFollows')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
/**
 * Returns the authenticated user's own UsersTable row. Unlike the Cognito
 * JWT (identity claims only), this exposes DynamoDB-only
 * attributes such as `isPlatformAdmin` to the frontend.
 */
exports.handler = async (event) => {
  const userId = event.requestContext?.authorizer?.claims?.sub
 
  if (!userId) {
    return {
      statusCode: 401,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Unauthorized' })
    }
  }
 
  try {
    const result = await docClient.send(
      new GetCommand({
        TableName: process.env.USERS_TABLE_NAME,
        Key: { id: userId }
      })
    )
 
    if (!result.Item) {
      return {
        statusCode: 404,
        headers: corsHeaders,
        body: JSON.stringify({ error: 'User not found' })
      }
    }
 
    let followerCount = 0
    let followingCount = 0
    Eif (process.env.USER_FOLLOWS_TABLE_NAME && process.env.USER_FOLLOWER_COUNT_TABLE_NAME) {
      try {
        const [followerCounts, following] = await Promise.all([
          getFollowerCounts(docClient, process.env.USER_FOLLOWER_COUNT_TABLE_NAME, [userId]),
          getFollowingCount(docClient, process.env.USER_FOLLOWS_TABLE_NAME, userId)
        ])
        followerCount = followerCounts.get(userId) ?? 0
        followingCount = following
      } catch (followCountsErr) {
        console.error('[get-me] Error fetching follow counts:', followCountsErr)
      }
    }
 
    return {
      statusCode: 200,
      headers: corsHeaders,
      body: JSON.stringify({
        id: result.Item.id,
        email: result.Item.email,
        nickname: result.Item.nickname || null,
        name: result.Item.name,
        createdAt: result.Item.createdAt,
        isPlatformAdmin: result.Item.isPlatformAdmin === true,
        followerCount,
        followingCount
      })
    }
  } catch (error) {
    console.error('Error fetching current user:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to fetch user' })
    }
  }
}