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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 2x 16x 16x 15x 1x 14x 1x 13x 13x 12x 12x 12x 12x 68x 62x 62x 62x 57x 57x 60x 59x 59x 58x 1x 62x 61x 11x 3x 3x | 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 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
exports.handler = async (event) => {
try {
const tenantId = event.requestContext?.authorizer?.claims?.['custom:tenantId']
if (!tenantId) {
return {
statusCode: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing tenant context' })
}
}
const entityId = event.pathParameters.entityId
// 1. Vérifier que l'entité appartient au tenant
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.tenantId !== tenantId) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Forbidden: Entity belongs to another tenant' })
}
}
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++
}
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' })
}
}
}
|