-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.js
More file actions
144 lines (120 loc) · 3.95 KB
/
scripts.js
File metadata and controls
144 lines (120 loc) · 3.95 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
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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var schedule = require('node-schedule');
var logger = require('./lib/logger');
var config = require('./config/config');
var Account = require('./models/account');
var Contract = require('./models/contract');
var Transaction = require('./models/transaction');
// Mongo
mongoose.Promise = global.Promise;
mongoose.connect(config.database.url + '/' + config.database.db);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// We're connected!
logger.log("info", "Connected correctly to database");
});
// Title
console.log("/*************************\n * EthApi blockchain scripts v0.1\n*************************/");
// Constructor
function Script() {
}
// Users list
Script.prototype.users = function() {
logger.info('Users list');
Account.find({}, function (err, accounts) {
if(err) return next(err);
console.log(accounts);
process.exit(1);
});
};
// Users update
Script.prototype.userUpdate = function(username, field, value) {
logger.info("Change '" + field + "' for user: " + username);
Account.findOne({'username': username}, function (err, account) {
if(err) return next(err);
if (account) {
if (account[field]) {
// Set data
var data = {};
data[field] = value;
// Update data
Account.update({'_id': account._id}, {'$set': data}, function(err, resp) {
if(err) return next(err);
console.log(field + " updated");
process.exit(1);
});
} else {
logger.error('Field not found');
process.exit(1);
}
} else {
logger.error('User not found');
process.exit(1);
}
});
};
// Transaction reset
Script.prototype.transactionReset = function(id) {
logger.info('Transaction reset: ' + id);
// Set data
var data = {};
data['process'] = 0;
data['tx_hash'] = null;
data['status'] = 0;
Transaction.update({'_id': id}, {'$set': data}, function(err, resp) {
if(err) return next(err);
console.log("Transaction reseted");
process.exit(1);
});
};
// Users delete
Script.prototype.userUpdate = function(id) {
logger.info("Delete user: " + id);
Account.remove({ _id: id }, function (err, resp) {
if(err) return next(err);
logger.info("Deleted");
process.exit(1);
})
};
// Check command
if (typeof(process.argv[2]) == "undefined") {
logger.error('No command');
process.exit(1);
}
var command = process.argv[2];
logger.error(command);
switch (command) {
case 'users:list':
var script = new Script();
script.users();
break;
case 'user:update':
var arg1 = process.argv[3];
var arg2 = process.argv[4];
var arg3 = process.argv[5];
var script = new Script();
script.userUpdate(arg1, arg2, arg3);
break;
case 'user:delete':
var arg1 = process.argv[3];
var script = new Script();
script.userDelete(arg1);
break;
case 'transaction:reset':
var arg1 = process.argv[3];
var script = new Script();
script.transactionReset(arg1);
break;
default:
logger.error('Not a valid command: ' + command);
break;
}