-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-request.js
116 lines (92 loc) · 3 KB
/
random-request.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
const axios = require('axios').default;
//const HOST = 'http://127.0.0.1:5003';
const HOST = 'https://pembayaran.ruangkarya.id';
//'http://127.0.0.1:5003';
var list_account = [];
var list_transaction = ['deposit', 'get-balance', 'withdraw', 'transfer'];
axios.get(HOST + '/wallet/list-wallet').then(result => {
list_account = result.data.data;
console.log(list_account);
random_request();
}).catch(error => {
console.log(error);
});
function random_request(){
/*do{
}while(true);*/
for(var i = 0; i < 1000; i++){
var index_transaction = Math.floor(Math.random() * list_transaction.length);
var transaction = list_transaction[index_transaction];
//var transaction = list_transaction[3];
if(transaction === 'deposit'){
var user_index = Math.floor(Math.random() * list_account.length);
var id_wallet = list_account[user_index].id;
var amount = Math.floor(Math.random() * 500);
deposit(id_wallet, amount);
}else if(transaction === 'withdraw'){
var user_index = Math.floor(Math.random() * list_account.length);
var id_wallet = list_account[user_index].id;
var amount = Math.floor(Math.random() * 500);
withdraw(id_wallet, amount);
}else if(transaction === 'get-balance'){
var user_index = Math.floor(Math.random() * list_account.length);
var id_wallet = list_account[user_index].id;
get_balance(id_wallet);
}else if(transaction === 'transfer'){
var user_index = Math.floor(Math.random() * list_account.length);
var from = list_account[user_index].id;
user_index = Math.floor(Math.random() * list_account.length);
var to = list_account[user_index].id;
var amount = Math.floor(Math.random() * 500);
if(from != to){
transfer(from, to, amount);
}
}else{
console.log(transaction);
}
}
}
function deposit(id_wallet, amount){
var body = {
id_wallet: id_wallet,
amount: amount
};
axios.post(HOST + '/wallet/deposit', body).then(result => {
console.log(result.data);
}).catch(error => {
console.log(error.message);
});
}
function withdraw(id_wallet, amount){
var uri = HOST + '/wallet/' + id_wallet + '/withdraw/' + amount;
axios.get(uri).then(result => {
console.log(result.data);
}).catch(error => {
console.log(error);
});
}
function get_balance(id_wallet){
var uri = HOST + '/wallet/' + id_wallet + '/balance';
axios.get(uri).then(result => {
console.log(result.data);
}).catch(error => {
console.log(error);
});
}
function transfer(from, to, amount){
if(from === to){
console.log(from + ' to ' + to);
return;
}else{
var body = {
from: from,
to: to,
amount: amount
};
axios.post(HOST + '/wallet/transfer', body).then(result => {
console.log(result.data);
}).catch(error => {
console.log(error);
});
}
}