From 2395ad3b66832e4df8673339cd2ffbad1de940b7 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 25 Sep 2017 17:10:34 +0100 Subject: [PATCH] implemented completed deposit (3 confirmations) notifications --- app.js | 43 +++++++++++++++++++++++++++++-- sql/ddl.sql | 13 +++++++++- templates/ondeposit.completed.txt | 4 +++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 templates/ondeposit.completed.txt diff --git a/app.js b/app.js index 409965c..41e5099 100644 --- a/app.js +++ b/app.js @@ -28,6 +28,7 @@ const messageTemplates = {}; const templateNames = [ 'onbalance', 'ondeposit', + 'ondeposit.completed', 'ongild', 'ongild.insufficientfunds', 'onsendtip', @@ -148,6 +149,41 @@ const createOrGetUserId = (username, callback) => { ], callback); }; +const processCompletedDeposits = (callback) => { + async.waterfall([ + (cb) => { + db.query('SELECT C.DepositId, D.Amount, U.Username, U.Balance FROM CompletedDepositConfirmations C JOIN Deposits D ON D.Id = C.DepositId JOIN Users U ON U.Id = C.UserId', cb); + }, + (res, fields, cb) => { + if (res.length > 0) { + return async.eachSeries(res, (completedDeposit, ecb) => { + setTimeout(() => { + sendPMUsingTemplate('ondeposit.complete', { amount: completedDeposit.Amount, balance: completedDeposit.Balance }, + 'Deposit completed!', completedDeposit.Username, (err) => { + if (err) { + return ecb(err, null); + } + + // remove the entry from the DB + return db.query('DELETE FROM CompletedDepositConfirmations WHERE DepositId = ?', [C.DepositId], (ierr) => { + if (ierr) { + return ecb(ierr, null); + } + + // success + return ecb(null, true); + }); + }); + }, 2000); // Wait 2 seconds between each request (if there are multiple to send) + // TODO: Implement inserting messages into a pending message queue instead + }, cb); + } + + return cb(null, null); + } + ], callback); +}; + const getBalance = (userId, callback) => { db.query('SELECT Balance FROM Users WHERE Id = ?', [userId], (err, res) => { if (err) { @@ -953,7 +989,7 @@ const processMessage = function(message, callback) { const runBot = () => { async.waterfall([ (cb) => { - if (!accessTokenTime || moment.duration(moment().diff(accessTokenTime)).asMinutes() >= 55) { + if (!accessTokenTime || moment.duration(moment().diff(accessTokenTime)).asMinutes() >= 59) { // remove old or expired tokens // TODO: Implement refreshToken if (fs.existsSync(config.accessTokenPath)) { @@ -975,7 +1011,10 @@ const runBot = () => { }, (token, cb) => { globalAccessToken = token; - retrieveUnreadMessages(token, cb); + processCompletedDeposits(cb); + }, + (success, cb) => { + retrieveUnreadMessages(globalAccessToken, cb); }, (unread, cb) => { async.eachSeries(unread, (message, ecb) => { diff --git a/sql/ddl.sql b/sql/ddl.sql index ff984ba..b703bb4 100644 --- a/sql/ddl.sql +++ b/sql/ddl.sql @@ -52,6 +52,15 @@ CREATE TABLE PendingMessageQueue PRIMARY KEY `PK_PendingMessageId` (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; +CREATE TABLE CompletedDepositConfirmations +( + `DepositId` BIGINT UNSIGNED NOT NULL, + `UserId` BIGINT UNSIGNED NOT NULL, + PRIMARY KEY `PK_DepositConfirmationId` (`DepositId`), + FOREIGN KEY `FK_CompletedDepositConfirmation` (`DepositId`) REFERENCES `Deposits` (`Id`), + FOREIGN KEY `FK_CompletedDepositUser` (`UserId`) REFERENCES `Users` (`Id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; + CREATE TABLE Deposits ( `Id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, @@ -86,6 +95,7 @@ FOR EACH ROW BEGIN IF NEW.Confirmations >= 3 THEN UPDATE Users U SET U.Balance = U.Balance + NEW.Amount WHERE U.Id = NEW.UserId; + INSERT INTO CompletedDepositConfirmations (DepositId, UserId) VALUES (NEW.Id, NEW.UserId); END IF; END; @@ -94,7 +104,8 @@ CREATE TRIGGER `Trg_OnDepositUpdated` FOR EACH ROW BEGIN IF OLD.Confirmations < 3 AND NEW.Confirmations >= 3 THEN - UPDATE Users U SET U.Balance = U.Balance + NEW.Amount WHERE U.Id = NEW.UserId; + UPDATE Users U SET U.Balance = U.Balance + OLD.Amount WHERE U.Id = OLD.UserId; + INSERT INTO CompletedDepositConfirmations (DepositId, UserId) VALUES (OLD.Id, OLD.UserId); END IF; END; // diff --git a/templates/ondeposit.completed.txt b/templates/ondeposit.completed.txt new file mode 100644 index 0000000..339a1ca --- /dev/null +++ b/templates/ondeposit.completed.txt @@ -0,0 +1,4 @@ +Great news! Your **{amount} LBC** deposit was completed successfully with 3 confirmations. Your new balance is **{balance} LBC**. + +---- +[^How ^to ^use]({how_to_use_url}) ^• [^What ^is ^LBRY?](https://lbry.io/faq/what-is-lbry) ^• ^r/lbry \ No newline at end of file