@crmcom/self-service-sdk 2.1.2

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/wallet.js ADDED
@@ -0,0 +1,478 @@
1
+ import { httpUtil } from "./httpUtil";
2
+ import { ErrorCodes, createResult, createCommonResult } from "./resultUtil";
3
+ import { logger } from './logger';
4
+ export const wallet = {
5
+ getWallet,
6
+ createCRMWallet,
7
+ requestWalletOtp,
8
+ linkCRMWallet,
9
+ getWalletConditionsBalances,
10
+ getWalletTransactions,
11
+ topupWallet,
12
+ updateWallet,
13
+ getWalletTopupSetting,
14
+ getWalletLimit,
15
+ updateWalletLimit,
16
+ transferMoney,
17
+ getWalletJounals,
18
+ getWalletId,
19
+ getWalletCode,
20
+ getWalletSummarisedTotals,
21
+ activateDeactivateCommercePool,
22
+ verifyWalletExists
23
+ }
24
+
25
+ async function getWallet(walletId) {
26
+ try {
27
+ if (!walletId) {
28
+ walletId = await getWalletId();
29
+ }
30
+
31
+ let response = await httpUtil.get({
32
+ resourcePath: '/v2/wallets/' + walletId,
33
+ withAccessToken: true,
34
+ logOutIfSessionInvalid: false,
35
+ });
36
+ //check return code here instead of put as there would be different intepretation for different API
37
+ return createCommonResult(response);
38
+ } catch (e) {
39
+ logger.error('Exception getWallet:', e);
40
+ return createResult(ErrorCodes.UNKNOWN, e);
41
+ }
42
+ }
43
+
44
+ async function getWalletId() {
45
+ let walletId = null;
46
+ try {
47
+ let id = httpUtil.getSession().sub;
48
+ //console.log('API: ', contact)
49
+ let response = await httpUtil.get({
50
+ resourcePath: "/v2/contacts/" + id,
51
+ });
52
+ if (response.code == 'OK') {
53
+ walletId = response.data && response.data.wallet ? response.data.wallet.id : null;
54
+ }
55
+ } catch (e) {
56
+ logger.error("Exception getWalletId:", e);
57
+ }
58
+ return walletId;
59
+ }
60
+
61
+ async function createCRMWallet({ type, value, country_code }) {
62
+ let id = httpUtil.getSession().sub;
63
+ try {
64
+ let body = {
65
+ identity: {
66
+ type,
67
+ value,
68
+ country_code,
69
+ }
70
+ }
71
+ let response = await httpUtil.post({
72
+ resourcePath: "/v2/contacts/" + id + "/wallets",
73
+ body: body,
74
+ });
75
+ return createCommonResult(response);
76
+ } catch (e) {
77
+ logger.error("Exception register:", e);
78
+ return createResult(ErrorCodes.UNKNOWN, e);
79
+ }
80
+ }
81
+
82
+ async function requestWalletOtp({ type, value, country_code }) {
83
+ try {
84
+ let body = {
85
+ identity: {
86
+ type,
87
+ value,
88
+ country_code,
89
+ }
90
+ }
91
+ let response = await httpUtil.post({
92
+ resourcePath: "/v2/wallets/otp",
93
+ body: body,
94
+ logOutIfSessionInvalid: false,
95
+ });
96
+ return createCommonResult(response);
97
+ } catch (e) {
98
+ logger.error("Exception register:", e);
99
+ return createResult(ErrorCodes.UNKNOWN, e);
100
+ }
101
+ }
102
+
103
+ async function linkCRMWallet(otp) {
104
+ let id = httpUtil.getSession().sub;
105
+ try {
106
+ let response = await httpUtil.post({
107
+ resourcePath: "/v2/contacts/" + id + "/wallets",
108
+ body: {
109
+ otp
110
+ },
111
+ });
112
+ return createCommonResult(response);
113
+ } catch (e) {
114
+ logger.error("Exception register:", e);
115
+ return createResult(ErrorCodes.UNKNOWN, e);
116
+ }
117
+ }
118
+
119
+ async function getWalletConditionsBalances({
120
+ page,
121
+ size,
122
+ order,
123
+ sort,
124
+ commerce_pool_id,
125
+ include_expiration = true,
126
+ include_total,
127
+ is_active
128
+ } = {}, walletId) {
129
+ try {
130
+ if (!walletId) {
131
+ walletId = await getWalletId();
132
+ }
133
+ let response = await httpUtil.get({
134
+ resourcePath: '/v2/wallets/' + walletId + '/commerce_balances',
135
+ queryParams: {
136
+ page,
137
+ size,
138
+ order,
139
+ sort,
140
+ commerce_pool_id,
141
+ include_expiration,
142
+ include_total,
143
+ is_active
144
+ },
145
+ withAccessToken: true,
146
+ logOutIfSessionInvalid: false,
147
+ });
148
+ //check return code here instead of put as there would be different intepretation for different API
149
+ return createCommonResult(response);
150
+ } catch (e) {
151
+ logger.error('Exception getWallet:', e);
152
+ return createResult(ErrorCodes.UNKNOWN, e);
153
+ }
154
+ }
155
+
156
+ async function activateDeactivateCommercePool({
157
+ commerce_pool_id,
158
+ action
159
+ } = {}) {
160
+ try {
161
+
162
+ let response = await httpUtil.put({
163
+ resourcePath: "/v2/commerce_pools/" + commerce_pool_id + "/actions",
164
+ body: {
165
+ action
166
+ }
167
+ });
168
+ return createCommonResult(response);
169
+ } catch (e) {
170
+ logger.error("Exception activateDeactivateCommercePool:", e);
171
+ return createResult(ErrorCodes.UNKNOWN, e);
172
+ }
173
+ }
174
+
175
+ async function getWalletTransactions({
176
+ page,
177
+ size = 20,
178
+ order,
179
+ sort,
180
+ include_total = true,
181
+ pocket,
182
+ type,
183
+ posted_on,
184
+ posted_on_gt,
185
+ posted_on_gte,
186
+ posted_on_lt,
187
+ posted_on_lte,
188
+ commerce_pool_id
189
+ } = {}, walletId) {
190
+ try {
191
+ if (!walletId) {
192
+ walletId = await getWalletId();
193
+ }
194
+ let response = await httpUtil.get({
195
+ resourcePath: '/v2/wallets/' + walletId + '/transactions',
196
+ queryParams: {
197
+ page,
198
+ size,
199
+ order,
200
+ sort,
201
+ include_total,
202
+ pocket,
203
+ type,
204
+ commerce_pool_id,
205
+ posted_on,
206
+ "posted_on[gt]": posted_on_gt,
207
+ "posted_on[gte]": posted_on_gte,
208
+ "posted_on[lt]": posted_on_lt,
209
+ "posted_on[lte]": posted_on_lte,
210
+ },
211
+ withAccessToken: true,
212
+ logOutIfSessionInvalid: false,
213
+ });
214
+ //check return code here instead of put as there would be different intepretation for different API
215
+ return createCommonResult(response);
216
+ } catch (e) {
217
+ logger.error('Exception getWallet:', e);
218
+ return createResult(ErrorCodes.UNKNOWN, e);
219
+ }
220
+ }
221
+
222
+ async function topupWallet({
223
+ payment_method_id,
224
+ amount,
225
+ wallet_id,
226
+ client_secret,
227
+ commerce_pool_id
228
+ }) {
229
+ try {
230
+ if (!wallet_id) {
231
+ wallet_id = await getWalletId();
232
+ }
233
+ let response = await httpUtil.post({
234
+ resourcePath: "/v2/topup",
235
+ body: {
236
+ wallet_id,
237
+ amount,
238
+ client_secret,
239
+ commerce_pool_id,
240
+ payment_method: {
241
+ type: "CARD",
242
+ id: payment_method_id
243
+ }
244
+ },
245
+ });
246
+ return createCommonResult(response);
247
+ } catch (e) {
248
+ logger.error("Exception register:", e);
249
+ return createResult(ErrorCodes.UNKNOWN, e);
250
+ }
251
+ }
252
+
253
+ async function updateWallet({
254
+ auto_topup,
255
+ termed_topup,
256
+ wallet_id
257
+ } = {}) {
258
+ try {
259
+ if (!wallet_id) {
260
+ wallet_id = await getWalletId();
261
+ }
262
+ let response = await httpUtil.put({
263
+ resourcePath: "/v2/wallets/" + wallet_id,
264
+ body: {
265
+ auto_topup,
266
+ termed_topup
267
+ }
268
+ });
269
+ return createCommonResult(response);
270
+ } catch (e) {
271
+ logger.error("Exception updateWallet:", e);
272
+ return createResult(ErrorCodes.UNKNOWN, e);
273
+ }
274
+ }
275
+
276
+ async function getWalletTopupSetting(wallet_id) {
277
+ try {
278
+ if (!wallet_id) {
279
+ wallet_id = await getWalletId();
280
+ }
281
+ let response = await httpUtil.get({
282
+ resourcePath: "/v2/wallets/" + wallet_id + "/topup_settings"
283
+ });
284
+ return createCommonResult(response);
285
+ } catch (e) {
286
+ logger.error("Exception getWalletTopupSetting:", e);
287
+ return createResult(ErrorCodes.UNKNOWN, e);
288
+ }
289
+ }
290
+
291
+ async function getWalletLimit(wallet_id) {
292
+ try {
293
+ if (!wallet_id) {
294
+ wallet_id = await getWalletId();
295
+ }
296
+ let response = await httpUtil.get({
297
+ resourcePath: "/v2/wallets/" + wallet_id + "/limits"
298
+ });
299
+ return createCommonResult(response);
300
+ } catch (e) {
301
+ logger.error("Exception getWalletLimit:", e);
302
+ return createResult(ErrorCodes.UNKNOWN, e);
303
+ }
304
+ }
305
+
306
+ async function updateWalletLimit({
307
+ limit_rules,
308
+ wallet_id
309
+ } = {}) {
310
+ try {
311
+ if (!wallet_id) {
312
+ wallet_id = await getWalletId();
313
+ }
314
+ let response = await httpUtil.put({
315
+ resourcePath: "/v2/wallets/" + wallet_id + "/limits",
316
+ body: {
317
+ limit_rules
318
+ }
319
+ });
320
+ return createCommonResult(response);
321
+ } catch (e) {
322
+ logger.error("Exception updateWalletLimit:", e);
323
+ return createResult(ErrorCodes.UNKNOWN, e);
324
+ }
325
+ }
326
+
327
+ async function transferMoney({ origin_wallet_id, destination_wallet_id, amount, cash_pocket }) {
328
+ try {
329
+ let body = {
330
+ origin: {
331
+ wallet_id: origin_wallet_id,
332
+ },
333
+ destination: {
334
+ wallet_id: destination_wallet_id,
335
+ },
336
+ amount: amount,
337
+ cash_pocket : cash_pocket
338
+ }
339
+ let response = await httpUtil.post({
340
+ resourcePath: "/v2/transfers",
341
+ body: body,
342
+ withAccessToken: true,
343
+ });
344
+ return createCommonResult(response);
345
+ } catch (e) {
346
+ logger.error("Exception transferMoney:", e);
347
+ return createResult(ErrorCodes.UNKNOWN, e);
348
+ }
349
+ }
350
+
351
+ async function getWalletJounals({
352
+ page,
353
+ size = 20,
354
+ order,
355
+ sort,
356
+ include_total = true,
357
+ commerce_pool_id,
358
+ is_commerce,
359
+ is_open,
360
+ is_wallet_fee,
361
+ posted_on,
362
+ posted_on_gt,
363
+ posted_on_gte,
364
+ posted_on_lt,
365
+ posted_on_lte,
366
+ include_unallocated_credit,
367
+ type,
368
+ } = {}, walletId) {
369
+ try {
370
+ if (!walletId) {
371
+ walletId = await getWalletId();
372
+ }
373
+ let response = await httpUtil.get({
374
+ resourcePath: '/v2/wallets/' + walletId + '/journals',
375
+ queryParams: {
376
+ page,
377
+ size,
378
+ order,
379
+ sort,
380
+ include_total,
381
+ commerce_pool_id,
382
+ is_commerce,
383
+ is_open,
384
+ is_wallet_fee,
385
+ posted_on,
386
+ "posted_on[gt]": posted_on_gt,
387
+ "posted_on[gte]": posted_on_gte,
388
+ "posted_on[lt]": posted_on_lt,
389
+ "posted_on[lte]": posted_on_lte,
390
+ include_unallocated_credit,
391
+ type,
392
+ },
393
+ withAccessToken: true,
394
+ logOutIfSessionInvalid: false,
395
+ });
396
+ //check return code here instead of put as there would be different intepretation for different API
397
+ return createCommonResult(response);
398
+ } catch (e) {
399
+ logger.error('Exception getWalletJounals:', e);
400
+ return createResult(ErrorCodes.UNKNOWN, e);
401
+ }
402
+ }
403
+
404
+ async function getWalletCode(wallet_id) {
405
+ try {
406
+ if (!wallet_id) {
407
+ wallet_id = await getWalletId();
408
+ }
409
+ let response = await httpUtil.get({
410
+ resourcePath: "/v2/wallets/" + wallet_id + "/code"
411
+ });
412
+ return createCommonResult(response);
413
+ } catch (e) {
414
+ logger.error("Exception getWalletCode:", e);
415
+ return createResult(ErrorCodes.UNKNOWN, e);
416
+ }
417
+ }
418
+
419
+ async function getWalletSummarisedTotals({
420
+ /*page,
421
+ size = 20,
422
+ order,
423
+ sort,
424
+ include_total = true,
425
+ pocket,
426
+ type,*/
427
+ posted_on,
428
+ posted_on_gt,
429
+ posted_on_gte,
430
+ posted_on_lt,
431
+ posted_on_lte,
432
+ // commerce_pool_id
433
+ } = {}, walletId) {
434
+ try {
435
+ if (!walletId) {
436
+ walletId = await getWalletId();
437
+ }
438
+ let response = await httpUtil.get({
439
+ resourcePath: '/v2/wallets/' + walletId + '/summarised_totals',
440
+ queryParams: {
441
+ /*page,
442
+ size,
443
+ order,
444
+ sort,
445
+ include_total,*/
446
+ posted_on,
447
+ "posted_on[gt]": posted_on_gt,
448
+ "posted_on[gte]": posted_on_gte,
449
+ "posted_on[lt]": posted_on_lt,
450
+ "posted_on[lte]": posted_on_lte,
451
+ },
452
+ withAccessToken: true,
453
+ logOutIfSessionInvalid: false,
454
+ });
455
+ //check return code here instead of put as there would be different intepretation for different API
456
+ return createCommonResult(response);
457
+ } catch (e) {
458
+ logger.error('Exception getWallet:', e);
459
+ return createResult(ErrorCodes.UNKNOWN, e);
460
+ }
461
+ }
462
+
463
+ async function verifyWalletExists({ type, value } = {}) {
464
+ try {
465
+ let response = await httpUtil.get({
466
+ logOutIfSessionInvalid: false,
467
+ resourcePath: "/v2/wallets/wallet_exists",
468
+ queryParams: {
469
+ type,
470
+ value
471
+ }
472
+ });
473
+ return createCommonResult(response);
474
+ } catch (e) {
475
+ logger.error("Exception getContact:", e);
476
+ return createResult(ErrorCodes.UNKNOWN, e);
477
+ }
478
+ }