@jetthai/cuora 1.1.359-dev-7 → 1.1.360

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.
@@ -0,0 +1,251 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import api from '../../api/payment.js'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'withdraw',
8
+ initial: 'loading',
9
+ context: {
10
+ crypto: null,
11
+ networks: [],
12
+ currentNetwork: null,
13
+ currentAddress: '',
14
+ withdrawMethods: [],
15
+ withdrawRules: [],
16
+ withdrawLimit: null,
17
+ withdrawAmount: null,
18
+ differAmountLimit: null,
19
+ tutorialUrls: [],
20
+ enableWithdraw: false,
21
+ description: {},
22
+ policy: '',
23
+ ruleName: '',
24
+ balance: null,
25
+ charge: null,
26
+ isFree: false,
27
+ },
28
+ states: {
29
+ loading: {
30
+ invoke: {
31
+ src: 'preset',
32
+ onDone: {
33
+ target: 'checking',
34
+ actions: ['setCrypto', 'setWithdrawCondition', 'checkMinimumWithdraw'],
35
+ },
36
+ onError: {
37
+ target: 'fail',
38
+ },
39
+ },
40
+ },
41
+ fail: {},
42
+ checking: {
43
+ always: [{ target: 'transferable', cond: 'isTransferable' }, { target: 'notTransferable' }],
44
+ },
45
+ notTransferable: {},
46
+ transferable: {
47
+ initial: 'operation',
48
+ states: {
49
+ operation: {
50
+ on: {
51
+ SUBMIT: [
52
+ {
53
+ target: '#withdraw.exchanging',
54
+ cond: 'isFree',
55
+ },
56
+ {
57
+ target: '#withdraw.transferring',
58
+ },
59
+ ],
60
+ },
61
+ },
62
+ calculating: {
63
+ entry: 'resetCharge',
64
+ invoke: {
65
+ src: 'fetchCalculation',
66
+ onDone: {
67
+ target: 'operation',
68
+ actions: 'setCalculatedResult',
69
+ },
70
+ onError: {
71
+ target: 'operation',
72
+ },
73
+ },
74
+ },
75
+ },
76
+ on: {
77
+ SET_NETWORK: {
78
+ actions: 'setNetwork',
79
+ },
80
+ SET_ADDRESS: {
81
+ actions: 'setAddress',
82
+ },
83
+ CALCULATE: {
84
+ target: '.calculating',
85
+ cond: 'isCalculable',
86
+ },
87
+ },
88
+ },
89
+ exchanging: {
90
+ invoke: {
91
+ src: 'submitWithdrawFree',
92
+ onDone: {
93
+ target: 'transferred',
94
+ actions: 'setOtp',
95
+ },
96
+ onError: {
97
+ target: 'transferable.operation',
98
+ },
99
+ },
100
+ },
101
+ transferring: {
102
+ invoke: {
103
+ src: 'submitWithdraw',
104
+ onDone: {
105
+ target: 'transferred',
106
+ actions: 'setOtp',
107
+ },
108
+ onError: {
109
+ target: 'transferable.operation',
110
+ },
111
+ },
112
+ },
113
+ transferred: {},
114
+ },
115
+ },
116
+ {
117
+ actions: {
118
+ setWithdrawCondition: assign((context, event) => {
119
+ const { adUrl, balance, description, differAmountLimit, enableWithdraw, enableWithdrawAmount, policy, ruleName, rules } =
120
+ event.data.withdraw
121
+
122
+ return {
123
+ differAmountLimit: differAmountLimit,
124
+ withdrawAmount: enableWithdrawAmount,
125
+ withdrawLimit: enableWithdraw
126
+ ? 0
127
+ : rules.find((rule) => balance < rule.greaterEqualThen)?.greaterEqualThen || parseFloat(balance) + parseFloat(differAmountLimit),
128
+ withdrawRules: rules.filter((rule) => rule.maxWithdrawAmount > 0),
129
+ tutorialUrls: adUrl.map((item) => item.url),
130
+ enableWithdraw,
131
+ description,
132
+ policy,
133
+ ruleName,
134
+ balance,
135
+ isFree: policy === 'FREE',
136
+ }
137
+ }),
138
+ setCrypto: assign((context, event) => {
139
+ const method = event.data.payment?.[0]
140
+ return {
141
+ crypto: method,
142
+ networks:
143
+ method?.networkName?.map?.((value, i) => {
144
+ return {
145
+ label: method?.networkOptional?.[i],
146
+ value,
147
+ icon: value,
148
+ }
149
+ }) || [],
150
+ }
151
+ }),
152
+ setNetwork: assign((context, event) => {
153
+ return {
154
+ currentNetwork: event.data,
155
+ }
156
+ }),
157
+ setAddress: assign((context, event) => {
158
+ return {
159
+ currentAddress: event.data,
160
+ }
161
+ }),
162
+ setCalculatedResult: assign((context, event) => {
163
+ return {
164
+ charge: event.data.charge,
165
+ }
166
+ }),
167
+ resetCharge: assign((context, event) => {
168
+ return {
169
+ charge: null,
170
+ }
171
+ }),
172
+ checkMinimumWithdraw: assign((context, event) => {
173
+ const { balance, withdrawLimit, crypto, isFree } = context
174
+ if (!isFree && crypto?.minimum > withdrawLimit) {
175
+ const limit = crypto?.minimum
176
+ const distance = Math.max(limit - balance, 0)
177
+ return {
178
+ differAmountLimit: distance,
179
+ withdrawLimit: limit,
180
+ }
181
+ }
182
+ }),
183
+ setOtp: assign((context, event) => {
184
+ const { requestId, status, target, ttl } = event.data
185
+ return {
186
+ requestId,
187
+ status,
188
+ target,
189
+ ttl,
190
+ }
191
+ }),
192
+ },
193
+ guards: {
194
+ isFree: (context, event) => {
195
+ return context.isFree
196
+ },
197
+ isTransferable: (context, event) => {
198
+ const { enableWithdraw, balance, withdrawLimit } = context
199
+ return enableWithdraw && balance >= withdrawLimit
200
+ },
201
+ isCalculable: (context, event) => {
202
+ const { currentNetwork, balance } = context
203
+ return currentNetwork && balance
204
+ },
205
+ },
206
+ services: {
207
+ preset: async (context, event) => {
208
+ const [withdraw, payment] = await Promise.all([
209
+ request.send(api.getCurrentWithdrawRule()),
210
+ request.send(api.getPaymentDetail({ transferType: 'WITHDRAW' })),
211
+ ])
212
+ return { withdraw, payment }
213
+ },
214
+ submitWithdraw: async (context, event) => {
215
+ const { crypto, currentNetwork, currentAddress, withdrawAmount } = context
216
+ const method = crypto.method
217
+ return await request.send(
218
+ api.submitWithdrawTransactionCrypto({
219
+ method,
220
+ amount: withdrawAmount,
221
+ cryptoCurrency: crypto.channel,
222
+ cryptoNetwork: currentNetwork.value,
223
+ targetAddress: currentAddress,
224
+ })
225
+ )
226
+ },
227
+ submitWithdrawFree: async (context, event) => {
228
+ const { currentNetwork, currentAddress } = context
229
+ return await request.send(
230
+ api.convertFreeCredit({
231
+ exchangeType: 1,
232
+ cryptoNetwork: currentNetwork.value,
233
+ targetAddress: currentAddress,
234
+ })
235
+ )
236
+ },
237
+ fetchCalculation: async (context, event) => {
238
+ const { crypto, currentNetwork, withdrawAmount } = context
239
+ const method = crypto.method
240
+ return await request.send(
241
+ api.getWithdrawExpectedResult({
242
+ method,
243
+ amount: withdrawAmount,
244
+ cryptoNetwork: currentNetwork.value,
245
+ })
246
+ )
247
+ },
248
+ },
249
+ }
250
+ )
251
+ }
@@ -65,7 +65,7 @@ export default function (request) {
65
65
  invoke: {
66
66
  src: 'fetchWithdrawRules',
67
67
  onDone: {
68
- target: 'cash',
68
+ target: 'mode',
69
69
  actions: 'updateWithdrawCondition',
70
70
  },
71
71
  onError: {
@@ -73,6 +73,88 @@ export default function (request) {
73
73
  },
74
74
  },
75
75
  },
76
+ mode: {
77
+ always: [{ target: 'activity', cond: 'isActivity' }, { target: 'cash' }],
78
+ },
79
+ activity: {
80
+ initial: 'checking',
81
+ states: {
82
+ checking: {
83
+ always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
84
+ },
85
+ notConvertible: {
86
+ on: {
87
+ GIVE_UP: {
88
+ target: 'fetchWithdrawRule',
89
+ },
90
+ },
91
+ },
92
+ fetchWithdrawRule: {
93
+ invoke: {
94
+ src: 'fetchWithdrawRulePreview',
95
+ onDone: {
96
+ target: 'giveUp',
97
+ actions: 'setWithdrawRulePreview',
98
+ },
99
+ onError: {
100
+ target: 'notConvertible',
101
+ },
102
+ },
103
+ },
104
+ giveUp: {
105
+ on: {
106
+ CONFIRM: {
107
+ target: 'givingUp',
108
+ },
109
+ BACK: {
110
+ target: 'notConvertible',
111
+ },
112
+ },
113
+ },
114
+ givingUp: {
115
+ invoke: {
116
+ src: 'giveUp',
117
+ onDone: {
118
+ target: 'gaveUp',
119
+ },
120
+ onError: {
121
+ target: 'convertible',
122
+ },
123
+ },
124
+ },
125
+ gaveUp: {},
126
+ convertible: {
127
+ on: {
128
+ SUBMIT: {
129
+ target: 'converting',
130
+ actions: 'setAttachmentType',
131
+ },
132
+ },
133
+ },
134
+ converting: {
135
+ invoke: {
136
+ src: 'convertFreeCredit',
137
+ onDone: [
138
+ // {
139
+ // target: '#withdraw_manual.otp',
140
+ // actions: 'setOtp',
141
+ // cond: 'isOtpRequired',
142
+ // },
143
+ { target: 'converted' },
144
+ ],
145
+ onError: {
146
+ target: 'convertible',
147
+ },
148
+ },
149
+ },
150
+ converted: {
151
+ // always: [
152
+ // { target: '#withdraw_manual.cash.unbound', cond: 'isBankNotBound' },
153
+ // { target: '#withdraw_manual.cash.withdrawable', cond: 'hasAttachmentType' },
154
+ // ],
155
+ },
156
+ },
157
+ },
76
158
  cash: {
77
159
  initial: 'checking',
78
160
  states: {
@@ -251,6 +333,10 @@ export default function (request) {
251
333
  target: 'reset',
252
334
  },
253
335
  BACK: [
336
+ {
337
+ target: '#withdraw_manual.activity.convertible',
338
+ cond: 'isActivity',
339
+ },
254
340
  {
255
341
  target: '#withdraw_manual.cash.withdrawable',
256
342
  },
@@ -261,6 +347,10 @@ export default function (request) {
261
347
  invoke: {
262
348
  src: 'acknowledge',
263
349
  onDone: [
350
+ {
351
+ target: '#withdraw_manual.activity.converted',
352
+ cond: 'isActivity',
353
+ },
264
354
  {
265
355
  target: '#withdraw_manual.cash.submitted',
266
356
  },
@@ -407,6 +497,9 @@ export default function (request) {
407
497
  const lastMax = Math.min(parseFloat(balance), maximum)
408
498
  return !!currentBank && amount > 0 && minimum <= amount && amount <= lastMax
409
499
  },
500
+ isActivity: (context, event) => {
501
+ return context.mode === 'ACTIVITY'
502
+ },
410
503
  hasAttachmentType: (context, event) => {
411
504
  return ['withdraw', 'pool'].includes(context.attachmentType)
412
505
  },