@live-change/balance-service 0.8.53 → 0.8.54

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/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2024 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/balance.js CHANGED
@@ -21,4 +21,6 @@ const Balance = definition.model({
21
21
  type: Date
22
22
  }
23
23
  },
24
- })
24
+ })
25
+
26
+ export { Balance }
package/operation.js CHANGED
@@ -32,8 +32,7 @@ const Operation = definition.model({
32
32
  validation: ['nonEmpty']
33
33
  },
34
34
 
35
- availableChange: config.currencyType,
36
- amountChange: config.currencyType,
35
+ change: config.currencyType,
37
36
  amountBefore: config.currencyType,
38
37
  amountAfter: config.currencyType,
39
38
 
@@ -73,16 +72,27 @@ definition.trigger({
73
72
  },
74
73
 
75
74
  queuedBy: ['balance'],
76
- async execute({ balance, causeType, cause, change }, { client, service }) {
75
+ async execute({ balance, causeType, cause, change }, { client, service, triggerService }) {
77
76
  const operation = app.generateUid()
78
77
  const balanceData = await Balance.get(balance)
79
-
80
78
  if(!config.changePossible(balanceData.available, change)) throw "insufficientFunds"
81
-
82
79
  const newAvailable = config.currencyAdd(balanceData.available, change)
83
-
84
-
85
-
80
+ await triggerService({
81
+ service: definition.name,
82
+ type: 'balance_createBalanceOwnedOperation',
83
+ }, {
84
+ state: 'started',
85
+ balance, causeType, cause, change,
86
+ amountBefore: balanceData.amount,
87
+ amountAfter: null
88
+ })
89
+ await triggerService({
90
+ service: definition.name,
91
+ type: 'balance_updateOwnerOwnedBalance',
92
+ }, {
93
+ ownerType: balanceData.ownerType, owner: balanceData.owner,
94
+ available: newAvailable
95
+ })
86
96
  return operation
87
97
  }
88
98
  })
@@ -90,10 +100,86 @@ definition.trigger({
90
100
  definition.trigger({
91
101
  name: 'finishOperation',
92
102
  properties: {
103
+ balance: {
104
+ type: Balance,
105
+ validation: ['nonEmpty']
106
+ },
93
107
  operation: {
94
108
  type: Operation,
95
109
  validation: ['nonEmpty']
96
110
  }
97
111
  },
112
+ queuedBy: ['balance'],
113
+ async execute({ balance, operation }, { client, service, triggerService }) {
114
+ const operationData = await Operation.get(operation)
115
+ if(!operationData) throw "operationNotFound"
116
+ if(operationData.state !== 'started') throw "operationNotStarted"
117
+ if(operationData.balance !== balance) throw "balanceMismatch"
118
+ const balanceData = await Balance.get(balance)
119
+ const newAmount = config.currencyAdd(balanceData.amount, operationData.change)
120
+ const newAvailable = config.currencyAdd(balanceData.available, config.currencyNegate(operationData.change))
121
+ await triggerService({
122
+ service: definition.name,
123
+ type: 'balance_updateBalanceOwnedOperation',
124
+ }, {
125
+ operation,
126
+ state: 'finished',
127
+ amountAfter: newAmount
128
+ })
129
+ await triggerService({
130
+ service: definition.name,
131
+ type: 'balance_updateOwnerOwnedBalance',
132
+ }, {
133
+ ownerType: balanceData.ownerType, owner: balanceData.owner,
134
+ available: newAvailable,
135
+ amount: newAmount
136
+ })
137
+ return operation
138
+ }
139
+ })
98
140
 
99
- })
141
+ definition.trigger({
142
+ name: 'doOperation',
143
+ properties: {
144
+ balance: {
145
+ type: Balance,
146
+ validation: ['nonEmpty']
147
+ },
148
+ causeType: {
149
+ type: String,
150
+ validation: ['nonEmpty']
151
+ },
152
+ cause: {
153
+ type: String,
154
+ validation: ['nonEmpty']
155
+ },
156
+ change: {
157
+ ...config.currencyType,
158
+ validation: ['nonEmpty']
159
+ }
160
+ },
161
+ queuedBy: ['balance'],
162
+ async execute({ balance, causeType, cause, change }, { client, service, triggerService }) {
163
+ const operation = app.generateUid()
164
+ const balanceData = await Balance.get(balance)
165
+ if(!config.changePossible(balanceData.available, change)) throw "insufficientFunds"
166
+ const newAmount = config.currencyAdd(balanceData.amount, change)
167
+ await triggerService({
168
+ service: definition.name,
169
+ type: 'balance_createBalanceOwnedOperation',
170
+ }, {
171
+ state: 'finished',
172
+ balance, causeType, cause, change,
173
+ amountBefore: balanceData.amount,
174
+ amountAfter: newAmount
175
+ })
176
+ await triggerService({
177
+ service: definition.name,
178
+ type: 'balance_updateOwnerOwnedBalance',
179
+ }, {
180
+ ownerType: balanceData.ownerType, owner: balanceData.owner,
181
+ amount: newAmount
182
+ })
183
+ return operation
184
+ }
185
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/balance-service",
3
- "version": "0.8.53",
3
+ "version": "0.8.54",
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.53",
25
- "@live-change/relations-plugin": "^0.8.53",
24
+ "@live-change/framework": "^0.8.54",
25
+ "@live-change/relations-plugin": "^0.8.54",
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": "39422adadd1863fa396fc4e875936e09428d6cf1",
31
+ "gitHead": "1558b41fc331424507874c0afd3d149ffb849ca8",
32
32
  "type": "module"
33
33
  }