-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_test_data.js
More file actions
49 lines (44 loc) · 1.46 KB
/
init_test_data.js
File metadata and controls
49 lines (44 loc) · 1.46 KB
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
// This script adds hardcoded data to the database just for testing
const mongoose = require('mongoose');
require('./models/user');
const User = mongoose.model('User');
const dotenv = require('dotenv');
dotenv.config();
const Poloniex = require('poloniex-api-node');
// connecting to mongo
mongoose.promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI);
User.remove({}, (err) => {
if(err) console.log(err);
console.log('Cleaned previous users.')
});
let user = new User({
poloniex_key: process.env.POLONIEX_KEY,
poloniex_secret: process.env.POLONIEX_SECRET,
balances: [],
movements: []
});
user.save((err, user) => {
if(err) console.log(err);
else console.log('Created user');
});
User.update({_id: user._id}, {$inc: {nonce: 1}}, (err, raw) => {
console.log('Updated nonce');
});
let poloniex = new Poloniex(user.poloniex_key, user.poloniex_secret);
poloniex.returnBalances((err, balances) => {
let new_balances = [];
for(let balance in balances){
if (balances.hasOwnProperty(balance)) {
if(balances[balance] > 0) new_balances.push({currency: balance, amount: balances[balance]});
}
}
User.update({_id: user._id},
{$set: {balances: new_balances}}, (err, raw) => {
if(err) console.log(err);
else {
console.log('Added balances');
mongoose.disconnect();
}
});
});