@jetthai/cuora 1.1.82 → 1.1.83-cash

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,383 @@
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
+
7
+ export default function (request) {
8
+ const { appSecret } = request.config
9
+ return createMachine(
10
+ {
11
+ id: 'withdraw_activity',
12
+ initial: 'loading',
13
+ context: {
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
+ withdrawLimit: null,
25
+ targetWithdrawLimit: null,
26
+ attachmentType: null, // null | 'withdraw' | 'pool'
27
+ maximumWithdrawAmount: 0,
28
+ enableWithdrawAmount: 0,
29
+ enableWithdraw: false,
30
+ withdrawRules: [],
31
+
32
+ amount: null,
33
+ bank: null,
34
+ branch: null,
35
+ accountNumber: null,
36
+ holderName: null,
37
+
38
+ // otp
39
+ requestId: null,
40
+ status: null,
41
+ target: null,
42
+ ttl: null,
43
+ },
44
+ states: {
45
+ loading: {
46
+ invoke: {
47
+ src: 'fetchInitData',
48
+ onDone: {
49
+ target: 'free',
50
+ actions: ['setInitData', 'preset', 'updateWithdrawCondition'],
51
+ },
52
+ onError: {
53
+ target: 'fail',
54
+ },
55
+ },
56
+ },
57
+ free: {
58
+ initial: 'checking',
59
+ states: {
60
+ checking: {
61
+ always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
62
+ },
63
+ notConvertible: {
64
+ on: {
65
+ GIVE_UP: {
66
+ target: 'givingUp',
67
+ },
68
+ },
69
+ },
70
+ givingUp: {
71
+ invoke: {
72
+ src: 'giveUp',
73
+ onDone: {
74
+ target: 'gaveUp',
75
+ },
76
+ onError: {
77
+ target: 'convertible',
78
+ },
79
+ },
80
+ },
81
+ gaveUp: {},
82
+ convertible: {
83
+ on: {
84
+ SUBMIT: {
85
+ target: 'converting',
86
+ actions: 'setAttachmentType',
87
+ },
88
+ },
89
+ },
90
+ converting: {
91
+ invoke: {
92
+ src: 'convertFreeCredit',
93
+ onDone: [
94
+ {
95
+ target: '#withdraw_vn.otp',
96
+ actions: 'setOtp',
97
+ cond: 'isOtpRequired',
98
+ },
99
+ { target: 'converted' },
100
+ ],
101
+ onError: {
102
+ target: 'convertible',
103
+ },
104
+ },
105
+ },
106
+ converted: {},
107
+ },
108
+ },
109
+ otp: {
110
+ type: 'parallel',
111
+ states: {
112
+ countdown: {
113
+ after: {
114
+ 1000: {
115
+ target: 'countdown',
116
+ actions: 'decreaseTtl',
117
+ cond: (context) => context.ttl > 0,
118
+ },
119
+ },
120
+ },
121
+ verification: {
122
+ initial: 'idle',
123
+ states: {
124
+ idle: {
125
+ on: {
126
+ ACKNOWLEDGE: {
127
+ target: 'acknowledging',
128
+ },
129
+ RESET_OTP: {
130
+ target: 'reset',
131
+ },
132
+ BACK: [
133
+ {
134
+ target: '#withdraw_vn.free.convertible',
135
+ cond: 'isFree',
136
+ },
137
+ {
138
+ target: '#withdraw_vn.cash.withdrawable',
139
+ },
140
+ ],
141
+ },
142
+ },
143
+ acknowledging: {
144
+ invoke: {
145
+ src: 'acknowledge',
146
+ onDone: [
147
+ {
148
+ target: '#withdraw_vn.free.converted',
149
+ cond: 'isFree',
150
+ },
151
+ {
152
+ target: '#withdraw_vn.cash.submitted',
153
+ },
154
+ ],
155
+ onError: {
156
+ target: 'idle',
157
+ },
158
+ },
159
+ },
160
+ reset: {
161
+ invoke: {
162
+ src: 'resetOtp',
163
+ onDone: {
164
+ target: 'idle',
165
+ actions: 'setVerificationData',
166
+ },
167
+ onError: { target: 'idle' },
168
+ },
169
+ },
170
+ },
171
+ },
172
+ },
173
+ },
174
+ fail: {},
175
+ },
176
+ },
177
+ {
178
+ actions: {
179
+ setInitData: assign((context, event) => {
180
+ return {
181
+ paymentDetail: event.data.paymentDetail,
182
+ bankInfo: event.data.bankInfo,
183
+ bank: event.data.bankInfo[0],
184
+ bonus: event.data.bonus,
185
+ bankList: event.data.bankList,
186
+ }
187
+ }),
188
+ setPaymentData: assign((context, event) => {
189
+ return {
190
+ bankInfo: event.data.bankInfo,
191
+ bonus: event.data.bonus,
192
+ paymentDetail: event.data.paymentDetail,
193
+ }
194
+ }),
195
+ preset: assign((context, event) => {
196
+ const { userInfo, siteConfig } = event.data
197
+ const { withdraw_limit, target_withdraw_limit } = userInfo || {}
198
+ const balance = parseFloat(userInfo?.balance)
199
+ const methods = siteConfig?.paymentMethods?.withdraw || []
200
+ return {
201
+ withdrawLimit: Math.abs(withdraw_limit),
202
+ targetWithdrawLimit: Math.abs(target_withdraw_limit),
203
+ methods,
204
+ balance,
205
+ siteConfig,
206
+ userInfo,
207
+ attachmentType: siteConfig.walletMode > 0 ? 'withdraw' : null,
208
+ }
209
+ }),
210
+ updateWithdrawCondition: assign((context, event) => {
211
+ const { differAmountLimit, enableWithdraw, enableWithdrawAmount, maxWithdrawStatus, maximumWithdrawAmount, minimumWithdrawAmount } =
212
+ event.data?.withdrawRule
213
+
214
+ const withdrawRules = event.data?.withdrawRule?.rules || []
215
+ const rules = withdrawRules.filter((rule) => rule.maxWithdrawAmount > 0)
216
+
217
+ const { balance } = context
218
+ const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
219
+
220
+ let maxWithdrawAmount = 0
221
+
222
+ if (lastMatchedIndex > -1) {
223
+ rules[lastMatchedIndex].match = true
224
+ maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
225
+ } else if (rules.length > 0) {
226
+ maxWithdrawAmount = rules[0].maxWithdrawAmount
227
+ } else {
228
+ maxWithdrawAmount = balance
229
+ }
230
+
231
+ return {
232
+ maxWithdrawAmount,
233
+ enableWithdrawAmount: +enableWithdrawAmount,
234
+ enableWithdraw,
235
+ withdrawRules,
236
+ }
237
+ }),
238
+ setOtp: assign((context, event) => {
239
+ const { requestId, status, target, ttl } = event.data
240
+ return {
241
+ requestId,
242
+ status,
243
+ target,
244
+ ttl,
245
+ }
246
+ }),
247
+ decreaseTtl: assign((context, evnet) => {
248
+ const second = context.ttl
249
+ return {
250
+ ttl: Math.max(second - 1, 0),
251
+ }
252
+ }),
253
+ setVerificationData: assign((context, event) => {
254
+ const { requestId, ttl, target } = event.data
255
+ return {
256
+ requestId,
257
+ ttl,
258
+ target,
259
+ }
260
+ }),
261
+ setAttachmentType: assign((context, event) => {
262
+ return {
263
+ attachmentType: event.data,
264
+ }
265
+ }),
266
+ },
267
+ guards: {
268
+ isFree: (context, event) => {
269
+ return context.userInfo?.withdraw_rule !== 'DEFAULT'
270
+ },
271
+ hasAttachmentType: (context, event) => {
272
+ return ['withdraw', 'pool'].includes(context.attachmentType)
273
+ },
274
+ haveProcessing: (context, event) => {
275
+ return (context.paymentDetail?.[0]?.processing || []).length > 0
276
+ },
277
+ isWithdrawDisable: (context, event) => {
278
+ const { siteConfig } = context
279
+ const methods = siteConfig?.paymentMethods?.withdraw || []
280
+ return methods.length === 0
281
+ },
282
+ isBankNotBound: (context, event) => {
283
+ return !context.bankInfo?.length
284
+ },
285
+ isSubmittable: (context, event) => {
286
+ const { bank, amount } = context
287
+ return bank && amount > 0
288
+ },
289
+ isConvertible: (context, event) => {
290
+ const { balance, enableWithdraw } = context
291
+ return balance > 0 && enableWithdraw
292
+ },
293
+ isNotWithdrawable: (context, event) => {
294
+ const { balance, enableWithdraw, paymentDetail } = context
295
+ const minWithdrawAmount = paymentDetail[0]?.minimum || 0
296
+ return !enableWithdraw || balance < minWithdrawAmount
297
+ },
298
+ isOtpRequired: (context, event) => {
299
+ return event.data?.status === 'otp-verifying'
300
+ },
301
+ isNewWalletMode: (context, event) => context.siteConfig.walletMode > 0,
302
+ },
303
+ services: {
304
+ fetchInitData: async (context, event) => {
305
+ const [{ config }, userInfo, paymentDetail = [], { bonus, bankInfo }, withdrawRule] = await Promise.all([
306
+ request.send(common.getConfig({ appSecret })),
307
+ request.send(user.getUserInfo()),
308
+ request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' })),
309
+ request.send(payment.getPaymentInfo()),
310
+ request.send(payment.getCurrentWithdrawRule()),
311
+ ])
312
+ const siteConfig = JSON.parse(window.atob(config))
313
+ const method = paymentDetail.find((payment) => payment.category === 'manual').method
314
+ const bankList = await request.send(payment.getBankList(method))
315
+ const newBankInfo = bankInfo.map((item) => {
316
+ return { ...item, ...bankList.find((bank) => bank.bankCode === item.bankCode) }
317
+ })
318
+ return { siteConfig, userInfo, paymentDetail, bonus, bankInfo: newBankInfo, bankList, withdrawRule }
319
+ },
320
+ fetchPaymentData: async (context, event) => {
321
+ const paymentDetail = await request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' }))
322
+ const { bonus, bankInfo } = await request.send(payment.getPaymentInfo())
323
+ return { bonus, bankInfo, paymentDetail }
324
+ },
325
+ updatePaymentInfo: async (context, event) => {
326
+ const { bank, branch, accountNumber, holderName } = context
327
+ return await request.send(
328
+ payment.updatePaymentInfo({
329
+ bankCode: bank.bankCode,
330
+ branch,
331
+ bankAccount: accountNumber,
332
+ holderName,
333
+ })
334
+ )
335
+ },
336
+ fetchWithdrawRules: async (context, event) => {
337
+ return await request.send(payment.getCurrentWithdrawRule())
338
+ },
339
+ submitWithdraw: async (context, event) => {
340
+ const { bank, amount } = context
341
+ const { method } = context.paymentDetail[0]
342
+ const branchTemp = bank.bankCode === 'GCASH' ? '' : bank.branch
343
+ return await request.send(
344
+ payment.submitWithdrawTransactionVN({
345
+ method,
346
+ amount,
347
+ applyerBankCode: bank.bankCode,
348
+ applyerAccountNumber: bank.accountNumber,
349
+ branch: branchTemp,
350
+ holderName: bank.holderName,
351
+ })
352
+ )
353
+ },
354
+ convertFreeCredit: async (context, event) => {
355
+ return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
356
+ },
357
+ resetOtp: async (context, event) => {
358
+ const { requestId, target } = context
359
+ return await request.send(user.otpReset({ requestId, target }))
360
+ },
361
+ acknowledge: async (context, event) => {
362
+ const { requestId, target } = context
363
+ const code = event.data
364
+ return await request.send(user.otpAcknowledge({ requestId, target, code }))
365
+ },
366
+ pool: async (context, event) => {
367
+ const { amount } = context
368
+ return await request.send(payment.restore({ amount }))
369
+ },
370
+ giveUp: async (context, event) => {
371
+ return await request.send(payment.giveUp())
372
+ },
373
+ getActivityWalletStatus: async (context, event) => {
374
+ return await request.send(payment.getDoubleWalletWithdrawRule({ walletType: 'ACTIVITY' }))
375
+
376
+ // 轉換條件: currentWithdrawLimit === 0
377
+ // -> settle
378
+ //
379
+ },
380
+ },
381
+ }
382
+ )
383
+ }
@@ -22,8 +22,8 @@ export default class Request {
22
22
  this.axios.interceptors.request.use((axiosConfig) => {
23
23
  axiosConfig.headers['X-TH1GAMES-VERSION'] = this.config.version
24
24
  axiosConfig.headers['X-TH1GAMES-PLATFORM'] = this.config.platform
25
- if (storage.get(this.key)) {
26
- axiosConfig.headers.Authorization = storage.get(this.key)
25
+ if (this.getToken()) {
26
+ axiosConfig.headers.Authorization = this.getToken()
27
27
  }
28
28
  return axiosConfig
29
29
  })
@@ -51,7 +51,7 @@ export default class Request {
51
51
  async send(req, customToken) {
52
52
  try {
53
53
  const cancelToken = customToken || this.source.token
54
- this._checkToken(req, storage.get(this.key))
54
+ this._checkToken(req)
55
55
  const requestConfig = this._createConfig(req, cancelToken)
56
56
  const { data } = await this.axios.request(requestConfig)
57
57
  return data
@@ -97,10 +97,14 @@ export default class Request {
97
97
  }
98
98
  }
99
99
 
100
- _checkToken(req, token) {
101
- if (!req.isPublic && !token) {
100
+ _checkToken(req) {
101
+ if (!req.isPublic && !this.getToken()) {
102
102
  console.error(req)
103
103
  throw { code: 'ERR_A103', url: req.url }
104
104
  }
105
105
  }
106
+
107
+ getToken() {
108
+ return storage.get(this.key)
109
+ }
106
110
  }
package/src/util/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { getDeviceToken } from './deviceToken.js'
2
2
  import storage from './storage.js'
3
3
  import * as constant from './constant.js'
4
+ import { max } from './math.js'
4
5
 
5
6
  export default { getDeviceToken }
6
- export { constant, storage }
7
+ export { constant, storage, max }
@@ -0,0 +1,4 @@
1
+ export function max(...args) {
2
+ const casted = args.map((arg) => arg || 0)
3
+ return Math.max(...casted)
4
+ }