@jetthai/cuora 1.1.363 → 1.1.364-dev-1

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.
@@ -1,540 +0,0 @@
1
- import { assign, createMachine } from 'xstate'
2
- import findLastIndex from 'lodash/findLastIndex.js'
3
- import payment from '../../api/payment.js'
4
- import common from '../../api/common.js'
5
- import user from '../../api/user.js'
6
- import api from '../../api/payment.js'
7
-
8
- export default function (request) {
9
- const { appSecret } = request.config
10
- return createMachine(
11
- {
12
- id: 'withdraw_crypto2',
13
- initial: 'loading',
14
- context: {
15
- mode: null,
16
- siteConfig: null,
17
- balance: null,
18
-
19
- methods: [],
20
- paymentDetail: [],
21
-
22
- currentWithdrawLimit: null,
23
- targetWithdrawLimit: null,
24
-
25
- maxWithdrawAmount: null,
26
- availableWithdrawAmount: null,
27
- enableWithdraw: false,
28
- enableWithdrawAmount: 0,
29
- withdrawRules: [],
30
- withdrawRule: {},
31
- attachmentType: null, // null | 'withdraw' | 'pool'
32
- networks: [],
33
- selectedNetwork: null,
34
- address: '',
35
- amount: null,
36
- charge: null,
37
- realAmount: null,
38
-
39
- // otp
40
- requestId: null,
41
- status: null,
42
- target: null,
43
- ttl: null,
44
- },
45
- states: {
46
- loading: {
47
- invoke: {
48
- src: 'fetchInitData',
49
- onDone: {
50
- target: 'pending',
51
- actions: ['setInitData', 'preset'],
52
- },
53
- onError: {
54
- target: 'fail',
55
- },
56
- },
57
- },
58
- pending: {
59
- on: {
60
- SET_MODE: {
61
- target: 'fetching',
62
- actions: 'setMode',
63
- },
64
- },
65
- },
66
- fetching: {
67
- invoke: {
68
- src: 'fetchWithdrawRules',
69
- onDone: {
70
- target: 'mode',
71
- actions: 'updateWithdrawCondition',
72
- },
73
- onError: {
74
- target: '#withdraw_crypto2.fail',
75
- },
76
- },
77
- },
78
- mode: {
79
- always: [{ target: 'activity', cond: 'isActivity' }, { target: 'cash' }],
80
- },
81
- activity: {
82
- initial: 'checking',
83
- states: {
84
- checking: {
85
- always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
86
- },
87
- notConvertible: {
88
- on: {
89
- GIVE_UP: {
90
- target: 'fetchWithdrawRule',
91
- },
92
- },
93
- },
94
- fetchWithdrawRule: {
95
- invoke: {
96
- src: 'fetchWithdrawRulePreview',
97
- onDone: {
98
- target: 'giveUp',
99
- actions: 'setWithdrawRulePreview',
100
- },
101
- onError: {
102
- target: 'notConvertible',
103
- },
104
- },
105
- },
106
- giveUp: {
107
- on: {
108
- CONFIRM: {
109
- target: 'givingUp',
110
- },
111
- BACK: {
112
- target: 'notConvertible',
113
- },
114
- },
115
- },
116
- givingUp: {
117
- invoke: {
118
- src: 'giveUp',
119
- onDone: {
120
- target: 'gaveUp',
121
- },
122
- onError: {
123
- target: '#withdraw_crypto2.fail',
124
- },
125
- },
126
- },
127
- gaveUp: {},
128
- convertible: {
129
- on: {
130
- SUBMIT: {
131
- target: 'converting',
132
- actions: 'setAttachmentType',
133
- },
134
- },
135
- },
136
- converting: {
137
- invoke: {
138
- src: 'convertFreeCredit',
139
- onDone: [
140
- {
141
- target: '#withdraw_crypto2.otp',
142
- actions: 'setOtp',
143
- cond: 'isOtpRequired',
144
- },
145
- { target: 'converted' },
146
- ],
147
- onError: {
148
- target: 'convertible',
149
- },
150
- },
151
- },
152
- converted: {
153
- always: [{ target: '#withdraw_crypto2.cash.withdrawable', cond: 'hasAttachmentType' }],
154
- },
155
- },
156
- },
157
- cash: {
158
- initial: 'checking',
159
- states: {
160
- checking: {
161
- always: [
162
- { target: 'inProcessing', cond: 'haveProcessing' },
163
- { target: 'notWithdrawable', cond: 'isNotWithdrawable' },
164
- { target: 'withdrawable' },
165
- ],
166
- },
167
- notWithdrawable: {},
168
- inProcessing: {},
169
- withdrawable: {
170
- on: {
171
- SET_NETWORK: {
172
- actions: ['resetCharge', 'setNetwork'],
173
- },
174
- SET_ADDRESS: {
175
- actions: 'setAddress',
176
- },
177
- SET_AMOUNT: {
178
- actions: ['resetCharge', 'setAmount'],
179
- },
180
- CALCULATE: {
181
- target: 'calculating',
182
- cond: 'isCalculable',
183
- },
184
- PREVIEW: {
185
- target: 'submittable',
186
- cond: 'isSubmittable',
187
- },
188
- SET_ATTACHMENT_TYPE: {
189
- actions: 'setAttachmentType',
190
- },
191
- POOL: {
192
- target: 'pooling',
193
- cond: (context, event) => {
194
- const { amount, attachmentType } = context
195
- return attachmentType === 'pool' && amount > 0
196
- },
197
- },
198
- },
199
- },
200
- calculating: {
201
- entry: 'resetCharge',
202
- invoke: {
203
- src: 'fetchCalculation',
204
- onDone: {
205
- target: 'withdrawable',
206
- actions: 'setCalculatedResult',
207
- },
208
- onError: {
209
- target: 'withdrawable',
210
- },
211
- },
212
- },
213
- submittable: {
214
- on: {
215
- SUBMIT: [
216
- {
217
- target: 'pooling',
218
- cond: (context, event) => {
219
- const { amount, attachmentType } = context
220
- return attachmentType === 'pool' && amount > 0
221
- },
222
- },
223
- {
224
- target: 'submitting',
225
- },
226
- ],
227
- BACK: {
228
- target: 'withdrawable',
229
- },
230
- },
231
- },
232
- submitting: {
233
- invoke: {
234
- src: 'submitWithdraw',
235
- onDone: [
236
- {
237
- target: '#withdraw_crypto2.otp',
238
- actions: 'setOtp',
239
- cond: 'isOtpRequired',
240
- },
241
- {
242
- target: 'submitted',
243
- },
244
- ],
245
- onError: {
246
- target: 'withdrawable',
247
- },
248
- },
249
- },
250
- submitted: {},
251
- pooling: {
252
- invoke: {
253
- src: 'pool',
254
- onDone: { target: 'pooled' },
255
- onError: {
256
- target: 'withdrawable',
257
- },
258
- },
259
- },
260
- pooled: {},
261
- },
262
- },
263
- otp: {
264
- type: 'parallel',
265
- states: {
266
- countdown: {
267
- after: {
268
- 1000: {
269
- target: 'countdown',
270
- actions: 'decreaseTtl',
271
- cond: (context) => context.ttl > 0,
272
- },
273
- },
274
- },
275
- verification: {
276
- initial: 'idle',
277
- states: {
278
- idle: {
279
- on: {
280
- ACKNOWLEDGE: {
281
- target: 'acknowledging',
282
- },
283
- RESET_OTP: {
284
- target: 'reset',
285
- },
286
- BACK: [
287
- {
288
- target: '#withdraw_crypto2.activity.convertible',
289
- cond: 'isFree',
290
- },
291
- {
292
- target: '#withdraw_crypto2.cash.submittable',
293
- },
294
- ],
295
- },
296
- },
297
- acknowledging: {
298
- invoke: {
299
- src: 'acknowledge',
300
- onDone: [
301
- {
302
- target: '#withdraw_crypto2.activity.converted',
303
- cond: 'isFree',
304
- },
305
- {
306
- target: '#withdraw_crypto2.cash.submitted',
307
- },
308
- ],
309
- onError: {
310
- target: 'idle',
311
- },
312
- },
313
- },
314
- reset: {
315
- invoke: {
316
- src: 'resetOtp',
317
- onDone: {
318
- target: 'idle',
319
- actions: 'setVerificationData',
320
- },
321
- onError: { target: 'idle' },
322
- },
323
- },
324
- },
325
- },
326
- },
327
- },
328
- fail: {},
329
- },
330
- on: {
331
- REFRESH: {
332
- target: 'loading',
333
- },
334
- },
335
- },
336
- {
337
- actions: {
338
- setInitData: assign((context, event) => {
339
- const method = event.data.paymentDetail?.[0]
340
- return {
341
- paymentDetail: event.data.paymentDetail,
342
- crypto: method,
343
- networks:
344
- method?.networkName?.map?.((value, i) => {
345
- return {
346
- label: method?.networkOptional?.[i],
347
- value,
348
- icon: value,
349
- }
350
- }) || [],
351
- }
352
- }),
353
- preset: assign((context, event) => {
354
- const { siteConfig } = context
355
- const methods = siteConfig?.paymentMethods?.withdraw || []
356
- return {
357
- methods,
358
- attachmentType: 'withdraw',
359
- }
360
- }),
361
- setMode: assign((context, event) => {
362
- return {
363
- mode: event.data,
364
- }
365
- }),
366
- updateWithdrawCondition: assign((context, event) => {
367
- const { enableWithdraw, enableWithdrawAmount, currentWithdrawLimit, targetWithdrawLimit, balance } = event.data
368
- const withdrawRules = event.data?.rules || []
369
- const rules = withdrawRules.filter((rule) => rule.maxWithdrawAmount > 0)
370
- const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
371
-
372
- let maxWithdrawAmount = 0
373
-
374
- if (lastMatchedIndex > -1) {
375
- rules[lastMatchedIndex].match = true
376
- maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
377
- } else if (rules.length > 0) {
378
- maxWithdrawAmount = rules[0].maxWithdrawAmount
379
- } else {
380
- maxWithdrawAmount = balance
381
- }
382
-
383
- return {
384
- balance,
385
- currentWithdrawLimit,
386
- targetWithdrawLimit,
387
- maxWithdrawAmount,
388
- enableWithdrawAmount: +enableWithdrawAmount,
389
- enableWithdraw,
390
- withdrawRules,
391
- }
392
- }),
393
- setWithdrawRulePreview: assign((context, event) => {
394
- return {
395
- withdrawRule: event.data,
396
- }
397
- }),
398
- setNetwork: assign((context, event) => {
399
- return {
400
- selectedNetwork: event.data,
401
- }
402
- }),
403
- setAddress: assign((context, event) => {
404
- return {
405
- address: event.data,
406
- }
407
- }),
408
- setAmount: assign((context, event) => {
409
- return {
410
- amount: event.data,
411
- }
412
- }),
413
- setCalculatedResult: assign((context, event) => {
414
- return {
415
- charge: event.data.charge,
416
- }
417
- }),
418
- resetCharge: assign((context, event) => {
419
- return {
420
- charge: null,
421
- }
422
- }),
423
- setOtp: assign((context, event) => {
424
- const { requestId, status, target, ttl } = event.data
425
- return {
426
- requestId,
427
- status,
428
- target,
429
- ttl,
430
- }
431
- }),
432
- decreaseTtl: assign((context, evnet) => {
433
- const second = context.ttl
434
- return {
435
- ttl: Math.max(second - 1, 0),
436
- }
437
- }),
438
- setVerificationData: assign((context, event) => {
439
- const { requestId, ttl, target } = event.data
440
- return {
441
- requestId,
442
- ttl,
443
- target,
444
- }
445
- }),
446
- setAttachmentType: assign((context, event) => {
447
- return {
448
- attachmentType: event.data,
449
- }
450
- }),
451
- },
452
- guards: {
453
- isActivity: (context, event) => {
454
- return context.mode === 'ACTIVITY'
455
- },
456
- hasAttachmentType: (context, event) => {
457
- return ['withdraw', 'pool'].includes(context.attachmentType)
458
- },
459
- haveProcessing: (context, event) => {
460
- return (context.paymentDetail?.[0]?.processing || []).length > 0
461
- },
462
- isSubmittable: (context, event) => {
463
- const { selectedNetwork, address, amount, charge } = context
464
- return selectedNetwork && address && amount && charge
465
- },
466
- isConvertible: (context, event) => {
467
- const { enableWithdraw } = context
468
- return enableWithdraw
469
- },
470
- isNotWithdrawable: (context, event) => {
471
- const { enableWithdraw } = context
472
- return !enableWithdraw
473
- },
474
- isCalculable: (context, event) => {
475
- const { selectedNetwork, amount } = context
476
- return selectedNetwork && amount
477
- },
478
- isOtpRequired: (context, event) => {
479
- return event.data?.status === 'otp-verifying'
480
- },
481
- },
482
- services: {
483
- fetchInitData: async (context, event) => {
484
- const paymentDetail = await request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' }))
485
- return { paymentDetail }
486
- },
487
- fetchWithdrawRules: async (context, event) => {
488
- const { mode } = context
489
- return await request.send(payment.getDoubleWalletWithdrawRule({ walletType: mode }))
490
- },
491
- fetchWithdrawRulePreview: async (context, event) => {
492
- const data = await request.send(payment.getWithdrawRulePreview({ applyType: 'giveup' }))
493
- return data
494
- },
495
- submitWithdraw: async (context, event) => {
496
- const { crypto, selectedNetwork, address, amount } = context
497
- return await request.send(
498
- api.submitWithdrawTransactionCrypto({
499
- method: crypto.method,
500
- amount,
501
- cryptoCurrency: crypto.channel,
502
- cryptoNetwork: selectedNetwork.value,
503
- targetAddress: address,
504
- })
505
- )
506
- },
507
- convertFreeCredit: async (context, event) => {
508
- return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
509
- },
510
- fetchCalculation: async (context, event) => {
511
- const { crypto, selectedNetwork, amount } = context
512
- const method = crypto.method
513
- return await request.send(
514
- api.getWithdrawExpectedResult({
515
- method,
516
- amount,
517
- cryptoNetwork: selectedNetwork.value,
518
- })
519
- )
520
- },
521
- resetOtp: async (context, event) => {
522
- const { requestId, target } = context
523
- return await request.send(user.otpReset({ requestId, target }))
524
- },
525
- acknowledge: async (context, event) => {
526
- const { requestId, target } = context
527
- const code = event.data
528
- return await request.send(user.otpAcknowledge({ requestId, target, code }))
529
- },
530
- pool: async (context, event) => {
531
- const { amount } = context
532
- return await request.send(payment.restore({ amount }))
533
- },
534
- giveUp: async (context, event) => {
535
- return await request.send(payment.giveUp())
536
- },
537
- },
538
- }
539
- )
540
- }