All files / store like.js

100% Statements 27/27
83.33% Branches 10/12
100% Functions 1/1
100% Lines 25/25

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 8x 8x 8x 7x   7x 7x   5x 5x                                                   3x 3x 3x 1x   2x 1x     1x     2x   1x 1x      
/**
 * POST /v1/store/{packId}/like
 *
 * Likes a pack with an approved version. The like and the counter are written in
 * one transaction, whose condition also closes the race with a concurrent like or
 * the pack's deletion.
 *
 * @implements UC-STORE-006.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, TransactWriteCommand } = require('@aws-sdk/lib-dynamodb')
const {
  authenticateModerator: authenticateUser,
  errorResponse,
  jsonResponse
} = require('../utils/moderation/access')
const { packsTable, likesTable, getPack } = require('../utils/packs')
const { buildPackLikesCountUpdate } = require('../utils/packCleanup')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateUser(event)
    if (caller.response) return caller.response
    const { userId } = caller.auth
 
    const pack = await getPack(docClient, event.pathParameters?.packId)
    if (!pack?.currentVersion) return errorResponse(404, 'Pack not found')
 
    try {
      await docClient.send(
        new TransactWriteCommand({
          TransactItems: [
            {
              Put: {
                TableName: likesTable(),
                Item: {
                  packId: pack.id,
                  userId,
                  likedAt: new Date().toISOString()
                },
                ConditionExpression: 'attribute_not_exists(packId)'
              }
            },
            {
              ConditionCheck: {
                TableName: packsTable(),
                Key: { id: pack.id },
                ConditionExpression: 'attribute_exists(id)'
              }
            },
            { Update: buildPackLikesCountUpdate(pack.id, 1) }
          ]
        })
      )
    } catch (error) {
      Eif (error.name === 'TransactionCanceledException') {
        const reasons = error.CancellationReasons || []
        if (reasons[0]?.Code === 'ConditionalCheckFailed') {
          return errorResponse(409, 'Already liked this pack')
        }
        if (reasons[1]?.Code === 'ConditionalCheckFailed') {
          return errorResponse(404, 'Pack not found')
        }
      }
      throw error
    }
 
    return jsonResponse(201, { packId: pack.id, likedByMe: true })
  } catch (error) {
    console.error('[store/like] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to like pack')
  }
}