-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzendomo.js
More file actions
257 lines (244 loc) · 7.91 KB
/
Copy pathzendomo.js
File metadata and controls
257 lines (244 loc) · 7.91 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const fetch = require('isomorphic-fetch');
// Welcome to Charlies house of blockchain^tm related fun.
// To use this library, require the file as 'const zendomo = require('PATH')' and use zendomo.methodName(args).
// Have fun.
// === create a user (when they sign up) ===
const createUser = async (id, firstName, lastName) => {
const first = await fetch('http://localhost:3000/api/Trader', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: id,
firstName: firstName,
lastName: lastName,
tokens: 0,
credits: 50
}),
mode: 'cors'
})
.then( () => console.log('<------Sent to zendomo------>'));
return true;
}
// === get one user, filtered by id ===
const getOneUser = async (id) => {
const result = await fetch('http://localhost:3000/api/Trader/' + id, {
headers: {
'Accept': 'application/json',
},
method: 'GET',
})
.then(res => res.json())
.then(res => {return res});
return result;
}
// === delete one user, filtered by id ===
const deleteOneUser = async (id) => {
fetch('http://localhost:3000/api/Trader/' + id, {
headers: {
'Accept': 'application/json',
},
method: 'DELETE',
})
.then(res => res.json())
.then(res => console.log('<------Deleted user from zendomo------>\n', res));
return true;
}
// === edit one user, filtered by id ===
const editOneUser = async (id, firstName, lastName) => {
fetch('http://localhost:3000/api/Trader/' + id, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'PUT',
credentials: 'same-origin',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: id,
firstName: firstName,
lastName: lastName,
tokens: 0,
credits: 0
})
})
.then(res => res.json())
.then(res => console.log('<------Editted user from zendomo------>\n', res));
return true;
}
// === get all users (admin tool) ===
const getAllUsers = async () => {
const users = await fetch('http://localhost:3000/api/Trader', {
headers: {
'Accept': 'application/json',
},
method: 'GET',
})
.then(res => res.json())
.then(res => {
console.log('<------Users from zendomo------>\n')
return res
});
return users;
}
// === transfer funds between users ===
const transferFunds = async (senderID, receiverID, ammount) => {
const sender = await getOneUser(senderID);
const receiver = await getOneUser(receiverID);
if (sender.tokens > ammount) {
const doFirst = await fetch('http://localhost:3000/api/Trader/' + senderID, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'PUT',
credentials: 'same-origin',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: sender.id,
firstName: sender.firstName,
lastName: sender.lastName,
tokens: sender.tokens - ammount,
credits: sender.credits
})
});
const thenDo = await fetch('http://localhost:3000/api/Trader/' + receiverID, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'PUT',
credentials: 'same-origin',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: receiver.id,
firstName: receiver.firstName,
lastName: receiver.lastName,
tokens: receiver.tokens,
credits: receiver.credits + ammount
})
})
.then(console.log(senderID + ' sent ' + receiverID + ' this much zen: ' + ammount));
return true;
}
return false;
}
// === admin tool to add funds to a person ===
const addFunds = async (id, ammount) => {
const person = await getOneUser(id)
.then( response => {
fetch('http://localhost:3000/api/Trader/' + response.tradeId, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
method: 'PUT',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: response.tradeId,
firstName: response.firstName,
lastName: response.lastName,
tokens: ammount,
credits: response.credits
}),
mode: 'cors'
});
});
return true;
}
// === admin tool to tip a user ===
const tipUser = async (id, ammount) => {
const person = await getOneUser(id)
.then( response => {
fetch('http://localhost:3000/api/Trader/' + response.tradeId, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
method: 'PUT',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: response.tradeId,
firstName: response.firstName,
lastName: response.lastName,
tokens: response.tokens + ammount,
credits: response.credits
}),
mode: 'cors'
});
});
return true;
}
// === user tool to purchase a product/service ===
const purchase = async (id, price) => {
let success= false;
const person = await getOneUser(id)
.then( response => {
if (response.tokens >= price) {
console.log('blockchain', response.credits);
fetch('http://localhost:3000/api/Trader/' + response.tradeId, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
method: 'PUT',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: response.tradeId,
firstName: response.firstName,
lastName: response.lastName,
tokens: response.tokens - price,
credits: response.credits
}),
mode: 'cors'
});
success = true;
}
});
return success;
}
const donation = async (id, price) => {
let success= false;
const person = await getOneUser(id)
.then( response => {
if (response.credits >= price) {
fetch('http://localhost:3000/api/Trader/' + response.tradeId, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
method: 'PUT',
body: JSON.stringify({
$class: "org.zendomo.biznet.Trader",
tradeId: response.tradeId,
firstName: response.firstName,
lastName: response.lastName,
tokens: response.tokens,
credits: response.credits - price
}),
mode: 'cors'
});
success = true;
}
});
return success;
}
module.exports = {
createUser,
getOneUser,
deleteOneUser,
editOneUser,
getAllUsers,
transferFunds,
addFunds,
tipUser,
purchase,
donation
}