All files / store mine.js

100% Statements 29/29
92.85% Branches 13/14
100% Functions 1/1
100% Lines 27/27

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                        1x 1x         1x 1x   1x   1x 1x   1x 5x 5x 5x 4x   4x 4x         4x 4x 2x 2x   2x 1x   1x       3x                       2x 2x   2x             1x 1x      
/**
 * GET /v1/store/mine
 *
 * The packs the caller authored, the most recently created first.
 *
 * Query params:
 * - limit: page size (default 30, max 100)
 * - nextToken: opaque cursor returned by the previous page
 *
 * @implements UC-STORE-007.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand } = require('@aws-sdk/lib-dynamodb')
const {
  authenticateModerator: authenticateUser,
  errorResponse,
  jsonResponse
} = require('../utils/moderation/access')
const { packsTable, buildPackPresenter } = require('../utils/packs')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
const DEFAULT_LIMIT = 30
const MAX_LIMIT = 100
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateUser(event)
    if (caller.response) return caller.response
    const { userId } = caller.auth
 
    const rawLimit = parseInt(event.queryStringParameters?.limit, 10)
    const limit = Number.isFinite(rawLimit)
      ? Math.min(Math.max(rawLimit, 1), MAX_LIMIT)
      : DEFAULT_LIMIT
 
    let exclusiveStartKey
    const nextToken = event.queryStringParameters?.nextToken
    if (nextToken) {
      try {
        const key = JSON.parse(Buffer.from(nextToken, 'base64').toString())
        // A client-controlled cursor must not point the Query at another user
        if (key?.authorId !== userId || typeof key.id !== 'string') throw new Error('bad')
        exclusiveStartKey = { id: key.id, authorId: key.authorId, createdAt: key.createdAt }
      } catch {
        return errorResponse(400, 'Invalid nextToken')
      }
    }
 
    const result = await docClient.send(
      new QueryCommand({
        TableName: packsTable(),
        IndexName: 'authorIndex',
        KeyConditionExpression: 'authorId = :authorId',
        ExpressionAttributeValues: { ':authorId': userId },
        ScanIndexForward: false,
        Limit: limit,
        ExclusiveStartKey: exclusiveStartKey
      })
    )
 
    const packs = result.Items || []
    const present = await buildPackPresenter(docClient, packs, userId)
 
    return jsonResponse(200, {
      items: packs.map(present),
      nextToken: result.LastEvaluatedKey
        ? Buffer.from(JSON.stringify(result.LastEvaluatedKey)).toString('base64')
        : null
    })
  } catch (error) {
    console.error('[store/mine] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to list your packs')
  }
}