All files / membership-requests get-join.js

100% Statements 24/24
100% Branches 12/12
100% Functions 4/4
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                  1x 1x 1x 1x   1x     1x                                                 1x 9x 189x     1x 14x 14x 14x   13x 12x 4x   4x                           4x   4x                             1x 1x      
// @implements UC-SPACE-004.1, UC-SPACE-020.1
/**
 * GET /v1/spaces/{slug}/join — the join form of a flow and the caller's own
 * request, for the signed-in user on /space/{slug}/join.
 *
 * Returns the membership entity's fields the form shows (definitions only:
 * the entity's items stay private), the privacy contact, whether the caller is
 * already a member, and their request's status (never other users').
 */
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { loadJoinableFlow, joinFormFields } = require('../utils/spaceJoin')
const { respond, spaceNotFound } = require('./shared')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
/** The attributes of a field definition the form needs, nothing else */
const FORM_FIELD_KEYS = [
  'fieldId',
  'name',
  'type',
  'mandatory',
  'placeholder',
  'options',
  'length',
  'maxLines',
  'min',
  'max',
  'step',
  // Images and files (UC-SPACE-020.1)
  'maxImages',
  'maxFiles',
  'aspectRatio',
  'minWidth',
  'maxWidth',
  'minHeight',
  'maxHeight',
  'resizeMaxWidth',
  'resizeMaxHeight',
  'cropEnabled'
]
 
const toFormField = (field) =>
  Object.fromEntries(
    FORM_FIELD_KEYS.filter((key) => field[key] !== undefined).map((key) => [key, field[key]])
  )
 
exports.handler = async (event) => {
  try {
    const userId = event.requestContext?.authorizer?.claims?.sub
    if (!userId) return respond(401, { error: 'Not authenticated' })
 
    const joinable = await loadJoinableFlow(docClient, event.pathParameters?.slug)
    if (!joinable) return spaceNotFound()
    const { flow, entity } = joinable
 
    const [membershipResult, requestResult] = await Promise.all([
      docClient.send(
        new GetCommand({
          TableName: process.env.FLOW_MEMBERSHIPS_TABLE_NAME,
          Key: { userId, flowId: flow.id }
        })
      ),
      docClient.send(
        new GetCommand({
          TableName: process.env.MEMBERSHIP_REQUESTS_TABLE_NAME,
          Key: { flowId: flow.id, userId }
        })
      )
    ])
    const request = requestResult.Item
 
    return respond(200, {
      name: flow.name,
      defaultLanguage: flow.defaultLanguage || 'en',
      isMember: membershipResult.Item?.status === 'active',
      fields: joinFormFields(entity, flow.space.join.userFieldId).map(toFormField),
      ...(flow.space.join.privacyContact && { privacyContact: flow.space.join.privacyContact }),
      request: request
        ? {
            status: request.status,
            createdAt: request.createdAt,
            ...(request.decidedAt && { decidedAt: request.decidedAt })
          }
        : null
    })
  } catch (error) {
    console.error('Error getting join form:', error)
    return respond(500, { error: 'Failed to get join form' })
  }
}