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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 2x 22x 22x 21x 1x 20x 1x 19x 19x 18x 18x 18x 18x 70x 70x 62x 62x 57x 57x 60x 59x 59x 58x 1x 70x 69x 69x 2x 69x 2x 69x 69x 2x 17x 1x 17x 17x 3x 3x 1x | const { getActiveFlowId } = require('../shared/activeFlow')
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const {
DynamoDBDocumentClient,
QueryCommand,
DeleteCommand,
GetCommand
} = require('@aws-sdk/lib-dynamodb')
const { S3Client, DeleteObjectCommand } = require('@aws-sdk/client-s3')
const { requirePermission } = require('../utils/requirePermission')
const { deleteAllReservationsForEntity } = require('../utils/uniqueFieldValue')
const { setItemCount } = require('../utils/entityItemCount')
const { deleteAllLikesForItem } = require('../utils/itemLikes')
const { deleteLikeCount } = require('../utils/itemLikeCount')
const { deleteAllCommentsForItem, commentTablesFromEnv } = require('../utils/commentCleanup')
const dynamoClient = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(dynamoClient)
const s3Client = new S3Client({})
const ITEMS_TABLE = process.env.TABLE_NAME
const ENTITIES_TABLE = process.env.ENTITIES_TABLE_NAME
const S3_BUCKET = process.env.ATTACHMENTS_BUCKET_NAME
const clearAllItemsHandler = async (event) => {
try {
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' })
}
}
const entityId = event.pathParameters.entityId
// 1. Vérifier que l'entité appartient au flow
const entityResult = await docClient.send(
new GetCommand({
TableName: ENTITIES_TABLE,
Key: { id: entityId }
})
)
if (!entityResult.Item) {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Entity not found' })
}
}
if (entityResult.Item.flowId !== flowId) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Forbidden: Entity belongs to another flow' })
}
}
const entity = entityResult.Item
// 2. Récupérer tous les items de l'entité
const queryResult = await docClient.send(
new QueryCommand({
TableName: ITEMS_TABLE,
KeyConditionExpression: 'entityId = :entityId',
ExpressionAttributeValues: {
':entityId': entityId
}
})
)
const items = queryResult.Items || []
let deletedCount = 0
let deletedImagesCount = 0
// 3. Pour chaque item, supprimer les images S3 puis l'item
for (const item of items) {
// 3.1 Supprimer les images S3
const imageFields = entity.fields.filter((f) => f.type === 'image' || f.type === 'image_set')
for (const field of imageFields) {
const imageData = item[field.fieldId]
if (!imageData) continue
// Gérer les deux formats: string (image) ou array (image_set)
const keys = Array.isArray(imageData) ? imageData : [imageData]
for (const key of keys) {
if (key && typeof key === 'string') {
try {
await s3Client.send(
new DeleteObjectCommand({
Bucket: S3_BUCKET,
Key: key
})
)
deletedImagesCount++
} catch (s3Error) {
console.error(`Failed to delete S3 object ${key}:`, s3Error)
// Continue même si une suppression S3 échoue
}
}
}
}
// 3.2 Supprimer l'item de DynamoDB
await docClient.send(
new DeleteCommand({
TableName: ITEMS_TABLE,
Key: {
entityId: item.entityId,
id: item.id
}
})
)
deletedCount++
// Its likes and like counter went with it, as when a single item is deleted
if (process.env.ITEM_LIKES_TABLE_NAME) {
await deleteAllLikesForItem(docClient, process.env.ITEM_LIKES_TABLE_NAME, item.id)
}
if (process.env.ITEM_LIKE_COUNT_TABLE_NAME) {
await deleteLikeCount(docClient, process.env.ITEM_LIKE_COUNT_TABLE_NAME, item.id)
}
// Its comments went with it (with their counter and pending moderation tasks)
const commentTables = commentTablesFromEnv()
if (commentTables) {
await deleteAllCommentsForItem(docClient, commentTables, item.id)
}
}
// Every unique-field reservation this entity's items held is now stale —
// bulk-release them via the entityIndex GSI rather than per-item, since
// every item was just deleted anyway.
if (process.env.FIELD_VALUE_INDEX_TABLE_NAME) {
// Covers both reservation (sk='unique') and filter-index (sk=itemId)
// rows for the entity, regardless of shape — every item was just
// deleted anyway.
await deleteAllReservationsForEntity(
docClient,
process.env.FIELD_VALUE_INDEX_TABLE_NAME,
entityId
)
}
Iif (process.env.ENTITY_ITEM_COUNT_TABLE_NAME) {
await setItemCount(docClient, process.env.ENTITY_ITEM_COUNT_TABLE_NAME, entityId, 0)
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
message: 'Entity cleared successfully',
deletedItems: deletedCount,
deletedImages: deletedImagesCount
})
}
} catch (error) {
console.error('Error clearing entity:', error)
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Failed to clear entity' })
}
}
}
// Wrap with permission check: require 'entities-{entityId}:items:delete' permission
exports.handler = requirePermission(clearAllItemsHandler, {
permission: (event) => `entities-${event.pathParameters?.entityId}:items:delete`
})
|