This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
logic.js
333 lines (286 loc) · 13.3 KB
/
logic.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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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';
/* global getFactory getAssetRegistry getParticipantRegistry emit */
/**
* Create the LOC asset
* @param {org.example.loc.InitialApplication} initalAppliation - the InitialApplication transaction
* @transaction
*/
async function initialApplication(application) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
const letter = factory.newResource(namespace, 'LetterOfCredit', application.letterId);
letter.applicant = factory.newRelationship(namespace, 'Customer', application.applicant.getIdentifier());
letter.beneficiary = factory.newRelationship(namespace, 'Customer', application.beneficiary.getIdentifier());
letter.issuingBank = factory.newRelationship(namespace, 'Bank', application.applicant.bank.getIdentifier());
letter.exportingBank = factory.newRelationship(namespace, 'Bank', application.beneficiary.bank.getIdentifier());
letter.rules = application.rules;
letter.productDetails = application.productDetails;
letter.evidence = [];
letter.approval = [factory.newRelationship(namespace, 'Customer', application.applicant.getIdentifier())];
letter.status = 'AWAITING_APPROVAL';
//save the application
const assetRegistry = await getAssetRegistry(letter.getFullyQualifiedType());
await assetRegistry.add(letter);
// emit event
const applicationEvent = factory.newEvent(namespace, 'InitialApplicationEvent');
applicationEvent.loc = letter;
emit(applicationEvent);
}
/**
* Update the LOC to show that it has been approved by a given person
* @param {org.example.loc.Approve} approve - the Approve transaction
* @transaction
*/
async function approve(approveRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = approveRequest.loc;
if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error ('This letter of credit has already been closed');
} else if (letter.approval.length === 4) {
throw new Error ('All four parties have already approved this letter of credit');
} else if (letter.approval.includes(approveRequest.approvingParty)) {
throw new Error ('This person has already approved this letter of credit');
} else if (approveRequest.approvingParty.getType() === 'BankEmployee') {
letter.approval.forEach((approvingParty) => {
let bankApproved = false;
try {
bankApproved = approvingParty.getType() === 'BankEmployee' && approvingParty.bank.getIdentifier() === approveRequest.approvingParty.bank.getIdentifier();
} catch (err) {
// ignore error as they don't have rights to access that participant
}
if (bankApproved) {
throw new Error('Your bank has already approved of this request');
}
});
}
letter.approval.push(factory.newRelationship(namespace, approveRequest.approvingParty.getType(), approveRequest.approvingParty.getIdentifier()));
// update the status of the letter if everyone has approved
if (letter.approval.length === 4) {
letter.status = 'APPROVED';
}
// update approval[]
const assetRegistry = await getAssetRegistry(approveRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const approveEvent = factory.newEvent(namespace, 'ApproveEvent');
approveEvent.loc = approveRequest.loc;
approveEvent.approvingParty = approveRequest.approvingParty;
emit(approveEvent);
}
/**
* Reject the LOC
* @param {org.example.loc.Reject} reject - the Reject transaction
* @transaction
*/
async function reject(rejectRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = rejectRequest.loc;
if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error('This letter of credit has already been closed');
} else if (letter.status === 'APPROVED') {
throw new Error('This letter of credit has already been approved');
} else {
letter.status = 'REJECTED';
letter.closeReason = rejectRequest.closeReason;
// update the status of the LOC
const assetRegistry = await getAssetRegistry(rejectRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const rejectEvent = factory.newEvent(namespace, 'RejectEvent');
rejectEvent.loc = rejectRequest.loc;
rejectEvent.closeReason = rejectRequest.closeReason;
emit(rejectEvent);
}
}
/**
* Suggest changes to the current rules in the LOC
* @param {org.example.loc.SuggestChanges} suggestChanges - the SuggestChanges transaction
* @transaction
*/
async function suggestChanges(changeRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = changeRequest.loc;
if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error ('This letter of credit has already been closed');
} else if (letter.status === 'APPROVED') {
throw new Error('This letter of credit has already been approved');
} else if (letter.status === 'SHIPPED' || letter.status === 'RECEIVED' || letter.status === 'READY_FOR_PAYMENT') {
throw new Error ('The product has already been shipped');
} else {
letter.rules = changeRequest.rules;
// the rules have been changed - clear the approval array and update status
letter.approval = [changeRequest.suggestingParty];
letter.status = 'AWAITING_APPROVAL';
// update the loc with the new rules
const assetRegistry = await getAssetRegistry(changeRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const changeEvent = factory.newEvent(namespace, 'SuggestChangesEvent');
changeEvent.loc = changeRequest.loc;
changeEvent.rules = changeRequest.rules;
changeEvent.suggestingParty = changeRequest.suggestingParty;
emit(changeEvent);
}
}
/**
* "Ship" the product
* @param {org.example.loc.ShipProduct} shipProduct - the ShipProduct transaction
* @transaction
*/
async function shipProduct(shipRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = shipRequest.loc;
if (letter.status === 'APPROVED') {
letter.status = 'SHIPPED';
letter.evidence.push(shipRequest.evidence);
//update the status of the loc
const assetRegistry = await getAssetRegistry(shipRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const shipEvent = factory.newEvent(namespace, 'ShipProductEvent');
shipEvent.loc = shipRequest.loc;
emit(shipEvent);
} else if (letter.status === 'AWAITING_APPROVAL') {
throw new Error ('This letter needs to be fully approved before the product can be shipped');
} else if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error ('This letter of credit has already been closed');
} else {
throw new Error ('The product has already been shipped');
}
}
/**
* "Recieve" the product that has been "shipped"
* @param {org.example.loc.ReceiveProduct} receiveProduct - the ReceiveProduct transaction
* @transaction
*/
async function receiveProduct(receiveRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = receiveRequest.loc;
if (letter.status === 'SHIPPED') {
letter.status = 'RECEIVED';
// update the status of the loc
const assetRegistry = await getAssetRegistry(receiveRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const receiveEvent = factory.newEvent(namespace, 'ReceiveProductEvent');
receiveEvent.loc = receiveRequest.loc;
emit(receiveEvent);
} else if (letter.status === 'AWAITING_APPROVAL' || letter.status === 'APPROVED'){
throw new Error('The product needs to be shipped before it can be received');
} else if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error ('This letter of credit has already been closed');
} else {
throw new Error('The product has already been received');
}
}
/**
* Mark a given letter as "ready for payment"
* @param {org.example.loc.ReadyForPayment} readyForPayment - the ReadyForPayment transaction
* @transaction
*/
async function readyForPayment(paymentRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = paymentRequest.loc;
if (letter.status === 'RECEIVED') {
letter.status = 'READY_FOR_PAYMENT';
// update the status of the loc
const assetRegistry = await getAssetRegistry(paymentRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const paymentEvent = factory.newEvent(namespace, 'ReadyForPaymentEvent');
paymentEvent.loc = paymentRequest.loc;
emit(paymentEvent);
} else if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error('This letter of credit has already been closed');
} else if (letter.status === 'READY_FOR_PAYMENT') {
throw new Error('The payment has already been made');
} else {
throw new Error('The payment cannot be made until the product has been received by the applicant');
}
}
/**
* Close the LOC
* @param {org.example.loc.Close} close - the Close transaction
* @transaction
*/
async function close(closeRequest) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
let letter = closeRequest.loc;
if (letter.status === 'READY_FOR_PAYMENT') {
letter.status = 'CLOSED';
letter.closeReason = closeRequest.closeReason;
// update the status of the loc
const assetRegistry = await getAssetRegistry(closeRequest.loc.getFullyQualifiedType());
await assetRegistry.update(letter);
// emit event
const closeEvent = factory.newEvent(namespace, 'CloseEvent');
closeEvent.loc = closeRequest.loc;
closeEvent.closeReason = closeRequest.closeReason;
emit(closeEvent);
} else if (letter.status === 'CLOSED' || letter.status === 'REJECTED') {
throw new Error('This letter of credit has already been closed');
} else {
throw new Error('Cannot close this letter of credit until it is fully approved and the product has been received by the applicant');
}
}
/**
* Create the participants needed for the demo
* @param {org.example.loc.CreateDemoParticipants} createDemoParticipants - the CreateDemoParticipants transaction
* @transaction
*/
async function createDemoParticipants() { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'org.example.loc';
// create the banks
const bankRegistry = await getParticipantRegistry(namespace + '.Bank');
const bank1 = factory.newResource(namespace, 'Bank', 'BoD');
bank1.name = 'Bank of Dinero';
await bankRegistry.add(bank1);
const bank2 = factory.newResource(namespace, 'Bank', 'EB');
bank2.name = 'Eastwood Banking';
await bankRegistry.add(bank2);
// create bank employees
const employeeRegistry = await getParticipantRegistry(namespace + '.BankEmployee');
const employee1 = factory.newResource(namespace, 'BankEmployee', 'matias');
employee1.name = 'Matías';
employee1.bank = factory.newRelationship(namespace, 'Bank', 'BoD');
await employeeRegistry.add(employee1);
const employee2 = factory.newResource(namespace, 'BankEmployee', 'ella');
employee2.name = 'Ella';
employee2.bank = factory.newRelationship(namespace, 'Bank', 'EB');
await employeeRegistry.add(employee2);
// create customers
const customerRegistry = await getParticipantRegistry(namespace + '.Customer');
const customer1 = factory.newResource(namespace, 'Customer', 'alice');
customer1.name = 'Alice';
customer1.lastName= 'Hamilton';
customer1.bank = factory.newRelationship(namespace, 'Bank', 'BoD');
customer1.companyName = 'QuickFix IT';
await customerRegistry.add(customer1);
const customer2 = factory.newResource(namespace, 'Customer', 'bob');
customer2.name = 'Bob';
customer2.lastName= 'Appleton';
customer2.bank = factory.newRelationship(namespace, 'Bank', 'EB');
customer2.companyName = 'Conga Computers';
await customerRegistry.add(customer2);
}