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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 1x 30x 2x 28x 27x 2x 25x 2x 23x 22x 22x 13x 13x 22x 2x 2x 1x 1x 22x 4x 4x 4x 2x 1x 1x 4x 4x 1x 1x 1x 4x 4x 3x 3x 4x 2x 22x 22x 1x 22x 1x 22x 22x 1x 22x 13x 22x 22x 2x 2x 1x | const { getActiveFlowId } = require('../shared/activeFlow')
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePermission } = require('../utils/requirePermission')
const { cascadeItemDeletionReferences } = require('../utils/cascadeItemFieldValue')
const { getReferencedEntityIds, adjustCounters } = require('../utils/referenceCounters')
const { getUniqueLanguageValues, buildReservationDelete } = require('../utils/uniqueFieldValue')
const {
needsEqualityIndex,
getEqualityIndexValues,
buildEqualityIndexDelete
} = require('../utils/equalityIndexValue')
const {
needsTextSearchIndex,
getTextSearchTokens,
buildTextSearchDeleteRequest
} = require('../utils/textSearchIndex')
const { batchWrite } = require('../utils/batchWrite')
const { adjustItemCount } = require('../utils/entityItemCount')
const { deleteLikeCount } = require('../utils/itemLikeCount')
const { deleteAllLikesForItem } = require('../utils/itemLikes')
const { deleteAllCommentsForItem, commentTablesFromEnv } = require('../utils/commentCleanup')
const { S3Client } = require('@aws-sdk/client-s3')
const { getItemFileKeys, entityKeyGuard, deleteKeys } = require('../utils/s3Cleanup')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const s3Client = new S3Client({ region: 'eu-central-1' })
const deleteItemHandler = async (event) => {
try {
const entityId = event.pathParameters?.entityId
const id = event.pathParameters?.itemId
const flowId = await getActiveFlowId(event)
if (!flowId) {
return {
statusCode: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing flow context' })
}
}
if (!entityId || !id) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing entityId or id' })
}
}
// Verify item exists and ownership
const result = await docClient.send(
new GetCommand({
TableName: process.env.TABLE_NAME,
Key: { entityId, id }
})
)
if (!result.Item) {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Item not found' })
}
}
// Only owner flow can delete
if (result.Item.flowId !== flowId) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Access denied - only owner can delete' })
}
}
await docClient.send(
new DeleteCommand({
TableName: process.env.TABLE_NAME,
Key: { entityId, id }
})
)
let entityFields = []
if (process.env.ENTITIES_TABLE_NAME) {
const entityResult = await docClient.send(
new GetCommand({
TableName: process.env.ENTITIES_TABLE_NAME,
Key: { id: entityId }
})
)
entityFields = entityResult.Item?.fields || []
}
if (process.env.ENTITY_REFERENCE_COUNTERS_TABLE_NAME) {
const referencedEntityIds = getReferencedEntityIds(entityFields, result.Item)
if (referencedEntityIds.size > 0) {
await adjustCounters(
docClient,
process.env.ENTITY_REFERENCE_COUNTERS_TABLE_NAME,
flowId,
new Map([...referencedEntityIds].map((targetEntityId) => [targetEntityId, -1]))
)
}
}
// Best-effort release of this item's index rows (unique reservations,
// scalar equality-index rows, and text-search trigram rows), batched
// together — a missed release just leaves that value/token blocked or
// findable slightly longer, not catastrophic (unlike a missed check at
// create time). Batched rather than per-row since a heavily-tokenized
// textarea field can produce hundreds of rows.
if (process.env.FIELD_VALUE_INDEX_TABLE_NAME) {
const indexDeleteRequests = []
for (const field of entityFields) {
if (!field.unique) continue
for (const { lang, value } of getUniqueLanguageValues(field, result.Item)) {
const { Delete } = buildReservationDelete(
process.env.FIELD_VALUE_INDEX_TABLE_NAME,
entityId,
field.fieldId,
lang,
value
)
indexDeleteRequests.push({ DeleteRequest: { Key: Delete.Key } })
}
}
for (const field of entityFields) {
if (!needsEqualityIndex(field)) continue
for (const { lang, value } of getEqualityIndexValues(field, result.Item)) {
const { Delete } = buildEqualityIndexDelete(
process.env.FIELD_VALUE_INDEX_TABLE_NAME,
entityId,
field.fieldId,
lang,
value,
id
)
indexDeleteRequests.push({ DeleteRequest: { Key: Delete.Key } })
}
}
for (const field of entityFields) {
if (!needsTextSearchIndex(field)) continue
for (const { lang, kind, token } of getTextSearchTokens(field, result.Item)) {
indexDeleteRequests.push(
buildTextSearchDeleteRequest(entityId, field.fieldId, lang, kind, token, id)
)
}
}
if (indexDeleteRequests.length > 0) {
await batchWrite(docClient, process.env.FIELD_VALUE_INDEX_TABLE_NAME, indexDeleteRequests)
}
}
Iif (process.env.ENTITY_ITEM_COUNT_TABLE_NAME) {
await adjustItemCount(docClient, process.env.ENTITY_ITEM_COUNT_TABLE_NAME, entityId, -1)
}
// This item is gone — any likes on it are stale.
if (process.env.ITEM_LIKES_TABLE_NAME) {
await deleteAllLikesForItem(docClient, process.env.ITEM_LIKES_TABLE_NAME, id)
}
if (process.env.ITEM_LIKE_COUNT_TABLE_NAME) {
await deleteLikeCount(docClient, process.env.ITEM_LIKE_COUNT_TABLE_NAME, id)
}
// ...and so are its comments (with their counter and pending moderation tasks).
const commentTables = commentTablesFromEnv()
if (commentTables) {
await deleteAllCommentsForItem(docClient, commentTables, id)
}
// onDelete cascade: other items in the same flow that reference this item
// through an Item Link (entity_ref) field get SET_NULL (or a placeholder if
// the field is mandatory). Cross-flow public-entity references are handled
// lazily at read time instead (see cascadeItemFieldValue.js for why).
if (process.env.ENTITIES_TABLE_NAME) {
await cascadeItemDeletionReferences({
docClient,
entitiesTableName: process.env.ENTITIES_TABLE_NAME,
itemsTableName: process.env.TABLE_NAME,
flowId,
deletedEntityId: entityId,
deletedItemId: id
})
}
// The item's files go last, once nothing in DynamoDB points at them anymore.
await deleteKeys(
s3Client,
process.env.ATTACHMENTS_BUCKET_NAME,
getItemFileKeys(entityFields, result.Item),
entityKeyGuard(entityId)
)
return {
statusCode: 204,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: ''
}
} catch (error) {
console.error('Error:', error)
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Failed to delete item' })
}
}
}
// Wrap with permission check: require 'entities-{entityId}:items:delete' permission
exports.handler = requirePermission(deleteItemHandler, {
permission: (event) => `entities-${event.pathParameters?.entityId}:items:delete`
})
|