@jetthai/cuora 1.1.85 → 1.1.86

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.1.85",
3
+ "version": "1.1.86",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -26,6 +26,7 @@ import bank_binding from './bank_binding.js'
26
26
  import bank_binding_amb from './bank_binding_amb.js'
27
27
  import withdraw_fc from './withdraw_fc.js'
28
28
  import withdraw_vn from './withdraw_vn.js'
29
+ import withdraw_vn2 from './withdraw_vn2.js'
29
30
  import withdraw_crypto from './withdraw_crypto.js'
30
31
  import withdraw_amb from './withdraw_amb.js'
31
32
  import deposit from './deposit/index.js'
@@ -67,6 +68,7 @@ export default {
67
68
  bank_binding_amb,
68
69
  withdraw_fc,
69
70
  withdraw_vn,
71
+ withdraw_vn2,
70
72
  withdraw_crypto,
71
73
  withdraw_amb,
72
74
  ...deposit,
@@ -0,0 +1,489 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import findLastIndex from 'lodash/findLastIndex.js'
3
+ import payment from '../api/payment.js'
4
+ import user from '../api/user.js'
5
+
6
+ export default function (request) {
7
+ const { appSecret } = request.config
8
+ return createMachine(
9
+ {
10
+ id: 'withdraw_vn2',
11
+ initial: 'loading',
12
+ context: {
13
+ mode: null,
14
+ siteConfig: null,
15
+ userInfo: null,
16
+ balance: null,
17
+ charge: null,
18
+ realAmount: null,
19
+ applyId: null,
20
+ methods: [],
21
+ paymentDetail: [],
22
+ bankInfo: [],
23
+ bankList: [],
24
+ currentWithdrawLimit: null,
25
+ targetWithdrawLimit: null,
26
+ attachmentType: null, // null | 'withdraw' | 'pool'
27
+ enableWithdrawAmount: 0,
28
+ enableWithdraw: false,
29
+ withdrawRules: [],
30
+
31
+ amount: null,
32
+ bank: null,
33
+ branch: null,
34
+ accountNumber: null,
35
+ holderName: null,
36
+
37
+ // otp
38
+ requestId: null,
39
+ status: null,
40
+ target: null,
41
+ ttl: null,
42
+ },
43
+ states: {
44
+ loading: {
45
+ invoke: {
46
+ src: 'fetchInitData',
47
+ onDone: {
48
+ target: 'pending',
49
+ actions: ['setInitData', 'preset'],
50
+ },
51
+ onError: {
52
+ target: 'fail',
53
+ },
54
+ },
55
+ },
56
+ pending: {
57
+ on: {
58
+ SET_MODE: {
59
+ target: 'fetching',
60
+ actions: 'setMode',
61
+ },
62
+ },
63
+ },
64
+ fetching: {
65
+ invoke: {
66
+ src: 'fetchWithdrawRules',
67
+ onDone: {
68
+ target: 'mode',
69
+ actions: 'updateWithdrawCondition',
70
+ },
71
+ onError: {
72
+ target: '#withdraw_vn2.fail',
73
+ },
74
+ },
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: 'givingUp',
89
+ },
90
+ },
91
+ },
92
+ givingUp: {
93
+ invoke: {
94
+ src: 'giveUp',
95
+ onDone: {
96
+ target: 'gaveUp',
97
+ },
98
+ onError: {
99
+ target: 'convertible',
100
+ },
101
+ },
102
+ },
103
+ gaveUp: {},
104
+ convertible: {
105
+ on: {
106
+ SUBMIT: {
107
+ target: 'converting',
108
+ actions: 'setAttachmentType',
109
+ },
110
+ },
111
+ },
112
+ converting: {
113
+ invoke: {
114
+ src: 'convertFreeCredit',
115
+ onDone: [
116
+ {
117
+ target: '#withdraw_vn2.otp',
118
+ actions: 'setOtp',
119
+ cond: 'isOtpRequired',
120
+ },
121
+ { target: 'converted' },
122
+ ],
123
+ onError: {
124
+ target: 'convertible',
125
+ },
126
+ },
127
+ },
128
+ converted: {
129
+ always: [
130
+ { target: '#withdraw_vn2.cash.unbound', cond: 'isBankNotBound' },
131
+ { target: '#withdraw_vn2.cash.withdrawable', cond: 'hasAttachmentType' },
132
+ ],
133
+ },
134
+ },
135
+ },
136
+ cash: {
137
+ initial: 'checking',
138
+ states: {
139
+ checking: {
140
+ always: [
141
+ { target: 'inProcessing', cond: 'haveProcessing' },
142
+ { target: 'notWithdrawable', cond: 'isNotWithdrawable' },
143
+ { target: 'unbound', cond: 'isBankNotBound' },
144
+ { target: 'withdrawable' },
145
+ ],
146
+ },
147
+ notWithdrawable: {},
148
+ unbound: {
149
+ // to bank_binding.js
150
+ },
151
+ inProcessing: {},
152
+ withdrawable: {
153
+ on: {
154
+ SET_CURRENT_BANK: {
155
+ actions: assign((context, event) => {
156
+ return {
157
+ bank: event.data,
158
+ }
159
+ }),
160
+ },
161
+ SET_AMOUNT: {
162
+ actions: assign((context, event) => {
163
+ return {
164
+ amount: event.data,
165
+ }
166
+ }),
167
+ },
168
+ SET_ATTACHMENT_TYPE: {
169
+ actions: 'setAttachmentType',
170
+ cond: 'isNewWalletMode',
171
+ },
172
+ SUBMIT: [
173
+ {
174
+ target: 'pooling',
175
+ cond: (context, event) => {
176
+ const { amount, attachmentType } = context
177
+ return attachmentType === 'pool' && amount > 0
178
+ },
179
+ },
180
+ {
181
+ target: 'submitting',
182
+ cond: 'isSubmittable',
183
+ },
184
+ ],
185
+ },
186
+ },
187
+ submitting: {
188
+ invoke: {
189
+ src: 'submitWithdraw',
190
+ onDone: [
191
+ {
192
+ target: '#withdraw_vn2.otp',
193
+ actions: 'setOtp',
194
+ cond: 'isOtpRequired',
195
+ },
196
+ {
197
+ target: 'submitted',
198
+ },
199
+ ],
200
+ onError: {
201
+ target: 'withdrawable',
202
+ },
203
+ },
204
+ },
205
+ submitted: {
206
+ entry: assign((context, event) => {
207
+ const { applyId, balance, realAmount, charge } = event.data
208
+ return { applyId, balance, realAmount, charge }
209
+ }),
210
+ invoke: {
211
+ src: 'fetchPaymentData',
212
+ onDone: {
213
+ target: 'inProcessing',
214
+ actions: 'setPaymentData',
215
+ },
216
+ onError: {
217
+ target: '#withdraw_vn2.fail',
218
+ },
219
+ },
220
+ },
221
+ pooling: {
222
+ invoke: {
223
+ src: 'pool',
224
+ onDone: { target: 'pooled' },
225
+ onError: {
226
+ target: 'withdrawable',
227
+ },
228
+ },
229
+ },
230
+ pooled: {},
231
+ },
232
+ },
233
+ otp: {
234
+ type: 'parallel',
235
+ states: {
236
+ countdown: {
237
+ after: {
238
+ 1000: {
239
+ target: 'countdown',
240
+ actions: 'decreaseTtl',
241
+ cond: (context) => context.ttl > 0,
242
+ },
243
+ },
244
+ },
245
+ verification: {
246
+ initial: 'idle',
247
+ states: {
248
+ idle: {
249
+ on: {
250
+ ACKNOWLEDGE: {
251
+ target: 'acknowledging',
252
+ },
253
+ RESET_OTP: {
254
+ target: 'reset',
255
+ },
256
+ BACK: [
257
+ {
258
+ target: '#withdraw_vn2.activity.convertible',
259
+ cond: 'isActivity',
260
+ },
261
+ {
262
+ target: '#withdraw_vn2.cash.withdrawable',
263
+ },
264
+ ],
265
+ },
266
+ },
267
+ acknowledging: {
268
+ invoke: {
269
+ src: 'acknowledge',
270
+ onDone: [
271
+ {
272
+ target: '#withdraw_vn2.activity.converted',
273
+ cond: 'isActivity',
274
+ },
275
+ {
276
+ target: '#withdraw_vn2.cash.submitted',
277
+ },
278
+ ],
279
+ onError: {
280
+ target: 'idle',
281
+ },
282
+ },
283
+ },
284
+ reset: {
285
+ invoke: {
286
+ src: 'resetOtp',
287
+ onDone: {
288
+ target: 'idle',
289
+ actions: 'setVerificationData',
290
+ },
291
+ onError: { target: 'idle' },
292
+ },
293
+ },
294
+ },
295
+ },
296
+ },
297
+ },
298
+ fail: {},
299
+ },
300
+ on: {
301
+ REFRESH: {
302
+ target: 'loading',
303
+ },
304
+ },
305
+ },
306
+ {
307
+ actions: {
308
+ setInitData: assign((context, event) => {
309
+ return {
310
+ paymentDetail: event.data.paymentDetail,
311
+ bankInfo: event.data.bankInfo,
312
+ bank: event.data.bankInfo[0],
313
+ bonus: event.data.bonus,
314
+ bankList: event.data.bankList,
315
+ }
316
+ }),
317
+ setPaymentData: assign((context, event) => {
318
+ return {
319
+ bankInfo: event.data.bankInfo,
320
+ bonus: event.data.bonus,
321
+ paymentDetail: event.data.paymentDetail,
322
+ }
323
+ }),
324
+ preset: assign((context, event) => {
325
+ const { siteConfig } = context
326
+ const methods = siteConfig?.paymentMethods?.withdraw || []
327
+ return {
328
+ methods,
329
+ siteConfig,
330
+ attachmentType: siteConfig.walletMode > 0 ? 'withdraw' : null,
331
+ }
332
+ }),
333
+ updateWithdrawCondition: assign((context, event) => {
334
+ const { enableWithdraw, enableWithdrawAmount, currentWithdrawLimit, targetWithdrawLimit, balance } = event.data
335
+ const withdrawRules = event.data?.rules || []
336
+ const rules = withdrawRules.filter((rule) => rule.maxWithdrawAmount > 0)
337
+ const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
338
+
339
+ let maxWithdrawAmount = 0
340
+
341
+ if (lastMatchedIndex > -1) {
342
+ rules[lastMatchedIndex].match = true
343
+ maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
344
+ } else if (rules.length > 0) {
345
+ maxWithdrawAmount = rules[0].maxWithdrawAmount
346
+ } else {
347
+ maxWithdrawAmount = balance
348
+ }
349
+
350
+ return {
351
+ balance,
352
+ currentWithdrawLimit,
353
+ targetWithdrawLimit,
354
+ maxWithdrawAmount,
355
+ enableWithdrawAmount: +enableWithdrawAmount,
356
+ enableWithdraw,
357
+ withdrawRules,
358
+ }
359
+ }),
360
+ setMode: assign((context, event) => {
361
+ return {
362
+ mode: event.data,
363
+ }
364
+ }),
365
+ setOtp: assign((context, event) => {
366
+ const { requestId, status, target, ttl } = event.data
367
+ return {
368
+ requestId,
369
+ status,
370
+ target,
371
+ ttl,
372
+ }
373
+ }),
374
+ decreaseTtl: assign((context, evnet) => {
375
+ const second = context.ttl
376
+ return {
377
+ ttl: Math.max(second - 1, 0),
378
+ }
379
+ }),
380
+ setVerificationData: assign((context, event) => {
381
+ const { requestId, ttl, target } = event.data
382
+ return {
383
+ requestId,
384
+ ttl,
385
+ target,
386
+ }
387
+ }),
388
+ setAttachmentType: assign((context, event) => {
389
+ return {
390
+ attachmentType: event.data,
391
+ }
392
+ }),
393
+ },
394
+ guards: {
395
+ isActivity: (context, event) => {
396
+ return context.mode === 'ACTIVITY'
397
+ },
398
+ hasAttachmentType: (context, event) => {
399
+ return ['withdraw', 'pool'].includes(context.attachmentType)
400
+ },
401
+ haveProcessing: (context, event) => {
402
+ return (context.paymentDetail?.[0]?.processing || []).length > 0
403
+ },
404
+ isBankNotBound: (context, event) => {
405
+ return !context.bankInfo?.length
406
+ },
407
+ isSubmittable: (context, event) => {
408
+ const { bank, amount } = context
409
+ return bank && amount > 0
410
+ },
411
+ isConvertible: (context, event) => {
412
+ const { balance, enableWithdraw } = context
413
+ return balance > 0 && enableWithdraw
414
+ },
415
+ isNotWithdrawable: (context, event) => {
416
+ const { balance, enableWithdraw, paymentDetail } = context
417
+ const minWithdrawAmount = paymentDetail[0]?.minimum || 0
418
+ return !enableWithdraw || balance < minWithdrawAmount
419
+ },
420
+ isOtpRequired: (context, event) => {
421
+ return event.data?.status === 'otp-verifying'
422
+ },
423
+ isNewWalletMode: (context, event) => context.siteConfig.walletMode > 0,
424
+ },
425
+ services: {
426
+ fetchInitData: async (context, event) => {
427
+ const [paymentDetail = [], { bonus, bankInfo }] = await Promise.all([
428
+ request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' })),
429
+ request.send(payment.getPaymentInfo()),
430
+ ])
431
+ const method = paymentDetail.find((payment) => payment.category === 'manual').method
432
+ const bankList = await request.send(payment.getBankList(method))
433
+ const newBankInfo = bankInfo.map((item) => {
434
+ return { ...item, ...bankList.find((bank) => bank.bankCode === item.bankCode) }
435
+ })
436
+ return { paymentDetail, bonus, bankInfo: newBankInfo, bankList }
437
+ },
438
+ fetchPaymentData: async (context, event) => {
439
+ const paymentDetail = await request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' }))
440
+ const { bonus, bankInfo } = await request.send(payment.getPaymentInfo())
441
+ return { bonus, bankInfo, paymentDetail }
442
+ },
443
+ fetchWithdrawRules: async (context, event) => {
444
+ const { siteConfig = {}, mode } = context
445
+ const { walletMode = null } = siteConfig
446
+ if (walletMode === '4') {
447
+ return await request.send(payment.getDoubleWalletWithdrawRule({ walletType: mode }))
448
+ } else {
449
+ return await request.send(payment.getCurrentWithdrawRule())
450
+ }
451
+ },
452
+ submitWithdraw: async (context, event) => {
453
+ const { bank, amount } = context
454
+ const { method } = context.paymentDetail[0]
455
+ const branchTemp = bank.bankCode === 'GCASH' ? '' : bank.branch
456
+ return await request.send(
457
+ payment.submitWithdrawTransactionVN({
458
+ method,
459
+ amount,
460
+ applyerBankCode: bank.bankCode,
461
+ applyerAccountNumber: bank.accountNumber,
462
+ branch: branchTemp,
463
+ holderName: bank.holderName,
464
+ })
465
+ )
466
+ },
467
+ convertFreeCredit: async (context, event) => {
468
+ return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
469
+ },
470
+ resetOtp: async (context, event) => {
471
+ const { requestId, target } = context
472
+ return await request.send(user.otpReset({ requestId, target }))
473
+ },
474
+ acknowledge: async (context, event) => {
475
+ const { requestId, target } = context
476
+ const code = event.data
477
+ return await request.send(user.otpAcknowledge({ requestId, target, code }))
478
+ },
479
+ pool: async (context, event) => {
480
+ const { amount } = context
481
+ return await request.send(payment.restore({ amount }))
482
+ },
483
+ giveUp: async (context, event) => {
484
+ return await request.send(payment.giveUp())
485
+ },
486
+ },
487
+ }
488
+ )
489
+ }