All files / entities links.js

100% Statements 27/27
100% Branches 6/6
100% Functions 2/2
100% Lines 27/27

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 921x 1x 1x 1x 1x   1x 1x   1x         1x 7x 7x 7x   7x 1x             6x             5x 1x             4x     4x 1x             3x             3x             3x                     1x 1x                 1x 8x    
const { getActiveFlowId } = require('../shared/activeFlow')
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePermission } = require('../utils/requirePermission')
const { getEntityLinkFlows } = require('../utils/entityLinks')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const CORS_HEADERS = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
const getEntityLinksHandler = async (event) => {
  try {
    const entityId = event.pathParameters?.entityId
    const flowId = await getActiveFlowId(event)
 
    if (!entityId) {
      return {
        statusCode: 400,
        headers: CORS_HEADERS,
        body: JSON.stringify({ error: 'Missing entity id' })
      }
    }
 
    const entityResult = await docClient.send(
      new GetCommand({
        TableName: process.env.ENTITIES_TABLE_NAME,
        Key: { id: entityId }
      })
    )
 
    if (!entityResult.Item) {
      return {
        statusCode: 404,
        headers: CORS_HEADERS,
        body: JSON.stringify({ error: 'Entity not found' })
      }
    }
 
    const entity = entityResult.Item
 
    // Only the owning flow can inspect who links this entity
    if (entity.flowId !== flowId) {
      return {
        statusCode: 403,
        headers: CORS_HEADERS,
        body: JSON.stringify({ error: 'Access denied - only owner can view links' })
      }
    }
 
    const ownerFlowResult = await docClient.send(
      new GetCommand({
        TableName: process.env.FLOWS_TABLE_NAME,
        Key: { id: entity.flowId }
      })
    )
 
    const { sameUserFlowIds, otherUserFlowIds } = await getEntityLinkFlows(docClient, {
      entityLinksTableName: process.env.ENTITY_LINKS_TABLE_NAME,
      flowsTableName: process.env.FLOWS_TABLE_NAME,
      entityId,
      ownerUserId: ownerFlowResult.Item?.ownerId
    })
 
    return {
      statusCode: 200,
      headers: CORS_HEADERS,
      body: JSON.stringify({
        entityId,
        sameUserFlowsCount: sameUserFlowIds.length,
        otherUserFlowsCount: otherUserFlowIds.length,
        totalFlowsCount: sameUserFlowIds.length + otherUserFlowIds.length
      })
    }
  } catch (error) {
    console.error('[entities/links] Exception:', error.message, error.stack)
    return {
      statusCode: 500,
      headers: CORS_HEADERS,
      body: JSON.stringify({ error: 'Failed to fetch entity links' })
    }
  }
}
 
// Wrap with permission check: viewing link usage gates the same 'update' action
exports.handler = requirePermission(getEntityLinksHandler, {
  permission: (event) => `entities-${event.pathParameters?.entityId}:update`
})