aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-03-21 15:12:18 -0400
committerAdam Soltys <[email protected]>2021-03-21 15:27:25 -0400
commit4fa6b3aeca3730a9c8235a001cf6862ce7ce8e0c (patch)
tree2a08c1b1b189ff7b2b1542c6e7f090260cefb4a5 /lib
parent9c9e909481555323ca027c9ede13e724c09b9b73 (diff)
downloadcoinos-server-4fa6b3aeca3730a9c8235a001cf6862ce7ce8e0c.tar.gz
coinos-server-4fa6b3aeca3730a9c8235a001cf6862ce7ce8e0c.zip
handle commas in email amount
Diffstat (limited to 'lib')
-rw-r--r--lib/mail.js134
1 files changed, 68 insertions, 66 deletions
diff --git a/lib/mail.js b/lib/mail.js
index 353954f1..02c7517d 100644
--- a/lib/mail.js
+++ b/lib/mail.js
@@ -13,81 +13,83 @@ function openInbox(cb) {
imap.once("ready", function() {
openInbox(function(err, box) {
if (err) throw err;
- setInterval(() => {
- let f = imap.seq.fetch(`${box.messages.total - 5}:*`, {
- bodies: ["TEXT"],
- struct: true
- });
-
- f.on("message", function(msg, seqno) {
- const prefix = "(#" + seqno + ") ";
- msg.on("body", function(stream, info) {
- let buffer = "";
- stream.on("data", function(chunk) {
- //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'}));
- buffer += chunk.toString("utf8");
- });
- stream.once("end", async function() {
- if (!emails[seqno] && buffer.includes(config.imap.secret)) {
- let amount = buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1");
- emails[seqno] = amount;
- console.log(
- seqno,
- buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1")
- );
+ let f = imap.seq.fetch(`${box.messages.total - 5}:*`, {
+ bodies: ["TEXT"],
+ struct: true
+ });
+ f.on("message", function(msg, seqno) {
+ const prefix = "(#" + seqno + ") ";
+ msg.on("body", function(stream, info) {
+ let buffer = "";
+ stream.on("data", function(chunk) {
+ //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'}));
+ buffer += chunk.toString("utf8");
+ });
+ stream.once("end", async function() {
+ if (!emails[seqno] && buffer.includes(config.imap.secret)) {
+ let cad = parseFloat(buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1").replace(",", ""));
+ emails[seqno] = cad;
- try {
- await db.transaction(async transaction => {
- const account = await db.Account.findOne({
- where: {
- user_id: config.imap.user_id,
- asset: config.liquid.btcasset,
- pubkey: null
- },
- include: {
- model: db.User,
- as: "user"
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ try {
+ await db.transaction(async transaction => {
+ const account = await db.Account.findOne({
+ where: {
+ user_id: config.imap.user_id,
+ asset: config.liquid.btcasset,
+ pubkey: null
+ },
+ include: {
+ model: db.User,
+ as: "user"
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
- let { user } = account;
+ let { user } = account;
- let rate = app.get("rates")["CAD"];
- console.log(rate);
+ let rate = app.get("rates")["CAD"];
+ let amount = Math.round((cad / rate) * 100000000)
- await account.increment({ balance: amount }, { transaction });
- let payment = await db.Payment.create({
- account_id: account.id,
- user_id: account.user_id,
- hash: `Interac deposit: ${amount} CAD`,
- amount: Math.round((amount / rate) * 100000000),
- currency: "CAD",
- rate,
- received: true,
- tip: 0,
- confirmed: true,
- address: "",
- network: "COINOS"
- });
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ await account.increment({ balance: amount }, { transaction });
- emit(user.username, "account", account);
- emit(user.username, "payment", payment);
+ let payment = await db.Payment.create({
+ account_id: account.id,
+ user_id: account.user_id,
+ hash: `Interac deposit: ${amount} CAD`,
+ amount,
+ currency: "CAD",
+ rate,
+ received: true,
+ tip: 0,
+ confirmed: true,
+ address: "",
+ network: "COINOS"
});
- } catch (e) {
- console.log(e);
- }
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
+
+ emit(user.username, "account", account);
+ emit(user.username, "payment", payment);
+
+ l.info("interac deposit detected for ", user.username, cad, amount);
+ });
+ } catch (e) {
+ console.log(e);
}
- });
- });
- msg.once("end", function() {
- // console.log(prefix + 'Finished');
+ }
});
+ });
+ msg.once("end", function() {
+ // console.log(prefix + 'Finished');
+ });
- imap.seq.addFlags(seqno, ["Seen"]);
+ imap.seq.addFlags(seqno, ["Seen"]);
+ });
+ setInterval(() => {
+ f = imap.seq.fetch(`${box.messages.total - 5}:*`, {
+ bodies: ["TEXT"],
+ struct: true
});
}, 3000);
});