From fc4d25cbb08b32400236ee238ab6b6695ac2506f Mon Sep 17 00:00:00 2001 From: Ian Geckeler Date: Mon, 8 Jul 2019 15:22:26 -0400 Subject: [PATCH] added functionality for pg create --- .../functions/databases/postgresql/send.js | 14 +++++++ functions/databases/postgres/send.js | 41 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/__tests__/functions/databases/postgresql/send.js b/__tests__/functions/databases/postgresql/send.js index e69de29..77387d0 100644 --- a/__tests__/functions/databases/postgresql/send.js +++ b/__tests__/functions/databases/postgresql/send.js @@ -0,0 +1,14 @@ +// const client = require("../../../../index")(process.env.SID, process.env.AUTH, { +// isPostgres: true, +// connectionURI: "postgres://student:ilovetesting@localhost/twoauthtests" +// }); + +// // client.create("ian", "+17604207520"); + +// client +// .send("ian") +// .then(res => console.log(res)) +// .catch(err => console.log(err)); +// // describe("Tests for Postgres Send", () => { + +// // }); diff --git a/functions/databases/postgres/send.js b/functions/databases/postgres/send.js index 8dc374a..be943f0 100644 --- a/functions/databases/postgres/send.js +++ b/functions/databases/postgres/send.js @@ -1,15 +1,42 @@ -module.exports = function(userID, phone) { +module.exports = function(userID) { + const { pgConnect, client } = this; return new Promise((resolve, reject) => { - this.pgConnect() + pgConnect() .then(({ database, done }) => { - // pgClient.query... blah blah logic - //invoke done before your resolve this promise - done(); + const query = "SELECT * FROM twoauthusers WHERE userID=$1"; + const values = [String(userID)]; + database.query(query, values, (err, res) => { + if (err) reject(err); + const { sid, phone } = res.rows[0]; + if (!sid) + reject(new Error("SID Error: No SID exists for this user.")); + if (!phone) + reject( + new Error( + "Phone Number Error: No phone number exists for this user." + ) + ); + //invoke done before your resolve this promise + client.verify + .services(sid) + .verifications.create({ + to: phone, + channel: "sms" + }) + .then(verification => { + done(); + resolve(verification); + }) + .catch(err => { + done(); + reject(err); + }); + }); }) - .catch((err, done) => { - //invoke done before you reject + .catch(err => { done(); reject(err); + //"userID Error: This userID has not been created yet." }); }); };