All files / space-pages list.js

100% Statements 24/24
90% Branches 18/20
100% Functions 5/5
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                1x 1x 1x 1x   1x   1x 3x     5x                     5x 3x     1x 5x 5x 5x 3x   3x 3x   2x   5x         1x 1x      
// @implements UC-SPACE-009.1, UC-SPACE-010.1
/**
 * GET /v1/flows/{flowId}/pages
 *
 * The flow's space pages for an active member of it: the published ones
 * (members-only included) and the menu, for the space header of a member;
 * every page with its draft state for a member holding `pages:update`.
 */
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { buildMenu, LIMITS } = require('../utils/spacePages')
const { respond, listPages, memberAccess, hasUnpublishedChanges } = require('./shared')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
const byMenuOrder = (a, b) =>
  (a.menuOrder ?? 0) - (b.menuOrder ?? 0) || a.slug.localeCompare(b.slug)
 
function summary(page, canEdit) {
  const base = {
    slug: page.slug,
    visibility: page.visibility,
    showInMenu: page.showInMenu,
    menuOrder: page.menuOrder,
    ...(page.icon && { icon: page.icon }),
    title: (canEdit ? page.draft : page.published).title,
    published: page.published
      ? { title: page.published.title, publishedAt: page.published.publishedAt }
      : null
  }
  if (!canEdit) return base
  return { ...base, hasUnpublishedChanges: hasUnpublishedChanges(page), updatedAt: page.updatedAt }
}
 
exports.handler = async (event) => {
  try {
    const access = await memberAccess(event)
    if (access.response) return access.response
    const { flowId, canEdit } = access
 
    const pages = await listPages(docClient, flowId)
    const visible = canEdit ? pages : pages.filter((page) => page.published)
 
    return respond(200, {
      canEdit,
      pages: visible.sort(byMenuOrder).map((page) => summary(page, canEdit)),
      menu: buildMenu(pages, { members: true }),
      ...(canEdit && { maxPages: LIMITS.pagesPerFlow })
    })
  } catch (error) {
    console.error('Error listing space pages:', error)
    return respond(500, { error: 'Failed to list pages' })
  }
}