All files / space-pages get.js

100% Statements 30/30
100% Branches 22/22
100% Functions 1/1
100% Lines 24/24

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                    2x 2x 2x                   2x   2x   2x 14x 14x 14x 12x   12x 12x 11x 11x 11x   10x 9x 2x 1x   7x   4x 2x           1x 1x      
// @implements UC-SPACE-009.1, UC-SPACE-010.1, UC-SPACE-019.1
/**
 * GET /v1/flows/{flowId}/pages/{pageSlug}[?version=draft]
 * GET /v1/flows/{flowId}/pages/{pageSlug}/blocks/{blockId}?offset= : the next
 *   items of one of the published page's item lists ("load more")
 *
 * For an active member: the published page, members-only included (404 when
 * not published). `?version=draft`, with `pages:update`: the page's settings
 * and draft, for the editor.
 */
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { PAGE_SLUG_REGEX } = require('../utils/spacePages')
const {
  respond,
  getPage,
  memberAccess,
  forbidden,
  pageNotFound,
  readableVersion,
  publishedBlockItems,
  editorView
} = require('./shared')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
exports.handler = async (event) => {
  try {
    const access = await memberAccess(event)
    if (access.response) return access.response
    const { flowId, canEdit } = access
 
    const pageSlug = event.pathParameters?.pageSlug
    if (typeof pageSlug !== 'string' || !PAGE_SLUG_REGEX.test(pageSlug)) return pageNotFound()
    const wantsItems = event.pathParameters?.blockId !== undefined
    const wantsDraft = !wantsItems && event.queryStringParameters?.version === 'draft'
    if (wantsDraft && !canEdit) return forbidden()
 
    const page = await getPage(docClient, flowId, pageSlug)
    if (wantsItems) {
      if (!page?.published) return pageNotFound()
      return await publishedBlockItems(docClient, event, page.published, flowId)
    }
    if (wantsDraft) return page ? respond(200, await editorView(docClient, page)) : pageNotFound()
 
    if (!page?.published) return pageNotFound()
    return respond(200, {
      slug: page.slug,
      visibility: page.visibility,
      ...(await readableVersion(docClient, page.published, flowId))
    })
  } catch (error) {
    console.error('Error getting a space page:', error)
    return respond(500, { error: 'Failed to get page' })
  }
}