All files / moderation vote.js

100% Statements 24/24
87.5% Branches 7/8
100% Functions 1/1
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                      1x 1x           1x 1x   1x 1x   1x 12x 12x 12x 11x   11x 11x     10x 10x   1x     9x 8x   2x   1x 1x      
/**
 * POST /v1/moderation/tasks/{taskId}/vote      Body: { vote: 'approve' | 'reject' }
 *
 * Casts the caller's vote on a pending content. One vote per person; never on
 * one's own content; nothing else restricts it — not reputation, not an owner's
 * block. When one side reaches the subject's threshold the content is decided,
 * in the same request. Voting is free: no credits are debited.
 *
 * @implements UC-MOD-002.1, UC-MOD-003.1, UC-MOD-004.1, UC-MOD-007.1, UC-MOD-009.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const {
  authenticateModerator,
  errorResponse,
  jsonResponse,
  refusalResponse
} = require('../utils/moderation/access')
const { castVote } = require('../utils/moderation/engine')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateModerator(event)
    if (caller.response) return caller.response
    const { auth } = caller
 
    const taskId = event.pathParameters?.taskId
    if (!taskId) return errorResponse(400, 'Missing taskId')
 
    let payload
    try {
      payload = JSON.parse(event.body || '{}')
    } catch {
      return errorResponse(400, 'Invalid JSON body')
    }
 
    const result = await castVote(docClient, { userId: auth.userId, taskId, vote: payload.vote })
    if (!result.ok) return refusalResponse(result.code)
 
    return jsonResponse(200, { taskId, status: result.status })
  } catch (error) {
    console.error('[moderation/vote] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to cast vote')
  }
}