-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdairy.js
184 lines (163 loc) · 6.4 KB
/
dairy.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
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Contract } = require('fabric-contract-api');
/**
* This contract allows all milk supply chain's involved actors to log their operations in a persistent way
*/
class diary extends Contract {
/**
* Register a new milk batch in the supply chain ecosystem
* @param {string} batchId identifier of the batch
* @param {string} ranchName name of the ranch where the milk was produced
* @param {string} location place where the ranch is located
* @param {number} amount amount of milk in the batch
*/
static async registerMilkBatch(ctx, batchId, ranchName, location, amount) {
console.info('============= START : registerMilkBatch ===========');
const batchesRegister = 'bacthesRegister';
const newBatch = {
ranchName: ranchName,
location: location,
amout: amount,
};
const batchesAsBytes = await ctx.stub.getState(batchesRegister);
const batches = JSON.parse(batchesAsBytes.toString());
batches[batchId] = newBatch;
await ctx.stub.putState(batchesRegister, Buffer.from(JSON.stringify(batches)));
ctx.stub.setEvent(
'milkBatchRegistered',
Buffer.from(
JSON.stringify({
[batchId]: newBatch,
}),
),
);
console.info('============= END : registerMilkBatch ===========');
}
/**
* Register that a milk batch was transported, assumed that one entire milk batch is transported at once
* @param {string} logisticsFirmName name of the firm that has transported the milk batch
* @param {string} toLocation place where the batch was transported to
* @param {string} batchId identifier of the milk batch
*/
async transportMilkBatch(ctx, logisticsFirmName, toLocation, batchId) {
console.info('============= START : transportMilkBatch ===========');
const batchesTransports = 'batchesTransports';
const newTransport = {
logisticsFirmName: logisticsFirmName,
toLocation: toLocation,
};
const transportsAsBytes = await ctx.stub.getState(batchesTransports);
const transports = JSON.parse(transportsAsBytes.toString());
transports[batchId] = newTransport;
await ctx.stub.putState(batchesTransports, Buffer.from(JSON.stringify(transports)));
ctx.stub.setEvent(
'milkBatchTransported',
Buffer.from(
JSON.stringify({
[batchId]: newTransport,
}),
),
);
console.info('============= END : transportMilkBatch ===========');
}
/**
* Register a new milk carton, assumed that at the processing facility, milk from multiple batches is mixed, therefore, a single milk carton could contain milk of multiple batches
* @param {string} processorFirmName name of the firm that has processed the milk carton
* @param {string} location place where the processing has taken place
* @param {string} originBatchIds identifiers of the batches from which the milk was taken
* @param {string} cartonId identifier of the new milk carton
*/
async registerMilkCarton(ctx, processorFirmName, location, originBatchIds, cartonId) {
console.info('============= START : registerMilkCarton ===========');
const cartonsRegister = 'cartonsRegister';
const newCarton = {
processorFirmName: processorFirmName,
location: location,
originBatchIds: originBatchIds,
};
const cartonsAsBytes = await ctx.stub.getState(cartonsRegister);
const cartons = JSON.parse(cartonsAsBytes.toString());
cartons[cartonId] = newCarton;
await ctx.stub.putState(cartonsRegister, Buffer.from(JSON.stringify(cartons)));
ctx.stub.setEvent(
'cartonRegistered',
Buffer.from(
JSON.stringify({
[cartonId]: newCarton,
}),
),
);
console.info('============= END : registerMilkCarton ===========');
}
/**
* Register that a new milk carton was transported
* @param {string} distributorName name of the distributor
* @param {string} toLocation place where the carton was transported to
* @param {string} cartonId identifier of the carton
*/
async transportCarton(ctx, distributorName, toLocation, cartonId) {
console.info('============= START : transportCarton ===========');
const cartonsTransports = 'cartonsTransports';
const newTransport = {
distributorName: distributorName,
toLocation: toLocation,
};
const transportsAsBytes = await ctx.stub.getState(cartonsTransports);
const transports = JSON.parse(transportsAsBytes.toString());
transports[cartonId] = newTransport;
await ctx.stub.putState(cartonsTransports, Buffer.from(JSON.stringify(transports)));
ctx.stub.setEvent(
'cartonTransported',
Buffer.from(
JSON.stringify({
[cartonId]: newTransport,
}),
),
);
console.info('============= END : transportCarton ===========');
}
/**
* Register that a milk carton was stored in the inventory
* @param {string} retailerName name of the reatiler who will sell the carton
* @param {string} cartonId identifier of the carton
*/
async registerCartonInInventory(ctx, retailerName, cartonId) {
console.info('============= START : registerCartonInInventory ===========');
const cartonsInventory = 'cartonsInventory';
const inventoryAsBytes = await ctx.stub.getState(cartonsInventory);
const inventory = JSON.parse(inventoryAsBytes.toString());
inventory[cartonId] = retailerName;
await ctx.stub.putState(cartonsInventory, Buffer.from(JSON.stringify(inventory)));
ctx.stub.setEvent(
'cartonRegisteredInInventory',
Buffer.from(
JSON.stringify({
[cartonId]: retailerName,
}),
),
);
console.info('============= END : registerCartonInInventory ===========');
}
/**
* Register a new sale
* @param {string} cartonId identifier of the carton
*/
async registerSelling(ctx, cartonId) {
console.info('============= START : registerSelling ===========');
const cartonsSales = 'cartonsSales';
const salesAsBytes = await ctx.stub.getState(cartonsSales);
const sales = JSON.parse(salesAsBytes.toString());
sales.push(cartonId);
await ctx.stub.putState(cartonsSales, Buffer.from(JSON.stringify(sales)));
ctx.stub.setEvent('cartonSold', Buffer.from(
JSON.stringify({
[cartonId]: retailerName,
})
));
console.info('============= END : registerSelling ===========');
}
}
module.exports = diary;