oneday-core
Advanced tools
Comparing version 1.0.46 to 1.0.47
11
db.js
@@ -26,3 +26,3 @@ const aws = require('aws-sdk'); | ||
async function getItem(params, filter, isThrow) { | ||
async function getItem(params, filter = null, isThrow = false) { | ||
const result = await dynamoDb.get(params).promise(); | ||
@@ -69,3 +69,3 @@ | ||
async function query(params, filter, isThrow) { | ||
async function query(params, filter = null, isThrow = false) { | ||
const result = await dynamoDb.query(params).promise(); | ||
@@ -104,4 +104,2 @@ | ||
async function create(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
@@ -115,2 +113,3 @@ params.Item.created = now; | ||
if (isPublish === true) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
await resource.publishToTopic(topicName, enums.event_type.CREATE, params.Item, requestId, []); | ||
@@ -130,3 +129,2 @@ } | ||
async function update(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
@@ -146,2 +144,3 @@ const expr = params.UpdateExpression; | ||
if (isPublish === true) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
let change = []; | ||
@@ -157,3 +156,2 @@ if (params.ExpressionAttributeNames !== null && util.isObject(params.ExpressionAttributeNames)) { | ||
async function softDelete(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
@@ -175,2 +173,3 @@ const expr = params.UpdateExpression; | ||
if (isPublish === true) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
let change = []; | ||
@@ -177,0 +176,0 @@ if (params.ExpressionAttributeNames !== null && util.isObject(params.ExpressionAttributeNames)) { |
89
index.js
@@ -17,2 +17,6 @@ 'use strict'; | ||
module.exports.verifyAndLogNotification = verifyAndLogNotification; | ||
module.exports.saveNotificationError = saveNotificationError; | ||
module.exports.updateNotificationLog = updateNotificationLog; | ||
module.exports.sendToQueue = resource.sendToQueue; | ||
@@ -45,2 +49,4 @@ module.exports.publishToTopic = resource.publishToTopic; | ||
/* Http Events */ | ||
async function saveEvent(event, type) { // Saves the incoming copy of data to a table as is. Must be called for every incoming http request. | ||
@@ -83,6 +89,7 @@ console.log(event); | ||
} | ||
return variable.getInstanceRequestId(); | ||
const requestId = variable.getInstanceRequestId(); | ||
console.log('RequestId: ' + requestId); | ||
return requestId; | ||
} | ||
async function updateEventLogResponseCode(responseCode) { | ||
@@ -100,4 +107,3 @@ const params = { | ||
async function generateErrorResponse(error, event, caller) { | ||
const requestId = variable.getInstanceRequestId(); | ||
const errorLog = saveError(error, event, caller, requestId); | ||
const errorLog = saveError(error, event, caller); | ||
const eventLog = updateEventLogResponseCode(error.statusCode); | ||
@@ -127,2 +133,75 @@ | ||
}; | ||
} | ||
} | ||
/* Notification Events */ | ||
async function verifyAndLogNotification(notification) { | ||
const message = notification.body; | ||
const messageAttributes = notification.messageAttributes; | ||
const entityType = messageAttributes.EntityType.stringValue; | ||
const eventType = messageAttributes.EventType.stringValue; | ||
const notificationId = messageAttributes.NotificationId.stringValue; | ||
const originalRequestId = messageAttributes.RequestId.stringValue; | ||
variable.setInstanceRequestId(notificationId); | ||
console.log('NotificationId: ' + notificationId); | ||
const params = { | ||
TableName: process.env.incomingTable, | ||
Key: {id: variable.getInstanceRequestId()} | ||
}; | ||
const currentEventLog = await core.getFromDb(params); | ||
if (currentEventLog.length > 0 && currentEventLog[0].responseCode === 200) { | ||
console.log('Event has already been successfully processed. NotificationId: ' + notificationId + ', Original RequestId: ' + originalRequestId); | ||
return; | ||
} | ||
if (currentEventLog.length === 0) { | ||
const params = { | ||
TableName: process.env.ingestTable, | ||
Item: { | ||
id: variable.getInstanceRequestId(), | ||
originalRequestId: originalRequestId, | ||
message: message, | ||
attributes: JSON.stringify(messageAttributes), | ||
entity: entityType, | ||
event: eventType | ||
} | ||
}; | ||
await core.create(params); | ||
console.log('Processing event. NotificationId: ' + notificationId + ', Original RequestId: ' + originalRequestId); | ||
} | ||
else { | ||
console.log('Retry attempt to process failed event. NotificationId: ' + notificationId + ', Original RequestId: ' + originalRequestId); | ||
} | ||
if (core.get(notification.body) === null) { | ||
await updateNotificationLog(200, 'Null record body. Skipped processing.'); | ||
} | ||
if (core.get(notification.messageAttributes.EntityType.stringValue) === null || | ||
core.get(notification.messageAttributes.EventType.stringValue) === null) { | ||
await updateNotificationLog(500, 'Missing mandatory params. Unable to process event.'); | ||
} | ||
} | ||
async function saveNotificationError(error) { // Saves the given error to the error table | ||
const params = { | ||
TableName: process.env.ingestTable, | ||
Key: {id: variable.getInstanceRequestId()}, | ||
UpdateExpression: "set #responseCode = :code, #message = :message, #error = :error", | ||
ExpressionAttributeNames: {'#responseCode': 'responseCode', '#message': 'message', '#error': 'error'}, | ||
ExpressionAttributeValues: {':code': 500, ':message': 'ERROR', ':error': error} | ||
}; | ||
await db.update(params, false, null); | ||
} | ||
async function updateNotificationLog(responseCode, message) { | ||
const params = { | ||
TableName: process.env.ingestTable, | ||
Key: {id: variable.getInstanceRequestId()}, | ||
UpdateExpression: "set #responseCode = :code, #message = :message", | ||
ExpressionAttributeNames: {'#responseCode': 'responseCode', '#message': 'message'}, | ||
ExpressionAttributeValues: {':code': responseCode, ':message': message} | ||
}; | ||
await db.update(params, false, null); | ||
} |
{ | ||
"name": "oneday-core", | ||
"version": "1.0.46", | ||
"version": "1.0.47", | ||
"description": "Basic AWS util functions to ease development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
30888
806
17