forked from aws-samples/non-profit-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
656 lines (559 loc) · 28.1 KB
/
app.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
*/
'use strict';
var log4js = require('log4js');
log4js.configure({
appenders: {
out: { type: 'stdout' },
},
categories: {
default: { appenders: ['out'], level: 'info' },
}
});
var logger = log4js.getLogger('NGOAPI');
const WebSocketServer = require('ws');
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var util = require('util');
var app = express();
var cors = require('cors');
var hfc = require('fabric-client');
const uuidv4 = require('uuid/v4');
var connection = require('./connection.js');
var query = require('./query.js');
var invoke = require('./invoke.js');
var blockListener = require('./blocklistener.js');
hfc.addConfigFile('config.json');
var host = 'localhost';
var port = 3000;
var username = "";
var orgName = "";
var channelName = hfc.getConfigSetting('channelName');
var chaincodeName = hfc.getConfigSetting('chaincodeName');
var peers = hfc.getConfigSetting('peers');
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SET CONFIGURATIONS ///////////////////////////
///////////////////////////////////////////////////////////////////////////////
app.options('*', cors());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(function(req, res, next) {
logger.info(' ##### New request for URL %s',req.originalUrl);
return next();
});
//wrapper to handle errors thrown by async functions. We can catch all
//errors thrown by async functions in a single place, here in this function,
//rather than having a try-catch in every function below. The 'next' statement
//used here will invoke the error handler function - see the end of this script
const awaitHandler = (fn) => {
return async (req, res, next) => {
try {
await fn(req, res, next)
}
catch (err) {
next(err)
}
}
}
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// START SERVER /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
var server = http.createServer(app).listen(port, function() {});
logger.info('****************** SERVER STARTED ************************');
logger.info('*************** Listening on: http://%s:%s ******************',host,port);
server.timeout = 240000;
function getErrorMessage(field) {
var response = {
success: false,
message: field + ' field is missing or Invalid in the request'
};
return response;
}
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// START WEBSOCKET SERVER ///////////////////////
///////////////////////////////////////////////////////////////////////////////
const wss = new WebSocketServer.Server({ server });
wss.on('connection', function connection(ws) {
logger.info('****************** WEBSOCKET SERVER - received connection ************************');
ws.on('message', function incoming(message) {
console.log('##### Websocket Server received message: %s', message);
});
ws.send('something');
});
///////////////////////////////////////////////////////////////////////////////
///////////////////////// REST ENDPOINTS START HERE ///////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Health check - can be called by load balancer to check health of REST API
app.get('/health', awaitHandler(async (req, res) => {
res.sendStatus(200);
}));
// Register and enroll user. A user must be registered and enrolled before any queries
// or transactions can be invoked
app.post('/users', awaitHandler(async (req, res) => {
logger.info('================ POST on Users');
username = req.body.username;
orgName = req.body.orgName;
logger.info('##### End point : /users');
logger.info('##### POST on Users- username : ' + username);
logger.info('##### POST on Users - userorg : ' + orgName);
let response = await connection.getRegisteredUser(username, orgName, true);
logger.info('##### POST on Users - returned from registering the username %s for organization %s', username, orgName);
logger.info('##### POST on Users - getRegisteredUser response secret %s', response.secret);
logger.info('##### POST on Users - getRegisteredUser response secret %s', response.message);
if (response && typeof response !== 'string') {
logger.info('##### POST on Users - Successfully registered the username %s for organization %s', username, orgName);
logger.info('##### POST on Users - getRegisteredUser response %s', response);
// Now that we have a username & org, we can start the block listener
await blockListener.startBlockListener(channelName, username, orgName, wss);
res.json(response);
} else {
logger.error('##### POST on Users - Failed to register the username %s for organization %s with::%s', username, orgName, response);
res.json({success: false, message: response});
}
}));
/************************************************************************************
* Donor methods
************************************************************************************/
// GET Donor
app.get('/donors', awaitHandler(async (req, res) => {
logger.info('================ GET on Donor');
let args = {};
let fcn = "queryAllDonors";
logger.info('##### GET on Donor - username : ' + username);
logger.info('##### GET on Donor - userOrg : ' + orgName);
logger.info('##### GET on Donor - channelName : ' + channelName);
logger.info('##### GET on Donor - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donor - fcn : ' + fcn);
logger.info('##### GET on Donor - args : ' + JSON.stringify(args));
logger.info('##### GET on Donor - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET a specific Donor
app.get('/donors/:donorUserName', awaitHandler(async (req, res) => {
logger.info('================ GET on Donor by ID');
logger.info('Donor username : ' + req.params);
let args = req.params;
let fcn = "queryDonor";
logger.info('##### GET on Donor by username - username : ' + username);
logger.info('##### GET on Donor by username - userOrg : ' + orgName);
logger.info('##### GET on Donor by username - channelName : ' + channelName);
logger.info('##### GET on Donor by username - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donor by username - fcn : ' + fcn);
logger.info('##### GET on Donor by username - args : ' + JSON.stringify(args));
logger.info('##### GET on Donor by username - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the Donations for a specific Donor
app.get('/donors/:donorUserName/donations', awaitHandler(async (req, res) => {
logger.info('================ GET on Donations for Donor');
logger.info('Donor username : ' + req.params);
let args = req.params;
let fcn = "queryDonationsForDonor";
logger.info('##### GET on Donations for Donor - username : ' + username);
logger.info('##### GET on Donations for Donor - userOrg : ' + orgName);
logger.info('##### GET on Donations for Donor - channelName : ' + channelName);
logger.info('##### GET on Donations for Donor - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donations for Donor - fcn : ' + fcn);
logger.info('##### GET on Donations for Donor - args : ' + JSON.stringify(args));
logger.info('##### GET on Donations for Donor - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// POST Donor
app.post('/donors', awaitHandler(async (req, res) => {
logger.info('================ POST on Donor');
var args = req.body;
var fcn = "createDonor";
logger.info('##### POST on Donor - username : ' + username);
logger.info('##### POST on Donor - userOrg : ' + orgName);
logger.info('##### POST on Donor - channelName : ' + channelName);
logger.info('##### POST on Donor - chaincodeName : ' + chaincodeName);
logger.info('##### POST on Donor - fcn : ' + fcn);
logger.info('##### POST on Donor - args : ' + JSON.stringify(args));
logger.info('##### POST on Donor - peers : ' + peers);
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* NGO methods
************************************************************************************/
// GET NGO
app.get('/ngos', awaitHandler(async (req, res) => {
logger.info('================ GET on NGO');
let args = {};
let fcn = "queryAllNGOs";
logger.info('##### GET on NGO - username : ' + username);
logger.info('##### GET on NGO - userOrg : ' + orgName);
logger.info('##### GET on NGO - channelName : ' + channelName);
logger.info('##### GET on NGO - chaincodeName : ' + chaincodeName);
logger.info('##### GET on NGO - fcn : ' + fcn);
logger.info('##### GET on NGO - args : ' + JSON.stringify(args));
logger.info('##### GET on NGO - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET a specific NGO
app.get('/ngos/:ngoRegistrationNumber', awaitHandler(async (req, res) => {
logger.info('================ GET on NGO by ID');
logger.info('NGO ngoRegistrationNumber : ' + req.params);
let args = req.params;
let fcn = "queryNGO";
logger.info('##### GET on NGO - username : ' + username);
logger.info('##### GET on NGO - userOrg : ' + orgName);
logger.info('##### GET on NGO - channelName : ' + channelName);
logger.info('##### GET on NGO - chaincodeName : ' + chaincodeName);
logger.info('##### GET on NGO - fcn : ' + fcn);
logger.info('##### GET on NGO - args : ' + JSON.stringify(args));
logger.info('##### GET on NGO - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the Donations for a specific NGO
app.get('/ngos/:ngoRegistrationNumber/donations', awaitHandler(async (req, res) => {
logger.info('================ GET on Donations for NGO');
logger.info('NGO ngoRegistrationNumber : ' + req.params);
let args = req.params;
let fcn = "queryDonationsForNGO";
logger.info('##### GET on Donations for NGO - username : ' + username);
logger.info('##### GET on Donations for NGO - userOrg : ' + orgName);
logger.info('##### GET on Donations for NGO - channelName : ' + channelName);
logger.info('##### GET on Donations for NGO - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donations for NGO - fcn : ' + fcn);
logger.info('##### GET on Donations for NGO - args : ' + JSON.stringify(args));
logger.info('##### GET on Donations for NGO - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the Spend for a specific NGO
app.get('/ngos/:ngoRegistrationNumber/spend', awaitHandler(async (req, res) => {
logger.info('================ GET on Spend for NGO');
logger.info('NGO ngoRegistrationNumber : ' + req.params);
let args = req.params;
let fcn = "querySpendForNGO";
logger.info('##### GET on Spend for NGO - username : ' + username);
logger.info('##### GET on Spend for NGO - userOrg : ' + orgName);
logger.info('##### GET on Spend for NGO - channelName : ' + channelName);
logger.info('##### GET on Spend for NGO - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Spend for NGO - fcn : ' + fcn);
logger.info('##### GET on Spend for NGO - args : ' + JSON.stringify(args));
logger.info('##### GET on Spend for NGO - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the Ratings for a specific NGO
app.get('/ngos/:ngoRegistrationNumber/ratings', awaitHandler(async (req, res) => {
logger.info('================ GET on Ratings for NGO');
logger.info('NGO ngoRegistrationNumber : ' + req.params);
let args = req.params;
let fcn = "queryRatingsForNGO";
logger.info('##### GET on Ratings for NGO - username : ' + username);
logger.info('##### GET on Ratings for NGO - userOrg : ' + orgName);
logger.info('##### GET on Ratings for NGO - channelName : ' + channelName);
logger.info('##### GET on Ratings for NGO - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Ratings for NGO - fcn : ' + fcn);
logger.info('##### GET on Ratings for NGO - args : ' + JSON.stringify(args));
logger.info('##### GET on Ratings for NGO - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// POST NGO
app.post('/ngos', awaitHandler(async (req, res) => {
logger.info('================ POST on NGO');
var args = req.body;
var fcn = "createNGO";
logger.info('##### POST on NGO - username : ' + username);
logger.info('##### POST on NGO - userOrg : ' + orgName);
logger.info('##### POST on NGO - channelName : ' + channelName);
logger.info('##### POST on NGO - chaincodeName : ' + chaincodeName);
logger.info('##### POST on NGO - fcn : ' + fcn);
logger.info('##### POST on NGO - args : ' + JSON.stringify(args));
logger.info('##### POST on NGO - peers : ' + peers);
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* Donation methods
************************************************************************************/
// GET Donation
app.get('/donations', awaitHandler(async (req, res) => {
logger.info('================ GET on Donation');
let args = {};
let fcn = "queryAllDonations";
logger.info('##### GET on Donation - username : ' + username);
logger.info('##### GET on Donation - userOrg : ' + orgName);
logger.info('##### GET on Donation - channelName : ' + channelName);
logger.info('##### GET on Donation - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donation - fcn : ' + fcn);
logger.info('##### GET on Donation - args : ' + JSON.stringify(args));
logger.info('##### GET on Donation - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET a specific Donation
app.get('/donations/:donationId', awaitHandler(async (req, res) => {
logger.info('================ GET on Donation by ID');
logger.info('Donation ID : ' + req.params);
let args = req.params;
let fcn = "queryDonation";
logger.info('##### GET on Donation - username : ' + username);
logger.info('##### GET on Donation - userOrg : ' + orgName);
logger.info('##### GET on Donation - channelName : ' + channelName);
logger.info('##### GET on Donation - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Donation - fcn : ' + fcn);
logger.info('##### GET on Donation - args : ' + JSON.stringify(args));
logger.info('##### GET on Donation - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the SpendAllocation records for a specific Donation
app.get('/donations/:donationId/spendallocations', awaitHandler(async (req, res) => {
logger.info('================ GET on SpendAllocation for Donation');
logger.info('Donation ID : ' + req.params);
let args = req.params;
let fcn = "querySpendAllocationForDonation";
logger.info('##### GET on SpendAllocation for Donation - username : ' + username);
logger.info('##### GET on SpendAllocation for Donation - userOrg : ' + orgName);
logger.info('##### GET on SpendAllocation for Donation - channelName : ' + channelName);
logger.info('##### GET on SpendAllocation for Donation - chaincodeName : ' + chaincodeName);
logger.info('##### GET on SpendAllocation for Donation - fcn : ' + fcn);
logger.info('##### GET on SpendAllocation for Donation - args : ' + JSON.stringify(args));
logger.info('##### GET on SpendAllocation for Donation - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// POST Donation
app.post('/donations', awaitHandler(async (req, res) => {
logger.info('================ POST on Donation');
var args = req.body;
var fcn = "createDonation";
logger.info('##### POST on Donation - username : ' + username);
logger.info('##### POST on Donation - userOrg : ' + orgName);
logger.info('##### POST on Donation - channelName : ' + channelName);
logger.info('##### POST on Donation - chaincodeName : ' + chaincodeName);
logger.info('##### POST on Donation - fcn : ' + fcn);
logger.info('##### POST on Donation - args : ' + JSON.stringify(args));
logger.info('##### POST on Donation - peers : ' + peers);
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* Spend methods
************************************************************************************/
// GET Spend
app.get('/spend', awaitHandler(async (req, res) => {
logger.info('================ GET on Spend');
let args = {};
let fcn = "queryAllSpend";
logger.info('##### GET on Spend - username : ' + username);
logger.info('##### GET on Spend - userOrg : ' + orgName);
logger.info('##### GET on Spend - channelName : ' + channelName);
logger.info('##### GET on Spend - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Spend - fcn : ' + fcn);
logger.info('##### GET on Spend - args : ' + JSON.stringify(args));
logger.info('##### GET on Spend - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET a specific Spend
app.get('/spend/:spendId', awaitHandler(async (req, res) => {
logger.info('================ GET on Spend by ID');
logger.info('Spend ID : ' + req.params);
let args = req.params;
let fcn = "querySpend";
logger.info('##### GET on Spend - username : ' + username);
logger.info('##### GET on Spend - userOrg : ' + orgName);
logger.info('##### GET on Spend - channelName : ' + channelName);
logger.info('##### GET on Spend - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Spend - fcn : ' + fcn);
logger.info('##### GET on Spend - args : ' + JSON.stringify(args));
logger.info('##### GET on Spend - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET the SpendAllocation records for a specific Spend
app.get('/spend/:spendId/spendallocations', awaitHandler(async (req, res) => {
logger.info('================ GET on SpendAllocation for Spend');
logger.info('Donation ID : ' + req.params);
let args = req.params;
let fcn = "querySpendAllocationForSpend";
logger.info('##### GET on SpendAllocation for Spend - username : ' + username);
logger.info('##### GET on SpendAllocation for Spend - userOrg : ' + orgName);
logger.info('##### GET on SpendAllocation for Spend - channelName : ' + channelName);
logger.info('##### GET on SpendAllocation for Spend - chaincodeName : ' + chaincodeName);
logger.info('##### GET on SpendAllocation for Spend - fcn : ' + fcn);
logger.info('##### GET on SpendAllocation for Spend - args : ' + JSON.stringify(args));
logger.info('##### GET on SpendAllocation for Spend - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// POST Spend
app.post('/spend', awaitHandler(async (req, res) => {
logger.info('================ dummySpend');
var args = req.body;
var fcn = "createSpend";
logger.info('##### dummySpend - username : ' + username);
logger.info('##### dummySpend - userOrg : ' + orgName);
logger.info('##### dummySpend - channelName : ' + channelName);
logger.info('##### dummySpend - chaincodeName : ' + chaincodeName);
logger.info('##### dummySpend - fcn : ' + fcn);
logger.info('##### dummySpend - args : ' + JSON.stringify(args));
logger.info('##### dummySpend - peers : ' + peers);
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* SpendAllocation methods
************************************************************************************/
// GET all SpendAllocation records
app.get('/spendallocations', awaitHandler(async (req, res) => {
logger.info('================ GET on spendAllocation');
let args = {};
let fcn = "queryAllSpendAllocations";
logger.info('##### GET on spendAllocationForDonation - username : ' + username);
logger.info('##### GET on spendAllocationForDonation - userOrg : ' + orgName);
logger.info('##### GET on spendAllocationForDonation - channelName : ' + channelName);
logger.info('##### GET on spendAllocationForDonation - chaincodeName : ' + chaincodeName);
logger.info('##### GET on spendAllocationForDonation - fcn : ' + fcn);
logger.info('##### GET on spendAllocationForDonation - args : ' + JSON.stringify(args));
logger.info('##### GET on spendAllocationForDonation - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* Ratings methods
************************************************************************************/
// POST Rating
app.post('/ratings', awaitHandler(async (req, res) => {
logger.info('================ POST on Ratings');
var args = req.body;
var fcn = "createRating";
logger.info('##### POST on Ratings - username : ' + username);
logger.info('##### POST on Ratings - userOrg : ' + orgName);
logger.info('##### POST on Ratings - channelName : ' + channelName);
logger.info('##### POST on Ratings - chaincodeName : ' + chaincodeName);
logger.info('##### POST on Ratings - fcn : ' + fcn);
logger.info('##### POST on Ratings - args : ' + JSON.stringify(args));
logger.info('##### POST on Ratings - peers : ' + peers);
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
// GET a specific Rating
app.get('/ratings/:ngoRegistrationNumber/:donorUserName', awaitHandler(async (req, res) => {
logger.info('================ GET on Rating by ID');
logger.info('Rating ID : ' + util.inspect(req.params));
let args = req.params;
let fcn = "queryDonorRatingsForNGO";
logger.info('##### GET on Rating - username : ' + username);
logger.info('##### GET on Rating - userOrg : ' + orgName);
logger.info('##### GET on Rating - channelName : ' + channelName);
logger.info('##### GET on Rating - chaincodeName : ' + chaincodeName);
logger.info('##### GET on Rating - fcn : ' + fcn);
logger.info('##### GET on Rating - args : ' + JSON.stringify(args));
logger.info('##### GET on Rating - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
res.send(message);
}));
/************************************************************************************
* Blockchain metadata methods
************************************************************************************/
// GET details of a blockchain transaction using the record key (i.e. the key used to store the transaction
// in the world state)
app.get('/blockinfos/:docType/keys/:key', awaitHandler(async (req, res) => {
logger.info('================ GET on blockinfo');
logger.info('Key is : ' + req.params);
let args = req.params;
let fcn = "queryHistoryForKey";
logger.info('##### GET on blockinfo - username : ' + username);
logger.info('##### GET on blockinfo - userOrg : ' + orgName);
logger.info('##### GET on blockinfo - channelName : ' + channelName);
logger.info('##### GET on blockinfo - chaincodeName : ' + chaincodeName);
logger.info('##### GET on blockinfo - fcn : ' + fcn);
logger.info('##### GET on blockinfo - args : ' + JSON.stringify(args));
logger.info('##### GET on blockinfo - peers : ' + peers);
let history = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
logger.info('##### GET on blockinfo - queryHistoryForKey : ' + util.inspect(history));
res.send(history);
}));
/************************************************************************************
* Utility function for creating dummy spend records. Mimics the behaviour of an NGO
* spending funds, which are allocated against donations
************************************************************************************/
async function dummySpend() {
if (!username) {
return;
}
// first, we get a list of donations and randomly choose one
let args = {};
let fcn = "queryAllDonations";
logger.info('##### dummySpend GET on Donation - username : ' + username);
logger.info('##### dummySpend GET on Donation - userOrg : ' + orgName);
logger.info('##### dummySpend GET on Donation - channelName : ' + channelName);
logger.info('##### dummySpend GET on Donation - chaincodeName : ' + chaincodeName);
logger.info('##### dummySpend GET on Donation - fcn : ' + fcn);
logger.info('##### dummySpend GET on Donation - args : ' + JSON.stringify(args));
logger.info('##### dummySpend GET on Donation - peers : ' + peers);
let message = await query.queryChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
let len = message.length;
if (len < 1) {
logger.info('##### dummySpend - no donations available');
}
logger.info('##### dummySpend - number of donation record: ' + len);
if (len < 1) {
return;
}
let ran = Math.floor(Math.random() * len);
logger.info('##### dummySpend - randomly selected donation record number: ' + ran);
logger.info('##### dummySpend - randomly selected donation record: ' + JSON.stringify(message[ran]));
let ngo = message[ran]['ngoRegistrationNumber'];
logger.info('##### dummySpend - randomly selected ngo: ' + ngo);
// then we create a spend record for the NGO that received the donation
fcn = "createSpend";
let spendId = uuidv4();
let spendAmt = Math.floor(Math.random() * 100) + 1;
args = {};
args["ngoRegistrationNumber"] = ngo;
args["spendId"] = spendId;
args["spendDescription"] = "Peter Pipers Poulty Portions for Pets";
args["spendDate"] = "2018-09-20T12:41:59.582Z";
args["spendAmount"] = spendAmt;
logger.info('##### dummySpend - username : ' + username);
logger.info('##### dummySpend - userOrg : ' + orgName);
logger.info('##### dummySpend - channelName : ' + channelName);
logger.info('##### dummySpend - chaincodeName : ' + chaincodeName);
logger.info('##### dummySpend - fcn : ' + fcn);
logger.info('##### dummySpend - args : ' + JSON.stringify(args));
logger.info('##### dummySpend - peers : ' + peers);
message = await invoke.invokeChaincode(peers, channelName, chaincodeName, args, fcn, username, orgName);
}
(function loop() {
var rand = Math.round(Math.random() * (20000 - 5000)) + 5000;
setTimeout(function() {
dummySpend();
loop();
}, rand);
}());
/************************************************************************************
* Error handler
************************************************************************************/
app.use(function(error, req, res, next) {
res.status(500).json({ error: error.toString() });
});