1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
(function() {
var Promise, config, db, moment, txid;
Promise = require('bluebird');
db = require('../redis');
config = require("../config");
moment = require('moment');
txid = function(transaction, user) {
if (transaction.match(/[a-z]/i)) {
return transaction;
}
return user + ":transactions:" + transaction;
};
module.exports = {
index: function(req, res) {
return res.render('transactions/index', {
user: req.params.user,
currency: req.session.user.currency,
layout: 'layout',
navigation: true,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
},
json: function(req, res) {
var pattern, promises, result, x;
result = {
'transactions': []
};
promises = [];
pattern = req.params.user;
if (req.session.user.username === 'admin') {
x = '*';
}
return db.keysAsync(pattern + ":transactions").then(function(keys) {
return Promise.all(keys.map(function(key) {
var user;
user = key.substr(0, key.indexOf(':'));
return db.lrangeAsync(key, 0, -1).then(function(transactions) {
return Promise.all(transactions.map(function(transaction) {
return db.hgetallAsync(txid(transaction, user)).then(function(transaction) {
if (transaction) {
transaction.user = user;
return result.transactions.push(transaction);
}
});
}));
});
}));
}).then(function() {
res.write(JSON.stringify(result));
return res.end();
});
},
create: function(req, res) {
var finish;
finish = function() {
db.hgetall("user:" + req.params.user.toLowerCase(), function(err, user) {
return res.render('transactions/notification', {
layout: 'mail',
amount: (req.body.received * req.body.exchange).toFixed(2).toString() + ' ' + user.currency,
address: req.body.address,
txid: req.body.txid,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
}, function(err, html) {
var content, from_email, helper, mail, request, sg, subject, to_email;
helper = require('sendgrid').mail;
from_email = new helper.Email('[email protected]');
to_email = new helper.Email(user.email);
subject = 'Payment Received';
content = new helper.Content('text/html', html);
mail = new helper.Mail(from_email, subject, to_email, content);
sg = require('sendgrid')(config.sendgrid_token);
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
return sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
return console.log(response.headers);
});
});
});
res.write(JSON.stringify(req.body));
return res.end();
};
db.watch(req.body.txid);
return db.exists(req.body.txid, function(err, result) {
var multi;
if (result) {
return finish();
}
multi = db.multi();
multi.hmset(req.body.txid, req.body);
multi.rpush(req.params.user + ":transactions", req.body.txid);
return multi.exec(function(err, replies) {
return finish();
});
});
},
update: function(req, res) {
return db.hset(req.params.txid, 'notes', req.body.notes, function() {
return res.end();
});
},
"delete": function(req, res) {
return db.del(req.params.txid, function() {
return db.lrem(req.params.user + ":transactions", 0, req.params.txid, function() {
return res.end();
});
});
}
};
}).call(this);
|