forked from slickage/baron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
204 lines (190 loc) · 5.88 KB
/
db.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var validate = require(__dirname + '/validate');
var config = require(__dirname + '/config');
var couchapp = require('couchapp');
var ddoc = require(__dirname + '/couchapp');
var nano = require('nano')(config.couchdb.url);
var dbName = config.couchdb.name;
var BigNumber = require('bignumber.js');
var baronDb;
var instantiateDb = function () {
nano.db.get(dbName, function(err) {
if (err) {
nano.db.create(dbName, function(err) {
if (err) {
console.log('Pleases ensure that couchdb is running.');
return process.exit(1);
}
console.log('Database created.');
baronDb = nano.use(dbName);
var dbUrl = config.couchdb.url + '/' + config.couchdb.name;
couchapp.createApp(ddoc, dbUrl, function(app) {
app.push();
});
});
}
else {
baronDb = nano.use(dbName);
}
});
};
var findInvoiceAndPayments = function(invoiceId, cb) {
baronDb.view(dbName, 'invoicesWithPayments', { key:invoiceId }, function(err, body) {
if (err) { return cb(err, null, null); }
var invoice;
var paymentsArr = [];
if (body.rows.length <= 0) {
var error = new Error('Error: No invoice found.');
return cb(error, null, null);
}
body.rows.forEach(function (row) {
if (row.value.type === 'invoice') {
invoice = row.value;
}
else if (row.value.type === 'payment') {
paymentsArr.push(row.value);
}
});
return cb(err, invoice, paymentsArr);
});
};
var findPaymentById = function(paymentId, cb) {
baronDb.view(dbName, 'paymentsById', { key:paymentId }, function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var payment = body.rows[0].value;
return cb(err, payment);
}
return cb(err, null);
});
};
var findPayments = function(address, cb) {
baronDb.view(dbName, 'payments', { key:address }, function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var paymentsArr = [];
body.rows.forEach(function(row) {
if (row.value.type === 'payment') {
paymentsArr.push(row.value);
}
});
return cb(null, paymentsArr);
}
var noPaymentsFoundErr = new Error('No payments found.');
return cb(noPaymentsFoundErr, null);
});
};
var findPaymentByTxId = function(txId, cb) {
baronDb.view(dbName, 'paymentsTxId', { key:txId }, function(err, body) {
var payment = null;
if (!err && body.rows && body.rows.length > 0) {
payment = body.rows[0].value;
}
if (payment) {
return cb(null, payment);
}
else if (!payment) {
var error = new Error('No invoice matching tx_id: ' + txId);
return cb(error, null);
} else {
return cb(err, null);
}
});
};
var findInvoice = function(invoiceId, cb) {
baronDb.view(dbName, 'invoices', { key:invoiceId }, function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var invoice = body.rows[0].value;
return cb(err, invoice);
}
return cb(err, null);
});
};
var getWatchedPayments = function(cb) {
baronDb.view(dbName, 'watchedPayments', function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var paymentsArr = [];
body.rows.forEach(function(row) {
if (row.value.type === 'payment') {
paymentsArr.push(row.value);
}
});
return cb(err, paymentsArr);
}
return cb(err, null);
});
};
var getPaymentByBlockHash = function(blockHash, cb) {
baronDb.view(dbName, 'paymentsBlockHash', { key:blockHash }, function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var paymentsArr = [];
body.rows.forEach(function(row) {
if (row.value.type === 'payment') {
paymentsArr.push(row.value);
}
});
return cb(err, paymentsArr);
}
return cb(err, null);
});
};
var getLastKnownBlockHash = function(cb) {
baronDb.view(dbName, 'lastBlockHash', function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var lastKnownBlockHash = body.rows[0].value;
return cb(err, lastKnownBlockHash);
}
return cb(err, null);
});
};
var createInvoice = function(invoice, cb) {
if (validate.invoice(invoice)) {
invoice.access_token = undefined;
invoice.created = new Date().getTime();
invoice.type = 'invoice';
var balanceDue = new BigNumber(0);
invoice.line_items.forEach(function(item) {
var lineCost = new BigNumber(item.amount).times(item.quantity);
balanceDue = balanceDue.plus(lineCost);
});
invoice.balance_due = Number(balanceDue.valueOf());
baronDb.insert(invoice, cb);
}
else {
var invalidErr = new Error('The received invoice failed validation. Verify that the invoice' +
' object being sent conforms to the specifications in the API');
return cb(invalidErr, null);
}
};
var getWebhooks = function (cb) {
baronDb.view(dbName, 'webhooks', function(err, body) {
if (!err && body.rows && body.rows.length > 0) {
var webhooksArr = [];
body.rows.forEach(function(row) {
if (row.value.type === 'webhook') {
webhooksArr.push(row.value);
}
});
return cb(err, webhooksArr);
}
return cb(err, null);
});
};
var insert = function(doc, cb) { // Used to update a payment or invoice
baronDb.insert(doc, cb);
};
var destroy = function(docId, docRev, cb) { // Used to update a payment or invoice
baronDb.destroy(docId, docRev, cb);
};
module.exports = {
instantiateDb: instantiateDb,
findInvoiceAndPayments: findInvoiceAndPayments,
findPaymentById: findPaymentById,
findPayments: findPayments,
findPaymentByTxId: findPaymentByTxId,
findInvoice: findInvoice,
getWatchedPayments: getWatchedPayments,
getPaymentByBlockHash: getPaymentByBlockHash,
getLastKnownBlockHash: getLastKnownBlockHash,
createInvoice: createInvoice,
getWebhooks: getWebhooks,
insert: insert,
destroy: destroy
};