All files / utils publicSpaces.js

100% Statements 12/12
83.33% Branches 5/6
100% Functions 1/1
100% Lines 11/11

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                2x   2x 2x               2x   2x 3x                 2x 3x   2x   1x     2x  
// @implements UC-SPACE-021.1
/**
 * The flows whose public space is published, for the sitemaps
 * (docs/architecture/FLOW_SPACE.md, "Space pages in the HTML"): a sparse GSI
 * of the flows table, `publicSpaceIndex`, holds the flows carrying
 * `publicSpace = PUBLIC_SPACE`, which PUT /v1/flows/{flowId} sets while the
 * space is published and removes otherwise.
 */
const { QueryCommand } = require('@aws-sdk/lib-dynamodb')
 
const PUBLIC_SPACE = 'SPACE'
const PUBLIC_SPACE_INDEX = 'publicSpaceIndex'
 
/**
 * The slugs of the published spaces of active flows, in slug order: a Query
 * on the index, a page at a time.
 * @returns {Promise<string[]>}
 */
async function listPublicSpaceSlugs(docClient, tableName) {
  const slugs = []
  let ExclusiveStartKey
  do {
    const page = await docClient.send(
      new QueryCommand({
        TableName: tableName,
        IndexName: PUBLIC_SPACE_INDEX,
        KeyConditionExpression: 'publicSpace = :space',
        ExpressionAttributeValues: { ':space': PUBLIC_SPACE },
        ExclusiveStartKey
      })
    )
    for (const flow of page.Items || []) {
      if (flow.status === 'active' && typeof flow.slug === 'string') slugs.push(flow.slug)
    }
    ExclusiveStartKey = page.LastEvaluatedKey
  } while (ExclusiveStartKey)
  return slugs
}
 
module.exports = { PUBLIC_SPACE, PUBLIC_SPACE_INDEX, listPublicSpaceSlugs }