@live-change/balance-service 0.8.62 → 0.8.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/balance.js +1 -4
- package/config.js +13 -6
- package/operation.js +115 -28
- package/package.json +4 -4
package/balance.js
CHANGED
package/config.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import definition from './definition.js'
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
|
-
|
|
5
|
-
type: Number,
|
|
6
|
-
default: 0
|
|
7
|
-
},
|
|
4
|
+
currencyZero = 0,
|
|
8
5
|
currencyAdd = (...args) => {
|
|
9
6
|
return args.reduce((a, b) => a + b, 0)
|
|
10
7
|
},
|
|
11
8
|
currencyNegate = (value) => -value,
|
|
9
|
+
currencyIsPositive = (value) => value > 0,
|
|
12
10
|
changePossible = (value, change) => {
|
|
13
11
|
return value + change >= 0
|
|
14
12
|
},
|
|
@@ -16,11 +14,20 @@ const {
|
|
|
16
14
|
recalculate = (value, time) => value, // no recalculation by default, will be used with vector balances
|
|
17
15
|
|
|
18
16
|
readerRoles = ['owner', 'admin'],
|
|
17
|
+
|
|
18
|
+
createBalanceIfNotExists = true,
|
|
19
|
+
} = definition.config
|
|
20
|
+
|
|
21
|
+
const {
|
|
22
|
+
currencyType = {
|
|
23
|
+
type: Number,
|
|
24
|
+
default: currencyZero
|
|
25
|
+
},
|
|
19
26
|
} = definition.config
|
|
20
27
|
|
|
21
28
|
const config = {
|
|
22
|
-
currencyType, currencyAdd, changePossible, currencyNegate,
|
|
23
|
-
nextRecalculateTime, recalculate, readerRoles
|
|
29
|
+
currencyType, currencyAdd, changePossible, currencyNegate, currencyZero, currencyIsPositive,
|
|
30
|
+
nextRecalculateTime, recalculate, readerRoles, createBalanceIfNotExists
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
export default config
|
package/operation.js
CHANGED
|
@@ -19,7 +19,8 @@ const Operation = definition.model({
|
|
|
19
19
|
type: String,
|
|
20
20
|
options: [
|
|
21
21
|
'started', // funds are locked, but amount is not changed yet
|
|
22
|
-
'finished' // amount is changed, and funds are unlocked
|
|
22
|
+
'finished', // amount is changed, and funds are unlocked,
|
|
23
|
+
'canceled' // funds are unlocked, but amount is not changed
|
|
23
24
|
],
|
|
24
25
|
},
|
|
25
26
|
|
|
@@ -147,9 +148,9 @@ definition.view({
|
|
|
147
148
|
},
|
|
148
149
|
accessControl: {
|
|
149
150
|
roles: config.readerRoles,
|
|
150
|
-
objects:
|
|
151
|
-
return [{ objectType: definition.name + '_Balance', object:
|
|
152
|
-
}
|
|
151
|
+
objects: ({ balance }) => {
|
|
152
|
+
return [{ objectType: definition.name + '_Balance', object: balance }]
|
|
153
|
+
},
|
|
153
154
|
},
|
|
154
155
|
async daoPath(params, { client, service }, method) {
|
|
155
156
|
const range = App.extractRange(params)
|
|
@@ -162,8 +163,28 @@ definition.view({
|
|
|
162
163
|
}
|
|
163
164
|
})
|
|
164
165
|
|
|
166
|
+
async function getBalance(balance, triggerService) {
|
|
167
|
+
const balanceData = await Balance.get(balance)
|
|
168
|
+
if(balanceData) return balanceData
|
|
169
|
+
if(config.createBalanceIfNotExists) {
|
|
170
|
+
const [ownerType, owner] = balance.split(':').map(v => JSON.parse(v))
|
|
171
|
+
await triggerService({
|
|
172
|
+
service: definition.name,
|
|
173
|
+
type: 'balance_setOwnerOwnedBalance',
|
|
174
|
+
}, {
|
|
175
|
+
ownerType: ownerType, owner: owner
|
|
176
|
+
})
|
|
177
|
+
return {
|
|
178
|
+
ownerType, owner,
|
|
179
|
+
available: config.currencyZero,
|
|
180
|
+
amount: config.currencyZero
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
throw "balanceNotFound"
|
|
184
|
+
}
|
|
185
|
+
|
|
165
186
|
definition.trigger({
|
|
166
|
-
name: "
|
|
187
|
+
name: "balance_startOperation",
|
|
167
188
|
properties: {
|
|
168
189
|
balance: {
|
|
169
190
|
type: Balance,
|
|
@@ -183,12 +204,11 @@ definition.trigger({
|
|
|
183
204
|
}
|
|
184
205
|
},
|
|
185
206
|
|
|
186
|
-
queuedBy:
|
|
187
|
-
async execute({ balance, causeType, cause, change }, { client, service, triggerService }) {
|
|
207
|
+
queuedBy: 'balance',
|
|
208
|
+
async execute({ balance, causeType, cause, change }, { client, service, triggerService }, emit) {
|
|
188
209
|
const operation = app.generateUid()
|
|
189
|
-
const balanceData = await
|
|
210
|
+
const balanceData = await getBalance(balance, triggerService)
|
|
190
211
|
if(!config.changePossible(balanceData.available, change)) throw "insufficientFunds"
|
|
191
|
-
const newAvailable = config.currencyAdd(balanceData.available, change)
|
|
192
212
|
await triggerService({
|
|
193
213
|
service: definition.name,
|
|
194
214
|
type: 'balance_createBalanceOwnedOperation',
|
|
@@ -198,19 +218,29 @@ definition.trigger({
|
|
|
198
218
|
amountBefore: balanceData.amount,
|
|
199
219
|
amountAfter: null
|
|
200
220
|
})
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
221
|
+
if(config.currencyIsPositive(change)) {
|
|
222
|
+
await triggerService({
|
|
223
|
+
service: definition.name,
|
|
224
|
+
type: 'balance_updateOwnerOwnedBalance',
|
|
225
|
+
}, {
|
|
226
|
+
ownerType: balanceData.ownerType, owner: balanceData.owner
|
|
227
|
+
})
|
|
228
|
+
} else {
|
|
229
|
+
const newAvailable = config.currencyAdd(balanceData.available, change)
|
|
230
|
+
await triggerService({
|
|
231
|
+
service: definition.name,
|
|
232
|
+
type: 'balance_updateOwnerOwnedBalance',
|
|
233
|
+
}, {
|
|
234
|
+
ownerType: balanceData.ownerType, owner: balanceData.owner,
|
|
235
|
+
available: newAvailable
|
|
236
|
+
})
|
|
237
|
+
}
|
|
208
238
|
return operation
|
|
209
239
|
}
|
|
210
240
|
})
|
|
211
241
|
|
|
212
242
|
definition.trigger({
|
|
213
|
-
name: '
|
|
243
|
+
name: 'balance_finishOperation',
|
|
214
244
|
properties: {
|
|
215
245
|
balance: {
|
|
216
246
|
type: Balance,
|
|
@@ -221,37 +251,92 @@ definition.trigger({
|
|
|
221
251
|
validation: ['nonEmpty']
|
|
222
252
|
}
|
|
223
253
|
},
|
|
224
|
-
queuedBy:
|
|
254
|
+
queuedBy: 'balance',
|
|
225
255
|
async execute({ balance, operation }, { client, service, triggerService }) {
|
|
226
256
|
const operationData = await Operation.get(operation)
|
|
227
257
|
if(!operationData) throw "operationNotFound"
|
|
228
258
|
if(operationData.state !== 'started') throw "operationNotStarted"
|
|
259
|
+
console.log("operationData", operationData, "balance", balance)
|
|
229
260
|
if(operationData.balance !== balance) throw "balanceMismatch"
|
|
230
|
-
const balanceData = await
|
|
261
|
+
const balanceData = await getBalance(balance, triggerService)
|
|
231
262
|
const newAmount = config.currencyAdd(balanceData.amount, operationData.change)
|
|
232
|
-
const newAvailable = config.currencyAdd(balanceData.available, config.currencyNegate(operationData.change))
|
|
233
263
|
await triggerService({
|
|
234
264
|
service: definition.name,
|
|
235
265
|
type: 'balance_updateBalanceOwnedOperation',
|
|
236
266
|
}, {
|
|
267
|
+
balance,
|
|
237
268
|
operation,
|
|
238
269
|
state: 'finished',
|
|
239
270
|
amountAfter: newAmount
|
|
240
271
|
})
|
|
272
|
+
if(config.currencyIsPositive(operationData.change)) {
|
|
273
|
+
const newAvailable = config.currencyAdd(balanceData.available, operationData.change)
|
|
274
|
+
await triggerService({
|
|
275
|
+
service: definition.name,
|
|
276
|
+
type: 'balance_updateOwnerOwnedBalance',
|
|
277
|
+
}, {
|
|
278
|
+
ownerType: balanceData.ownerType, owner: balanceData.owner,
|
|
279
|
+
available: newAvailable,
|
|
280
|
+
amount: newAmount
|
|
281
|
+
})
|
|
282
|
+
} else {
|
|
283
|
+
await triggerService({
|
|
284
|
+
service: definition.name,
|
|
285
|
+
type: 'balance_updateOwnerOwnedBalance',
|
|
286
|
+
}, {
|
|
287
|
+
ownerType: balanceData.ownerType, owner: balanceData.owner,
|
|
288
|
+
amount: newAmount
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
return operation
|
|
292
|
+
}
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
definition.trigger({
|
|
296
|
+
name: 'balance_cancelOperation',
|
|
297
|
+
properties: {
|
|
298
|
+
balance: {
|
|
299
|
+
type: Balance,
|
|
300
|
+
validation: ['nonEmpty']
|
|
301
|
+
},
|
|
302
|
+
operation: {
|
|
303
|
+
type: Operation,
|
|
304
|
+
validation: ['nonEmpty']
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
queuedBy: 'balance',
|
|
308
|
+
async execute({ balance, operation }, { client, service, triggerService }) {
|
|
309
|
+
const operationData = await Operation.get(operation)
|
|
310
|
+
if(!operationData) throw "operationNotFound"
|
|
311
|
+
if(operationData.state !== 'started') throw "operationNotStarted"
|
|
312
|
+
if(operationData.balance !== balance) throw "balanceMismatch"
|
|
313
|
+
const balanceData = await getBalance(balance, triggerService)
|
|
314
|
+
const newAmount = balanceData.amount
|
|
241
315
|
await triggerService({
|
|
242
316
|
service: definition.name,
|
|
243
|
-
type: '
|
|
317
|
+
type: 'balance_updateBalanceOwnedOperation',
|
|
244
318
|
}, {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
319
|
+
balance,
|
|
320
|
+
operation,
|
|
321
|
+
state: 'canceled',
|
|
322
|
+
amountAfter: newAmount
|
|
248
323
|
})
|
|
324
|
+
if(!config.currencyIsPositive(operationData.change)) {
|
|
325
|
+
const newAvailable = config.currencyAdd(balanceData.available, config.currencyNegate(operationData.change))
|
|
326
|
+
await triggerService({
|
|
327
|
+
service: definition.name,
|
|
328
|
+
type: 'balance_updateOwnerOwnedBalance',
|
|
329
|
+
}, {
|
|
330
|
+
ownerType: balanceData.ownerType, owner: balanceData.owner,
|
|
331
|
+
available: newAvailable
|
|
332
|
+
})
|
|
333
|
+
}
|
|
249
334
|
return operation
|
|
250
335
|
}
|
|
251
336
|
})
|
|
252
337
|
|
|
253
338
|
definition.trigger({
|
|
254
|
-
name: '
|
|
339
|
+
name: 'balance_doOperation',
|
|
255
340
|
properties: {
|
|
256
341
|
balance: {
|
|
257
342
|
type: Balance,
|
|
@@ -270,12 +355,13 @@ definition.trigger({
|
|
|
270
355
|
validation: ['nonEmpty']
|
|
271
356
|
}
|
|
272
357
|
},
|
|
273
|
-
queuedBy:
|
|
358
|
+
queuedBy: 'balance',
|
|
274
359
|
async execute({ balance, causeType, cause, change }, { client, service, triggerService }) {
|
|
275
360
|
const operation = app.generateUid()
|
|
276
|
-
const balanceData = await
|
|
361
|
+
const balanceData = await getBalance(balance, triggerService)
|
|
277
362
|
if(!config.changePossible(balanceData.available, change)) throw "insufficientFunds"
|
|
278
363
|
const newAmount = config.currencyAdd(balanceData.amount, change)
|
|
364
|
+
const newAvailable = config.currencyAdd(balanceData.available, change)
|
|
279
365
|
await triggerService({
|
|
280
366
|
service: definition.name,
|
|
281
367
|
type: 'balance_createBalanceOwnedOperation',
|
|
@@ -290,7 +376,8 @@ definition.trigger({
|
|
|
290
376
|
type: 'balance_updateOwnerOwnedBalance',
|
|
291
377
|
}, {
|
|
292
378
|
ownerType: balanceData.ownerType, owner: balanceData.owner,
|
|
293
|
-
amount: newAmount
|
|
379
|
+
amount: newAmount,
|
|
380
|
+
available: newAvailable
|
|
294
381
|
})
|
|
295
382
|
return operation
|
|
296
383
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/balance-service",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.64",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"url": "https://www.viamage.com/"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/framework": "^0.8.
|
|
25
|
-
"@live-change/relations-plugin": "^0.8.
|
|
24
|
+
"@live-change/framework": "^0.8.64",
|
|
25
|
+
"@live-change/relations-plugin": "^0.8.64",
|
|
26
26
|
"lru-cache": "^7.12.0",
|
|
27
27
|
"pluralize": "^8.0.0",
|
|
28
28
|
"progress-stream": "^2.0.0",
|
|
29
29
|
"prosemirror-model": "^1.18.1"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "cf14b283170cd39b80a57865e9b5eb9cf2073f39",
|
|
32
32
|
"type": "module"
|
|
33
33
|
}
|