musora-content-services 1.2.5 → 1.3.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.
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @module Railcontent-Services
3
3
  */
4
- import {contentStatusCompleted} from "./contentProgress.js";
4
+ import { contentStatusCompleted } from './contentProgress.js'
5
5
 
6
- import { globalConfig } from "./config.js";
6
+ import { globalConfig } from './config.js'
7
7
 
8
8
  /**
9
9
  * Exported functions that are excluded from index generation.
@@ -11,17 +11,17 @@ import { globalConfig } from "./config.js";
11
11
  * @type {string[]}
12
12
  */
13
13
  const excludeFromGeneratedIndex = [
14
- 'fetchUserLikes',
15
- 'postContentLiked',
16
- 'postContentUnliked',
17
- 'postRecordWatchSession',
18
- 'postContentStarted',
19
- 'postContentCompleted',
20
- 'postContentReset',
21
- 'fetchUserPermissionsData'
22
- ];
23
-
24
- let challengeIndexMetaDataPromise = null;
14
+ 'fetchUserLikes',
15
+ 'postContentLiked',
16
+ 'postContentUnliked',
17
+ 'postRecordWatchSession',
18
+ 'postContentStarted',
19
+ 'postContentCompleted',
20
+ 'postContentReset',
21
+ 'fetchUserPermissionsData',
22
+ ]
23
+
24
+ let challengeIndexMetaDataPromise = null
25
25
 
26
26
  /**
27
27
  * Fetches the completion status of a specific lesson for the current user.
@@ -34,30 +34,29 @@ let challengeIndexMetaDataPromise = null;
34
34
  * .catch(error => console.error(error));
35
35
  */
