Skip to content

Commit

Permalink
Add implementation to send call records to RDS
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr1sh1 committed Mar 2, 2024
1 parent 0cfc5d2 commit 4f51ffe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
51 changes: 48 additions & 3 deletions functions/statusCallback.protected.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { ServerlessFunctionSignature } from '@twilio-labs/serverless-runtime-types/types';
import { StatusCallbackServerlessEventObject, TwilioEnvironmentVariables } from './types/interfaces';
import { InsertParams, StatusCallbackServerlessEventObject, TwilioEnvironmentVariables } from './types/interfaces';
import { connect, config, Request } from 'mssql';

export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, StatusCallbackServerlessEventObject> = function(
const constructRequest = (request: Request, params: InsertParams) => {
const columns = Object.keys(params).join(", ")
const values = "@" + Object.keys(params).join(", @")
const builtRequest = Object.entries(params).reduce((req, [key, value]) => req.input(key, value), request)

return { columns, values, builtRequest }
}

export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, StatusCallbackServerlessEventObject> = async function(
context,
event,
callback,
Expand All @@ -12,7 +21,43 @@ export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, St
const callStartTimestamp = decodeURIComponent(event.request.cookies.callStartTimestamp)
const callEndTimestamp = event.Timestamp
const callDurationInSeconds = event.CallDuration
// Send into AWS RDS

const serverConfig: config = {
user: context.RDS_USER,
password: context.RDS_PASSWORD,
server: context.RDS_SERVER,
port: +context.RDS_PORT,
options: {
encrypt: false,
trustServerCertificate: false
}
};

const pool = await connect(serverConfig)

let insertParams: InsertParams = {
callerNumber,
callStartTimestamp,
callEndTimestamp,
callDurationInSeconds
}

if (logFileName !== "null") {
insertParams = {
...insertParams,
logFileName
}
}

const { columns, values, builtRequest } = constructRequest(pool.request(), insertParams)

let sqlQuery = `
INSERT INTO CallRecords (${columns})
VALUES (${values})
`

await builtRequest.query(sqlQuery)
await pool.close()
}
const response = new Response();
return callback(null, response)
Expand Down
8 changes: 8 additions & 0 deletions functions/types/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ export interface TwilioEnvironmentVariables extends EnvironmentVariables {
RDS_SERVER: string;
RDS_PORT: string;
}

export interface InsertParams {
callStartTimestamp: string;
callEndTimestamp: string;
callDurationInSeconds: string;
callerNumber: string;
logFileName?: string;
}

0 comments on commit 4f51ffe

Please sign in to comment.