Last active
October 30, 2024 14:17
-
-
Save igoralves1/280fab933cf3bd02b49ad16ae4f68758 to your computer and use it in GitHub Desktop.
dynamConn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//// This is a connection that does not work | |
const { DynamoDBDocument } = require('@aws-sdk/lib-dynamodb'); | |
const { DynamoDB, | |
BatchWriteItemCommand, | |
ScanCommand, | |
} = require('@aws-sdk/client-dynamodb'); | |
const { marshall, unmarshall } = require("@aws-sdk/util-dynamodb"); | |
const ddb = DynamoDBDocument.from(new DynamoDB()); | |
const responses = require('./library/api_responses'); | |
const { setGlobalVars } = require('./library/global-vars'); | |
const logCat = 'Dynamodb-Update-Caller'; | |
const logSubCat = `${logCat}-handler`; | |
module.exports.handler = async (event) => { | |
try { | |
setGlobalVars(); | |
Logger.info({ | |
logCat, logSubCat, message: 'Initial event Info from the nameRemoveSpacesCaller_v2 request - EVENT', data: event, | |
}); | |
/* Payload example | |
{ | |
"start_date": "2024-07-01T23:59:00-04:00", | |
"end_date": "2024-09-03T23:59:00-04:00" | |
} | |
*/ | |
const { start_date, end_date } = JSON.parse(event.body); | |
const records = await getRecordsBetweenDates(start_date, end_date) | |
const trimmedRecords = await trimSpaceFromRecord(records) | |
await batchUpdateRecords(trimmedRecords) | |
const recordsUpdated = await getRecordsBetweenDates(start_date, end_date) | |
console.log("Records Updated =>", recordsUpdated) | |
return responses._200(response); | |
} catch (e) { | |
Logger.error({ | |
logCat, logSubCat, message: 'Exception caught', err: e, alarm: true, | |
}); | |
return responses._400({ error: e.message }); | |
} | |
}; | |
async function getRecordsBetweenDates(start_date, end_date) { | |
try { | |
const { AWSDynamoDB } = process.env | |
const params = { | |
TableName: AWSDynamoDB, | |
FilterExpression: "date_time BETWEEN :start_date AND :end_date", | |
ExpressionAttributeValues: { | |
":start_date": { S: start_date }, | |
":end_date": { S: end_date }, | |
}, | |
ProjectionExpression: 'sid, prospectName, agentName, date_time, meeting_date', | |
}; | |
const command = new ScanCommand(params); | |
const data = await ddb.send(command); | |
const records = data.Items.map((item) => unmarshall(item)); | |
return records; | |
} catch (error) { | |
Logger.error({ | |
logCat, logSubCat, message: 'Exception caught', err: e, alarm: true, | |
}); | |
return responses._400({ error: e.message }); | |
} | |
} | |
async function trimSpaceFromRecord(records) { | |
try { | |
const trimmedRecords = records.map((record) => { | |
const trimmedAgentName = record.agentName.trim().replace(/\s+/g, " "); | |
const trimmedProspectName = record.prospectName.trim().replace(/\s+/g, " "); | |
return { ...record, agentName: trimmedAgentName, prospectName: trimmedProspectName }; | |
}); | |
return trimmedRecords | |
} catch (error) { | |
Logger.error({ | |
logCat, logSubCat, message: 'Exception caught', err: e, alarm: true, | |
}); | |
return responses._400({ error: e.message }); | |
} | |
} | |
async function batchUpdateRecords(trimmedRecords) { | |
try { | |
const BATCH_SIZE = 25; | |
const batches = []; | |
const { AWSDynamoDB } = process.env | |
for (let i = 0; i < trimmedRecords.length; i += BATCH_SIZE) { | |
const batch = trimmedRecords.slice(i, i + BATCH_SIZE).map((record) => ({ | |
PutRequest: { | |
Item: marshall(record), | |
}, | |
})); | |
batches.push({ | |
RequestItems: { | |
[AWSDynamoDB]: batch, | |
}, | |
}); | |
} | |
for (const batch of batches) { | |
const command = new BatchWriteItemCommand(batch); | |
await ddb.send(command); | |
} | |
return true | |
} catch (error) { | |
Logger.error({ | |
logCat, logSubCat, message: 'Exception caught', err: e, alarm: true, | |
}); | |
return responses._400({ error: e.message }); | |
} | |
} | |
UPDATE "twilio-video-logs-dev" | |
SET agentName = REGEXP_REPLACE(agentName, '\\s+', ' '), | |
prospectName = REGEXP_REPLACE(prospectName, '\\s+', ' ') | |
WHERE contains(sid, '-hfcomposition-copied') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is a file that a working connection with Dynamo see line 61-74 | |
/* eslint-disable no-param-reassign */ | |
/* eslint-disable object-curly-newline */ | |
const { DynamoDBDocument } = require('@aws-sdk/lib-dynamodb'); | |
const { DynamoDB } = require('@aws-sdk/client-dynamodb'); | |
const { getSsoToken } = require('./library/twilio'); | |
const ddb = DynamoDBDocument.from(new DynamoDB()); | |
const responses = require('./library/api_responses'); | |
const { setGlobalVars } = require('./library/global-vars'); | |
const { validateToken } = require('./validateToken'); | |
const logCat = 'twilioSearch_v2'; | |
const { AWSDynamoDB } = process.env; | |
function onQuery(err, data) { | |
if (err) { | |
return responses._400({ 'DynamoDB Error': err }); | |
} | |
return responses._200(data); | |
} | |
module.exports.handler = async (event) => { | |
const logSubCat = `${logCat}-handler`; | |
try { | |
setGlobalVars(); | |
let logMessage; | |
logMessage = 'Initial event Info from the request'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: event }); | |
const data = JSON.parse(event.body); | |
const queryValue = data.input; | |
const tokenSource = event.headers['token-source'] || event.headers['Token-Source']; | |
const tokenKey = event.headers['x-api-key'] || event.headers['X-Api-Key']; | |
if (tokenSource === 'flex') { | |
try { | |
const ssoToken = await getSsoToken(tokenKey, process.env); | |
if (!ssoToken.valid) { | |
return responses._401({ error: 'Invalid SSO Token!' }); | |
} | |
} catch (ex) { | |
return responses._401({ error: `Not authorized to search video ${ex}` }); | |
} | |
} else { | |
if (!validateToken(tokenKey)) { | |
return responses._401({ error: 'Not authorized to search video' }); | |
} | |
} | |
const { meetingId, agentName, prospectName, startDate, endDate } = queryValue; | |
let result; | |
let uniqueData; | |
const meetingIdSearch = async (value) => { | |
logMessage = 'Searching for Meeting id'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: value }); | |
const params = { | |
TableName: AWSDynamoDB, | |
IndexName: 'sid-eventStatus-index', | |
KeyConditionExpression: 'sid = :meetingId', | |
ExpressionAttributeValues: { | |
':meetingId': `${value}-hf-composition-copied`, | |
}, | |
ProjectionExpression: 'sid, body, prospectName, agentName, date_time ,fileTransfer, meeting_date', | |
}; | |
logMessage = 'Params for Searching for Meeting id'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: params }); | |
const output = await ddb | |
.query(params, onQuery()) | |
.then((res) => res.Items); | |
logMessage = `Search result for the Meeting- ${value}`; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: output }); | |
return output; | |
}; | |
const agentSearch = async (value) => { | |
logMessage = 'Searching for Agent'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: value }); | |
const paramsAgent = { | |
TableName: AWSDynamoDB, | |
IndexName: 'agentname-eventStatus-index', | |
KeyConditionExpression: 'agentName = :agentName AND eventStatus =:eventStatus ', | |
ExpressionAttributeValues: { | |
':eventStatus': 'hf-composition-copied', | |
':agentName': value, | |
}, | |
ProjectionExpression: 'sid, body, prospectName, agentName, date_time ,fileTransfer, meeting_date', | |
}; | |
logMessage = 'Params for Searching for Agent'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: paramsAgent }); | |
const output = await ddb | |
.query(paramsAgent, onQuery()) | |
.then((res) => res.Items); | |
logMessage = `Search result for the Agent- ${value}`; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: output }); | |
return output; | |
}; | |
const prospectNameSearch = async (value) => { | |
logMessage = 'Searching for Prospect'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: value }); | |
const paramsProspect = { | |
TableName: AWSDynamoDB, | |
IndexName: 'prospectname-eventStatus-index', | |
KeyConditionExpression: 'prospectName = :prospectName AND eventStatus =:eventStatus ', | |
ExpressionAttributeValues: { | |
':eventStatus': 'hf-composition-copied', | |
':prospectName': value, | |
}, | |
ProjectionExpression: 'sid, body, prospectName, agentName, date_time ,fileTransfer, meeting_date', | |
}; | |
logMessage = 'Params for Searching for Prospect'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: paramsProspect }); | |
const output = await ddb | |
.query(paramsProspect, onQuery()) | |
.then((res) => res.Items); | |
logMessage = `Search result for the Prospect- ${value}`; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: output }); | |
return output; | |
}; | |
const getUniqueArrayByKey = (array =[], key) => { | |
const seen = new Set(); | |
return array.filter(item => { | |
const keyValue = item.body[key]; | |
if(seen.has(keyValue)){ | |
return false; | |
} | |
seen.add(keyValue); | |
return true; | |
}) | |
} | |
if (meetingId !== '') { | |
result = await meetingIdSearch(meetingId); | |
} else if (agentName !== '' && prospectName !== '') { | |
const resultAgent = await agentSearch(agentName); | |
result = resultAgent.filter((p) => p.prospectName.includes(prospectName)); | |
logMessage = `Search result for the Agent/Prospect - ${agentName} / ${prospectName}`; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: result }); | |
} else if (agentName !== '') { | |
result = await agentSearch(agentName); | |
} else if (prospectName !== '') { | |
result = await prospectNameSearch(prospectName); | |
} | |
if (result) { | |
if (startDate && endDate) { | |
const start = new Date(startDate); | |
const end = new Date(endDate); | |
end.setDate(end.getDate() + 1); | |
result = result | |
.filter((d) => Date.parse(d.date_time) >= start && Date.parse(d.date_time) <= end); | |
logMessage = `Result is filtered between - ${startDate} and ${endDate}`; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: result }); | |
} | |
result = result.sort((a, b) => { | |
const key1 = new Date(a.date_time).getTime(); | |
const key2 = new Date(b.date_time).getTime(); | |
if (key1 > key2) return -1; | |
if (key1 < key2) return 1; | |
return 0; | |
}); | |
logMessage = 'Returned Succefully the Result'; | |
Logger.info({ logCat, logSubCat, message: logMessage, data: result }); | |
uniqueData = getUniqueArrayByKey(result, "RoomSid"); | |
Logger.info({ logCat, logSubCat, message: "Unique records based on RoomSid", data: uniqueData }); | |
} | |
return responses._200(uniqueData); | |
} catch (e) { | |
Logger.error({ logCat, logSubCat, message: 'Exception caught', err: e, alarm: true }); | |
return responses._400({ error: e.message }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment