-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic.js
168 lines (129 loc) · 4.43 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
/**
* Sample transaction
* @param {test.AccountTransfer} accountTransfer
* @transaction
*/
async function AccountTransfer(accountTransfer){
let currentuser = getCurrentParticipant()
if(accountTransfer.from.getFullyQualifiedIdentifier() == currentuser.account.getFullyQualifiedIdentifier()){
if(accountTransfer.from.balance < accountTransfer.amount){
throw new Error("insufficient funds")
}
else{
accountTransfer.from.balance -= accountTransfer.amount
accountTransfer.to.balance += accountTransfer.amount
return getAssetRegistry('test.Account')
.then(function(assetRegistry){
return assetRegistry.update(accountTransfer.from)
})
.then(function(){
return getAssetRegistry('test.Account')
})
.then(function(assetRegistry){
return assetRegistry.update(accountTransfer.to)
})
}
}
else{
throw new Error("this is not the current user")
}
}
/**
* Sample transaction
* @param {test.fundsTransfer} fundTransfer
* @transaction
*/
async function fundsTransfer(fundTransfer){
let currentuser = getCurrentParticipant()
if(fundTransfer.from.getFullyQualifiedIdentifier() == currentuser.wallet.getFullyQualifiedIdentifier()){
if(fundTransfer.from.walletAmount < fundTransfer.fundAmount){
throw new Error("insufficient amount in the manager wallet")
}
else{
fundTransfer.from.walletAmount -= fundTransfer.fundAmount
fundTransfer.fund.fundAmount += fundTransfer.fundAmount
return getAssetRegistry('test.wallet')
.then(function(assetRegistry){
return assetRegistry.update(fundTransfer.from)
})
.then(function(){
return getAssetRegistry('test.Funds')
})
.then(function(assetRegistry){
return assetRegistry.update(fundTransfer.fund)
})
}
}
else{
throw new Error("this is not the manager wallet")
}
}
/**
* Sample transaction
* @param {test.salaryEmployees} se
* @transaction
*/
async function salaryEmployees(se){
const employeeRegistry = await getParticipantRegistry('test.Employee')
//check if the employee exists
const employeeExists = await employeeRegistry.exists(se.employee.EmployeeId)
if(employeeExists){
const walletRegistry = await getAssetRegistry('test.wallet')
//check if the wallet of employee exists or not
const walletExists = await walletRegistry.exists(se.employee.wallet.walletId)
if(walletExists){
let currentuser = getCurrentParticipant()
if(se.employee.wallet.getFullyQualifiedIdentifier() == currentuser.wallet.getFullyQualifiedIdentifier()){
if(se.employee.salary > se.fund.fundAmount){
throw new Error("insufficient amount in fund")
}
else if(se.employee.status == 'UNPAID'){
se.employee.wallet.walletAmount += se.employee.salary
se.fund.fundAmount -= se.employee.salary
se.employee.status = 'PAID'
//updating the registries after the transaction
return getAssetRegistry('test.wallet')
.then(function(assetRegistry){
return assetRegistry.update(se.employee.wallet)
})
.then(function(){
return getAssetRegistry('test.Funds')
})
.then(function(assetRegistry){
return assetRegistry.update(se.fund)
}).then(function(){
return getParticipantRegistry('test.Employee')//updating the participant due to change in status
})
.then(function(participantRegistry){
return participantRegistry.update(se.employee)
})
}
else{
throw new Error("employee is already paid")
}
}
else{
throw new Error("account invalid")
}
}
else{
throw new Error("wallet of employee does not exists")
}
}
else{
throw new Error("employee does not exists")
}
}
/**
* Sample transaction
* @param {test.statusChange} sc
* @transaction
*/
async function statusChange(sc){
const employeeRegistry = await getParticipantRegistry('test.Employee');
var getEmployees = await employeeRegistry.getAll();
for(let i=0; i<getEmployees.length; i++) {
getEmployees[i].status = 'UNPAID';
await employeeRegistry.update(getEmployees[i]);
}
}