Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var AWS = require('aws-sdk');
- var dt = 'hrhub-prod1-Downloads';
- var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
- exports.handler = async function(event, context, callback) {
- var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
- var parts = srcKey.split('/');
- var fileName = srcKey;
- if (parts.length === 4 && parts[0] !== 'none') {
- var area = parts[0];
- var recipient = parts[1];
- var file = parts[2];
- var fileVersion = parts[3];
- await dltRecs(area, recipient, file, fileVersion, 'File');
- await dltRecs(area, recipient, file, fileVersion, 'Permission');
- console.log('File permissions deleted');
- if (!fileExists(area, '*', '*')) {
- await dltRecs(area, '*', '*', '*', 'Permission');
- await dltRecs(area, '*', '*', '*', 'FileRetention');
- await dltRecs(area, recipient, '*', '*', 'Permission');
- await dltRecs(area, recipient, '*', '*', 'FileRetention');
- await dltRecs(area, recipient, file, '*', 'Permission');
- await dltRecs(area, recipient, file, '*', 'FileRetention');
- return;
- }
- if (!fileExists(area, recipient, '*')) {
- await dltRecs(area, recipient, '*', '*', 'Permission');
- await dltRecs(area, recipient, '*', '*', 'FileRetention');
- await dltRecs(area, recipient, file, '*', 'Permission');
- await dltRecs(area, recipient, file, '*', 'FileRetention');
- return;
- }
- if (!fileExists(area, recipient, file)) {
- await dltRecs(area, recipient, file, '*', 'Permission');
- await dltRecs(area, recipient, file, '*', 'FileRetention');
- return;
- }
- }
- };
- async function fileExists(area, recipient, fileGroup) {
- var params = {
- TableName: dt,
- ProjectionExpression: 'ID',
- KeyConditionExpression: 'ID = :ID',
- FilterExpression: '(Recipient = :Recipient or :x = :Recipient) and (FileGroup = :FileGroup or :x = :FileGroup)',
- ExpressionAttributeValues: {
- ':ID': { S: area },
- ':Recipient': { S: recipient },
- ':FileGroup': { S: fileGroup },
- ':x': { S: '*' }
- }
- };
- var data = await ddb.query(params).promise();
- return data.Items.length < 0;
- }
- async function dltRecs(area, recipient, fileGroup, fileVersion, type) {
- var params = {
- TableName: dt,
- IndexName: 'RecordType-index',
- ProjectionExpression: 'ID, SortKey',
- KeyConditionExpression: 'RecordType = :RecordType',
- FilterExpression: 'Area = :Area and (Recipient = :Recipient or :x = :Recipient) and (FileGroup = :FileGroup or x: = :FileGroup) and (FileVersion = :FileVersion or x: = :FileVersion)',
- ExpressionAttributeValues: {
- ':RecordType': { S: type },
- ':Area': { S: area },
- ':Recipient': { S: recipient },
- ':FileGroup': { S: fileGroup },
- ':FileVersion': { S: fileVersion },
- ':x': { S: '*' }
- }
- };
- var data = await ddb.query(params).promise();
- var deleteItems = [];
- data.Items.forEach(function(item) {
- var id = item.ID.S;
- var sortKey = item.SortKey.S;
- var item = {
- DeleteRequest: {
- Key: {
- 'ID': { S: id },
- 'SortKey': { S: sortKey }
- }
- }
- };
- deleteItems.push(item);
- });
- do {
- await batchDelete(deleteItems.splice(0, 25));
- }
- while (deleteItems.length > 0)
- }
- async function batchDelete(deleteItems) {
- if (deleteItems.length > 0) {
- let params = {
- RequestItems: {
- [dt]: deleteItems
- }
- };
- do {
- var data = await ddb.batchWriteItem(params).promise();
- params = data.UnprocessedItems;
- }
- while (params.length > 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement