import AWS from 'aws-sdk'
import WError from 'verror'
import { resources } from './cloudFormation'

const dynamoDbTable = resources.JobStreamDynamoDBTable.PhysicalResourceId

export function updateRecord (object) {
  const { REGION: region } = process.env
  const awsDynamoDb = new AWS.DynamoDB({ region })
  return new Promise((resolve, reject) => {
    const params = {
      TableName: dynamoDbTable,
      Key: { id: { S: 'jobs' } },
      AttributeUpdates: Object.keys(object).reduce((prev, key) => ({
        [key]: {
          Action: 'PUT',
          Value: {
            S: object[key]
          }
        },
        ...prev
      }), {})
    }
    awsDynamoDb.updateItem(params, (err, result) => {
      if (err) {
        return reject(new WError(err, 'DynamoDB'))
      }
      resolve(result)
    })
  })
}

@@ -1,12 +1,38 @@
+import https from 'https'%0A
 import AWS f
@@ -45,16 +45,16 @@
 ws-sdk'%0A
-
 import W
@@ -275,16 +275,91 @@
 ess.env%0A
+  // Include workaround from: https://github.com/aws/aws-sdk-js/issues/862%0A
   const 
@@ -390,23 +390,405 @@
 namoDB(%7B
+%0A   
  region
+,%0A    httpOptions: %7B%0A      agent: new https.Agent(%7B%0A        rejectUnauthorized: true,%0A        // keepAlive: true,                // workaround part i.%0A                                        // shouldn't be used in AWS Lambda functions%0A        secureProtocol: %22TLSv1_method%22, // workaround part ii.%0A        ciphers: %22ALL%22                  // workaround part ii.%0A      %7D)%0A    %7D%0A 
  %7D)%0A  re

Add workaround for DynamoDB EPROTO errors issue aws/aws-sdk-js#862