@live-change/balance-service 0.8.53

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 ADDED
@@ -0,0 +1,24 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+ import config from './config.js'
6
+
7
+ const Balance = definition.model({
8
+ name: "Balance",
9
+ propertyOfAny: {
10
+ readAccessControl: {
11
+ roles: ['owner', 'admin']
12
+ }
13
+ },
14
+ properties: {
15
+ available: config.currencyType,
16
+ amount: {
17
+ type: Number,
18
+ defaultValue: 0
19
+ },
20
+ lastUpdate: {
21
+ type: Date
22
+ }
23
+ },
24
+ })
package/config.js ADDED
@@ -0,0 +1,24 @@
1
+ import definition from './definition.js'
2
+
3
+ const {
4
+ currencyType = {
5
+ type: Number,
6
+ default: 0
7
+ },
8
+ currencyAdd = (...args) => {
9
+ return args.reduce((a, b) => a + b, 0)
10
+ },
11
+ currencyNegate = (value) => -value,
12
+ changePossible = (value, change) => {
13
+ return value + change >= 0
14
+ },
15
+ nextRecalculateTime = (value) => null, // no recalculation by default, will be used with vector balances
16
+ recalculate = (value, time) => value // no recalculation by default, will be used with vector balances
17
+ } = definition.config
18
+
19
+ const config = {
20
+ currencyType, currencyAdd, changePossible, currencyNegate,
21
+ nextRecalculateTime, recalculate
22
+ }
23
+
24
+ export default config
package/definition.js ADDED
@@ -0,0 +1,12 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import relationsPlugin from '@live-change/relations-plugin'
5
+ import accessControlService from '@live-change/access-control-service'
6
+
7
+ const definition = app.createServiceDefinition({
8
+ name: "balance",
9
+ use: [ relationsPlugin, accessControlService ]
10
+ })
11
+
12
+ export default definition
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+
6
+ import './balance.js'
7
+ import './operation.js'
8
+
9
+ export default definition
package/operation.js ADDED
@@ -0,0 +1,99 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+ import config from './config.js'
6
+
7
+ import { Balance } from './balance.js'
8
+
9
+ const Operation = definition.model({
10
+ name: "Operation",
11
+ itemOf: {
12
+ what: Balance,
13
+ readAccessControl: {
14
+ roles: ['owner', 'admin']
15
+ }
16
+ },
17
+ properties: {
18
+ state: {
19
+ type: String,
20
+ options: [
21
+ 'started', // funds are locked, but amount is not changed yet
22
+ 'finished' // amount is changed, and funds are unlocked
23
+ ],
24
+ },
25
+
26
+ causeType: {
27
+ type: String,
28
+ validation: ['nonEmpty']
29
+ },
30
+ cause: {
31
+ type: String,
32
+ validation: ['nonEmpty']
33
+ },
34
+
35
+ availableChange: config.currencyType,
36
+ amountChange: config.currencyType,
37
+ amountBefore: config.currencyType,
38
+ amountAfter: config.currencyType,
39
+
40
+ updatedAt: {
41
+ type: Date,
42
+ updated: () => new Date(),
43
+ },
44
+ createdAt: {
45
+ type: Date,
46
+ default: () => new Date(),
47
+ },
48
+ },
49
+ })
50
+
51
+ export { Operation }
52
+
53
+
54
+ definition.trigger({
55
+ name: "startOperation",
56
+ properties: {
57
+ balance: {
58
+ type: Balance,
59
+ validation: ['nonEmpty']
60
+ },
61
+ causeType: {
62
+ type: String,
63
+ validation: ['nonEmpty']
64
+ },
65
+ cause: {
66
+ type: String,
67
+ validation: ['nonEmpty']
68
+ },
69
+ change: {
70
+ ...config.currencyType,
71
+ validation: ['nonEmpty']
72
+ }
73
+ },
74
+
75
+ queuedBy: ['balance'],
76
+ async execute({ balance, causeType, cause, change }, { client, service }) {
77
+ const operation = app.generateUid()
78
+ const balanceData = await Balance.get(balance)
79
+
80
+ if(!config.changePossible(balanceData.available, change)) throw "insufficientFunds"
81
+
82
+ const newAvailable = config.currencyAdd(balanceData.available, change)
83
+
84
+
85
+
86
+ return operation
87
+ }
88
+ })
89
+
90
+ definition.trigger({
91
+ name: 'finishOperation',
92
+ properties: {
93
+ operation: {
94
+ type: Operation,
95
+ validation: ['nonEmpty']
96
+ }
97
+ },
98
+
99
+ })
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@live-change/balance-service",
3
+ "version": "0.8.53",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "NODE_ENV=test tape tests/*"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/live-change/live-change-stack.git"
12
+ },
13
+ "license": "MIT",
14
+ "bugs": {
15
+ "url": "https://github.com/live-change/live-change-stack/issues"
16
+ },
17
+ "homepage": "https://github.com/live-change/live-change-stack",
18
+ "author": {
19
+ "email": "michal@laszczewski.pl",
20
+ "name": "Michał Łaszczewski",
21
+ "url": "https://www.viamage.com/"
22
+ },
23
+ "dependencies": {
24
+ "@live-change/framework": "^0.8.53",
25
+ "@live-change/relations-plugin": "^0.8.53",
26
+ "lru-cache": "^7.12.0",
27
+ "pluralize": "^8.0.0",
28
+ "progress-stream": "^2.0.0",
29
+ "prosemirror-model": "^1.18.1"
30
+ },
31
+ "gitHead": "39422adadd1863fa396fc4e875936e09428d6cf1",
32
+ "type": "module"
33
+ }