oneday-core
Advanced tools
Comparing version 1.0.34 to 1.0.35
41
db.js
@@ -5,2 +5,5 @@ const aws = require('aws-sdk'); | ||
const resource = require('./resource'); | ||
const enums = require('./enum'); | ||
module.exports.getFromDb = getFromDb; | ||
@@ -31,3 +34,4 @@ module.exports.getFromDbElseThrow = getFromDbElseThrow; | ||
}; | ||
} else { | ||
} | ||
else { | ||
return []; | ||
@@ -45,3 +49,4 @@ } | ||
}; | ||
} else { | ||
} | ||
else { | ||
return []; | ||
@@ -72,3 +77,4 @@ } | ||
}; | ||
} else { | ||
} | ||
else { | ||
return []; | ||
@@ -86,3 +92,4 @@ } | ||
}; | ||
} else { | ||
} | ||
else { | ||
return []; | ||
@@ -96,3 +103,3 @@ } | ||
async function create(params) { | ||
async function create(params, topicName, requestId, isPublish = true) { | ||
@@ -104,3 +111,12 @@ const now = new Date().toISOString(); | ||
console.log('Creating entry in table: ' + params.TableName); | ||
return await dynamoDb.put(params).promise(); | ||
const result = await dynamoDb.put(params).promise(); | ||
console.log(result); | ||
if (isPublish === true) { | ||
if (topicName === null) { | ||
topicName = process.env.eventTopic; | ||
} | ||
await resource.publishToTopic(topicName, enums.event_type.CREATE, params, requestId, []); | ||
} | ||
return result; | ||
} | ||
@@ -116,3 +132,3 @@ | ||
async function update(params) { | ||
async function update(params, topicName, requestId, isPublish = true) { | ||
const now = new Date().toISOString(); | ||
@@ -128,3 +144,12 @@ const expr = params.UpdateExpression; | ||
console.log('Updating table: ' + params.TableName); | ||
return await dynamoDb.update(params).promise(); | ||
const result = await dynamoDb.update(params).promise(); | ||
console.log(result); | ||
if (isPublish === true) { | ||
if (topicName === null) { | ||
topicName = process.env.eventTopic; | ||
} | ||
await resource.publishToTopic(topicName, enums.event_type.UPDATE, params, requestId, []); | ||
} | ||
return result; | ||
} |
59
index.js
'use strict'; | ||
const aws = require('aws-sdk'); | ||
aws.config.update({region: process.env.region}); | ||
const sqs = new aws.SQS({apiVersion: '2012-11-05'}); | ||
const sns = new aws.SNS({region: process.env.region}); | ||
const uuid = require('uuid/v4'); | ||
const db = require('./db'); | ||
const util = require('./util'); | ||
const resource = require('./resource'); | ||
const enums = require('./enum'); | ||
@@ -15,4 +11,2 @@ const error = require('./error'); | ||
module.exports.saveError = saveError; | ||
module.exports.sendToQueue = sendToQueue; | ||
module.exports.publishToTopic = publishToTopic; | ||
module.exports.getRequestId = getRequestId; | ||
@@ -22,2 +16,5 @@ module.exports.generateErrorResponse = generateErrorResponse; | ||
module.exports.sendToQueue = resource.sendToQueue; | ||
module.exports.publishToTopic = resource.publishToTopic; | ||
module.exports.getFromDb = db.getFromDb; | ||
@@ -45,2 +42,3 @@ module.exports.queryDb = db.queryDb; | ||
//ToDo: Add response code, global and lambda request tracking numbers | ||
@@ -58,3 +56,3 @@ async function saveEvent(event, type, requestId) { // Saves the incoming copy of data to a table as is | ||
}; | ||
return await db.create(params); | ||
return await db.create(params, null, false); | ||
} | ||
@@ -78,46 +76,5 @@ | ||
}; | ||
return await db.create(params); | ||
return await db.create(params, null, false); | ||
} | ||
async function sendToQueue(body, queueName) { // Sends a message to the given queue | ||
try { | ||
var params = {QueueName: queueName}; | ||
const queueInfo = await sqs.getQueueUrl(params); | ||
params = { | ||
MessageBody: JSON.stringify(body), | ||
QueueUrl: queueInfo.QueueUrl | ||
}; | ||
const result = await sqs.sendMessage(params); | ||
console.log(result); | ||
} catch (err) { | ||
await saveError('Unable to send message to (' + queueName + ') queue: ', err, params, 'EXTERNAL'); | ||
} | ||
} | ||
async function publishToTopic(topicName, type, message) { // Sends the given message object to given sns topic | ||
try { | ||
if (Object.keys(enums.event_type).indexOf(type.toUpperCase()) < 0) { | ||
throw new Error('Cannot publish to topic (' + topicName + ' ). Invalid event type: ' + type); | ||
} | ||
const topicArn = util.getSnsArn(process.env.region, process.env.accountid, topicName); | ||
const params = { | ||
Message: JSON.stringify({ | ||
type: type, | ||
body: message | ||
}), | ||
TopicArn: topicArn | ||
}; | ||
const result = await sns.publish(params); | ||
console.log(result); | ||
} catch (err) { | ||
await saveError('Unable to publish message to topic (' + topicName + ') : ', err, message, 'EXTERNAL'); | ||
} | ||
} | ||
function getRequestId(event) { | ||
@@ -127,2 +84,3 @@ return event.requestContext.authorizer !== undefined && event.requestContext.authorizer.requestId !== undefined ? event.requestContext.authorizer.requestId : uuid(); | ||
async function updateEventLogResponseCode(requestId, responseCode) { | ||
@@ -150,3 +108,2 @@ const params = { | ||
}; | ||
console.log(params); | ||
return params; | ||
@@ -153,0 +110,0 @@ } |
{ | ||
"name": "oneday-core", | ||
"version": "1.0.34", | ||
"version": "1.0.35", | ||
"description": "Basic AWS util functions to ease development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
53
test.js
@@ -34,27 +34,30 @@ var assert = require('chai').assert; | ||
describe('get', function () { | ||
const obj = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: { | ||
e: 3 | ||
} | ||
}, | ||
f: null | ||
}; | ||
it('should return obj when given param is present', function () { | ||
assert.equal(index.get(obj.a), 1); | ||
assert.equal(index.get(obj.b.c), 2); | ||
assert.isNotNull(index.get(obj.b)); | ||
assert.isNotNull(index.get(obj.b.d)); | ||
}); | ||
it('should return null when given param is not present', function () { | ||
assert.isNull(index.get(obj.f)); | ||
assert.isNull(index.get(obj.z)); | ||
assert.isNull(index.get(obj.a.z)); | ||
assert.isNull(index.get(null)); | ||
assert.isNull(index.get(undefined)); | ||
}); | ||
}); | ||
describe('hasProperties', function () { | ||
// describe('test', function(){ | ||
// var obj = JSON.parse('{\n' + | ||
// '\t\'username\' : \'praneet@oneday.com\',\n' + | ||
// '\t\'firstname\' : \'John\',\n' + | ||
// '\t\'lastname\': \'Doe\',\n' + | ||
// '\t\'channel_uuid\' : \'6617e852-3a67-454d-8f82-e1fc5f4a9b17\',\n' + | ||
// '\t\'access_type\' : \'moderator\',\n' + | ||
// '\t\'can_invite\' : \'true\',\n' + | ||
// '\t\'valid_months\' : \'6\'\n' + | ||
// '}'); | ||
// | ||
// const validation = index.hasProperties(obj, ['username', 'channel_uuid', 'access_type']); | ||
// console.log(validation); | ||
// console.log(obj.constructor); | ||
// console.log(['username', 'channel_uuid', 'access_type'].constructor); | ||
// if (validation.status === false) { | ||
// //base.saveError('Missing params: (' + validation.props.toString() + '). ' + ' Unable to issue an invitation. ', body, FUNCTION_TYPE.CREATE_INVITATION); | ||
// //return {statusCode: 400}; | ||
// console.log('=> error'); | ||
// } | ||
// console.log('=> done'); | ||
// }); | ||
describe('invalid input', function () { | ||
@@ -66,2 +69,6 @@ it('should return false when given input is invalid', function () { | ||
assert.isFalse(index.hasProperties({'a': 1, 'b': 2}, {'a': 1, 'b': 2}).status); | ||
const obj = {a: 1, b: null, c: {d: 2, e: null}}; | ||
assert.isFalse(index.hasProperties(obj, ['a', 'b']).status); | ||
assert.isFalse(index.hasProperties(obj, ['a', 'c.e']).status); | ||
}); | ||
@@ -68,0 +75,0 @@ }); |
12
util.js
@@ -39,3 +39,4 @@ module.exports.getDynamoDbArn = getDynamoDbArn; | ||
const property = props[each]; | ||
console.log("==>"); | ||
console.log(property); | ||
if (property.toString().indexOf('.') > 0) { | ||
@@ -48,2 +49,9 @@ let splitProperty = property.split('.'); | ||
currentObj = currentObj[currentProp]; | ||
console.log(">"); | ||
console.log(currentObj); | ||
if (currentObj === null || currentObj.toString().trim() === '') { | ||
console.log(currentObj); | ||
missingProps.push(property); | ||
break; | ||
} | ||
} | ||
@@ -57,3 +65,3 @@ else { | ||
else { | ||
if (Object.keys(obj).indexOf(property) < 0) { | ||
if (Object.keys(obj).indexOf(property) < 0 || obj[property] === null || obj[property].toString().trim() === '') { | ||
missingProps.push(property); | ||
@@ -60,0 +68,0 @@ } |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
23100
10
631
12