All files / invitations accept.js

100% Statements 29/29
100% Branches 10/10
100% Functions 2/2
100% Lines 28/28

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131  2x 2x 2x   2x 2x               8x 8x     8x 7x               1x               2x 15x 15x 15x   15x 1x                   14x 1x                     13x             12x 1x                   11x 2x                     9x                             8x   8x                 2x 2x                    
// @implements UC-SPACE-006.2
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, UpdateCommand } = require('@aws-sdk/lib-dynamodb')
const { ensureMembershipItem } = require('../utils/membershipItem')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
/**
 * The new member's membership item, when the flow has a membership entity
 * (utils/membershipItem.js). Best-effort: the invitation stays accepted, and
 * a flow member can still create the item from the Members page.
 */
async function giveMembershipItem(flowId, membership) {
  try {
    const { Item: flow } = await docClient.send(
      new GetCommand({ TableName: process.env.FLOWS_TABLE_NAME, Key: { id: flowId } })
    )
    if (!flow) return
    await ensureMembershipItem({
      docClient,
      itemsTableName: process.env.TABLE_NAME,
      flow,
      userId: membership.userId,
      createdBy: membership.invitedBy
    })
  } catch (error) {
    console.error('Failed to give the new member their membership item:', {
      flowId,
      userId: membership.userId,
      error
    })
  }
}
 
exports.handler = async (event) => {
  try {
    const flowId = event.pathParameters?.flowId
    const userId = event.requestContext?.authorizer?.claims?.sub
 
    if (!userId) {
      return {
        statusCode: 401,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({ error: 'Not authenticated' })
      }
    }
 
    if (!flowId) {
      return {
        statusCode: 400,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({ error: 'Missing flowId' })
      }
    }
 
    // Get pending membership
    const membership = await docClient.send(
      new GetCommand({
        TableName: process.env.FLOW_MEMBERSHIPS_TABLE_NAME,
        Key: { userId, flowId }
      })
    )
 
    if (!membership.Item) {
      return {
        statusCode: 404,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({ error: 'Invitation not found' })
      }
    }
 
    if (membership.Item.status !== 'pending') {
      return {
        statusCode: 400,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({ error: `Invitation status is ${membership.Item.status}` })
      }
    }
 
    // Update membership to active
    const result = await docClient.send(
      new UpdateCommand({
        TableName: process.env.FLOW_MEMBERSHIPS_TABLE_NAME,
        Key: { userId, flowId },
        UpdateExpression: 'SET #status = :status',
        ExpressionAttributeNames: {
          '#status': 'status'
        },
        ExpressionAttributeValues: {
          ':status': 'active'
        },
        ReturnValues: 'ALL_NEW'
      })
    )
 
    await giveMembershipItem(flowId, result.Attributes)
 
    return {
      statusCode: 200,
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      },
      body: JSON.stringify(result.Attributes)
    }
  } catch (error) {
    console.error('Error:', error)
    return {
      statusCode: 500,
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      },
      body: JSON.stringify({ error: 'Failed to accept invitation' })
    }
  }
}