-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabcar.js
166 lines (138 loc) · 5.64 KB
/
fabcar.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
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Contract } = require('fabric-contract-api');
class FabCar extends Contract {
async initLedger(ctx) {
console.info('============= START : Initialize Ledger ===========');
const users = [
{
role:"logistic",
userEmail:"test1",
password:"pasword123",
logistic: [],
companyName:"testCompany",
companyAddress:"testAddreess",
companyEmail:"testCompanyEmail",
phone:"0711111111"
},
{
role:"warehouse",
userEmail:"test2",
password:"pasword123",
logistic: [],
companyName:"testCompany2",
companyAddress:"testAddreess2",
companyEmail:"testCompanyEmail2",
phone:"0711111112"
}
];
for (let i = 0; i < users.length; i++) {
users[i].docType = 'car';
await ctx.stub.putState('USER' + i, Buffer.from(JSON.stringify(users[i])));
console.info('Added <--> ', users[i]);
}
console.info('============= END : Initialize Ledger ===========');
}
async login(ctx, UserId) {
const carAsBytes = await ctx.stub.getState(UserId); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${UserId} does not exist`);
}
console.log(carAsBytes.toString());
return carAsBytes.toString();
}
async createCar(ctx, id, role, userEmail, password, logistic,companyName,companyAddress,companyEmail,phone) {
console.info('============= START : Create User ===========');
console.log("workung ereate car")
const car = {
docType: 'car',
role,
userEmail,
password,
logistic: [],
companyName,
companyAddress,
companyEmail,
phone
};
await ctx.stub.putState(id, Buffer.from(JSON.stringify(car)));
console.info('============= END : Create User ===========');
}
async queryAllusers(ctx) {
const startKey = '';
const endKey = '';
const allResults = [];
for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) {
const strValue = Buffer.from(value).toString('utf8');
let record;
try {
record = JSON.parse(strValue);
} catch (err) {
console.log(err);
record = strValue;
}
allResults.push({ Key: key, Record: record });
}
console.info(allResults);
return JSON.stringify(allResults);
}
async changeCarOwner(ctx, carNumber, newOwner) {
console.info('============= START : changeCarOwner ===========');
const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
car.owner = newOwner;
await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
console.info('============= END : changeCarOwner ===========');
}
// #########################add logistic####################################
async addLogistic(ctx, userid, orderID, retailerid,product,quantity,status,supplierID) {
console.info('============= START : changeCarOwner ===========');
const carAsBytes = await ctx.stub.getState(userid); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${userid} does not exist`);
}
const logis = {
orderID,
retailerid,
docType: 'logis',
product,
quantity,
status,
supplierID
};
const car = JSON.parse(carAsBytes.toString());
console.log(car.logistic);
car.logistic.push(logis);
// const car = JSON.parse(carAsBytes.toString());
await ctx.stub.putState(userid, Buffer.from(JSON.stringify(car)));
console.info('============= END : changeCarOwner ===========');
}
// ######################add logistic########################################
// ########################update status############################
async UpdateStatus(ctx, UserId,OrderId, newStatus) {
console.info('============= START : changeCarOwner ===========');
const carAsBytes = await ctx.stub.getState(UserId); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${id} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
const resultUpStatus=car.logistic;
// car.logistic = newOwner;
for(let i=0;i<resultUpStatus.length;i++){
if(resultUpStatus[i].orderID==OrderId){
resultUpStatus[i].status= newStatus;
}
}
await ctx.stub.putState(UserId, Buffer.from(JSON.stringify(car)));
console.info('============= END : changeCarOwner ===========');
}
// ###################update status###########################
}
module.exports = FabCar;