SHARE
TWEET

Untitled

lakerka Oct 16th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var AWS = require('aws-sdk');
  2. var dt = 'hrhub-prod1-Downloads';
  3. var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
  4.  
  5. exports.handler = async function(event, context, callback) {
  6.     var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  7.     var parts = srcKey.split('/');
  8.     var fileName = srcKey;
  9.  
  10.     if (parts.length === 4 && parts[0] !== 'none') {
  11.         var area = parts[0];
  12.         var recipient = parts[1];
  13.         var file = parts[2];
  14.         var fileVersion = parts[3];
  15.  
  16.         await dltRecs(area, recipient, file, fileVersion, 'File');
  17.         await dltRecs(area, recipient, file, fileVersion, 'Permission');
  18.         console.log('File permissions deleted');
  19.  
  20.         if (!fileExists(area, '*', '*')) {
  21.             await dltRecs(area, '*', '*', '*', 'Permission');
  22.             await dltRecs(area, '*', '*', '*', 'FileRetention');
  23.             await dltRecs(area, recipient, '*', '*', 'Permission');
  24.             await dltRecs(area, recipient, '*', '*', 'FileRetention');
  25.             await dltRecs(area, recipient, file, '*', 'Permission');
  26.             await dltRecs(area, recipient, file, '*', 'FileRetention');
  27.  
  28.             return;
  29.         }
  30.  
  31.         if (!fileExists(area, recipient, '*')) {
  32.             await dltRecs(area, recipient, '*', '*', 'Permission');
  33.             await dltRecs(area, recipient, '*', '*', 'FileRetention');
  34.             await dltRecs(area, recipient, file, '*', 'Permission');
  35.             await dltRecs(area, recipient, file, '*', 'FileRetention');
  36.  
  37.             return;
  38.         }
  39.  
  40.         if (!fileExists(area, recipient, file)) {
  41.             await dltRecs(area, recipient, file, '*', 'Permission');
  42.             await dltRecs(area, recipient, file, '*', 'FileRetention');
  43.            
  44.             return;
  45.         }
  46.     }
  47. };
  48.  
  49. async function fileExists(area, recipient, fileGroup) {
  50.     var params = {
  51.         TableName: dt,
  52.         ProjectionExpression: 'ID',
  53.         KeyConditionExpression: 'ID = :ID',
  54.         FilterExpression: '(Recipient = :Recipient or :x = :Recipient) and (FileGroup = :FileGroup or :x = :FileGroup)',
  55.         ExpressionAttributeValues: {
  56.             ':ID': { S: area },
  57.             ':Recipient': { S: recipient },
  58.             ':FileGroup': { S: fileGroup },
  59.             ':x': { S: '*' }
  60.         }
  61.     };
  62.     var data = await ddb.query(params).promise();
  63.  
  64.     return data.Items.length < 0;
  65. }
  66.  
  67. async function dltRecs(area, recipient, fileGroup, fileVersion, type) {
  68.     var params = {
  69.         TableName: dt,
  70.         IndexName: 'RecordType-index',
  71.         ProjectionExpression: 'ID, SortKey',
  72.         KeyConditionExpression: 'RecordType = :RecordType',
  73.         FilterExpression: 'Area = :Area and (Recipient = :Recipient or :x = :Recipient) and (FileGroup = :FileGroup or x: = :FileGroup) and (FileVersion = :FileVersion or x: = :FileVersion)',
  74.         ExpressionAttributeValues: {
  75.             ':RecordType': { S: type },
  76.             ':Area': { S: area },
  77.             ':Recipient': { S: recipient },
  78.             ':FileGroup': { S: fileGroup },
  79.             ':FileVersion': { S: fileVersion },
  80.             ':x': { S: '*' }
  81.         }
  82.     };
  83.  
  84.     var data = await ddb.query(params).promise();
  85.     var deleteItems = [];
  86.  
  87.     data.Items.forEach(function(item) {
  88.         var id = item.ID.S;
  89.         var sortKey = item.SortKey.S;
  90.         var item = {
  91.             DeleteRequest: {
  92.                 Key: {
  93.                     'ID': { S: id },
  94.                     'SortKey': { S: sortKey }
  95.                 }
  96.             }
  97.         };
  98.  
  99.         deleteItems.push(item);
  100.     });
  101.  
  102.     do {
  103.         await batchDelete(deleteItems.splice(0, 25));
  104.     }
  105.     while (deleteItems.length > 0)
  106. }
  107.  
  108. async function batchDelete(deleteItems) {
  109.  
  110.     if (deleteItems.length > 0) {
  111.         let params = {
  112.             RequestItems: {
  113.                 [dt]: deleteItems
  114.             }
  115.         };
  116.  
  117.         do {
  118.             var data = await ddb.batchWriteItem(params).promise();
  119.             params = data.UnprocessedItems;
  120.         }
  121.         while (params.length > 0)
  122.     }
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top