-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbn-factory-submit-txn.js
119 lines (98 loc) · 3.21 KB
/
bn-factory-submit-txn.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
'use strict';
/**
* Part of a course on Hyperledger Fabric:
* http://ACloudFan.com
*
* Composer 0.19.0
*
* Demostrates the use of factory received in the BN connection using
* the getFactory( ) method to submit a transaction
*
* Pre-Req
* 1. Start the fabric
* 2. Deploy & start airlinev7
* 3. Start the REST Server
* 4. Make sure there is no Flight resource with id="AE101-05-12-2019"
* Delete it if you find one - Remember the code for CreateFlight
* Transaction has the flightId hardcoded :-)
*
* Demo Steps
* 1. Use the bn-connection-util to create the connection to airlinev7
* 2. Get the Busines Network Definition from Runtime
* 3. Get the factory from the Business Network definition
* 4. Create a new Transaction instance
* 5. Set the property values in the transaction object
* 6. Submit the transaction
*/
// Constant values - change as per your needs
const namespace = "org.fabriccare.organ";
const transactionType = "donateOrgan";
// 1. Connect to airlinev7
const bnUtil = require('./bn-connection-util');
bnUtil.connect(main);
function main(error){
// Check for error
if(error){
console.log(error);
process.exit(1);
}
// 2. Get the Business Network Definition
let bnDef = bnUtil.connection.getBusinessNetwork();
console.log("2. Received Definition from Runtime: ",
bnDef.getName()," ",bnDef.getVersion());
// 3. Get the factory
let factory = bnDef.getFactory();
// 4. Lets create a new Resource of type = Transaction
// Here is the sample data
// {
// "$class": "org.acme.airline.flight.CreateFlight",
// "flightNumber": "AE101-06-06-2019",
// "origin": "MSP",
// "destination": "SEA",
// "schedule": "2019-06-06T18:49:58.273Z"
// }
// 4. Create an instance of transaction
let options = {
generate: false,
includeOptionalFields: false
}
let organId = "AE105-05-06-2019";
let transaction = factory.newTransaction(namespace,transactionType,organId,options);
// 5. Set up the properties of the transaction object
transaction.setPropertyValue('organId','3344');
transaction.setPropertyValue('type',1);
transaction.setPropertyValue('donorName', 'Jasmeet');
transaction.setPropertyValue('bloodgroup' , 'O+');
transaction.setPropertyValue('donorId' , 101);
// 6. Submit the transaction
return bnUtil.connection.submitTransaction(transaction).then(()=>{
console.log("6. Transaction Submitted/Processed Successfully!!")
bnUtil.disconnect();
}).catch((error)=>{
console.log(error);
bnUtil.disconnect();
});
}
/**
* Test Data for adding flight in registry
{
"$class": "org.acme.airline.flight.Flight",
"flightId": "AE101-05-05-2019",
"flightNumber": "AE101",
"route": {
"$class": "org.acme.airline.flight.Route",
"origin": "ATL",
"destination": "EWR",
"schedule": "2019-12-17T18:49:58.288Z",
"id": "string"
}
}
* Adding flight using the createFlight transaction
{
"$class": "org.acme.airline.flight.CreateFlight",
"flightNumber": "AE101-06-06-2019",
"origin": "MSP",
"destination": "SEA",
"schedule": "2019-06-06T18:49:58.273Z"
}
*/