@crmcom/self-service-sdk 2.1.2 → 3.0.0-build.11
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/README.md +165 -0
- package/authentication.js +33 -91
- package/config.js +0 -4
- package/connectx.js +1 -2
- package/contacts.js +13 -25
- package/eventListener.js +20 -20
- package/httpBackOfficeUtil.js +27 -4
- package/httpUtil.js +76 -29
- package/index.d.ts +22 -7
- package/index.js +17 -1
- package/orders.js +4 -56
- package/package.json +2 -2
- package/resultUtil.js +72 -30
- package/rewards.js +23 -24
- package/subscriptions.js +5 -18
- package/wallet.js +5 -0
- package/dataUtil.js +0 -2186
package/httpUtil.js
CHANGED
|
@@ -3,14 +3,27 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
//import querystring from 'querystring';
|
|
6
|
-
import {
|
|
7
|
-
import { jwtDecode } from 'jwt-decode';
|
|
8
|
-
import { connection } from '../../portal.config';
|
|
6
|
+
import { jwtDecode } from 'jwt-decode';
|
|
9
7
|
import { logger } from './logger';
|
|
10
8
|
|
|
9
|
+
export const ACCESS_TOKEN = 'crmcom_access_token';
|
|
10
|
+
export const REFRESH_TOKEN = 'crmcom_refresh_token';
|
|
11
|
+
|
|
12
|
+
const initOptionHeader = () => {
|
|
13
|
+
return {
|
|
14
|
+
'User-Agent': 'request',
|
|
15
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
11
19
|
// import { showMessageError } from '../../utils/util';
|
|
20
|
+
let _initialized = false;
|
|
21
|
+
|
|
12
22
|
export const httpUtil = {
|
|
13
|
-
|
|
23
|
+
init,
|
|
24
|
+
/** @deprecated Use init() instead */
|
|
25
|
+
setupChannel: init,
|
|
26
|
+
isInitialized: () => _initialized,
|
|
14
27
|
put,
|
|
15
28
|
post,
|
|
16
29
|
get,
|
|
@@ -35,6 +48,15 @@ export const httpUtil = {
|
|
|
35
48
|
|
|
36
49
|
/** local variables */
|
|
37
50
|
|
|
51
|
+
function _assertInitialized() {
|
|
52
|
+
if (!_initialized) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
'CRM.COM SDK not initialized. Call init() before using any SDK methods. ' +
|
|
55
|
+
'Example: import { init } from "@crmcom/self-service-sdk"; await init({ apiKey, host, ... });'
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
38
60
|
let _storeKVFn;
|
|
39
61
|
let _getKVFn;
|
|
40
62
|
let _sessionInvalidCallback;
|
|
@@ -55,13 +77,9 @@ let _middleware_host;
|
|
|
55
77
|
let _middleware_apiKey;
|
|
56
78
|
let _mwNodejs_host;
|
|
57
79
|
let _mwNodejs_apiKey;
|
|
58
|
-
/**
|
|
59
|
-
*
|
|
60
|
-
* TODO: do we need a call back for no internet connection ?
|
|
61
|
-
* when no connection refresh token will not be available -> should not kick user out.
|
|
62
|
-
*/
|
|
63
80
|
|
|
64
|
-
|
|
81
|
+
|
|
82
|
+
async function init({
|
|
65
83
|
storeKVFn, //function to store key value
|
|
66
84
|
getKVFn, //function to get value by key from the storage
|
|
67
85
|
sessionInvalidCallback, //function to call when api_key or token key is invalid and refresh token if available was failed
|
|
@@ -110,7 +128,7 @@ async function setupChannel({
|
|
|
110
128
|
_sessionData = undefined;
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
|
-
|
|
131
|
+
_initialized = true;
|
|
114
132
|
}
|
|
115
133
|
|
|
116
134
|
async function setupApiKey(apikey){
|
|
@@ -278,9 +296,11 @@ async function post({
|
|
|
278
296
|
isMwNodejs = false,
|
|
279
297
|
plugin,
|
|
280
298
|
}) {
|
|
299
|
+
_assertInitialized();
|
|
281
300
|
try {
|
|
282
|
-
let logoutStatus = await getData('LOGOUT_STATUS');
|
|
283
|
-
if(logoutStatus == 'PROCESSING') return;
|
|
301
|
+
//let logoutStatus = await getData('LOGOUT_STATUS');
|
|
302
|
+
//if(logoutStatus == 'PROCESSING') return;
|
|
303
|
+
|
|
284
304
|
let uri = getURI(isBackend, resourcePath, isMiddleware, isMwNodejs, plugin);
|
|
285
305
|
var options = {};
|
|
286
306
|
options.headers = initOptionHeader();
|
|
@@ -367,8 +387,8 @@ async function postRefreshToken({
|
|
|
367
387
|
isBackend
|
|
368
388
|
}) {
|
|
369
389
|
try {
|
|
370
|
-
let logoutStatus = await getData('LOGOUT_STATUS');
|
|
371
|
-
if(logoutStatus == 'PROCESSING') return;
|
|
390
|
+
//let logoutStatus = await getData('LOGOUT_STATUS');
|
|
391
|
+
//if(logoutStatus == 'PROCESSING') return;
|
|
372
392
|
let uri = getURI(isBackend, resourcePath);
|
|
373
393
|
var options = {};
|
|
374
394
|
options.headers = initOptionHeader();
|
|
@@ -416,9 +436,10 @@ async function get({
|
|
|
416
436
|
isMwNodejs = false,
|
|
417
437
|
plugin,
|
|
418
438
|
}) {
|
|
439
|
+
_assertInitialized();
|
|
419
440
|
try {
|
|
420
|
-
|
|
421
|
-
if(logoutStatus == 'PROCESSING') return;
|
|
441
|
+
// let logoutStatus = await getData('LOGOUT_STATUS');
|
|
442
|
+
//if(logoutStatus == 'PROCESSING') return;
|
|
422
443
|
let uri = getURI(isBackend, resourcePath, isMiddleware, isMwNodejs, plugin);
|
|
423
444
|
var options = {};
|
|
424
445
|
options.headers = initOptionHeader();
|
|
@@ -476,16 +497,38 @@ async function get({
|
|
|
476
497
|
return { code: response.status, bodyText: bodyText, error: json2Obj(bodyText) };
|
|
477
498
|
}
|
|
478
499
|
} catch (e) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
//
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
//
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
500
|
+
logger.error("Request error:", e);
|
|
501
|
+
|
|
502
|
+
// If fetch throws (CORS/network), e is usually a TypeError with message "Failed to fetch"
|
|
503
|
+
const message = e?.message || String(e);
|
|
504
|
+
|
|
505
|
+
// Some codebases throw a Response object; handle that too (rare, but possible)
|
|
506
|
+
const isResponseLike = e && typeof e.text === "function" && typeof e.status !== "undefined";
|
|
507
|
+
|
|
508
|
+
if (isResponseLike) {
|
|
509
|
+
let bodyText = "";
|
|
510
|
+
try {
|
|
511
|
+
bodyText = await e.text();
|
|
512
|
+
} catch (_) {}
|
|
513
|
+
return { code: String(e.status || "UNKNOWN"), bodyText, error: bodyText ? json2Obj(bodyText) : null };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// No response exists => network/CORS/etc.
|
|
517
|
+
return {
|
|
518
|
+
code: "NETWORK_ERROR",
|
|
519
|
+
bodyText: message,
|
|
520
|
+
error: { message }
|
|
521
|
+
};
|
|
522
|
+
// logger.error("Request error:", e);
|
|
523
|
+
// let bodyText = await e.text();
|
|
524
|
+
// // if (e.status == '401') {
|
|
525
|
+
// // let uri = getURI(isBackend, resourcePath);
|
|
526
|
+
// // if (queryParams)
|
|
527
|
+
// // uri = uri + '?' + querystring.encode(cleanObj(queryParams));
|
|
528
|
+
// // var result = await processRefreshToken(uri,logOutIfSessionInvalid,returnText);
|
|
529
|
+
// // return result;
|
|
530
|
+
// // }
|
|
531
|
+
// return { code: e.status, bodyText: bodyText, error: json2Obj(bodyText) };
|
|
489
532
|
}
|
|
490
533
|
}
|
|
491
534
|
|
|
@@ -498,6 +541,7 @@ async function sendDelete({
|
|
|
498
541
|
isMiddleware = false,
|
|
499
542
|
isMwNodejs = false,
|
|
500
543
|
}) {
|
|
544
|
+
_assertInitialized();
|
|
501
545
|
try {
|
|
502
546
|
let uri = getURI(isBackend, resourcePath, isMiddleware, isMwNodejs);
|
|
503
547
|
var options = {};
|
|
@@ -577,6 +621,7 @@ async function put({
|
|
|
577
621
|
isMwNodejs = false,
|
|
578
622
|
accessToken,
|
|
579
623
|
}) {
|
|
624
|
+
_assertInitialized();
|
|
580
625
|
try {
|
|
581
626
|
let uri = getURI(isBackend, resourcePath, isMiddleware, isMwNodejs);
|
|
582
627
|
var options = {};
|
|
@@ -665,8 +710,8 @@ function cleanObj(obj) {
|
|
|
665
710
|
|
|
666
711
|
async function processRefreshToken(uri, logOutIfSessionInvalid, returnText) {
|
|
667
712
|
try {
|
|
668
|
-
let logoutStatus = await getData('LOGOUT_STATUS');
|
|
669
|
-
if(logoutStatus == 'PROCESSING') return;
|
|
713
|
+
//let logoutStatus = await getData('LOGOUT_STATUS');
|
|
714
|
+
//if(logoutStatus == 'PROCESSING') return;
|
|
670
715
|
var refreshResult = await refreshToken(logOutIfSessionInvalid);
|
|
671
716
|
if (refreshResult.code == 'OK') {
|
|
672
717
|
var options = {};
|
|
@@ -709,6 +754,7 @@ async function uploadFile({
|
|
|
709
754
|
keyParam = 'file',
|
|
710
755
|
disalbedContentType = false,
|
|
711
756
|
}) {
|
|
757
|
+
_assertInitialized();
|
|
712
758
|
try {
|
|
713
759
|
let uri = getURI(isBackend, resourcePath, isMiddleware);
|
|
714
760
|
var options = {};
|
|
@@ -796,6 +842,7 @@ async function uploadFileNew({
|
|
|
796
842
|
accessToken,
|
|
797
843
|
isMiddleware = false
|
|
798
844
|
}) {
|
|
845
|
+
_assertInitialized();
|
|
799
846
|
try {
|
|
800
847
|
let uri = getURI(isBackend, resourcePath, isMiddleware);
|
|
801
848
|
|
package/index.d.ts
CHANGED
|
@@ -69,7 +69,7 @@ export declare const logger: {
|
|
|
69
69
|
// HTTP Utilities
|
|
70
70
|
// ============================================================================
|
|
71
71
|
|
|
72
|
-
interface
|
|
72
|
+
export interface InitOptions {
|
|
73
73
|
storeKVFn: (key: string, value: any) => void | Promise<void>;
|
|
74
74
|
getKVFn: (key: string) => any | Promise<any>;
|
|
75
75
|
sessionInvalidCallback: (shouldLogout: boolean) => void | Promise<void>;
|
|
@@ -129,7 +129,10 @@ interface UploadFileOptions {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
export declare const httpUtil: {
|
|
132
|
-
|
|
132
|
+
init(options: InitOptions): Promise<void>;
|
|
133
|
+
/** @deprecated Use init() instead */
|
|
134
|
+
setupChannel(options: InitOptions): Promise<void>;
|
|
135
|
+
isInitialized(): boolean;
|
|
133
136
|
setupApiKey(apiKey: string): Promise<void>;
|
|
134
137
|
post(options: HttpRequestOptions): Promise<SDKResult>;
|
|
135
138
|
get(options: HttpRequestOptions): Promise<SDKResult>;
|
|
@@ -152,7 +155,10 @@ export declare const httpUtil: {
|
|
|
152
155
|
};
|
|
153
156
|
|
|
154
157
|
export declare const httpBackOfficeUtil: {
|
|
155
|
-
|
|
158
|
+
init(options: Pick<InitOptions, 'storeKVFn' | 'getKVFn' | 'sessionInvalidCallback' | 'apiKey' | 'host' | 'fetchFn'>): Promise<void>;
|
|
159
|
+
/** @deprecated Use init() instead */
|
|
160
|
+
setupChannel(options: Pick<InitOptions, 'storeKVFn' | 'getKVFn' | 'sessionInvalidCallback' | 'apiKey' | 'host' | 'fetchFn'>): Promise<void>;
|
|
161
|
+
isInitialized(): boolean;
|
|
156
162
|
post(options: HttpRequestOptions): Promise<SDKResult>;
|
|
157
163
|
get(options: HttpRequestOptions): Promise<SDKResult>;
|
|
158
164
|
put(options: HttpRequestOptions): Promise<SDKResult>;
|
|
@@ -519,9 +525,18 @@ export declare const eventListener: {
|
|
|
519
525
|
};
|
|
520
526
|
|
|
521
527
|
// ============================================================================
|
|
522
|
-
//
|
|
528
|
+
// Top-level SDK Initialization
|
|
523
529
|
// ============================================================================
|
|
524
530
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
531
|
+
/**
|
|
532
|
+
* Initialize the Self-Service SDK. Must be called before using any other SDK module.
|
|
533
|
+
* Alias for httpUtil.init().
|
|
534
|
+
*/
|
|
535
|
+
export declare function init(options: InitOptions): Promise<void>;
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Initialize the Back-Office SDK. Must be called before using any back-office module.
|
|
539
|
+
* Alias for httpBackOfficeUtil.init().
|
|
540
|
+
*/
|
|
541
|
+
export declare function initBackOffice(options: Pick<InitOptions, 'storeKVFn' | 'getKVFn' | 'sessionInvalidCallback' | 'apiKey' | 'host' | 'fetchFn'>): Promise<void>;
|
|
542
|
+
|
package/index.js
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
export { httpUtil } from './httpUtil.js';
|
|
2
2
|
export { httpBackOfficeUtil } from './httpBackOfficeUtil.js';
|
|
3
|
+
|
|
4
|
+
// Top-level convenience aliases for SDK initialization
|
|
5
|
+
import { httpUtil as _httpUtil } from './httpUtil.js';
|
|
6
|
+
import { httpBackOfficeUtil as _httpBackOfficeUtil } from './httpBackOfficeUtil.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Initialize the Self-Service SDK. Must be called before using any other SDK module.
|
|
10
|
+
* @see httpUtil.init
|
|
11
|
+
*/
|
|
12
|
+
export const init = _httpUtil.init;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initialize the Back-Office SDK. Must be called before using any back-office module.
|
|
16
|
+
* @see httpBackOfficeUtil.init
|
|
17
|
+
*/
|
|
18
|
+
export const initBackOffice = _httpBackOfficeUtil.init;
|
|
19
|
+
|
|
3
20
|
export { authentication } from './authentication.js';
|
|
4
21
|
export { contacts } from './contacts.js';
|
|
5
22
|
export { orders } from './orders.js';
|
|
@@ -18,7 +35,6 @@ export { jcccards } from './jcccards.js';
|
|
|
18
35
|
export { mobilepass } from './mobilepass.js';
|
|
19
36
|
export { paymentgateway } from './paymentgateway.js';
|
|
20
37
|
export { payouts } from './payouts.js';
|
|
21
|
-
export { dataUtil } from './dataUtil.js';
|
|
22
38
|
export { ErrorCodes, createResult, createCommonResult } from './resultUtil.js';
|
|
23
39
|
export { eventListener, eventTypes } from './eventListener.js';
|
|
24
40
|
export { logger } from './logger.js';
|
package/orders.js
CHANGED
|
@@ -69,14 +69,7 @@ async function getOrder(id) {
|
|
|
69
69
|
resourcePath: '/v2/orders/' + id,
|
|
70
70
|
withAccessToken: true
|
|
71
71
|
});
|
|
72
|
-
|
|
73
|
-
return createResult(ErrorCodes.OK, response.data);
|
|
74
|
-
else {
|
|
75
|
-
if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.CANNOTFULFILLORDEREXCEPTION" || response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION") {
|
|
76
|
-
return createResult(ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION, response.error);
|
|
77
|
-
}
|
|
78
|
-
return createCommonResult(response);
|
|
79
|
-
}
|
|
72
|
+
return createCommonResult(response);
|
|
80
73
|
} catch (e) {
|
|
81
74
|
logger.error('Exception getOrder:', e);
|
|
82
75
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
@@ -444,16 +437,7 @@ async function estimateOrderFulfillment({
|
|
|
444
437
|
},
|
|
445
438
|
withAccessToken: true
|
|
446
439
|
});
|
|
447
|
-
|
|
448
|
-
if (response.code == 'OK') {
|
|
449
|
-
return createCommonResult(response);
|
|
450
|
-
} else {
|
|
451
|
-
if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.CANNOTFULFILLORDEREXCEPTION"
|
|
452
|
-
|| response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION" || response.error && response.error.error == "CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION") {
|
|
453
|
-
return createResult(ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION, response.error);
|
|
454
|
-
}
|
|
455
|
-
return createCommonResult(response);
|
|
456
|
-
}
|
|
440
|
+
return createCommonResult(response);
|
|
457
441
|
} catch (e) {
|
|
458
442
|
logger.error('Exception orderFulfillment:', e);
|
|
459
443
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
@@ -509,30 +493,7 @@ async function estimateOrder({
|
|
|
509
493
|
},
|
|
510
494
|
withAccessToken: true
|
|
511
495
|
});
|
|
512
|
-
|
|
513
|
-
//console.log('response=1=====',response)
|
|
514
|
-
if (response.code == 'OK') {
|
|
515
|
-
return createCommonResult(response);
|
|
516
|
-
} else {
|
|
517
|
-
if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.CANNOTFULFILLORDEREXCEPTION"
|
|
518
|
-
|| response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION" || response.error && response.error.error == "CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION") {
|
|
519
|
-
return createResult(ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION, response.error);
|
|
520
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION" || response.error && response.error.error == "COM.CRM.EXCEPTIONS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION") {
|
|
521
|
-
return createResult(ErrorCodes.MINIMUM_ORDER_AMOUNT_NOT_REACHED, response.error);
|
|
522
|
-
}
|
|
523
|
-
else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.SERVICEALREADYEXISTSONSUBSCRIPTIONEXCEPTION") {
|
|
524
|
-
return createResult(ErrorCodes.SERVICE_ALREADY_EXIST, response.error);
|
|
525
|
-
}
|
|
526
|
-
else if (response.error && response.error.error == "FINANCE.EXCEPTIONS.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION") {
|
|
527
|
-
return createResult(ErrorCodes.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION, response.error);
|
|
528
|
-
}
|
|
529
|
-
else if (response.error && response.error.error == "CRM.EXCEPTIONS.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION") {
|
|
530
|
-
return createResult(ErrorCodes.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION, response.error);
|
|
531
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTEXECUTESUBSCRIPTIONACTIONEXCEPTION") {
|
|
532
|
-
return createResult(ErrorCodes.CANNOTEXECUTESUBSCRIPTIONACTIONEXCEPTION, response.error);
|
|
533
|
-
}
|
|
534
|
-
return createCommonResult(response);
|
|
535
|
-
}
|
|
496
|
+
return createCommonResult(response);
|
|
536
497
|
} catch (e) {
|
|
537
498
|
logger.error('Exception estimateOrder:', e);
|
|
538
499
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
@@ -570,20 +531,7 @@ async function makeOrder({
|
|
|
570
531
|
},
|
|
571
532
|
withAccessToken: true
|
|
572
533
|
});
|
|
573
|
-
|
|
574
|
-
return createCommonResult(response);
|
|
575
|
-
} else {
|
|
576
|
-
if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.CANNOTFULFILLORDEREXCEPTION"
|
|
577
|
-
|| response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION" || response.error && response.error.error == "CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION") {
|
|
578
|
-
return createResult(ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION, response.error);
|
|
579
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.ORDERS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION" || response.error && response.error.error == "COM.CRM.EXCEPTIONS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION") {
|
|
580
|
-
return createResult(ErrorCodes.MINIMUM_ORDER_AMOUNT_NOT_REACHED, response.error);
|
|
581
|
-
}
|
|
582
|
-
else if (response.error && response.error.error == "FINANCE.EXCEPTIONS.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION") {
|
|
583
|
-
return createResult(ErrorCodes.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION, response.error);
|
|
584
|
-
}
|
|
585
|
-
return createCommonResult(response);
|
|
586
|
-
}
|
|
534
|
+
return createCommonResult(response);
|
|
587
535
|
} catch (e) {
|
|
588
536
|
logger.error('Exception addDevice:', e);
|
|
589
537
|
return createResult(ErrorCodes.UNKNOWN, e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crmcom/self-service-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-build.11",
|
|
4
4
|
"description": "Official CRM.COM Self-Service JavaScript SDK for consumer-facing API integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"./mobilepass": "./mobilepass.js",
|
|
28
28
|
"./paymentgateway": "./paymentgateway.js",
|
|
29
29
|
"./payouts": "./payouts.js",
|
|
30
|
-
"./dataUtil": "./dataUtil.js",
|
|
31
30
|
"./resultUtil": "./resultUtil.js",
|
|
32
31
|
"./eventListener": "./eventListener.js",
|
|
33
32
|
"./logger": "./logger.js"
|
|
@@ -35,6 +34,7 @@
|
|
|
35
34
|
"files": [
|
|
36
35
|
"*.js",
|
|
37
36
|
"*.d.ts",
|
|
37
|
+
"!dataUtil.js",
|
|
38
38
|
"!add_imports.ps1"
|
|
39
39
|
],
|
|
40
40
|
"keywords": [
|
package/resultUtil.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { logger } from './logger';
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
export const ErrorCodes = {
|
|
5
4
|
OK: 'OK',
|
|
6
5
|
UNKNOWN: 'UNKNOWN',
|
|
@@ -42,6 +41,60 @@ export const ErrorCodes = {
|
|
|
42
41
|
INVALIDENTITYLIFECYCLESTATEEXCEPTION: "INVALIDENTITYLIFECYCLESTATEEXCEPTION"
|
|
43
42
|
}
|
|
44
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Centralized mapping from API exception strings to SDK ErrorCodes.
|
|
46
|
+
* Multiple API exception variants can map to the same ErrorCode.
|
|
47
|
+
* Both `response.error.error` and `response.error.message` fields are checked.
|
|
48
|
+
*/
|
|
49
|
+
const EXCEPTION_MAP = {
|
|
50
|
+
// Authentication
|
|
51
|
+
'COM.CRM.EXCEPTIONS.INVALIDLOGINEXCEPTION': ErrorCodes.INVALID_LOGIN,
|
|
52
|
+
'COM.CRM.EXCEPTIONS.EMAILNOTVERIFIEDEXCEPTION': ErrorCodes.EMAIL_NOT_VERIFIED,
|
|
53
|
+
'COM.CRM.EXCEPTIONS.INVALIDPASSWORDEXCEPTION': ErrorCodes.INVALID_PASSWORD_EXCEPTION,
|
|
54
|
+
'CRM.EXCEPTIONS.INVALIDPASSWORDEXCEPTION': ErrorCodes.INVALID_PASSWORD_EXCEPTION,
|
|
55
|
+
'COM.CRM.EXCEPTIONS.INVALIDCONTACTPASSWORDEXCEPTION': ErrorCodes.PASSWORD_LENGTH_EXCEPTION,
|
|
56
|
+
'CRM.EXCEPTIONS.INVALIDCONTACTPASSWORDEXCEPTION': ErrorCodes.PASSWORD_LENGTH_EXCEPTION,
|
|
57
|
+
'COM.CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION': ErrorCodes.INVALIDVALUEEXCEPTION,
|
|
58
|
+
'CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION': ErrorCodes.INVALIDVALUEEXCEPTION,
|
|
59
|
+
|
|
60
|
+
// Contact / Registration
|
|
61
|
+
'COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION': ErrorCodes.CUSTOMER_EMAIL_ALREADY_EXIST,
|
|
62
|
+
'CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION': ErrorCodes.CUSTOMER_EMAIL_ALREADY_EXIST,
|
|
63
|
+
'COM.CRM.EXCEPTIONS.CONTACTUNIQUELYIDENTIFYEXCEPTION': ErrorCodes.CONTACTUNIQUELYIDENTIFYEXCEPTION,
|
|
64
|
+
'COM.CRM.EXCEPTIONS.CANNOTUNREGISTERCONTACTEXCEPTION': ErrorCodes.CANNOTUNREGISTERCONTACTEXCEPTION,
|
|
65
|
+
'COM.CRM.EXCEPTIONS.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION': ErrorCodes.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION,
|
|
66
|
+
'COM.CRM.EXCEPTIONS.MULTIPLECONTACTSSAMEPHONEEXCEPTION': ErrorCodes.MULTIPLECONTACTSSAMEPHONEEXCEPTION,
|
|
67
|
+
'COM.CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION': ErrorCodes.MULTIPLECONTACTSSAMEPHONEEXCEPTION,
|
|
68
|
+
'CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION': ErrorCodes.MULTIPLECONTACTSSAMEPHONEEXCEPTION,
|
|
69
|
+
|
|
70
|
+
// Orders
|
|
71
|
+
'COM.CRM.EXCEPTIONS.ORDERS.CANNOTFULFILLORDEREXCEPTION': ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION,
|
|
72
|
+
'COM.CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION': ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION,
|
|
73
|
+
'CRM.EXCEPTIONS.CANNOTFULFILLORDEREXCEPTION': ErrorCodes.CAN_NOT_FULFILL_ORDER_EXCEPTION,
|
|
74
|
+
'COM.CRM.EXCEPTIONS.ORDERS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION': ErrorCodes.MINIMUM_ORDER_AMOUNT_NOT_REACHED,
|
|
75
|
+
'COM.CRM.EXCEPTIONS.MINIMUMORDERAMOUNTNOTREACHEDEXCEPTION': ErrorCodes.MINIMUM_ORDER_AMOUNT_NOT_REACHED,
|
|
76
|
+
|
|
77
|
+
// Subscriptions / Services
|
|
78
|
+
'COM.CRM.EXCEPTIONS.SERVICEALREADYEXISTSONSUBSCRIPTIONEXCEPTION': ErrorCodes.SERVICE_ALREADY_EXIST,
|
|
79
|
+
'COM.CRM.EXCEPTIONS.CANNOTEXECUTESUBSCRIPTIONACTIONEXCEPTION': ErrorCodes.CANNOTEXECUTESUBSCRIPTIONACTIONEXCEPTION,
|
|
80
|
+
|
|
81
|
+
// Finance / Wallet
|
|
82
|
+
'COM.CRM.EXCEPTIONS.SPENDAMOUNTNOTFULLYCOVEREDEXCEPTION': ErrorCodes.SPEND_AMOUNT_NOT_FULLY_COVERED_EXCEPTION,
|
|
83
|
+
'FINANCE.EXCEPTIONS.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION': ErrorCodes.CANNOTEXECUTEACTIONCREDITLIMITEXCEPTION,
|
|
84
|
+
'CRM.EXCEPTIONS.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION': ErrorCodes.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION,
|
|
85
|
+
|
|
86
|
+
// Rewards / Passes
|
|
87
|
+
'COM.CRM.EXCEPTIONS.THISPASSHASALREADYBEENREDEEMEDEXCEPTION': ErrorCodes.REDEEM_PASS_USED,
|
|
88
|
+
'COM.CRM.EXCEPTIONS.ONLYACTIVEPASSESCANBEREDEEMEDEXCEPTION': ErrorCodes.REDEEM_PASS_NOT_ACTIVE,
|
|
89
|
+
'CRM.EXCEPTIONS.NOTFOUNDEXCEPTION': ErrorCodes.NOTFOUNDEXCEPTION,
|
|
90
|
+
|
|
91
|
+
// Community
|
|
92
|
+
'COM.CRM.EXCEPTIONS.COMMUNITYPARENTCHILDEXCEPTION': ErrorCodes.COMMUNITYPARENTCHILDEXCEPTION,
|
|
93
|
+
|
|
94
|
+
// Lifecycle
|
|
95
|
+
'CRM.EXCEPTIONS.INVALIDENTITYLIFECYCLESTATEEXCEPTION': ErrorCodes.INVALIDENTITYLIFECYCLESTATEEXCEPTION,
|
|
96
|
+
};
|
|
97
|
+
|
|
45
98
|
export function createResult(errorCode, data) {
|
|
46
99
|
return {
|
|
47
100
|
code: errorCode,
|
|
@@ -51,36 +104,25 @@ export function createResult(errorCode, data) {
|
|
|
51
104
|
|
|
52
105
|
export function createCommonResult(response, requestType) {
|
|
53
106
|
logger.debug("createCommonResult response:", response);
|
|
54
|
-
|
|
107
|
+
|
|
108
|
+
// Success responses
|
|
109
|
+
if (response.code == 'OK' || response.code == '204') {
|
|
55
110
|
return createResult(ErrorCodes.OK, response.data);
|
|
56
|
-
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// HTTP status code mappings
|
|
114
|
+
if (response.code == '429') {
|
|
57
115
|
return createResult(ErrorCodes.TOO_MANY_REQUESTS, response.error);
|
|
58
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.SPENDAMOUNTNOTFULLYCOVEREDEXCEPTION") {
|
|
59
|
-
return createResult(ErrorCodes.SPEND_AMOUNT_NOT_FULLY_COVERED_EXCEPTION, response.error);
|
|
60
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CANNOTUNREGISTERCONTACTEXCEPTION") {
|
|
61
|
-
return createResult(ErrorCodes.CANNOTUNREGISTERCONTACTEXCEPTION, response.error)
|
|
62
|
-
} else if (response.error && response.error.error == "CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION") {
|
|
63
|
-
return createResult(ErrorCodes.INVALIDVALUEEXCEPTION, response.error)
|
|
64
|
-
} else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CONTACTUNIQUELYIDENTIFYEXCEPTION") {
|
|
65
|
-
return createResult(ErrorCodes.CONTACTUNIQUELYIDENTIFYEXCEPTION, response.error)
|
|
66
|
-
} else if (response.error && response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION") {
|
|
67
|
-
return createResult(ErrorCodes.CUSTOMER_EMAIL_ALREADY_EXIST, response.error)
|
|
68
|
-
} else if (response.error && response.error.error == "CRM.EXCEPTIONS.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION") {
|
|
69
|
-
return createResult(ErrorCodes.CANNOTSPENDAMOUNTWALLETBALANCENOTENOUGHEXCEPTION, response.error)
|
|
70
|
-
}else if (response.error && response.error.error == "CRM.EXCEPTIONS.NOTFOUNDEXCEPTION") {
|
|
71
|
-
return createResult(ErrorCodes.NOTFOUNDEXCEPTION, response.error)
|
|
72
|
-
}else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.MULTIPLECONTACTSSAMEPHONEEXCEPTION") {
|
|
73
|
-
return createResult(ErrorCodes.MULTIPLECONTACTSSAMEPHONEEXCEPTION, response.error)
|
|
74
|
-
}else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.COMMUNITYPARENTCHILDEXCEPTION") {
|
|
75
|
-
return createResult(ErrorCodes.COMMUNITYPARENTCHILDEXCEPTION, response.error)
|
|
76
|
-
}else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION") {
|
|
77
|
-
return createResult(ErrorCodes.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION, response.error)
|
|
78
|
-
}else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION") {
|
|
79
|
-
return createResult(ErrorCodes.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION, response.error)
|
|
80
|
-
}else if (response.error && response.error.error == "CRM.EXCEPTIONS.INVALIDENTITYLIFECYCLESTATEEXCEPTION") {
|
|
81
|
-
return createResult(ErrorCodes.INVALIDENTITYLIFECYCLESTATEEXCEPTION, response.error)
|
|
82
116
|
}
|
|
83
|
-
|
|
84
|
-
|
|
117
|
+
|
|
118
|
+
// Exception mapping — check both error.error and error.message fields
|
|
119
|
+
if (response.error) {
|
|
120
|
+
const exceptionKey = response.error.error || response.error.message;
|
|
121
|
+
if (exceptionKey && EXCEPTION_MAP[exceptionKey]) {
|
|
122
|
+
return createResult(EXCEPTION_MAP[exceptionKey], response.error);
|
|
123
|
+
}
|
|
85
124
|
}
|
|
86
|
-
|
|
125
|
+
|
|
126
|
+
// Fallback
|
|
127
|
+
return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
|
|
128
|
+
}
|
package/rewards.js
CHANGED
|
@@ -16,7 +16,8 @@ export const rewards = {
|
|
|
16
16
|
signOutRewardScheme,
|
|
17
17
|
getOfferDetail,
|
|
18
18
|
getDonationDetail,
|
|
19
|
-
getPassPlans
|
|
19
|
+
getPassPlans,
|
|
20
|
+
getProductRewards
|
|
20
21
|
}
|
|
21
22
|
async function getRewards() {
|
|
22
23
|
try {
|
|
@@ -33,6 +34,21 @@ async function getRewards() {
|
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
async function getProductRewards() {
|
|
38
|
+
try {
|
|
39
|
+
let id = httpUtil.getSession().sub;
|
|
40
|
+
|
|
41
|
+
let response = await httpUtil.get({
|
|
42
|
+
resourcePath: '/v2/product_rewards',
|
|
43
|
+
});
|
|
44
|
+
//check return code here instead of put as there would be different intepretation for different API
|
|
45
|
+
return createCommonResult(response);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
logger.error('Exception getProductRewards:', e);
|
|
48
|
+
return createResult(ErrorCodes.UNKNOWN, e);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
36
52
|
async function getOffers({
|
|
37
53
|
countries,
|
|
38
54
|
include_total = true,
|
|
@@ -233,31 +249,14 @@ async function redeemPass({
|
|
|
233
249
|
wallet_id: wallet_id,
|
|
234
250
|
}
|
|
235
251
|
});
|
|
236
|
-
|
|
237
|
-
if (response.code == "OK")
|
|
252
|
+
if (response.code == "OK") {
|
|
238
253
|
return createResult(ErrorCodes.OK, response.data);
|
|
239
|
-
else {
|
|
240
|
-
if(response.error && response.error.error == "CRM.EXCEPTIONS.NOTFOUNDEXCEPTION"){
|
|
241
|
-
return createResult(
|
|
242
|
-
ErrorCodes.REDEEM_PASS_INVALID,
|
|
243
|
-
response.error
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
else if(response.error && response.error.error == "COM.CRM.EXCEPTIONS.THISPASSHASALREADYBEENREDEEMEDEXCEPTION"){
|
|
247
|
-
return createResult(
|
|
248
|
-
ErrorCodes.REDEEM_PASS_USED,
|
|
249
|
-
response.error
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
else if(response.error && response.error.error == "COM.CRM.EXCEPTIONS.ONLYACTIVEPASSESCANBEREDEEMEDEXCEPTION"){
|
|
253
|
-
return createResult(
|
|
254
|
-
ErrorCodes.REDEEM_PASS_NOT_ACTIVE,
|
|
255
|
-
response.error
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
else
|
|
259
|
-
return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
|
|
260
254
|
}
|
|
255
|
+
// Pass-specific: NOTFOUNDEXCEPTION means invalid pass in this context
|
|
256
|
+
if (response.error && response.error.error == "CRM.EXCEPTIONS.NOTFOUNDEXCEPTION") {
|
|
257
|
+
return createResult(ErrorCodes.REDEEM_PASS_INVALID, response.error);
|
|
258
|
+
}
|
|
259
|
+
return createCommonResult(response);
|
|
261
260
|
} catch (e) {
|
|
262
261
|
logger.error('Exception redeemPass:', e);
|
|
263
262
|
return createResult(ErrorCodes.UNKNOWN, e);
|
package/subscriptions.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { httpUtil } from './httpUtil'
|
|
2
2
|
import { ErrorCodes, createResult, createCommonResult } from './resultUtil'
|
|
3
|
-
import { getData } from '../../utils/common';
|
|
4
3
|
import { logger } from './logger';
|
|
5
4
|
export const subscriptions = {
|
|
6
5
|
getListContactServices,
|
|
@@ -263,11 +262,7 @@ async function onServiceDelivery({
|
|
|
263
262
|
},
|
|
264
263
|
withAccessToken: true,
|
|
265
264
|
});
|
|
266
|
-
|
|
267
|
-
return createResult(ErrorCodes.OK, response.data);
|
|
268
|
-
else {
|
|
269
|
-
return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
|
|
270
|
-
}
|
|
265
|
+
return createCommonResult(response);
|
|
271
266
|
} catch (e) {
|
|
272
267
|
logger.error("Exception onServiceDelivery:", e);
|
|
273
268
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
@@ -287,17 +282,13 @@ async function onEstimateBilling({
|
|
|
287
282
|
contact_id: id,
|
|
288
283
|
account_id: account_id,
|
|
289
284
|
subscription_id: subscription_id,
|
|
290
|
-
upcoming_billing_cycles : upcoming_billing_cycles
|
|
285
|
+
upcoming_billing_cycles : upcoming_billing_cycles
|
|
291
286
|
},
|
|
292
287
|
withAccessToken: true,
|
|
293
288
|
});
|
|
294
|
-
|
|
295
|
-
return createResult(ErrorCodes.OK, response.data);
|
|
296
|
-
else {
|
|
297
|
-
return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
|
|
298
|
-
}
|
|
289
|
+
return createCommonResult(response);
|
|
299
290
|
} catch (e) {
|
|
300
|
-
logger.error("Exception
|
|
291
|
+
logger.error("Exception onEstimateBilling:", e);
|
|
301
292
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
302
293
|
}
|
|
303
294
|
}
|
|
@@ -310,11 +301,7 @@ async function onUpdateServiceWithBody(service_id, body) {
|
|
|
310
301
|
body: body,
|
|
311
302
|
withAccessToken: false,
|
|
312
303
|
});
|
|
313
|
-
|
|
314
|
-
return createResult(ErrorCodes.OK, response.data);
|
|
315
|
-
else {
|
|
316
|
-
return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
|
|
317
|
-
}
|
|
304
|
+
return createCommonResult(response);
|
|
318
305
|
} catch (e) {
|
|
319
306
|
logger.error("Exception onUpdateServiceWithBody:", e);
|
|
320
307
|
return createResult(ErrorCodes.UNKNOWN, e);
|
package/wallet.js
CHANGED
|
@@ -24,18 +24,23 @@ export const wallet = {
|
|
|
24
24
|
|
|
25
25
|
async function getWallet(walletId) {
|
|
26
26
|
try {
|
|
27
|
+
console.log("walletId", walletId)
|
|
28
|
+
|
|
27
29
|
if (!walletId) {
|
|
28
30
|
walletId = await getWalletId();
|
|
29
31
|
}
|
|
32
|
+
console.log("walletId1", walletId)
|
|
30
33
|
|
|
31
34
|
let response = await httpUtil.get({
|
|
32
35
|
resourcePath: '/v2/wallets/' + walletId,
|
|
33
36
|
withAccessToken: true,
|
|
34
37
|
logOutIfSessionInvalid: false,
|
|
35
38
|
});
|
|
39
|
+
console.log("result", response)
|
|
36
40
|
//check return code here instead of put as there would be different intepretation for different API
|
|
37
41
|
return createCommonResult(response);
|
|
38
42
|
} catch (e) {
|
|
43
|
+
console.log("error", e)
|
|
39
44
|
logger.error('Exception getWallet:', e);
|
|
40
45
|
return createResult(ErrorCodes.UNKNOWN, e);
|
|
41
46
|
}
|