const { doSearch } = require('../util')

const findPrimaryKey = context => {
  const params = {
    TableName: context.tableName
  }

  return context.dynamodb.describeTable(params).promise()
    .then(tableDescription => {
      context.primaryKey = tableDescription.Table.KeySchema.find(
        element => element.KeyType === 'HASH').AttributeName
      return context
    })
}

const deleteAllElements = context => {
  const deleteRequests = []
  let counter = 0
  const MAX_OPERATIONS = 25
  const params = {
    RequestItems: {
      [context.tableName]: []
    }
  }

  for (const item of context.items) {
    params.RequestItems[context.tableName].push({
      DeleteRequest: {
        Key: {
          [context.primaryKey]: item[context.primaryKey]
        }
      }
    })

    counter++

    if (counter % MAX_OPERATIONS === 0) {
      deleteRequests.push(context.dynamodb.batchWriteItem(params).promise())
      params.RequestItems[context.tableName] = []
    }
  }

  if (counter % MAX_OPERATIONS !== 0) {
    deleteRequests.push(context.dynamodb.batchWriteItem(params).promise())
    params.RequestItems[context.tableName] = []
  }

  return Promise.all(deleteRequests)
}

const findAllElements = context => {
  const scanParams = {
    ProjectionExpression: context.primaryKey,
  }

  return doSearch(context.dynamodb, context.tableName, scanParams)
    .then(items => {
      context.items = items
      return context
    })
}


/**
 * This function deletes all record from a given table within dynamodb.
 *
 * It functions as follows:
 *  1) Determine the primary key of the table by calling describeTable
 *  2) Scan all records and store them in an array
 *  3) Pass the records to #deleteAllElements which in turn sends a delete request for each
 *  of them
 *  4) Return a list of promises using Promise.all() to the caller
 *
 * @param tableName the table we want to purge
 * @param dynamodb the AWS dynamodb service that holds the connection
 * @returns {Promise<any[] | never>} concatenation of all delete request promises
 */
const purgeTable = (tableName, dynamodb) => {
  const context = {
    tableName,
    dynamodb
  }

  return findPrimaryKey(context)
    .then(findAllElements)
    .then(deleteAllElements)
}

module.exports = {purgeTable}

@@ -54,16 +54,17 @@
 imaryKey
+s
  = conte
@@ -63,32 +63,32 @@
  = context =%3E %7B%0A
-
   const params =
@@ -227,31 +227,30 @@
      con
-text.primaryKey
+st tableSchema
  = table
@@ -280,61 +280,213 @@
 hema
-.find(%0A        element =%3E element.KeyType === 'HASH')
+%0A%0A      context.primaryKeys = %5B'HASH', 'RANGE'%5D%0A        .map(keyType =%3E tableSchema.find(element =%3E element.KeyType === keyType))%0A        .filter(attribute =%3E attribute)%0A        .map(attribute =%3E attribute
 .Att
@@ -495,16 +495,19 @@
 buteName
+)%0A%0A
 %0A      r
@@ -851,76 +851,12 @@
 ey: 
-%7B%0A          %5Bcontext.primaryKey%5D: item%5Bcontext.primaryKey%5D%0A        %7D
+item
 %0A   
@@ -1375,16 +1375,28 @@
 imaryKey
+s.join(', ')
 ,%0A  %7D%0A%0A 
@@ -2270,16 +2270,17 @@
 imaryKey
+s
 (context
@@ -2358,16 +2358,17 @@
 orts = %7B
+ 
 purgeTab
@@ -2369,10 +2369,11 @@
 rgeTable
+ 
 %7D%0A

fix purge table when table has many keys