36
36
  export async function fetchCompletedState(content_id) {
37
- const url = `/content/user_progress/${globalConfig.railcontentConfig.userId}?content_ids[]=${content_id}`;
38
-
39
- const headers = {
40
- 'Content-Type': 'application/json',
41
- 'Accept': 'application/json',
42
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
43
- };
44
-
45
- try {
46
- const response = await fetchAbsolute(url, {headers});
47
- const result = await response.json();
48
-
49
- if (result && result[content_id]) {
50
- return result[content_id]; // Return the correct object
51
- } else {
52
- return null; // Handle unexpected structure
53
- }
54
- } catch (error) {
55
- console.error('Fetch error:', error);
56
- return null;
37
+ const url = `/content/user_progress/${globalConfig.railcontentConfig.userId}?content_ids[]=${content_id}`
38
+
39
+ const headers = {
40
+ 'Content-Type': 'application/json',
41
+ Accept: 'application/json',
42
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
43
+ }
44
+
45
+ try {
46
+ const response = await fetchAbsolute(url, { headers })
47
+ const result = await response.json()
48
+
49
+ if (result && result[content_id]) {
50
+ return result[content_id] // Return the correct object
51
+ } else {
52
+ return null // Handle unexpected structure
57
53
  }
54
+ } catch (error) {
55
+ console.error('Fetch error:', error)
56
+ return null
57
+ }
58
58
  }
59
59
 
60
-
61
60
  /**
62
61
  * Fetches the completion status for multiple songs for the current user.
63
62
  *
@@ -69,26 +68,26 @@ export async function fetchCompletedState(content_id) {
69
68
  * .catch(error => console.error(error));
70
69
  */
71
70
  export async function fetchAllCompletedStates(contentIds) {
72
- const url = `/content/user_progress/${globalConfig.railcontentConfig.userId}?${contentIds.map(id => `content_ids[]=${id}`).join('&')}`;
73
-
74
- const headers = {
75
- 'Content-Type': 'application/json',
76
- 'Accept': 'application/json',
77
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
78
- };
79
-
80
- try {
81
- const response = await fetchAbsolute(url, {headers});
82
- const result = await response.json();
83
- if (result) {
84
- return result;
85
- } else {
86
- console.log('result not json');
87
- }
88
- } catch (error) {
89
- console.error('Fetch error:', error);
90
- return null;
71
+ const url = `/content/user_progress/${globalConfig.railcontentConfig.userId}?${contentIds.map((id) => `content_ids[]=${id}`).join('&')}`
72
+
73
+ const headers = {
74
+ 'Content-Type': 'application/json',
75
+ Accept: 'application/json',
76
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
77
+ }
78
+
79
+ try {
80
+ const response = await fetchAbsolute(url, { headers })
81
+ const result = await response.json()
82
+ if (result) {
83
+ return result
84
+ } else {
85
+ console.log('result not json')
91
86
  }
87
+ } catch (error) {
88
+ console.error('Fetch error:', error)
89
+ return null
90
+ }
92
91
  }
93
92
 
94
93
  /**
@@ -102,27 +101,27 @@ export async function fetchAllCompletedStates(contentIds) {
102
101
  * .catch(error => console.error(error));
103
102
  */
104
103
  export async function fetchSongsInProgress(brand) {
105
- const url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?content_type=song&brand=${brand}`;
106
-
107
- const headers = {
108
- 'Content-Type': 'application/json',
109
- 'Accept': 'application/json',
110
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
111
- };
112
-
113
- try {
114
- const response = await fetchAbsolute(url, {headers});
115
- const result = await response.json();
116
- if (result) {
117
- //console.log('fetchSongsInProgress', result);
118
- return result;
119
- } else {
120
- console.log('result not json');
121
- }
122
- } catch (error) {
123
- console.error('Fetch error:', error);
124
- return null;
104
+ const url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?content_type=song&brand=${brand}`
105
+
106
+ const headers = {
107
+ 'Content-Type': 'application/json',
108
+ Accept: 'application/json',
109
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
110
+ }
111
+
112
+ try {
113
+ const response = await fetchAbsolute(url, { headers })
114
+ const result = await response.json()
115
+ if (result) {
116
+ //console.log('fetchSongsInProgress', result);
117
+ return result
118
+ } else {
119
+ console.log('result not json')
125
120
  }
121
+ } catch (error) {
122
+ console.error('Fetch error:', error)
123
+ return null
124
+ }
126
125
  }
127
126
 
128
127
  /**
@@ -138,34 +137,34 @@ export async function fetchSongsInProgress(brand) {
138
137
  * .then(songs => console.log(songs))
139
138
  * .catch(error => console.error(error));
140
139
  */
141
- export async function fetchContentInProgress(type = "all", brand, {page, limit} = {}) {
142
- let url;
143
- const limitString = limit ? `&limit=${limit}` : '';
144
- const pageString = page ? `&page=${page}` : '';
145
-
146
- if (type === "all") {
147
- url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?brand=${brand}${limitString}${pageString}`;
140
+ export async function fetchContentInProgress(type = 'all', brand, { page, limit } = {}) {
141
+ let url
142
+ const limitString = limit ? `&limit=${limit}` : ''
143
+ const pageString = page ? `&page=${page}` : ''
144
+
145
+ if (type === 'all') {
146
+ url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?brand=${brand}${limitString}${pageString}`
147
+ } else {
148
+ url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?content_type=${type}&brand=${brand}${limitString}${pageString}`
149
+ }
150
+ const headers = {
151
+ 'Content-Type': 'application/json',
152
+ Accept: 'application/json',
153
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
154
+ }
155
+ try {
156
+ const response = await fetchAbsolute(url, { headers })
157
+ const result = await response.json()
158
+ if (result) {
159
+ //console.log('contentInProgress', result);
160
+ return result
148
161
  } else {
149
- url = `/content/in_progress/${globalConfig.railcontentConfig.userId}?content_type=${type}&brand=${brand}${limitString}${pageString}`;
150
- }
151
- const headers = {
152
- 'Content-Type': 'application/json',
153
- 'Accept': 'application/json',
154
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
155
- };
156
- try {
157
- const response = await fetchAbsolute(url, {headers});
158
- const result = await response.json();
159
- if (result) {
160
- //console.log('contentInProgress', result);
161
- return result;
162
- } else {
163
- console.log('result not json');
164
- }
165
- } catch (error) {
166
- console.error('Fetch error:', error);
167
- return null;
162
+ console.log('result not json')
168
163
  }
164
+ } catch (error) {
165
+ console.error('Fetch error:', error)
166
+ return null
167
+ }
169
168
  }
170
169
 
171
170
  /**
@@ -181,37 +180,36 @@ export async function fetchContentInProgress(type = "all", brand, {page, limit}
181
180
  * .then(songs => console.log(songs))
182
181
  * .catch(error => console.error(error));
183
182
  */
184
- export async function fetchCompletedContent(type = "all", brand, {page, limit} = {}) {
185
- let url;
186
- const limitString = limit ? `&limit=${limit}` : '';
187
- const pageString = page ? `&page=${page}` : '';
188
-
189
- if (type === "all") {
190
- url = `/content/completed/${globalConfig.railcontentConfig.userId}?brand=${brand}${limitString}${pageString}`;
183
+ export async function fetchCompletedContent(type = 'all', brand, { page, limit } = {}) {
184
+ let url
185
+ const limitString = limit ? `&limit=${limit}` : ''
186
+ const pageString = page ? `&page=${page}` : ''
187
+
188
+ if (type === 'all') {
189
+ url = `/content/completed/${globalConfig.railcontentConfig.userId}?brand=${brand}${limitString}${pageString}`
190
+ } else {
191
+ url = `/content/completed/${globalConfig.railcontentConfig.userId}?content_type=${type}&brand=${brand}${limitString}${pageString}`
192
+ }
193
+ const headers = {
194
+ 'Content-Type': 'application/json',
195
+ Accept: 'application/json',
196
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
197
+ }
198
+ try {
199
+ const response = await fetchAbsolute(url, { headers })
200
+ const result = await response.json()
201
+ if (result) {
202
+ //console.log('completed content', result);
203
+ return result
191
204
  } else {
192
- url = `/content/completed/${globalConfig.railcontentConfig.userId}?content_type=${type}&brand=${brand}${limitString}${pageString}`;
193
- }
194
- const headers = {
195
- 'Content-Type': 'application/json',
196
- 'Accept': 'application/json',
197
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
198
- };
199
- try {
200
- const response = await fetchAbsolute(url, {headers});
201
- const result = await response.json();
202
- if (result) {
203
- //console.log('completed content', result);
204
- return result;
205
- } else {
206
- console.log('result not json');
207
- }
208
- } catch (error) {
209
- console.error('Fetch error:', error);
210
- return null;
205
+ console.log('result not json')
211
206
  }
207
+ } catch (error) {
208
+ console.error('Fetch error:', error)
209
+ return null
210
+ }
212
211
  }
213
212
 
214
-
215
213
  /**
216
214
  * Fetches user context data for a specific piece of content.
217
215
  *
@@ -223,26 +221,26 @@ export async function fetchCompletedContent(type = "all", brand, {page, limit} =
223
221
  * .catch(error => console.error(error));
224
222
  */
225
223
  export async function fetchContentPageUserData(contentId) {
226
- let url = `/content/${contentId}/user_data/${globalConfig.railcontentConfig.userId}`;
227
- const headers = {
228
- 'Content-Type': 'application/json',
229
- 'Accept': 'application/json',
230
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
231
- };
232
-
233
- try {
234
- const response = await fetchAbsolute(url, {headers});
235
- const result = await response.json();
236
- if (result) {
237
- console.log('fetchContentPageUserData', result);
238
- return result;
239
- } else {
240
- console.log('result not json');
241
- }
242
- } catch (error) {
243
- console.error('Fetch error:', error);
244
- return null;
224
+ let url = `/content/${contentId}/user_data/${globalConfig.railcontentConfig.userId}`
225
+ const headers = {
226
+ 'Content-Type': 'application/json',
227
+ Accept: 'application/json',
228
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
229
+ }
230
+
231
+ try {
232
+ const response = await fetchAbsolute(url, { headers })
233
+ const result = await response.json()
234
+ if (result) {
235
+ console.log('fetchContentPageUserData', result)
236
+ return result
237
+ } else {
238
+ console.log('result not json')
245
239
  }
240
+ } catch (error) {
241
+ console.error('Fetch error:', error)
242
+ return null
243
+ }
246
244
  }
247
245
 
248
246
  /**
@@ -252,123 +250,130 @@ export async function fetchContentPageUserData(contentId) {
252
250
  * @returns {Promise<Object|null>} - Returns and Object with the id and type of the next piece of content if found, otherwise null.
253
251
  */
254
252
  export async function fetchNextContentDataForParent(contentId) {
255
- let url = `/content/${contentId}/next/${globalConfig.railcontentConfig.userId}`;
256
- const headers = {
257
- 'Content-Type': 'application/json',
258
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token
259
- };
260
-
261
- try {
262
- const response = await fetchAbsolute(url, {headers});
263
- const result = await response.json();
264
- if (result) {
265
- // console.log('fetchNextContentDataForParent', result);
266
- return result.next;
267
- } else {
268
- console.log('fetchNextContentDataForParent result not json');
269
- return null;
270
- }
271
- } catch (error) {
272
- console.error('Fetch error:', error);
273
- return null;
253
+ let url = `/content/${contentId}/next/${globalConfig.railcontentConfig.userId}`
254
+ const headers = {
255
+ 'Content-Type': 'application/json',
256
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
257
+ }
258
+
259
+ try {
260
+ const response = await fetchAbsolute(url, { headers })
261
+ const result = await response.json()
262
+ if (result) {
263
+ // console.log('fetchNextContentDataForParent', result);
264
+ return result.next
265
+ } else {
266
+ console.log('fetchNextContentDataForParent result not json')
267
+ return null
274
268
  }
269
+ } catch (error) {
270
+ console.error('Fetch error:', error)
271
+ return null
272
+ }
275
273
  }
276
274
 
277
-
278
275
  export async function fetchUserPermissionsData() {
279
- let url = `/content/user/permissions`;
280
- // in the case of an unauthorized user, we return empty permissions
281
- return await fetchHandler(url, 'get') ?? [];
276
+ let url = `/content/user/permissions`
277
+ // in the case of an unauthorized user, we return empty permissions
278
+ return (await fetchHandler(url, 'get')) ?? []
282
279
  }
283
280
 
284
- async function fetchDataHandler(url, dataVersion, method = "get") {
285
- return fetchHandler(url, method, dataVersion);
281
+ async function fetchDataHandler(url, dataVersion, method = 'get') {
282
+ return fetchHandler(url, method, dataVersion)
286
283
  }
287
284
 
288
285
  async function postDataHandler(url, data) {
289
- return fetchHandler(url, 'post', null, data);
286
+ return fetchHandler(url, 'post', null, data)
290
287
  }
291
288
 
292
289
  async function patchDataHandler(url, data) {
293
- return fetchHandler(url, 'patch', null, data);}
294
-
295
- export async function fetchHandler(url, method = "get", dataVersion = null, body = null) {
296
- let headers = {
297
- 'Content-Type': 'application/json',
298
- 'Accept': 'application/json',
299
- 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
300
- };
301
-
302
- if (!globalConfig.isMA) {
303
- const params = new URLSearchParams(window.location.search);
304
- if (params.get('testNow')) {
305
- headers['testNow'] = params.get('testNow');
306
- }
307
- if (params.get('timezone')) {
308
- headers['M-Client-Timezone'] = params.get('timezone');
309
- }
310
- }
311
-
312
- if (globalConfig.localTimezoneString) {
313
- headers['M-Client-Timezone'] = globalConfig.localTimezoneString;
314
- }
290
+ return fetchHandler(url, 'patch', null, data)
291
+ }
315
292
 
316
- if (globalConfig.railcontentConfig.authToken) {
317
- headers['Authorization'] = `Bearer ${globalConfig.railcontentConfig.authToken}`;
293
+ export async function fetchHandler(url, method = 'get', dataVersion = null, body = null) {
294
+ let headers = {
295
+ 'Content-Type': 'application/json',
296
+ Accept: 'application/json',
297
+ 'X-CSRF-TOKEN': globalConfig.railcontentConfig.token,
298
+ }
299
+
300
+ if (!globalConfig.isMA) {
301
+ const params = new URLSearchParams(window.location.search)
302
+ if (params.get('testNow')) {
303
+ headers['testNow'] = params.get('testNow')
318
304
  }
319
-
320
- if (dataVersion) headers['Data-Version'] = dataVersion;
321
- const options = {
322
- method,
323
- headers,
324
- };
325
- if (body) {
326
- options.body = JSON.stringify(body);
305
+ if (params.get('timezone')) {
306
+ headers['M-Client-Timezone'] = params.get('timezone')
327
307
  }
328
- try {
329
- const response = await fetchAbsolute(url, options);
330
- if (response.ok) {
331
- return await response.json();
332
- } else {
333
- console.error(`Fetch error: ${method} ${url} ${response.status} ${response.statusText}`);
334
- console.log(response);
335
- }
336
- } catch (error) {
337
- console.error('Fetch error:', error);
308
+ }
309
+
310
+ if (globalConfig.localTimezoneString) {
311
+ headers['M-Client-Timezone'] = globalConfig.localTimezoneString
312
+ }
313
+
314
+ if (globalConfig.railcontentConfig.authToken) {
315
+ headers['Authorization'] = `Bearer ${globalConfig.railcontentConfig.authToken}`
316
+ }
317
+
318
+ if (dataVersion) headers['Data-Version'] = dataVersion
319
+ const options = {
320
+ method,
321
+ headers,
322
+ }
323
+ if (body) {
324
+ options.body = JSON.stringify(body)
325
+ }
326
+ try {
327
+ const response = await fetchAbsolute(url, options)
328
+ if (response.ok) {
329
+ return await response.json()
330
+ } else {
331
+ console.error(`Fetch error: ${method} ${url} ${response.status} ${response.statusText}`)
332
+ console.log(response)
338
333
  }
339
- return null;
334
+ } catch (error) {
335
+ console.error('Fetch error:', error)
336
+ }
337
+ return null
340
338
  }
341
339
 
342
340
  export async function fetchUserLikes(currentVersion) {
343
- let url = `/content/user/likes/all`;
344
- return fetchDataHandler(url, currentVersion);
341
+ let url = `/content/user/likes/all`
342
+ return fetchDataHandler(url, currentVersion)
345
343
  }
346
344
 
347
345
  export async function postContentLiked(contentId) {
348
- let url = `/content/user/likes/like/${contentId}`;
349
- return await postDataHandler(url);
346
+ let url = `/content/user/likes/like/${contentId}`
347
+ return await postDataHandler(url)
350
348
  }
351
349
 
352
350
  export async function postContentUnliked(contentId) {
353
- let url = `/content/user/likes/unlike/${contentId}`;
354
- return await postDataHandler(url);
351
+ let url = `/content/user/likes/unlike/${contentId}`
352
+ return await postDataHandler(url)
355
353
  }
356
354
 
357
355
  export async function fetchContentProgress(currentVersion) {
358
- let url = `/content/user/progress/all`;
359
- return fetchDataHandler(url, currentVersion);
356
+ let url = `/content/user/progress/all`
357
+ return fetchDataHandler(url, currentVersion)
360
358
  }
361
359
 
362
- export async function postRecordWatchSession(contentId, mediaTypeId, mediaLengthSeconds, currentSeconds, secondsPlayed, sessionId) {
363
- let url = `/railtracker/v2/media-playback-session`;
364
- return postDataHandler(url, {
365
- "content_id": contentId,
366
- "media_type_id": mediaTypeId,
367
- "media_length_seconds": mediaLengthSeconds,
368
- "current_second": currentSeconds,
369
- "seconds_played": secondsPlayed,
370
- "session_id": sessionId
371
- });
360
+ export async function postRecordWatchSession(
361
+ contentId,
362
+ mediaTypeId,
363
+ mediaLengthSeconds,
364
+ currentSeconds,
365
+ secondsPlayed,
366
+ sessionId
367
+ ) {
368
+ let url = `/railtracker/v2/media-playback-session`
369
+ return postDataHandler(url, {
370
+ content_id: contentId,
371
+ media_type_id: mediaTypeId,
372
+ media_length_seconds: mediaLengthSeconds,
373
+ current_second: currentSeconds,
374
+ seconds_played: secondsPlayed,
375
+ session_id: sessionId,
376
+ })
372
377
  }
373
378
 
374
379
  /**
@@ -378,8 +383,8 @@ export async function postRecordWatchSession(contentId, mediaTypeId, mediaLength
378
383
  * @returns {Promise<any|null>}
379
384
  */
380
385
  export async function fetchChallengeMetadata(contentId) {
381
- let url = `/challenges/${contentId}`;
382
- return await fetchHandler(url, 'get');
386
+ let url = `/challenges/${contentId}`
387
+ return await fetchHandler(url, 'get')
383
388
  }
384
389
 
385
390
  /**
@@ -389,11 +394,10 @@ export async function fetchChallengeMetadata(contentId) {
389
394
  * @returns {Promise<any|null>}
390
395
  */
391
396
  export async function fetchChallengeLessonData(contentId) {
392
- let url = `/challenges/lessons/${contentId}`;
393
- return await fetchHandler(url, 'get');
397
+ let url = `/challenges/lessons/${contentId}`
398
+ return await fetchHandler(url, 'get')
394
399
  }
395
400
 
396
-
397
401
  /**
398
402
  * Fetch all owned brand challenges for user
399
403
  * @param {string|null} brand - brand
@@ -402,10 +406,10 @@ export async function fetchChallengeLessonData(contentId) {
402
406
  * @returns {Promise<any|null>}
403
407
  */
404
408
  export async function fetchOwnedChallenges(brand = null, page, limit) {
405
- let brandParam = brand ? `&brand=${brand}` : '';
406
- let pageAndLimit = `?page=${page}&limit=${limit}`;
407
- let url = `/challenges/tab_owned/get${pageAndLimit}${brandParam}`;
408
- return await fetchHandler(url, 'get');
409
+ let brandParam = brand ? `&brand=${brand}` : ''
410
+ let pageAndLimit = `?page=${page}&limit=${limit}`
411
+ let url = `/challenges/tab_owned/get${pageAndLimit}${brandParam}`
412
+ return await fetchHandler(url, 'get')
409
413
  }
410
414
 
411
415
  /**
@@ -416,13 +420,12 @@ export async function fetchOwnedChallenges(brand = null, page, limit) {
416
420
  * @returns {Promise<any|null>}
417
421
  */
418
422
  export async function fetchCompletedChallenges(brand = null, page, limit) {
419
- let brandParam = brand ? `&brand=${brand}` : '';
420
- let pageAndLimit = `?page=${page}&limit=${limit}`;
421
- let url = `/challenges/tab_completed/get${pageAndLimit}${brandParam}`;
422
- return await fetchHandler(url, 'get');
423
+ let brandParam = brand ? `&brand=${brand}` : ''
424
+ let pageAndLimit = `?page=${page}&limit=${limit}`
425
+ let url = `/challenges/tab_completed/get${pageAndLimit}${brandParam}`
426
+ return await fetchHandler(url, 'get')
423
427
  }
424
428
 
425
-
426
429
  /**
427
430
  * Fetch challenge, lesson, and user metadata for a given challenge
428
431
  *
@@ -430,8 +433,8 @@ export async function fetchCompletedChallenges(brand = null, page, limit) {
430
433
  * @returns {Promise<any|null>}
431
434
  */
432
435
  export async function fetchUserChallengeProgress(contentId) {
433
- let url = `/challenges/user_data/${contentId}`;
434
- return await fetchHandler(url, 'get');
436
+ let url = `/challenges/user_data/${contentId}`
437
+ return await fetchHandler(url, 'get')
435
438
  }
436
439
 
437
440
  /**
@@ -441,8 +444,8 @@ export async function fetchUserChallengeProgress(contentId) {
441
444
  * @returns {Promise<any|null>} - streamed PDF
442
445
  */
443
446
  export async function fetchUserAward(contentId) {
444
- let url = `/challenges/download_award/${contentId}`;
445
- return await fetchHandler(url, 'get');
447
+ let url = `/challenges/download_award/${contentId}`
448
+ return await fetchHandler(url, 'get')
446
449
  }
447
450
 
448
451
  /**
@@ -453,35 +456,34 @@ export async function fetchUserAward(contentId) {
453
456
  * @returns {Promise<any|null>}
454
457
  */
455
458
  export async function fetchChallengeIndexMetadata(contentIds) {
456
- if (!challengeIndexMetaDataPromise) {
457
- challengeIndexMetaDataPromise = getChallengeIndexMetadataPromise();
458
- }
459
- let results = await challengeIndexMetaDataPromise;
460
- if(Array.isArray(contentIds)){
461
- results = results.filter(function(challenge){
462
- return contentIds.includes(challenge.content_id);
463
- });
464
- }
465
- return results;
459
+ if (!challengeIndexMetaDataPromise) {
460
+ challengeIndexMetaDataPromise = getChallengeIndexMetadataPromise()
461
+ }
462
+ let results = await challengeIndexMetaDataPromise
463
+ if (Array.isArray(contentIds)) {
464
+ results = results.filter(function (challenge) {
465
+ return contentIds.includes(challenge.content_id)
466
+ })
467
+ }
468
+ return results
466
469
  }
467
470
 
468
471
  async function getChallengeIndexMetadataPromise() {
469
- let url = `/challenges/user_progress_for_index_page/get`;
470
- const result = await fetchHandler(url, 'get');
471
- challengeIndexMetaDataPromise = null;
472
- return result;
472
+ let url = `/challenges/user_progress_for_index_page/get`
473
+ const result = await fetchHandler(url, 'get')
474
+ challengeIndexMetaDataPromise = null
475
+ return result
473
476
  }
474
477
 
475
-
476
478
  /**
477
479
  * Get active brand challenges for the authorized user
478
480
  *
479
481
  * @returns {Promise<any|null>}
480
482
  */
481
483
  export async function fetchChallengeUserActiveChallenges(brand = null) {
482
- let brandParam = brand ? `?brand=${brand}` : '';
483
- let url = `/challenges/user_active_challenges/get${brandParam}`;
484
- return await fetchHandler(url, 'get');
484
+ let brandParam = brand ? `?brand=${brand}` : ''
485
+ let url = `/challenges/user_active_challenges/get${brandParam}`
486
+ return await fetchHandler(url, 'get')
485
487
  }
486
488
 
487
489
  /**
@@ -490,9 +492,9 @@ export async function fetchChallengeUserActiveChallenges(brand = null) {
490
492
  * @returns {Promise<any|null>}
491
493
  */
492
494
  export async function fetchCarouselCardData(brand = null) {
493
- const brandParam = brand ? `?brand=${brand}` : '';
494
- let url = `/api/v1/content/carousel${brandParam}`;
495
- return await fetchHandler(url, 'get');
495
+ const brandParam = brand ? `?brand=${brand}` : ''
496
+ let url = `/api/v1/content/carousel${brandParam}`
497
+ return await fetchHandler(url, 'get')
496
498
  }
497
499
 
498
500
  /**
@@ -502,9 +504,9 @@ export async function fetchCarouselCardData(brand = null) {
502
504
  * @returns {Promise<any|null>}
503
505
  */
504
506
  export async function fetchUserBadges(brand = null) {
505
- let brandParam = brand ? `?brand=${brand}` : '';
506
- let url = `/challenges/user_badges/get${brandParam}`;
507
- return await fetchHandler(url, 'get');
507
+ let brandParam = brand ? `?brand=${brand}` : ''
508
+ let url = `/challenges/user_badges/get${brandParam}`
509
+ return await fetchHandler(url, 'get')
508
510
  }
509
511
 
510
512
  /**
@@ -516,8 +518,8 @@ export async function fetchUserBadges(brand = null) {
516
518
  * @returns {Promise<any|null>}
517
519
  */
518
520
  export async function postChallengesSetStartDate(contentId, startDate) {
519
- let url = `/challenges/set_start_date/${contentId}?start_date=${startDate}`;
520
- return await fetchHandler(url, 'post');
521
+ let url = `/challenges/set_start_date/${contentId}?start_date=${startDate}`
522
+ return await fetchHandler(url, 'post')
521
523
  }
522
524
 
523
525
  /**
@@ -528,8 +530,8 @@ export async function postChallengesSetStartDate(contentId, startDate) {
528
530
  * @returns {Promise<any|null>}
529
531
  */
530
532
  export async function postChallengesUnlock(contentId) {
531
- let url = `/challenges/unlock/${contentId}`;
532
- return await fetchHandler(url, 'post');
533
+ let url = `/challenges/unlock/${contentId}`
534
+ return await fetchHandler(url, 'post')
533
535
  }
534
536
 
535
537
  /**
@@ -540,8 +542,8 @@ export async function postChallengesUnlock(contentId) {
540
542
  * @returns {Promise<any|null>}
541
543
  */
542
544
  export async function postChallengesEnroll(contentId) {
543
- let url = `/challenges/enroll/${contentId}`;
544
- return await fetchHandler(url, 'post');
545
+ let url = `/challenges/enroll/${contentId}`
546
+ return await fetchHandler(url, 'post')
545
547
  }
546
548
 
547
549
  /**
@@ -552,8 +554,8 @@ export async function postChallengesEnroll(contentId) {
552
554
  * @returns {Promise<any|null>}
553
555
  */
554
556
  export async function postChallengesLeave(contentId) {
555
- let url = `/challenges/leave/${contentId}`;
556
- return await fetchHandler(url, 'post');
557
+ let url = `/challenges/leave/${contentId}`
558
+ return await fetchHandler(url, 'post')
557
559
  }
558
560
 
559
561
  /**
@@ -563,8 +565,8 @@ export async function postChallengesLeave(contentId) {
563
565
  * @returns {Promise<any|null>}
564
566
  */
565
567
  export async function postChallengesEnrollmentNotification(contentId) {
566
- let url = `/challenges/notifications/enrollment_open/${contentId}`;
567
- return await fetchHandler(url, 'post');
568
+ let url = `/challenges/notifications/enrollment_open/${contentId}`
569
+ return await fetchHandler(url, 'post')
568
570
  }
569
571
 
570
572
  /**
@@ -574,8 +576,8 @@ export async function postChallengesEnrollmentNotification(contentId) {
574
576
  * @returns {Promise<any|null>}
575
577
  */
576
578
  export async function postChallengesCommunityNotification(contentId) {
577
- let url = `/challenges/notifications/community_reminders/${contentId}`;
578
- return await fetchHandler(url, 'post');
579
+ let url = `/challenges/notifications/community_reminders/${contentId}`
580
+ return await fetchHandler(url, 'post')
579
581
  }
580
582
 
581
583
  /**
@@ -585,8 +587,8 @@ export async function postChallengesCommunityNotification(contentId) {
585
587
  * @returns {Promise<any|null>}
586
588
  */
587
589
  export async function postChallengesSoloNotification(contentId) {
588
- let url = `/challenges/notifications/solo_reminders/${contentId}`;
589
- return await fetchHandler(url, 'post');
590
+ let url = `/challenges/notifications/solo_reminders/${contentId}`
591
+ return await fetchHandler(url, 'post')
590
592
  }
591
593
 
592
594
  /**
@@ -596,9 +598,9 @@ export async function postChallengesSoloNotification(contentId) {
596
598
  * @returns {Promise<any|null>} - Modal data to display
597
599
  */
598
600
  export async function postChallengesCompleteLesson(contentId) {
599
- let url = `/challenges/complete_lesson/${contentId}`;
600
- await contentStatusCompleted(contentId);
601
- return await fetchHandler(url, 'post');
601
+ let url = `/challenges/complete_lesson/${contentId}`
602
+ await contentStatusCompleted(contentId)
603
+ return await fetchHandler(url, 'post')
602
604
  }
603
605
 
604
606
  /**
@@ -608,8 +610,8 @@ export async function postChallengesCompleteLesson(contentId) {
608
610
  * @returns {Promise<any|null>}
609
611
  */
610
612
  export async function postChallengesHideCompletedBanner(contentId) {
611
- let url = `/challenges/hide_completed_banner/${contentId}`;
612
- return await fetchHandler(url, 'post');
613
+ let url = `/challenges/hide_completed_banner/${contentId}`
614
+ return await fetchHandler(url, 'post')
613
615
  }
614
616
 
615
617
  /**
@@ -631,18 +633,20 @@ export async function postChallengesHideCompletedBanner(contentId) {
631
633
  * .then(playlists => console.log(playlists))
632
634
  * .catch(error => console.error(error));
633
635
  */
634
- export async function fetchUserPlaylists(brand, {page, limit, sort, searchTerm, content_id, categories} = {}) {
635
- let url;
636
- const limitString = limit ? `&limit=${limit}` : '';
637
- const pageString = page ? `&page=${page}` : '';
638
- const sortString = sort ? `&sort=${sort}` : '';
639
- const searchFilter = searchTerm ? `&term=${searchTerm}` : '';
640
- const content = content_id ? `&content_id=${content_id}` : '';
641
- const categoryString = categories && categories.length
642
- ? categories.map(cat => `categories[]=${cat}`).join('&')
643
- : '';
644
- url = `/playlists/all?brand=${brand}${limitString}${pageString}${sortString}${searchFilter}${content}${categoryString ? `&${categoryString}` : ''}`;
645
- return await fetchHandler(url);
636
+ export async function fetchUserPlaylists(
637
+ brand,
638
+ { page, limit, sort, searchTerm, content_id, categories } = {}
639
+ ) {
640
+ let url
641
+ const limitString = limit ? `&limit=${limit}` : ''
642
+ const pageString = page ? `&page=${page}` : ''
643
+ const sortString = sort ? `&sort=${sort}` : ''
644
+ const searchFilter = searchTerm ? `&term=${searchTerm}` : ''
645
+ const content = content_id ? `&content_id=${content_id}` : ''
646
+ const categoryString =
647
+ categories && categories.length ? categories.map((cat) => `categories[]=${cat}`).join('&') : ''
648
+ url = `/playlists/all?brand=${brand}${limitString}${pageString}${sortString}${searchFilter}${content}${categoryString ? `&${categoryString}` : ''}`
649
+ return await fetchHandler(url)
646
650
  }
647
651
 
648
652
  /**
@@ -674,8 +678,8 @@ export async function fetchUserPlaylists(brand, {page, limit, sort, searchTerm,
674
678
  * .catch(error => console.error('Error duplicating playlist:', error));
675
679
  */
676
680
  export async function duplicatePlaylist(playlistId, playlistData) {
677
- let url = `/playlists/duplicate/${playlistId}`;
678
- return await fetchHandler(url, "post", null, playlistData);
681
+ let url = `/playlists/duplicate/${playlistId}`
682
+ return await fetchHandler(url, 'post', null, playlistData)
679
683
  }
680
684
 
681
685
  /**
@@ -702,8 +706,8 @@ export async function duplicatePlaylist(playlistId, playlistData) {
702
706
  * .catch(error => console.error('Error deleting playlist:', error));
703
707
  */
704
708
  export async function deletePlaylist(playlistId) {
705
- let url = `/playlists/playlist/${playlistId}`;
706
- return await fetchHandler(url, "delete");
709
+ let url = `/playlists/playlist/${playlistId}`
710
+ return await fetchHandler(url, 'delete')
707
711
  }
708
712
 
709
713
  /**
@@ -737,8 +741,8 @@ export async function deletePlaylist(playlistId) {
737
741
  * .catch(error => console.error('Error updating playlist:', error));
738
742
  */
739
743
  export async function updatePlaylist(playlistId, updatedData) {
740
- const url = `/playlists/playlist/${playlistId}`;
741
- return await fetchHandler(url, "PUT", null, updatedData);
744
+ const url = `/playlists/playlist/${playlistId}`
745
+ return await fetchHandler(url, 'PUT', null, updatedData)
742
746
  }
743
747
 
744
748
  /**
@@ -767,8 +771,8 @@ export async function updatePlaylist(playlistId, updatedData) {
767
771
  * .catch(error => console.error('Error creating playlist:', error));
768
772
  */
769
773
  export async function createPlaylist(playlistData) {
770
- const url = `/playlists/playlist`;
771
- return await fetchHandler(url, "POST", null, playlistData);
774
+ const url = `/playlists/playlist`
775
+ return await fetchHandler(url, 'POST', null, playlistData)
772
776
  }
773
777
 
774
778
  /**
@@ -800,9 +804,9 @@ export async function createPlaylist(playlistData) {
800
804
  * .catch(error => console.error('Error liking playlist:', error));
801
805
  */
802
806
  export async function likePlaylist(playlistId) {
803
- const url = `/playlists/like`;
804
- const payload = {playlist_id: playlistId};
805
- return await fetchHandler(url, "PUT", null, payload);
807
+ const url = `/playlists/like`
808
+ const payload = { playlist_id: playlistId }
809
+ return await fetchHandler(url, 'PUT', null, payload)
806
810
  }
807
811
 
808
812
  /**
@@ -827,9 +831,9 @@ export async function likePlaylist(playlistId) {
827
831
  * .catch(error => console.error('Error removing playlist like:', error));
828
832
  */
829
833
  export async function deletePlaylistLike(playlistId) {
830
- const url = `/playlists/like`;
831
- const payload = {playlist_id: playlistId};
832
- return await fetchHandler(url, "DELETE", null, payload);
834
+ const url = `/playlists/like`
835
+ const payload = { playlist_id: playlistId }
836
+ return await fetchHandler(url, 'DELETE', null, payload)
833
837
  }
834
838
 
835
839
  /**
@@ -849,8 +853,8 @@ export async function deletePlaylistLike(playlistId) {
849
853
  * .catch(error => console.error('Error fetching playlist:', error));
850
854
  */
851
855
  export async function fetchPlaylist(playlistId) {
852
- const url = `/playlists/playlist/${playlistId}`;
853
- return await fetchHandler(url, "GET");
856
+ const url = `/playlists/playlist/${playlistId}`
857
+ return await fetchHandler(url, 'GET')
854
858
  }
855
859
 
856
860
  /**
@@ -868,10 +872,10 @@ export async function fetchPlaylist(playlistId) {
868
872
  * .then(items => console.log(items))
869
873
  * .catch(error => console.error('Error fetching playlist items:', error));
870
874
  */
871
- export async function fetchPlaylistItems(playlistId, {sort} = {}) {
872
- const sortString = sort ? `&sort=${sort}` : '';
873
- const url = `/playlists/playlist-lessons?playlist_id=${playlistId}${sortString}`;
874
- return await fetchHandler(url, "GET");
875
+ export async function fetchPlaylistItems(playlistId, { sort } = {}) {
876
+ const sortString = sort ? `&sort=${sort}` : ''
877
+ const url = `/playlists/playlist-lessons?playlist_id=${playlistId}${sortString}`
878
+ return await fetchHandler(url, 'GET')
875
879
  }
876
880
 
877
881
  /**
@@ -910,8 +914,8 @@ export async function fetchPlaylistItems(playlistId, {sort} = {}) {
910
914
  * });
911
915
  */
912
916
  export async function updatePlaylistItem(updatedData) {
913
- const url = `/playlists/item`;
914
- return await fetchHandler(url, "POST", null, updatedData);
917
+ const url = `/playlists/item`
918
+ return await fetchHandler(url, 'POST', null, updatedData)
915
919
  }
916
920
 
917
921
  /**
@@ -945,8 +949,8 @@ export async function updatePlaylistItem(updatedData) {
945
949
  * });
946
950
  */
947
951
  export async function deletePlaylistItem(payload) {
948
- const url = `/playlists/item`;
949
- return await fetchHandler(url, "DELETE", null, payload);
952
+ const url = `/playlists/item`
953
+ return await fetchHandler(url, 'DELETE', null, payload)
950
954
  }
951
955
 
952
956
  /**
@@ -977,19 +981,19 @@ export async function deletePlaylistItem(payload) {
977
981
  * });
978
982
  */
979
983
  export async function fetchPlaylistItem(payload) {
980
- const playlistItemId = payload.user_playlist_item_id;
981
- const url = `/playlists/item/${playlistItemId}`;
982
- return await fetchHandler(url);
984
+ const playlistItemId = payload.user_playlist_item_id
985
+ const url = `/playlists/item/${playlistItemId}`
986
+ return await fetchHandler(url)
983
987
  }
984
988
 
985
989
  export async function postContentCompleted(contentId) {
986
- let url = `/content/user/progress/complete`;
987
- return postDataHandler(url, {"contentId": contentId});
990
+ let url = `/content/user/progress/complete`
991
+ return postDataHandler(url, { contentId: contentId })
988
992
  }
989
993
 
990
994
  export async function postContentReset(contentId) {
991
- let url = `/content/user/progress/reset`;
992
- return postDataHandler(url, {"contentId": contentId});
995
+ let url = `/content/user/progress/reset`
996
+ return postDataHandler(url, { contentId: contentId })
993
997
  }
994
998
 
995
999
  /**
@@ -1033,8 +1037,8 @@ export async function postContentReset(contentId) {
1033
1037
  * });
1034
1038
  */
1035
1039
  export async function addItemToPlaylist(payload) {
1036
- const url = `/playlists/add-item`;
1037
- return await fetchHandler(url, "POST", null, payload);
1040
+ const url = `/playlists/add-item`
1041
+ return await fetchHandler(url, 'POST', null, payload)
1038
1042
  }
1039
1043
 
1040
1044
  /**
@@ -1063,8 +1067,8 @@ export async function addItemToPlaylist(payload) {
1063
1067
  * });
1064
1068
  */
1065
1069
  export async function countAssignmentsAndLessons(contentId) {
1066
- const url = `/playlists/count-lessons-and-assignments/${contentId}`;
1067
- return await fetchHandler(url);
1070
+ const url = `/playlists/count-lessons-and-assignments/${contentId}`
1071
+ return await fetchHandler(url)
1068
1072
  }
1069
1073
 
1070
1074
  /**
@@ -1095,8 +1099,8 @@ export async function countAssignmentsAndLessons(contentId) {
1095
1099
  * });
1096
1100
  */
1097
1101
  export async function pinPlaylist(playlistId) {
1098
- const url = `/playlists/pin/${playlistId}`;
1099
- return await fetchHandler(url, "PUT");
1102
+ const url = `/playlists/pin/${playlistId}`
1103
+ return await fetchHandler(url, 'PUT')
1100
1104
  }
1101
1105
 
1102
1106
  /**
@@ -1127,8 +1131,8 @@ export async function pinPlaylist(playlistId) {
1127
1131
  * });
1128
1132
  */
1129
1133
  export async function unpinPlaylist(playlistId) {
1130
- const url = `/playlists/unpin/${playlistId}`;
1131
- return await fetchHandler(url, "PUT");
1134
+ const url = `/playlists/unpin/${playlistId}`
1135
+ return await fetchHandler(url, 'PUT')
1132
1136
  }
1133
1137
 
1134
1138
  /**
@@ -1159,26 +1163,26 @@ export async function unpinPlaylist(playlistId) {
1159
1163
  * });
1160
1164
  */
1161
1165
  export async function fetchPinnedPlaylists(brand) {
1162
- const url = `/playlists/my-pinned-playlists?brand=${brand}`;
1163
- return await fetchHandler(url, "GET");
1166
+ const url = `/playlists/my-pinned-playlists?brand=${brand}`
1167
+ return await fetchHandler(url, 'GET')
1164
1168
  }
1165
1169
 
1166
1170
  /**
1167
1171
  * Report playlist endpoint
1168
- *
1172
+ *
1169
1173
  * @param playlistId
1170
1174
  * @param issue
1171
1175
  * @returns {Promise<any|null>}
1172
1176
  */
1173
- export async function reportPlaylist(playlistId, {issue} = {}) {
1174
- const issueString = issue ? `?issue=${issue}` : '';
1175
- const url = `/playlists/report/${playlistId}${issueString}`;
1176
- return await fetchHandler(url, "PUT");
1177
+ export async function reportPlaylist(playlistId, { issue } = {}) {
1178
+ const issueString = issue ? `?issue=${issue}` : ''
1179
+ const url = `/playlists/report/${playlistId}${issueString}`
1180
+ return await fetchHandler(url, 'PUT')
1177
1181
  }
1178
1182
 
1179
1183
  export async function playback(playlistId) {
1180
- const url = `/playlists/play/${playlistId}`;
1181
- return await fetchHandler(url, "GET");
1184
+ const url = `/playlists/play/${playlistId}`
1185
+ return await fetchHandler(url, 'GET')
1182
1186
  }
1183
1187
 
1184
1188
  /**
@@ -1189,20 +1193,20 @@ export async function playback(playlistId) {
1189
1193
  * @returns {Promise<any|null>}
1190
1194
  */
1191
1195
  export async function setStudentViewForUser(userId, enable) {
1192
- let url = `/user-management-system/user/update/${userId}`;
1193
- let data = {'use_student_view' : enable ? 1 : 0};
1194
- return await patchDataHandler(url, data);
1196
+ let url = `/user-management-system/user/update/${userId}`
1197
+ let data = { use_student_view: enable ? 1 : 0 }
1198
+ return await patchDataHandler(url, data)
1195
1199
  }
1196
1200
 
1197
1201
  function fetchAbsolute(url, params) {
1198
- if (globalConfig.railcontentConfig.authToken) {
1199
- params.headers['Authorization'] = `Bearer ${globalConfig.railcontentConfig.authToken}`;
1200
- }
1202
+ if (globalConfig.railcontentConfig.authToken) {
1203
+ params.headers['Authorization'] = `Bearer ${globalConfig.railcontentConfig.authToken}`
1204
+ }
1201
1205
 
1202
- if (globalConfig.railcontentConfig.baseUrl) {
1203
- if (url.startsWith('/')) {
1204
- return fetch(globalConfig.railcontentConfig.baseUrl + url, params)
1205
- }
1206
+ if (globalConfig.railcontentConfig.baseUrl) {
1207
+ if (url.startsWith('/')) {
1208
+ return fetch(globalConfig.railcontentConfig.baseUrl + url, params)
1206
1209
  }
1207
- return fetch(url, params);
1210
+ }
1211
+ return fetch(url, params)
1208
1212
  }