Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
249105e
update pg and sequelize versions in package.json
joshuasorkin Sep 30, 2022
3b500c8
comment out session-related code in appInitializer
joshuasorkin Sep 30, 2022
d40c76f
remove the sequelize and sequelize_ssl from database.js, the ones tha…
joshuasorkin Sep 30, 2022
621453e
change sequelize.query to this.sequelize.query
joshuasorkin Sep 30, 2022
e76824c
add default null strings for reqBody items
joshuasorkin Sep 30, 2022
6c5c15b
make insertEvent async/await
joshuasorkin Sep 30, 2022
74171c9
add try catch to insertEvent
joshuasorkin Sep 30, 2022
1a71680
use console.log
joshuasorkin Sep 30, 2022
2d6883f
add todo comment for 'event' relation
joshuasorkin Sep 30, 2022
1e23dc4
define database in worker using this.database
joshuasorkin Sep 30, 2022
dc8c301
in worker, replace all 'database' with 'this.database'
joshuasorkin Sep 30, 2022
0b5a08e
fix command calling in sms
joshuasorkin Sep 30, 2022
6b23216
in availableNotifier, move singleton assignment into constructor and …
joshuasorkin Sep 30, 2022
96620fb
add default function
joshuasorkin Sep 30, 2022
7fa1446
include default in key binding
joshuasorkin Sep 30, 2022
ffff75d
use let to declare vars
joshuasorkin Sep 30, 2022
52c9796
fix manualResponse
joshuasorkin Sep 30, 2022
994a38a
create new instance of Password()
joshuasorkin Sep 30, 2022
6749b10
change database to this.database in password.js
joshuasorkin Sep 30, 2022
025fa64
bypass admin password check until I can populate the db correctly
joshuasorkin Sep 30, 2022
3d678c5
database change
joshuasorkin Sep 30, 2022
d8d95f0
comment out callerWorkerSid attribute, per Twilio API this attribute …
joshuasorkin Sep 30, 2022
8c6f80a
additional console logging
joshuasorkin Oct 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ npm-debug.log*
*.dump
*.sh
*.ignore
*.log
586 changes: 301 additions & 285 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"passport": "^0.6.0",
"passport-local": "^1.0.0",
"path": "^0.12.7",
"pg": "^7.11.0",
"pg": "^8.7.3",
"pug": "^3.0.1",
"querystring": "^0.2.0",
"request": "^2.88.2",
"sequelize": "^5.21.1",
"sequelize": "^6.20.1",
"swagger-jsdoc": "^6.2.5",
"swagger-ui-express": "^4.5.0",
"tonegenerator": "^0.3.2",
Expand Down
5 changes: 3 additions & 2 deletions src/appInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class AppInitializer {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/other_route", require("./other_route").router);
app.use(session({ secret: process.env.SESSION_SECRET }));
//todo: re-enable this session code? or outsource session management to a SaaS like firebase?
//app.use(session({ secret: process.env.SESSION_SECRET }));
app.use(passport.initialize());
app.use(passport.session());
//app.use(passport.session());
Comment thread
minademian marked this conversation as resolved.
app.use(flash());
app.use("/public", express.static(__dirname + "/public"));
app.use(express.static("public"));
Expand Down
124 changes: 66 additions & 58 deletions src/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ require("env2")(".env");
const Sequelize = require("sequelize");

class Database {
sequelize;
sequelize_ssl;


constructor() {
if (Database._instance) {
throw new Error(
Expand All @@ -26,7 +24,7 @@ class Database {

try {
this.sequelize.authenticate();
console.log("Database onnection has been established successfully.");
console.log("Database connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}
Expand Down Expand Up @@ -60,7 +58,7 @@ class Database {
const attributes = JSON.parse(worker.attributes);
const contact_uri = attributes.contact_uri;
console.log("createWorker: now attempting sequelize insert worker");
return sequelize.query(
return this.sequelize.query(
"insert into worker (contact_uri,sid) values ('" +
contact_uri +
"','" +
Expand All @@ -71,11 +69,11 @@ class Database {

createWorkerApply(contact_uri, friendlyName, authenticateCode) {
console.log("createWorkerApply: now attempting sequelize insert row");
return sequelize.query(
return this.sequelize.query(
"insert into workerapply (contact_uri,friendlyName,authenticateCode,status) values(?,?,?,'incomplete')",
{
replacements: [contact_uri, friendlyName, authenticateCode],
type: sequelize.QueryTypes.INSERT,
type: this.sequelize.QueryTypes.INSERT,
}
);
}
Expand All @@ -93,14 +91,14 @@ class Database {
}

getRowFromWorkerTable(contact_uri) {
return sequelize.query(
return this.sequelize.query(
"select * from worker where contact_uri='" + contact_uri + "'",
{ type: sequelize.QueryTypes.SELECT }
{ type: this.sequelize.QueryTypes.SELECT }
);
}

async updateWorkerContact_uri(oldContact_uri, newContact_uri) {
var result = await sequelize.query(
var result = await this.sequelize.query(
"update worker set contact_uri='" +
newContact_uri +
"' where contact_uri='" +
Expand Down Expand Up @@ -129,9 +127,9 @@ class Database {
}

async getWorkerIdFromSid(workerSid) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from worker where sid='" + workerSid + "'",
{ type: sequelize.QueryTypes.SELECT }
{ type: this.sequelize.QueryTypes.SELECT }
);
console.log(
"getWorkerIdFromSid: selectResult: " + JSON.stringify(selectResult)
Expand All @@ -147,11 +145,11 @@ class Database {
}

async getFunctionalityStatus(functionality) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from systemstatus where function=?",
{
replacements: [functionality],
type: sequelize.QueryTypes.SELECT,
type: this.sequelize.QueryTypes.SELECT,
}
);
if (selectResult.length == 0) {
Expand Down Expand Up @@ -193,59 +191,60 @@ class Database {

insertConferenceParticipant(workerSid, callSid, conferenceSid) {
console.log("insertConferenceParticipant");
return sequelize.query(
return this.sequelize.query(
"insert into conference_participant (workerSid,callSid,conferenceSid) " +
"values(?,?,?)",
{
replacements: [workerSid, callSid, conferenceSid],
type: sequelize.QueryTypes.INSERT,
type: this.sequelize.QueryTypes.INSERT,
}
);
}

insertCallSidWorkerSid(callSid, workerSid) {
console.log("insertCallSidWorkerSid");
return sequelize.query(
return this.sequelize.query(
"insert into callsid_workersid (callsid,workersid) " + "values(?,?)",
{
replacements: [callSid, workerSid],
type: sequelize.QueryTypes.INSERT,
type: this.sequelize.QueryTypes.INSERT,
}
);
}

insertAdminPassword(workerId, passwordHash, adminTaskId) {
return sequelize.query(
return this.sequelize.query(
"insert into adminPassword " +
"(workerId,passwordHash,adminTaskId) " +
"values " +
"(?,?,?)",
{
replacements: [workerId, passwordHash, adminTaskId],
type: sequelize.QueryTypes.INSERT,
type: this.sequelize.QueryTypes.INSERT,
}
);
}

updateAdminPassword(workerId, passwordHash, adminTaskId) {
return sequelize.query(
return this.sequelize.query(
"update adminPassword " +
"set passwordHash=? " +
"where workerId=? " +
"and adminTaskId=?",
{
replacements: [passwordHash, workerId, adminTaskId],
type: sequelize.QueryTypes.UPDATE,
type: this.sequelize.QueryTypes.UPDATE,
}
);
}

async getAdminTaskId(adminTask) {
var selectResult = await sequelize.query(
console.log({adminTask});
var selectResult = await this.sequelize.query(
"select * from adminTask " + "where adminTask=?",
{
replacements: [adminTask],
type: sequelize.QueryTypes.SELECT,
type: this.sequelize.QueryTypes.SELECT,
}
);
if (selectResult.length == 0) {
Expand All @@ -260,11 +259,11 @@ class Database {

//todo: this will need to get updated after we establish the 'canceled' unique constraint
async getPasswordHash(workerId, adminTaskId) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from adminPassword " + "where workerId=? and adminTaskId=?",
{
replacements: [workerId, adminTaskId],
type: sequelize.QueryTypes.SELECT,
type: this.sequelize.QueryTypes.SELECT,
}
);
if (selectResult.length == 0) {
Expand All @@ -281,7 +280,7 @@ class Database {
if (id == null) {
throw workerSid + " does not exist in worker table";
} else {
return sequelize.query(
return this.sequelize.query(
"update available_notification_request set notification_sent=true" +
" where worker_id=" +
id +
Expand All @@ -293,12 +292,12 @@ class Database {
//we pass in workerSid here because we don't want the worker who just went to Idle to get
//a notification of an available worker (since calling themselves isn't an option)
iterateThroughUnsentNotificationsForMessaging(callback, workerSid) {
sequelize
this.sequelize
.query(
"select * from available_notification_request_worker where notification_sent=false and sid!='" +
workerSid +
"'",
{ type: sequelize.QueryTypes.SELECT }
{ type: this.sequelize.QueryTypes.SELECT }
)
.then(function (result) {
console.log(result);
Expand All @@ -320,11 +319,11 @@ class Database {
}

async getWorkerSidFromCallSid(callSid) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from callsid_workersid where callsid=?",
{
replacements: [callSid],
type: sequelize.QueryTypes.SELECT,
type: this.sequelize.QueryTypes.SELECT,
}
);
console.log(
Expand All @@ -346,13 +345,13 @@ class Database {
//or array of workerSids for multiple participants
//eventually it should just have a single return type of array
async getOtherParticipantWorkerSid(conferenceSid, callSid) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from conference_participant " +
"where conferenceSid=? and " +
"callSid!=?",
{
replacements: [conferenceSid, callSid],
type: sequelize.QueryTypes.SELECT,
type: this.sequelize.QueryTypes.SELECT,
}
);
console.log(
Expand All @@ -376,25 +375,34 @@ class Database {
}
}

insertEvent(reqBody) {
return sequelize.query(
"insert into event " +
"(eventType,eventDescription,timestamp,resourceType,resourceSid,workerSid,data) " +
"values " +
"(?,?,?,?,?,?,?)",
{
replacements: [
reqBody.EventType,
reqBody.EventDescription,
reqBody.Timestamp,
reqBody.ResourceType,
reqBody.ResourceSid,
reqBody.WorkerSid,
reqBody.Data,
],
type: sequelize.QueryTypes.INSERT,
}
);
//todo: I'm getting the error:
// SequelizeDatabaseError: relation "event" does not exist
//verify whether the schema I gave Mina has this relation in it
async insertEvent(reqBody) {
try{
let result = await this.sequelize.query(
"insert into event " +
"(eventType,eventDescription,timestamp,resourceType,resourceSid,workerSid,data) " +
"values " +
"(?,?,?,?,?,?,?)",
{
replacements: [
reqBody.EventType || "",
reqBody.EventDescription || "",
reqBody.Timestamp || "",
reqBody.ResourceType || "",
reqBody.ResourceSid || "",
reqBody.WorkerSid || "",
reqBody.Data || JSON.stringify({data:null}),
],
type: this.sequelize.QueryTypes.INSERT,
}
);
return result;
}
catch(err){
console.log(`error: ${err}`);
}
}

insertConference(
Expand All @@ -404,7 +412,7 @@ class Database {
outboundWorkerSid,
conferenceSid
) {
return sequelize.query(
return this.sequelize.query(
"insert into conference " +
"(inboundCallSid,outboundCallSid,inboundWorkerId,outboundWorkerId,conferenceSid) " +
"values " +
Expand All @@ -420,20 +428,20 @@ class Database {
outboundWorkerSid,
conferenceSid,
],
type: sequelize.QueryTypes.INSERT,
type: this.sequelize.QueryTypes.INSERT,
}
);
}

async getMembershipRequest(contact_uri, authenticateCode) {
var selectResult = await sequelize.query(
var selectResult = await this.sequelize.query(
"select * from workerapply where contact_uri='" +
contact_uri +
"' and status='incomplete' " +
"and authenticatecode='" +
authenticateCode +
"'",
{ type: sequelize.QueryTypes.SELECT }
{ type: this.sequelize.QueryTypes.SELECT }
);
console.log(
"getMembershipRequest: selectResult: " + JSON.stringify(selectResult)
Expand All @@ -446,11 +454,11 @@ class Database {
}

async updateMembershipRequestToComplete(contact_uri) {
return sequelize.query(
return this.sequelize.query(
"update workerapply " + "set status='complete' " + "where contact_uri=?",
{
replacements: [contact_uri],
type: sequelize.QueryTypes.UPDATE,
type: this.sequelize.QueryTypes.UPDATE,
}
);
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib/availableNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ var database = Database.getInstance();
class AvailableNotifier {
constructor() {
this.phoneNumberRegex = new RegExp("^\\+\\d+$");
this.database = Database.getInstance();
}
async create(workerSid) {
var result = await database.createAvailableNotificationRequest(workerSid);
var result = await this.database.createAvailableNotificationRequest(workerSid);
return result;
}

async updateToSent(workerSid) {
var result = await database.updateNotificationToSent(workerSid);
var result = await this.database.updateNotificationToSent(workerSid);
return result;
}

iterateSend(workerSid) {
var result = database.iterateThroughUnsentNotificationsForMessaging(
var result = this.database.iterateThroughUnsentNotificationsForMessaging(
this.send.bind(this),
workerSid
);
Expand All @@ -48,7 +49,7 @@ class AvailableNotifier {
})
.then((message) => {
console.log("send: message.sid: " + message.sid);
database.updateNotificationToSent(callbackParam.sid);
this.database.updateNotificationToSent(callbackParam.sid);
});
} else {
console.log(contact_uri + " is not a valid phone number.");
Expand Down
Loading