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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 9x 9x 9x 1x 8x 8x 7x 1x 6x 6x 6x 1x 5x 5x 1x 4x 4x 4x 4x 4x 2x 1x 3x 3x 2x 1x 1x 1x 1x | /**
* DELETE /v1/entities/{entityId}/items/{itemId}/like
*
* Removes the calling user's like on an item, if any. No RBAC permission
* beyond `entities-{entityId}:items:read` (none at all on a public entity,
* see hasEntityReadPermission) — likes are personal, so a user only ever
* manages their own.
*
* @implements UC-ITEM-008.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const {
DynamoDBDocumentClient,
GetCommand,
TransactWriteCommand
} = require('@aws-sdk/lib-dynamodb')
const {
authenticateRequest,
hasEntityReadPermission,
canAccessEntity
} = require('../shared/auth-helper')
const { buildLikeCountUpdate } = require('../utils/itemLikeCount')
const { isCountFloorCancellation } = require('../utils/counterFloor')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const CORS_HEADERS = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
const unlikeItemHandler = async (event) => {
try {
const auth = await authenticateRequest(event)
if (!auth || auth.error) {
return {
statusCode: 401,
headers: CORS_HEADERS,
body: JSON.stringify({ error: auth?.error || 'Authentication required' })
}
}
const entityId = event.pathParameters?.entityId
const itemId = event.pathParameters?.itemId
if (!entityId || !itemId) {
return {
statusCode: 400,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Missing entityId or itemId' })
}
}
Iif (!auth.userId) {
return {
statusCode: 401,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Likes require a signed-in user' })
}
}
const entityResult = await docClient.send(
new GetCommand({
TableName: process.env.ENTITIES_TABLE_NAME,
Key: { id: entityId }
})
)
if (!entityResult.Item) {
return {
statusCode: 404,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Entity not found' })
}
}
Iif (!canAccessEntity(auth, entityResult.Item)) {
return {
statusCode: 403,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Access denied to this entity' })
}
}
const requiredPermission = `entities-${entityId}:items:read`
if (!(await hasEntityReadPermission(auth, entityResult.Item, requiredPermission))) {
return {
statusCode: 403,
headers: CORS_HEADERS,
body: JSON.stringify({ error: `Permission denied: ${requiredPermission} required` })
}
}
const existingLike = await docClient.send(
new GetCommand({
TableName: process.env.ITEM_LIKES_TABLE_NAME,
Key: { itemId, userId: auth.userId }
})
)
if (!existingLike.Item) {
return {
statusCode: 404,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Like not found' })
}
}
const transactItems = [
{
Delete: {
TableName: process.env.ITEM_LIKES_TABLE_NAME,
Key: { itemId, userId: auth.userId },
ConditionExpression: 'attribute_exists(itemId)'
}
}
]
Eif (process.env.ITEM_LIKE_COUNT_TABLE_NAME) {
transactItems.push(buildLikeCountUpdate(process.env.ITEM_LIKE_COUNT_TABLE_NAME, itemId, -1))
}
try {
await docClient.send(new TransactWriteCommand({ TransactItems: transactItems }))
} catch (error) {
if (!isCountFloorCancellation(error)) throw error
// Count already at 0: still remove the row, leave the count alone.
await docClient.send(new TransactWriteCommand({ TransactItems: transactItems.slice(0, 1) }))
}
console.log('[unlike] Item unliked:', { userId: auth.userId, entityId, itemId })
return {
statusCode: 204,
headers: CORS_HEADERS,
body: ''
}
} catch (error) {
if (
error.name === 'ConditionalCheckFailedException' ||
(error.name === 'TransactionCanceledException' &&
error.CancellationReasons?.[0]?.Code === 'ConditionalCheckFailed')
) {
return {
statusCode: 404,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Like not found' })
}
}
console.error('[unlike] Exception:', error.message, error.stack)
return {
statusCode: 500,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'Failed to unlike item' })
}
}
}
module.exports = { unlikeItemHandler }
|