@como/como-client 0.1.8 → 0.1.9-1.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.
- package/README.md +14 -0
- package/index.js +3 -0
- package/lib/como.js +2119 -492
- package/lib/config/env.js +19 -5
- package/lib/constants/resourceNames.js +3 -0
- package/lib/interceptors/refresh.js +92 -36
- package/lib/interceptors/response.js +46 -0
- package/lib/services/auth.service.js +212 -46
- package/lib/services/form.service.js +96 -0
- package/lib/services/index.js +25 -12
- package/lib/services/log.service.js +332 -0
- package/lib/services/loyalty.service.js +769 -14
- package/lib/services/ssr.service.js +75 -0
- package/lib/utils/comoExternalUrlTranslator.js +242 -0
- package/lib/utils/urls.js +27 -0
- package/package.json +16 -7
- package/src/como.js +930 -0
- package/.babelrc +0 -15
- package/.eslintignore +0 -1
- package/.eslintrc +0 -46
- package/.vscode/launch.json +0 -14
- package/como.js +0 -413
- package/webpack.config.js +0 -13
- package/yarn.lock +0 -5026
package/src/como.js
ADDED
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
const moment = require('moment-timezone');
|
|
2
|
+
const axios = require('axios');
|
|
3
|
+
const ramda = require('ramda');
|
|
4
|
+
const environmentConfig = require('./config/env');
|
|
5
|
+
const APIService = require('./services');
|
|
6
|
+
const comoExternalUrlTranslator = require('./utils/comoExternalUrlTranslator');
|
|
7
|
+
const resourceNames = require('./constants/resourceNames');
|
|
8
|
+
const urlsUtils = require('./utils/urls');
|
|
9
|
+
|
|
10
|
+
const INITIAL_COMO_DATA = {
|
|
11
|
+
env: environmentConfig,
|
|
12
|
+
token: null,
|
|
13
|
+
locationId: null,
|
|
14
|
+
locationState: null,
|
|
15
|
+
installationId: null,
|
|
16
|
+
membership: null,
|
|
17
|
+
assets: null,
|
|
18
|
+
mainResources: {},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
class Como {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.comoData = {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async refreshTokenIfNeeded() {
|
|
27
|
+
const { refresher, expiresIn } = this.comoData;
|
|
28
|
+
if(!refresher){
|
|
29
|
+
console.log('No refresh for %o', this.comoData);
|
|
30
|
+
}
|
|
31
|
+
if (refresher && expiresIn && moment(expiresIn).subtract(10, 'seconds').isBefore(moment())) {
|
|
32
|
+
await refresher();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Sign the user and returns guest token & refresh token.
|
|
39
|
+
* Requires init.
|
|
40
|
+
*/
|
|
41
|
+
async signon(source) {
|
|
42
|
+
const { api } = this.comoData;
|
|
43
|
+
try {
|
|
44
|
+
const data = await api.signOn(this.comoData.installationId, this.comoData.locationId, this.comoData.refreshToken, source);
|
|
45
|
+
if (data && data.config && data.config.loyaltyCDN) {
|
|
46
|
+
this.comoData.env_params.services_root_cdn = data.config.loyaltyCDN;
|
|
47
|
+
}
|
|
48
|
+
return data;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error.response && error.response.data) {
|
|
51
|
+
const { code, message } = error.response.data;
|
|
52
|
+
throw new Error(code || message);
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @param {Object} environment Environemnt object
|
|
61
|
+
* @param {string} environment.id Environment Id
|
|
62
|
+
* @param {string} environment.locationId Location Id
|
|
63
|
+
* @param {string} environment.installationId Unique installation id
|
|
64
|
+
* @param {Object} credentials Member credentials (Optional)
|
|
65
|
+
* @param {string} accessToken session id string (Optional)
|
|
66
|
+
* @param {string} refreshToken refresh token string (Optional)
|
|
67
|
+
* @param {string} expiresIn access token expiration date formatted (Optional)
|
|
68
|
+
* @param {function} refreshCallback an event callback which fires each time user's tokens gets refreshed
|
|
69
|
+
* @param {boolean} enableSignon if disabled signOn api call will not be fired. Enabled by default.
|
|
70
|
+
*/
|
|
71
|
+
async init(environment, credentials, refreshCallback, enableSignon = true) {
|
|
72
|
+
const isExists = (x => !ramda.isNil(x) && !ramda.isEmpty(x));
|
|
73
|
+
const { id: environmentId, locationId, installationId, source } = environment;
|
|
74
|
+
if (!isExists(environmentId) || !isExists(locationId) || !isExists(installationId)) {
|
|
75
|
+
throw new Error(`missing parameters: ${environment}`);
|
|
76
|
+
}
|
|
77
|
+
this.comoData = {
|
|
78
|
+
...INITIAL_COMO_DATA,
|
|
79
|
+
...credentials,
|
|
80
|
+
installationId,
|
|
81
|
+
locationId,
|
|
82
|
+
source,
|
|
83
|
+
env_params: {
|
|
84
|
+
...INITIAL_COMO_DATA.env[environmentId],
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const updateCredentials = (creds, isNew = false) => {
|
|
89
|
+
this.updateLocalCreds(creds);
|
|
90
|
+
if (refreshCallback && isNew) {
|
|
91
|
+
refreshCallback(creds);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const getCredentials = this.getCredentials.bind(this);
|
|
96
|
+
const { gateway } = this.comoData.env_params;
|
|
97
|
+
const { service, refresher } = APIService
|
|
98
|
+
.getService(gateway, installationId, getCredentials, updateCredentials, source, locationId);
|
|
99
|
+
this.comoData.api = service;
|
|
100
|
+
this.comoData.refresher = refresher;
|
|
101
|
+
|
|
102
|
+
const { accessToken, refreshToken, expiresIn } = credentials;
|
|
103
|
+
const credsNotExist = !isExists(accessToken) || !isExists(refreshToken);
|
|
104
|
+
let isExpired = !credsNotExist && expiresIn && moment(expiresIn).isBefore(moment());
|
|
105
|
+
if(credsNotExist){
|
|
106
|
+
try{
|
|
107
|
+
await this.signon(installationId, locationId, source);
|
|
108
|
+
}catch(e){
|
|
109
|
+
//TODO: need to parse error
|
|
110
|
+
console.log(`Failed to signOn, installationId:${installationId}, locationId:${locationId}, source:${source}`);
|
|
111
|
+
isExpired = true;// user uid exist - do refresh
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
let refreshFailed = false;
|
|
115
|
+
if(isExpired){
|
|
116
|
+
try{
|
|
117
|
+
await this.refreshTokenIfNeeded();
|
|
118
|
+
}catch(e){
|
|
119
|
+
console.log(`Failed to refresh token, installationId:${installationId}, locationId:${locationId}, source:${source}`);
|
|
120
|
+
//TODO: need to parse error
|
|
121
|
+
//refresh failed - need to revoke and signOn again
|
|
122
|
+
refreshFailed = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if(refreshFailed){
|
|
126
|
+
await this.signon(installationId, locationId, source);
|
|
127
|
+
}
|
|
128
|
+
return this.getCredentials();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Updates the como.js local credentials settings.
|
|
133
|
+
* Used when making api calls to janua.
|
|
134
|
+
*
|
|
135
|
+
* @param {Object} credentials credentials to update.
|
|
136
|
+
* @param {Object} accessToken access token to update.
|
|
137
|
+
* @param {Object} refreshToken refresh token to update.
|
|
138
|
+
* @param {Object} expiresIn access token expiry date to update.
|
|
139
|
+
*/
|
|
140
|
+
updateLocalCreds(credentials = {}) {
|
|
141
|
+
this.comoData.accessToken = credentials.accessToken;
|
|
142
|
+
this.comoData.token = credentials.accessToken;
|
|
143
|
+
this.comoData.refreshToken = credentials.refreshToken;
|
|
144
|
+
this.comoData.expiresIn = credentials.expiresIn;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
getExternalUrlByID(itemID, gpsData, assetID, accessibilityData) {
|
|
148
|
+
const locationData = this.comoData.mainResources[resourceNames.LOCATION_DATA];
|
|
149
|
+
if (locationData && locationData.WebViews && locationData.WebViews[0].item) {
|
|
150
|
+
const possibleWebViews = locationData.WebViews[0].item.filter(u => u.id === itemID);
|
|
151
|
+
const url = possibleWebViews && possibleWebViews[0] && possibleWebViews[0].url;
|
|
152
|
+
return this.translateExternalUrl(url, gpsData, assetID, accessibilityData);
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async translateExternalUrl(url, gpsData, assetID, accessibilityData) {
|
|
158
|
+
const legacyUrl = await this.getAppModulesLegacyBaseUrl();
|
|
159
|
+
let externalUrl = url;
|
|
160
|
+
if (externalUrl && externalUrl.startsWith(legacyUrl)) {
|
|
161
|
+
externalUrl = externalUrl.replace(legacyUrl, this.comoData.env_params.app_modules_root);
|
|
162
|
+
}
|
|
163
|
+
const {
|
|
164
|
+
api, locationId, token, env_params: env, membership, assets,
|
|
165
|
+
} = this.comoData;
|
|
166
|
+
const locationData = this.comoData.mainResources && this.comoData.mainResources[resourceNames.LOCATION_DATA];
|
|
167
|
+
const apiObjects = { api, servicesRoot: env.services_root };
|
|
168
|
+
const dataToUse = {
|
|
169
|
+
locationId, token, membership, locationData, assets, gpsData, assetID, accessibilityData,
|
|
170
|
+
};
|
|
171
|
+
return comoExternalUrlTranslator.translateUrl(externalUrl, apiObjects, dataToUse);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async getFeedbackFormData() {
|
|
175
|
+
const {
|
|
176
|
+
locationId, token, env_params: env, api,
|
|
177
|
+
} = this.comoData;
|
|
178
|
+
return api.getFeedbackFormData(env.zapp_root, locationId, token);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
*
|
|
183
|
+
* @param {Object} form registration form values
|
|
184
|
+
* @param {Object} verification flow
|
|
185
|
+
* @param {strin} reCaptchaToken
|
|
186
|
+
* @param {Object} source(registration origin)
|
|
187
|
+
*/
|
|
188
|
+
async register(form, verification, reCaptchaToken, source) {
|
|
189
|
+
const { api } = this.comoData;
|
|
190
|
+
try {
|
|
191
|
+
const data = await api.register(form, verification, reCaptchaToken, source);
|
|
192
|
+
return data;
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (error.response && error.response.data) {
|
|
195
|
+
const { code, message } = error.response.data;
|
|
196
|
+
throw new Error(code || message);
|
|
197
|
+
}
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
* @param {string} verification flow (code & verification step)
|
|
205
|
+
* @param {string} type verification type
|
|
206
|
+
* @param {Object} form registration form values
|
|
207
|
+
* @param {string} reCaptchaToken
|
|
208
|
+
*/
|
|
209
|
+
async registerVerify(verification, type, form, reCaptchaToken) {
|
|
210
|
+
const { api } = this.comoData;
|
|
211
|
+
try {
|
|
212
|
+
const data = await api.registerVerify(verification, type, form, reCaptchaToken);
|
|
213
|
+
return data;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error.response && error.response.data) {
|
|
216
|
+
const { code, message } = error.response.data;
|
|
217
|
+
throw new Error(code || message);
|
|
218
|
+
}
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
*
|
|
225
|
+
* @param {Object} form update form values
|
|
226
|
+
* @param {string} reCaptchaToken
|
|
227
|
+
* @param {string} source request source
|
|
228
|
+
*/
|
|
229
|
+
async updateProfile(form, reCaptchaToken, source) {
|
|
230
|
+
const { api } = this.comoData;
|
|
231
|
+
try {
|
|
232
|
+
const data = await api.updateProfile(form, reCaptchaToken, source);
|
|
233
|
+
return data;
|
|
234
|
+
} catch (error) {
|
|
235
|
+
if (error.response && error.response.data) {
|
|
236
|
+
const { code, message } = error.response.data;
|
|
237
|
+
throw new Error(code || message);
|
|
238
|
+
}
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
*
|
|
245
|
+
* @param {string} verificationCode code verification sent by 3rd party
|
|
246
|
+
* @param {string} type verification type
|
|
247
|
+
* @param {Object} form update form values
|
|
248
|
+
* @param {string} reCaptchaToken
|
|
249
|
+
* @param {string} source request source
|
|
250
|
+
*/
|
|
251
|
+
async updateProfileVerify(verificationCode, type, form, reCaptchaToken, source) {
|
|
252
|
+
const { api } = this.comoData;
|
|
253
|
+
try {
|
|
254
|
+
const data = await api.updateProfileVerify(verificationCode, type, form, reCaptchaToken, source);
|
|
255
|
+
return data;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error.response && error.response.data) {
|
|
258
|
+
const { code, message } = error.response.data;
|
|
259
|
+
throw new Error(code || message);
|
|
260
|
+
}
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
*
|
|
267
|
+
* @param {string} unsubscribeSms unsubscribe user at location
|
|
268
|
+
* @param {string} PhoneNumber phone number to identify user
|
|
269
|
+
*/
|
|
270
|
+
async unsubscribeSms(PhoneNumber) {
|
|
271
|
+
const { api, locationId } = this.comoData;
|
|
272
|
+
try {
|
|
273
|
+
const identifier = { PhoneNumber };
|
|
274
|
+
const data = await api.unsubscribeSms(locationId, identifier);
|
|
275
|
+
return data;
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error.response && error.response.data) {
|
|
278
|
+
const { code, message } = error.response.data;
|
|
279
|
+
throw new Error(code || message);
|
|
280
|
+
}
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
*
|
|
287
|
+
* @param {Object} identifier an object which identifies the user by its credentials
|
|
288
|
+
* @param {string} type login type i.e PhoneNumber/Email/etc
|
|
289
|
+
*/
|
|
290
|
+
async login(identifier, type) {
|
|
291
|
+
const { api } = this.comoData;
|
|
292
|
+
try {
|
|
293
|
+
const data = await api.logIn(type, identifier);
|
|
294
|
+
return data;
|
|
295
|
+
} catch (error) {
|
|
296
|
+
if (error.response && error.response.data) {
|
|
297
|
+
const { code, message } = error.response.data;
|
|
298
|
+
throw new Error(code || message);
|
|
299
|
+
}
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
*
|
|
306
|
+
* @param {Object} identifier key-value identifiers
|
|
307
|
+
* @param {*} type login type i.e PhoneNumber/Email/etc
|
|
308
|
+
* @param {*} code a 2-factor verification code sent to the client
|
|
309
|
+
*/
|
|
310
|
+
async loginVerify(identifier, type, code) {
|
|
311
|
+
const { api } = this.comoData;
|
|
312
|
+
try {
|
|
313
|
+
const { consent } = await api.logInVerify(code, type, identifier);
|
|
314
|
+
this.comoData.consent = consent;
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (error.response && error.response.data) {
|
|
317
|
+
const { code: errCode, message } = error.response.data;
|
|
318
|
+
if (message && message === 'Invalid verification Code') {
|
|
319
|
+
throw new Error(message);
|
|
320
|
+
}
|
|
321
|
+
throw new Error(errCode || message);
|
|
322
|
+
}
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return this.getCredentials();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Reload and sets the internal membership state.
|
|
331
|
+
* To get the reloaded membership make sure to call getMembership();
|
|
332
|
+
* @param onlyAssetKeys - include assets list full data or only the asset keys
|
|
333
|
+
* @returns {Promise<void>}
|
|
334
|
+
*/
|
|
335
|
+
async reloadMembership(onlyAssetKeys = false) {
|
|
336
|
+
const {
|
|
337
|
+
env_params: env, locationId, api,
|
|
338
|
+
} = this.comoData;
|
|
339
|
+
const membership = await api.getUserAtLocation(env.zapp_root, locationId, onlyAssetKeys);
|
|
340
|
+
this.comoData.membership = membership;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Send an Open app request to update server.
|
|
345
|
+
*/
|
|
346
|
+
async openApp(data) {
|
|
347
|
+
const {
|
|
348
|
+
api, locationId, token, env_params: env,
|
|
349
|
+
} = this.comoData;
|
|
350
|
+
|
|
351
|
+
return api.openApp(env.zapp_root, token, locationId, data);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Update membership status (permissions).
|
|
356
|
+
* @param {boolean} pushEnable
|
|
357
|
+
* @param {boolean} locationEnable
|
|
358
|
+
*/
|
|
359
|
+
async clientUpdateMembership(pushEnable, locationEnable) {
|
|
360
|
+
const {
|
|
361
|
+
api, locationId, token, env_params: env,
|
|
362
|
+
} = this.comoData;
|
|
363
|
+
|
|
364
|
+
return api.clientUpdateMembership(env.services_root, token, locationId, pushEnable, locationEnable);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Downloads the main resource by its id and sets the resource in the main resource's map.
|
|
369
|
+
* @param {string} resourceId Main resource id
|
|
370
|
+
*/
|
|
371
|
+
|
|
372
|
+
async downloadMainResource(resourceId) {
|
|
373
|
+
const resource = this.comoData.mainResourcesMap[resourceId];
|
|
374
|
+
if (!resource) {
|
|
375
|
+
const err = `resource ${resourceId} not found!`;
|
|
376
|
+
throw new Error(err);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const { env_params: env, api } = this.comoData;
|
|
380
|
+
const responseJson = await api.getLoyaltyResource(
|
|
381
|
+
env.services_root_cdn,
|
|
382
|
+
resource.id,
|
|
383
|
+
('Namespace' in resource) ? resource.Namespace : `location_${this.comoData.locationId}`,
|
|
384
|
+
resource.Version,
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
this.comoData.mainResources[resourceId] = responseJson;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Reload and sets the internal location state.
|
|
393
|
+
* To get the reloaded location state make sure to call getLocationState();
|
|
394
|
+
*/
|
|
395
|
+
async reloadLocationState() {
|
|
396
|
+
const { api } = this.comoData;
|
|
397
|
+
const state = await api.getLocationState(this.comoData.env_params.zapp_root, this.comoData.locationId);
|
|
398
|
+
this.comoData.locationState = state;
|
|
399
|
+
const mainResources = state.MainResources.Resource;
|
|
400
|
+
|
|
401
|
+
this.comoData.mainResourcesMap = mainResources.reduce((total, obj) => {
|
|
402
|
+
total[obj.id] = obj; // eslint-disable-line no-param-reassign
|
|
403
|
+
return total;
|
|
404
|
+
}, {});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Requests an app client id by the user's location id.
|
|
409
|
+
*/
|
|
410
|
+
async getAppClientID() {
|
|
411
|
+
const {
|
|
412
|
+
env_params: env, locationId, api,
|
|
413
|
+
} = this.comoData;
|
|
414
|
+
const responseJson = await api.getShortUserId(env.zapp_root, locationId);
|
|
415
|
+
return responseJson.ID;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Requests a generated payment code from the loyalty server
|
|
420
|
+
*/
|
|
421
|
+
async getPaymentCode() {
|
|
422
|
+
const {
|
|
423
|
+
env_params: env, locationId, api,
|
|
424
|
+
} = this.comoData;
|
|
425
|
+
const responseJson = await api.getPaymentCode(env.zapp_root, locationId);
|
|
426
|
+
return responseJson.VerificationCode;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Logout the user identified by its accessToken
|
|
431
|
+
*/
|
|
432
|
+
async logout() {
|
|
433
|
+
const { api } = this.comoData;
|
|
434
|
+
try {
|
|
435
|
+
const response = await api.logOut();
|
|
436
|
+
return response;
|
|
437
|
+
} catch (error) {
|
|
438
|
+
if (error.response && error.response.data) {
|
|
439
|
+
const { code: errCode, message } = error.response.data;
|
|
440
|
+
throw new Error(errCode || message);
|
|
441
|
+
}
|
|
442
|
+
throw error;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
*
|
|
448
|
+
* @param {string} assetKey asset's id
|
|
449
|
+
*/
|
|
450
|
+
async redeemAsset(assetKey) {
|
|
451
|
+
const {
|
|
452
|
+
env_params: env, locationId, api,
|
|
453
|
+
} = this.comoData;
|
|
454
|
+
const responseJson = await api.redeemAsset(env.zapp_root, locationId, assetKey);
|
|
455
|
+
return responseJson;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
*
|
|
460
|
+
* @param {string} itemId item's id
|
|
461
|
+
*/
|
|
462
|
+
async purchaseAsset(itemId) {
|
|
463
|
+
const {
|
|
464
|
+
env_params: env, token, locationId, api,
|
|
465
|
+
} = this.comoData;
|
|
466
|
+
const responseJson = await api.purchaseAsset(env.zapp_root, locationId, token, itemId);
|
|
467
|
+
return responseJson;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
async getUserPointShopDataAPI2() {
|
|
471
|
+
const {
|
|
472
|
+
env_params: env, locationId, api,
|
|
473
|
+
} = this.comoData;
|
|
474
|
+
const responseJson = await api.getUserPointShopDataAPI2(env.zapp_root, locationId);
|
|
475
|
+
return responseJson;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
async purchaseAssetAPI2(assetID) {
|
|
480
|
+
const {
|
|
481
|
+
env_params: env, locationId, api,
|
|
482
|
+
} = this.comoData;
|
|
483
|
+
const responseJson = await api.purchaseAssetAPI2(env.zapp_root, locationId, assetID);
|
|
484
|
+
return responseJson;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Reload and sets the internal assets (expired and valid) state.
|
|
489
|
+
* To get the reloaded assets make sure to call como.getAssets();
|
|
490
|
+
*/
|
|
491
|
+
async reloadAssets() {
|
|
492
|
+
const {
|
|
493
|
+
env_params: env, locationId, api,
|
|
494
|
+
} = this.comoData;
|
|
495
|
+
const responseJson = await api.memberAssets(env.services_root, locationId);
|
|
496
|
+
this.comoData.assets = responseJson;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Merge the default app text with the location specific app texts
|
|
501
|
+
* Both DefaultAppTexts and AppTexts are a MainResource that needs to be downloaded before merge
|
|
502
|
+
*/
|
|
503
|
+
mergeTexts() {
|
|
504
|
+
if (this.comoData.mainResources.AppTexts && this.comoData.mainResources.DefaultAppTexts) {
|
|
505
|
+
this.comoData.app_texts = this.comoData.mainResources.DefaultAppTexts;
|
|
506
|
+
Object.keys(this.comoData.mainResources.AppTexts).forEach(key => {
|
|
507
|
+
this.comoData.app_texts[key] = this.comoData.mainResources.AppTexts[key];
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
*
|
|
514
|
+
* @param {string} key app text key
|
|
515
|
+
*/
|
|
516
|
+
getTextForKey(key) { return this.comoData.app_texts[key]; }
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Downloads the main resources:
|
|
520
|
+
* - LocationData
|
|
521
|
+
* - Menu
|
|
522
|
+
* - Updates
|
|
523
|
+
* - TCAlts
|
|
524
|
+
* - AppTexts
|
|
525
|
+
* - DefaultAppTexts
|
|
526
|
+
*/
|
|
527
|
+
async downloadMainResources() {
|
|
528
|
+
const resources = [
|
|
529
|
+
'LocationData',
|
|
530
|
+
'Menu',
|
|
531
|
+
'Updates',
|
|
532
|
+
'TCAlts',
|
|
533
|
+
'AppTexts',
|
|
534
|
+
'DefaultAppTexts',
|
|
535
|
+
];
|
|
536
|
+
const downloadMainResource = this.downloadMainResource.bind(this);
|
|
537
|
+
const downloads = resources.map(downloadMainResource);
|
|
538
|
+
await Promise.all(downloads);
|
|
539
|
+
this.mergeTexts();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
*
|
|
544
|
+
* @param {string} type form type
|
|
545
|
+
*/
|
|
546
|
+
loadForm(type) {
|
|
547
|
+
const { api, locationId } = this.comoData;
|
|
548
|
+
|
|
549
|
+
return api.getForm(type, locationId);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
*
|
|
554
|
+
* @param {string} type configuration type
|
|
555
|
+
*/
|
|
556
|
+
getConfigration(type) {
|
|
557
|
+
const { api, locationId } = this.comoData;
|
|
558
|
+
const locationState = this.getLocationState();
|
|
559
|
+
let version = null;
|
|
560
|
+
if (locationState) {
|
|
561
|
+
version = locationState.PublishVersion;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
return api.getConfiguration(type, locationId, version);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
*
|
|
569
|
+
* @param {string} type resource type
|
|
570
|
+
*/
|
|
571
|
+
getResource(type) {
|
|
572
|
+
const { api, locationId } = this.comoData;
|
|
573
|
+
return api.getResource(type, locationId);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
*
|
|
578
|
+
* Get all legacy resources (Zapp)
|
|
579
|
+
*/
|
|
580
|
+
getLegacyResources() {
|
|
581
|
+
const { api, locationId } = this.comoData;
|
|
582
|
+
return api.getZappResource(locationId);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Generates a new referral code for friend-referral
|
|
587
|
+
*/
|
|
588
|
+
generateReferralCode() {
|
|
589
|
+
const {
|
|
590
|
+
api, locationId, token, env_params: env,
|
|
591
|
+
} = this.comoData;
|
|
592
|
+
|
|
593
|
+
return api.generateReferralCode(env.services_root, token, locationId);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Check that referral code is valid
|
|
598
|
+
*/
|
|
599
|
+
verifyReferralCode(code) {
|
|
600
|
+
const {
|
|
601
|
+
api, locationId, token, env_params: env,
|
|
602
|
+
} = this.comoData;
|
|
603
|
+
|
|
604
|
+
return api.verifyReferralCode(env.services_root, token, locationId, code);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Returns current accessToken and refreshToken
|
|
609
|
+
* Caution: make sure to pass refresh callback to init to get the latest credentials
|
|
610
|
+
*/
|
|
611
|
+
getCredentials() {
|
|
612
|
+
const {
|
|
613
|
+
accessToken, refreshToken, expiresIn, consent,
|
|
614
|
+
} = this.comoData;
|
|
615
|
+
return {
|
|
616
|
+
accessToken, refreshToken, expiresIn, consent,
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Returns the current accessToken
|
|
622
|
+
* Caution: make sure to pass refresh callback to como.init to get the latest credentials
|
|
623
|
+
*/
|
|
624
|
+
getToken() { return this.comoData.token; }
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Returns the como.js initialized installation id
|
|
628
|
+
*/
|
|
629
|
+
getInstallationId() { return this.comoData.installationId; }
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Return the user membership object
|
|
633
|
+
* Make sure to call reloadMembership() to reload the membership object.
|
|
634
|
+
*/
|
|
635
|
+
getMembership() { return this.comoData.membership; }
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Return the user assets object
|
|
639
|
+
* Make sure to call reloadAssets() to reload the assets object.
|
|
640
|
+
*/
|
|
641
|
+
getAssets() { return this.comoData.assets; }
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Returns the location specific registration web url
|
|
645
|
+
*/
|
|
646
|
+
async getRegistrationUrl(overrides) {
|
|
647
|
+
await this.refreshTokenIfNeeded();
|
|
648
|
+
const {
|
|
649
|
+
token, installationId, locationId, env_params: env,
|
|
650
|
+
} = this.comoData;
|
|
651
|
+
|
|
652
|
+
return urlsUtils.appendQuery(
|
|
653
|
+
`${env.app_modules_root}/register?token=${token}&locationid=${locationId}`,
|
|
654
|
+
{
|
|
655
|
+
...this.getCredentials(),
|
|
656
|
+
installationId,
|
|
657
|
+
disableSuccessScreen: 'true',
|
|
658
|
+
...overrides,
|
|
659
|
+
},
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Returns the location specific profile web url
|
|
665
|
+
*/
|
|
666
|
+
async getProfileUrl(overrides) {
|
|
667
|
+
await this.refreshTokenIfNeeded();
|
|
668
|
+
const {
|
|
669
|
+
token, installationId, locationId, env_params: env,
|
|
670
|
+
} = this.comoData;
|
|
671
|
+
|
|
672
|
+
return urlsUtils.appendQuery(
|
|
673
|
+
`${env.app_modules_root}/profile?token=${token}&locationid=${locationId}`,
|
|
674
|
+
{
|
|
675
|
+
...this.getCredentials(),
|
|
676
|
+
installationId,
|
|
677
|
+
disableSuccessScreen: 'true',
|
|
678
|
+
...overrides,
|
|
679
|
+
},
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Returns the location specific login web url
|
|
685
|
+
*/
|
|
686
|
+
async getLoginUrl() {
|
|
687
|
+
await this.refreshTokenIfNeeded();
|
|
688
|
+
const { token, locationId, env_params: env } = this.comoData;
|
|
689
|
+
return `${env.app_modules_root}/login?token=${token}&locationid=${locationId}`;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Returns the location data Portability url
|
|
694
|
+
*/
|
|
695
|
+
async getDataPortabilityUrl(url, overrides) {
|
|
696
|
+
await this.refreshTokenIfNeeded();
|
|
697
|
+
const {
|
|
698
|
+
installationId,
|
|
699
|
+
} = this.comoData;
|
|
700
|
+
|
|
701
|
+
return urlsUtils.appendQuery(
|
|
702
|
+
`${url}`,
|
|
703
|
+
{
|
|
704
|
+
installationId,
|
|
705
|
+
...overrides,
|
|
706
|
+
},
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Returns app-modules legacy root url
|
|
712
|
+
*/
|
|
713
|
+
async getAppModulesLegacyBaseUrl() {
|
|
714
|
+
await this.refreshTokenIfNeeded();
|
|
715
|
+
const { env_params: env } = this.comoData;
|
|
716
|
+
return `${env.app_modules_legacy_root}`;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Returns the location specific friend referral web url
|
|
721
|
+
*/
|
|
722
|
+
getFriendReferralInviteLink(referral) { return `${this.comoData.env_params.gateway}/ssr-modules/friend-referral/${this.comoData.locationId}?referral=${referral}`; }
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Return the location state
|
|
726
|
+
* Make sure to call reloadLocationState() to reload the location state object.
|
|
727
|
+
*/
|
|
728
|
+
getLocationState() { return this.comoData.locationState; }
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Returns the location point shop items
|
|
732
|
+
*/
|
|
733
|
+
getPointShopItems() { return this.comoData.mainResources.menu.gifts_shop_item; }
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
*
|
|
737
|
+
* @param {string} resourceName resource name
|
|
738
|
+
*/
|
|
739
|
+
getMainResource(resourceName) { return this.comoData.mainResources[resourceName]; }
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
*
|
|
743
|
+
* @param {string} resourceId resource id
|
|
744
|
+
* @param {string} namespace resource namespace
|
|
745
|
+
* @param {string|number} version resource version
|
|
746
|
+
*/
|
|
747
|
+
getResourceUrl(resourceId, namespace, version) {
|
|
748
|
+
const { api, env_params: env } = this.comoData;
|
|
749
|
+
return api.getResourceUrl(env.services_root_cdn, resourceId, namespace, version);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
getCdnUrl() {
|
|
753
|
+
return this.comoData.env_params.services_root_cdn;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* @param {string} identifier fcm token created by firebase
|
|
758
|
+
* @param {boolean} isFirebaseProjectMigrated is migrated to new firebase project
|
|
759
|
+
*/
|
|
760
|
+
sendPushChannel(identifier, isFirebaseProjectMigrated = false) {
|
|
761
|
+
const {
|
|
762
|
+
api, locationId, token, env_params: env,
|
|
763
|
+
} = this.comoData;
|
|
764
|
+
return api.sendPushChannel(env.services_root, locationId, token, identifier, isFirebaseProjectMigrated);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
async getRegionConfig() {
|
|
768
|
+
const {
|
|
769
|
+
api, locationId, token, env_params: env,
|
|
770
|
+
} = this.comoData;
|
|
771
|
+
return api.getClientBusinessBackend(env.zapp_root, token, locationId);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* @param {string} code coupon code entered by user on App
|
|
776
|
+
*/
|
|
777
|
+
sendCouponCode(code) {
|
|
778
|
+
const {
|
|
779
|
+
api, locationId, token, env_params: env,
|
|
780
|
+
} = this.comoData;
|
|
781
|
+
return api.sendCouponCode(env.zapp_root, locationId, token, code);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
async addPaymentMethod(cardToken) {
|
|
785
|
+
const {
|
|
786
|
+
api, locationId, token, env_params: env,
|
|
787
|
+
} = this.comoData;
|
|
788
|
+
return api.addPaymentMethod(env.services_root, locationId, token, cardToken);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
async knowYourData(email) {
|
|
792
|
+
const {
|
|
793
|
+
api, locationId, token, env_params: env,
|
|
794
|
+
} = this.comoData;
|
|
795
|
+
return api.knowYourData(env.services_root, locationId, token, email);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
async consentWithdraw(consentData) {
|
|
799
|
+
const {
|
|
800
|
+
api, locationId, token, env_params: env,
|
|
801
|
+
} = this.comoData;
|
|
802
|
+
return api.consentWithdraw(env.services_root, locationId, token, consentData);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
async getPaymentMethods() {
|
|
806
|
+
const {
|
|
807
|
+
env_params: env, locationId, api,
|
|
808
|
+
} = this.comoData;
|
|
809
|
+
const responseJson = await api.getPaymentMethods(env.services_root, locationId);
|
|
810
|
+
return responseJson;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* @param {Integer} amount to charge
|
|
815
|
+
* @param {String} creditCardToken to charge from
|
|
816
|
+
* @param {String} itemName for submit purchase
|
|
817
|
+
* @param {String} itemNumber for submit purchase
|
|
818
|
+
* @param {String} membershipKey for user identification
|
|
819
|
+
*/
|
|
820
|
+
async payInApp(amount, creditCardToken, itemName, itemNumber, membershipKey) {
|
|
821
|
+
const {
|
|
822
|
+
env_params: env, token, locationId, api,
|
|
823
|
+
} = this.comoData;
|
|
824
|
+
const responseJson = await api.payInApp(env.services_root, locationId, token, amount, creditCardToken, itemName, itemNumber, membershipKey);
|
|
825
|
+
return responseJson;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* @param {string} creditCardToken which card to delete
|
|
830
|
+
*/
|
|
831
|
+
async deletePayment(creditCardToken) {
|
|
832
|
+
const {
|
|
833
|
+
env_params: env, token, locationId, api,
|
|
834
|
+
} = this.comoData;
|
|
835
|
+
const responseJson = await api.deletePayment(env.services_root, locationId, token, creditCardToken);
|
|
836
|
+
return responseJson;
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* @param {string} tag for Pop-up Message Trigger (Optional param)
|
|
840
|
+
*/
|
|
841
|
+
getMemberMessages(tag, limit, offset) {
|
|
842
|
+
const {
|
|
843
|
+
api, locationId, env_params: env,
|
|
844
|
+
} = this.comoData;
|
|
845
|
+
return api.getMemberMessages(env.services_root, locationId, tag, limit, offset);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/**
|
|
849
|
+
* @param {string} keysList keys of the messages to mark as read
|
|
850
|
+
*/
|
|
851
|
+
markAsReadMemberMessages(keysList) {
|
|
852
|
+
const {
|
|
853
|
+
api, locationId, env_params: env,
|
|
854
|
+
} = this.comoData;
|
|
855
|
+
return api.markAsReadMemberMessages(env.services_root, locationId, keysList);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* @param {String} receiver to share with
|
|
860
|
+
* @param {String} sender name
|
|
861
|
+
* @param {Integer} amount to share
|
|
862
|
+
* @param {String} type of wallet
|
|
863
|
+
*/
|
|
864
|
+
async budgetTransfer(receiver, sender, amount, wallet) {
|
|
865
|
+
const {
|
|
866
|
+
api, locationId, env_params: env,
|
|
867
|
+
} = this.comoData;
|
|
868
|
+
return api.budgetTransfer(env.services_root, locationId, receiver, sender, amount, wallet);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* @param {string} claimId key of the credit claim to read the data for
|
|
873
|
+
*/
|
|
874
|
+
getCreditClaimData(claimId) {
|
|
875
|
+
const {
|
|
876
|
+
api, env_params: env,
|
|
877
|
+
} = this.comoData;
|
|
878
|
+
return api.getCreditClaimData(env.services_root, claimId);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* @param {String} claimCode to use for budget transfer claim
|
|
884
|
+
*/
|
|
885
|
+
async budgetTransferClaim(claimCode) {
|
|
886
|
+
const {
|
|
887
|
+
api, locationId, env_params: env,
|
|
888
|
+
} = this.comoData;
|
|
889
|
+
return api.budgetTransferClaim(env.services_root, locationId, claimCode);
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* API call for claim generic action. To specify your trigger button, you need to associate it with a particular item, asset or tag.
|
|
894
|
+
* @param {string} assetKey - asset to claim
|
|
895
|
+
* @param {string} itemId - item id to claim
|
|
896
|
+
* @param {string} tag - claim all with tag
|
|
897
|
+
*/
|
|
898
|
+
claim(assetKey, itemId, tag) {
|
|
899
|
+
const {
|
|
900
|
+
env_params: env, locationId, api, token,
|
|
901
|
+
} = this.comoData;
|
|
902
|
+
return api.claim(env.zapp_root, locationId, token, assetKey, itemId, tag);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Log user action
|
|
907
|
+
* @param {string} action
|
|
908
|
+
* @param {string} logData - log information
|
|
909
|
+
*/
|
|
910
|
+
async logUserAction(action, logData) {
|
|
911
|
+
const {
|
|
912
|
+
env_params: env, locationId, api, token,
|
|
913
|
+
} = this.comoData;
|
|
914
|
+
return api.logUserAction(env.zapp_root, locationId, token, action, logData);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* Member purchase history
|
|
919
|
+
* @param {string} limit - how manay items to return (if exists) - default value is 25
|
|
920
|
+
* @param {string} offset - default value is 0(next offset would be 25)
|
|
921
|
+
*/
|
|
922
|
+
async getPurchaseHistory(limit, offset) {
|
|
923
|
+
const {
|
|
924
|
+
locationId, api, membership
|
|
925
|
+
} = this.comoData;
|
|
926
|
+
return api.getPurchaseHistory(locationId, membership.Key, limit, offset);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
module.exports = Como;
|