musora-content-services 1.0.7 → 1.0.10
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/CHANGELOG.md +4 -0
- package/README.md +8 -1
- package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
- package/docs/global.html +1881 -1588
- package/docs/index.html +33 -10
- package/docs/index.js.html +304 -99
- package/docs/scripts/collapse.js +39 -0
- package/docs/scripts/commonNav.js +28 -0
- package/docs/scripts/nav.js +12 -0
- package/docs/scripts/polyfill.js +4 -0
- package/docs/scripts/search.js +99 -265
- package/docs/styles/jsdoc.css +776 -0
- package/docs/styles/prettify.css +80 -0
- package/jest.config.js +198 -0
- package/jsdoc.json +1 -1
- package/package.json +8 -5
- package/src/index.js +190 -49
- package/test/sanityQueryService.test.js +88 -0
package/src/index.js
CHANGED
|
@@ -10,7 +10,7 @@ let globalConfig = {};
|
|
|
10
10
|
* @param {string} config.dataset - The dataset name in Sanity.
|
|
11
11
|
* @param {string} config.version - The API version to use.
|
|
12
12
|
* @param {boolean} [config.debug=false] - Optional flag to enable debug mode, which logs the query and results.
|
|
13
|
-
*
|
|
13
|
+
* @param {boolean} [config.useCachedAPI=true] - Optional flag to disable cached API. *
|
|
14
14
|
* @example
|
|
15
15
|
* // Initialize the Sanity service in your app.js
|
|
16
16
|
* initializeSanityService({
|
|
@@ -19,6 +19,7 @@ let globalConfig = {};
|
|
|
19
19
|
* dataset: 'your-dataset-name',
|
|
20
20
|
* version: '2021-06-07',
|
|
21
21
|
* debug: true // Optional: Enable debug mode
|
|
22
|
+
* useCachedAPI: true // Optional: Use cached API
|
|
22
23
|
* });
|
|
23
24
|
*/
|
|
24
25
|
function initializeSanityService(config) {
|
|
@@ -39,6 +40,7 @@ async function fetchSongById(documentId) {
|
|
|
39
40
|
'album',
|
|
40
41
|
'instrumentless',
|
|
41
42
|
'soundslice',
|
|
43
|
+
'railcontent_id',
|
|
42
44
|
'"resources": resource[]{resource_url, resource_name}',
|
|
43
45
|
];
|
|
44
46
|
|
|
@@ -46,7 +48,7 @@ async function fetchSongById(documentId) {
|
|
|
46
48
|
*[_type == "song" && railcontent_id == ${documentId}]{
|
|
47
49
|
${fields.join(', ')}
|
|
48
50
|
}`;
|
|
49
|
-
return fetchSanity(query);
|
|
51
|
+
return fetchSanity(query, false);
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
/**
|
|
@@ -63,6 +65,16 @@ async function fetchArtists(brand) {
|
|
|
63
65
|
return fetchSanity(query, true);
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Fetch current number of artists for songs within a brand.
|
|
70
|
+
* @param {string} brand - The current brand.
|
|
71
|
+
* @returns {Promise<int|null>} - The fetched count of artists.
|
|
72
|
+
*/
|
|
73
|
+
async function fetchSongArtistCount(brand) {
|
|
74
|
+
const query = `count(*[_type == 'artist']{'lessonsCount': count(*[_type == 'song' && brand == '${brand}' && references(^._id)]._id)}[lessonsCount > 0])`;
|
|
75
|
+
return fetchSanity(query, false);
|
|
76
|
+
}
|
|
77
|
+
|
|
66
78
|
/**
|
|
67
79
|
* Fetch related songs for a specific brand and song ID.
|
|
68
80
|
* @param {string} brand - The brand for which to fetch related songs.
|
|
@@ -134,7 +146,7 @@ async function fetchRelatedSongs(brand, songId) {
|
|
|
134
146
|
])[0...10]
|
|
135
147
|
}`;
|
|
136
148
|
|
|
137
|
-
return fetchSanity(query);
|
|
149
|
+
return fetchSanity(query, true);
|
|
138
150
|
}
|
|
139
151
|
|
|
140
152
|
/**
|
|
@@ -149,7 +161,14 @@ async function fetchRelatedSongs(brand, songId) {
|
|
|
149
161
|
* @param {string} [params.groupBy=""] - The field to group the results by.
|
|
150
162
|
* @returns {Promise<Object|null>} - The fetched song data or null if not found.
|
|
151
163
|
*/
|
|
152
|
-
async function fetchAllSongs(brand, {
|
|
164
|
+
async function fetchAllSongs(brand, {
|
|
165
|
+
page = 1,
|
|
166
|
+
limit = 10,
|
|
167
|
+
searchTerm = "",
|
|
168
|
+
sort = "-published_on",
|
|
169
|
+
includedFields = [],
|
|
170
|
+
groupBy = ""
|
|
171
|
+
}) {
|
|
153
172
|
console.log('groupBy', groupBy)
|
|
154
173
|
const start = (page - 1) * limit;
|
|
155
174
|
const end = start + limit;
|
|
@@ -267,7 +286,7 @@ async function fetchAllSongs(brand, { page = 1, limit = 10, searchTerm = "", sor
|
|
|
267
286
|
`;
|
|
268
287
|
}
|
|
269
288
|
|
|
270
|
-
return fetchSanity(query);
|
|
289
|
+
return fetchSanity(query, false);
|
|
271
290
|
}
|
|
272
291
|
|
|
273
292
|
/**
|
|
@@ -275,7 +294,7 @@ async function fetchAllSongs(brand, { page = 1, limit = 10, searchTerm = "", sor
|
|
|
275
294
|
* @param {string} brand - The brand for which to fetch filter options.
|
|
276
295
|
* @returns {Promise<Object|null>} - The fetched filter options or null if not found.
|
|
277
296
|
*/
|
|
278
|
-
async function
|
|
297
|
+
async function fetchSongFilterOptions(brand) {
|
|
279
298
|
const query = `
|
|
280
299
|
{
|
|
281
300
|
"difficulty": [
|
|
@@ -296,7 +315,7 @@ async function fetchFilterOptions(brand) {
|
|
|
296
315
|
}
|
|
297
316
|
`;
|
|
298
317
|
|
|
299
|
-
return fetchSanity(query);
|
|
318
|
+
return fetchSanity(query, false);
|
|
300
319
|
}
|
|
301
320
|
|
|
302
321
|
/**
|
|
@@ -306,7 +325,7 @@ async function fetchFilterOptions(brand) {
|
|
|
306
325
|
*/
|
|
307
326
|
async function fetchSongCount(brand) {
|
|
308
327
|
const query = `count(*[_type == 'song' && brand == "${brand}"])`;
|
|
309
|
-
return fetchSanity(query);
|
|
328
|
+
return fetchSanity(query, false);
|
|
310
329
|
}
|
|
311
330
|
|
|
312
331
|
/**
|
|
@@ -326,7 +345,7 @@ async function fetchWorkouts(brand) {
|
|
|
326
345
|
web_url_path,
|
|
327
346
|
published_on
|
|
328
347
|
} | order(published_on desc)[0...5]`
|
|
329
|
-
return fetchSanity(query);
|
|
348
|
+
return fetchSanity(query, true);
|
|
330
349
|
}
|
|
331
350
|
|
|
332
351
|
/**
|
|
@@ -354,7 +373,7 @@ async function fetchNewReleases(brand) {
|
|
|
354
373
|
web_url_path,
|
|
355
374
|
published_on
|
|
356
375
|
} | order(published_on desc)[0...5]`
|
|
357
|
-
return fetchSanity(query);
|
|
376
|
+
return fetchSanity(query, true);
|
|
358
377
|
}
|
|
359
378
|
|
|
360
379
|
/**
|
|
@@ -384,7 +403,7 @@ async function fetchUpcomingEvents(brand) {
|
|
|
384
403
|
web_url_path,
|
|
385
404
|
published_on
|
|
386
405
|
} | order(published_on asc)[0...5]`;
|
|
387
|
-
return fetchSanity(query);
|
|
406
|
+
return fetchSanity(query, true);
|
|
388
407
|
}
|
|
389
408
|
|
|
390
409
|
/**
|
|
@@ -393,7 +412,7 @@ async function fetchUpcomingEvents(brand) {
|
|
|
393
412
|
* @returns {Promise<Object|null>} - The fetched content data or null if not found.
|
|
394
413
|
*/
|
|
395
414
|
async function fetchByRailContentId(id) {
|
|
396
|
-
const query = `*[railcontent_id
|
|
415
|
+
const query = `*[railcontent_id == ${id}]{
|
|
397
416
|
railcontent_id,
|
|
398
417
|
title,
|
|
399
418
|
"image": thumbnail.asset->url,
|
|
@@ -404,7 +423,7 @@ async function fetchByRailContentId(id) {
|
|
|
404
423
|
web_url_path,
|
|
405
424
|
published_on
|
|
406
425
|
}`
|
|
407
|
-
return fetchSanity(query);
|
|
426
|
+
return fetchSanity(query, false);
|
|
408
427
|
}
|
|
409
428
|
|
|
410
429
|
/**
|
|
@@ -425,7 +444,7 @@ async function fetchByRailContentIds(ids) {
|
|
|
425
444
|
web_url_path,
|
|
426
445
|
published_on
|
|
427
446
|
}`
|
|
428
|
-
return fetchSanity(query);
|
|
447
|
+
return fetchSanity(query, true);
|
|
429
448
|
}
|
|
430
449
|
|
|
431
450
|
/**
|
|
@@ -441,7 +460,14 @@ async function fetchByRailContentIds(ids) {
|
|
|
441
460
|
* @param {string} [params.groupBy=""] - The field to group the results by (e.g., 'artist', 'genre').
|
|
442
461
|
* @returns {Promise<Object|null>} - The fetched content data or null if not found.
|
|
443
462
|
*/
|
|
444
|
-
async function fetchAll(brand, type, {
|
|
463
|
+
async function fetchAll(brand, type, {
|
|
464
|
+
page = 1,
|
|
465
|
+
limit = 10,
|
|
466
|
+
searchTerm = "",
|
|
467
|
+
sort = "-published_on",
|
|
468
|
+
includedFields = [],
|
|
469
|
+
groupBy = ""
|
|
470
|
+
}) {
|
|
445
471
|
const start = (page - 1) * limit;
|
|
446
472
|
const end = start + limit;
|
|
447
473
|
|
|
@@ -486,7 +512,8 @@ async function fetchAll(brand, type, { page = 1, limit = 10, searchTerm = "", so
|
|
|
486
512
|
|
|
487
513
|
// Determine the group by clause
|
|
488
514
|
let query = "";
|
|
489
|
-
|
|
515
|
+
let manyReference = true; //TODO: define whether reference is one to one or one to many
|
|
516
|
+
if (groupBy !== "" && !manyReference) {
|
|
490
517
|
query = `
|
|
491
518
|
{
|
|
492
519
|
"total": count(*[_type == '${groupBy}' && count(*[_type == '${type}' && brand == '${brand}' && ^._id == ${groupBy}._ref ]._id) > 0]),
|
|
@@ -511,6 +538,31 @@ async function fetchAll(brand, type, { page = 1, limit = 10, searchTerm = "", so
|
|
|
511
538
|
|order(${sortOrder})
|
|
512
539
|
[${start}...${end}]
|
|
513
540
|
}`;
|
|
541
|
+
} else if (groupBy !== "" && manyReference) {
|
|
542
|
+
query = `
|
|
543
|
+
{
|
|
544
|
+
"total": count(*[_type == '${groupBy}' && count(*[_type == '${type}' && brand == '${brand}' && ^._id in ${groupBy}[]._ref]._id)>0]),
|
|
545
|
+
"entity": *[_type == '${groupBy}' && count(*[_type == '${type}' && brand == '${brand}' && ^._id in ${groupBy}[]._ref]._id) > 0]
|
|
546
|
+
{
|
|
547
|
+
'id': _id,
|
|
548
|
+
'type': _type,
|
|
549
|
+
name,
|
|
550
|
+
'head_shot_picture_url': thumbnail_url.asset->url,
|
|
551
|
+
'all_lessons_count': count(*[_type == '${type}' && brand == '${brand}' && ^._id in ${groupBy}[]._ref ]._id),
|
|
552
|
+
'lessons': *[_type == '${type}' && brand == '${brand}' && ^._id in ${groupBy}[]._ref ]{
|
|
553
|
+
railcontent_id,
|
|
554
|
+
title,
|
|
555
|
+
"image": thumbnail.asset->url,
|
|
556
|
+
difficulty,
|
|
557
|
+
difficulty_string,
|
|
558
|
+
web_url_path,
|
|
559
|
+
published_on,
|
|
560
|
+
${groupBy}
|
|
561
|
+
}[0...10]
|
|
562
|
+
}
|
|
563
|
+
|order(${sortOrder})
|
|
564
|
+
[${start}...${end}]
|
|
565
|
+
}`;
|
|
514
566
|
} else {
|
|
515
567
|
query = `
|
|
516
568
|
{
|
|
@@ -528,7 +580,65 @@ async function fetchAll(brand, type, { page = 1, limit = 10, searchTerm = "", so
|
|
|
528
580
|
`;
|
|
529
581
|
}
|
|
530
582
|
|
|
531
|
-
return fetchSanity(query);
|
|
583
|
+
return fetchSanity(query, false);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Fetches all available filter options based on various criteria such as brand, filters, style, artist, content type, and search term.
|
|
588
|
+
*
|
|
589
|
+
* This function constructs a query to retrieve the total number of results and filter options such as difficulty, instrument type, and genre.
|
|
590
|
+
* The filter options are dynamically generated based on the provided filters, style, artist, and content type.
|
|
591
|
+
*
|
|
592
|
+
* @param {string} brand - The brand for which to fetch the filter options.
|
|
593
|
+
* @param {string} filters - Additional filters to apply to the query, typically in the format of Sanity GROQ queries.
|
|
594
|
+
* @param {string} [style] - Optional style or genre to filter the results. If provided, the query will check if the style exists in the genre array.
|
|
595
|
+
* @param {string} [artist] - Optional artist name to filter the results. If provided, the query will check if the artist's name matches.
|
|
596
|
+
* @param {string} contentType - The content type to fetch (e.g., 'song', 'lesson').
|
|
597
|
+
* @param {string} [term] - Optional search term to match against various fields such as title, album, artist name, and genre.
|
|
598
|
+
*
|
|
599
|
+
* @returns {Promise<Object|null>} - A promise that resolves to an object containing the total results and filter options, or null if the query fails.
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* // Example usage:
|
|
603
|
+
* fetchAllFilterOptions('myBrand', '', 'Rock', 'John Doe', 'song', 'Love')
|
|
604
|
+
* .then(options => console.log(options))
|
|
605
|
+
* .catch(error => console.error(error));
|
|
606
|
+
*/
|
|
607
|
+
async function fetchAllFilterOptions(
|
|
608
|
+
brand,
|
|
609
|
+
filters,
|
|
610
|
+
style,
|
|
611
|
+
artist,
|
|
612
|
+
contentType,
|
|
613
|
+
term
|
|
614
|
+
){
|
|
615
|
+
const query = `
|
|
616
|
+
{
|
|
617
|
+
"meta": {
|
|
618
|
+
"totalResults": count(*[_type == '${contentType}' && brand == "${brand}" && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} ${filters}
|
|
619
|
+
${term ? `&& (title match "${term}" || album match "${term}" || artist->name match "${term}" || genre[]->name match "${term}")` : ''}]),
|
|
620
|
+
"filterOptions": {
|
|
621
|
+
"difficulty": [
|
|
622
|
+
{"type": "Introductory", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && difficulty_string == "Introductory" ${filters}])},
|
|
623
|
+
{"type": "Beginner", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && difficulty_string == "Beginner" ${filters}])},
|
|
624
|
+
{"type": "Intermediate", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && difficulty_string == "Intermediate" ${filters}])},
|
|
625
|
+
{"type": "Advanced", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && difficulty_string == "Advanced" ${filters}])},
|
|
626
|
+
{"type": "Expert", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && difficulty_string == "Expert" ${filters}])}
|
|
627
|
+
][count > 0],
|
|
628
|
+
"instrumentless": [
|
|
629
|
+
{"type": "Full Song Only", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && instrumentless == false ${filters}])},
|
|
630
|
+
{"type": "Instrument Removed", "count": count(*[_type == '${contentType}' && brand == '${brand}' && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && instrumentless == true ${filters}])}
|
|
631
|
+
][count > 0],
|
|
632
|
+
"genre": *[_type == 'genre' && '${contentType}' in filter_types] {
|
|
633
|
+
"type": name,
|
|
634
|
+
"count": count(*[_type == '${contentType}' && brand == "${brand}" && ${style ? `'${style}' in genre[]->name` : `artist->name == '${artist}'`} && references(^._id)])
|
|
635
|
+
}[count > 0]
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
`;
|
|
640
|
+
|
|
641
|
+
return fetchSanity(query, false);
|
|
532
642
|
}
|
|
533
643
|
|
|
534
644
|
/**
|
|
@@ -549,7 +659,7 @@ async function fetchChildren(railcontentId) {
|
|
|
549
659
|
web_url_path,
|
|
550
660
|
published_on
|
|
551
661
|
} | order(published_on asc)`
|
|
552
|
-
return fetchSanity(query);
|
|
662
|
+
return fetchSanity(query, true);
|
|
553
663
|
}
|
|
554
664
|
|
|
555
665
|
/**
|
|
@@ -570,7 +680,7 @@ async function fetchMethodNextLesson(railcontentId) {
|
|
|
570
680
|
web_url_path,
|
|
571
681
|
published_on
|
|
572
682
|
}`
|
|
573
|
-
return fetchSanity(query);
|
|
683
|
+
return fetchSanity(query, false);
|
|
574
684
|
}
|
|
575
685
|
|
|
576
686
|
/**
|
|
@@ -601,35 +711,61 @@ async function fetchNextPreviousLesson(railcontentId) {
|
|
|
601
711
|
web_url_path,
|
|
602
712
|
published_on
|
|
603
713
|
}`
|
|
604
|
-
return fetchSanity(query);
|
|
714
|
+
return fetchSanity(query, false);
|
|
605
715
|
}
|
|
606
716
|
|
|
607
717
|
/**
|
|
608
|
-
* Fetch
|
|
609
|
-
* @param {string}
|
|
610
|
-
* @
|
|
718
|
+
* Fetch the page data for a specific lesson by Railcontent ID.
|
|
719
|
+
* @param {string} railContentId - The Railcontent ID of the current lesson.
|
|
720
|
+
* @returns {Promise<Object|null>} - The fetched page data or null if found.
|
|
721
|
+
*/
|
|
722
|
+
async function fetchLessonContent(railContentId) {
|
|
723
|
+
const query = `*[railcontent_id == ${railContentId} ]
|
|
724
|
+
{title, published_on,"type":_type, "resources": resource, difficulty, difficulty_string, brand, soundslice, instrumentless, railcontent_id, "id":railcontent_id, slug, artist->,"thumbnail_url":thumbnail.asset->url, "url": web_url_path, soundslice_slug,description,
|
|
725
|
+
"chapters": chapter[]{
|
|
726
|
+
chapter_description,
|
|
727
|
+
chapter_timecode,
|
|
728
|
+
"chapter_thumbnail_url": chapter_thumbnail_url.asset->url
|
|
729
|
+
},
|
|
730
|
+
"coaches": instructor[]-> {
|
|
731
|
+
name,
|
|
732
|
+
"id":_id,
|
|
733
|
+
"coach_profile_image":thumbnail_url.asset->url
|
|
734
|
+
},
|
|
735
|
+
"instructors":instructor[]->name,
|
|
736
|
+
instructor[]->,
|
|
737
|
+
"assignments":assignment[]{
|
|
738
|
+
"id": railcontent_id,
|
|
739
|
+
"soundslice_slug": assignment_soundslice,
|
|
740
|
+
"title": assignment_title,
|
|
741
|
+
"sheet_music_image_url": assignment_sheet_music_image,
|
|
742
|
+
"timecode": assignment_timecode,
|
|
743
|
+
"description": assignment_description
|
|
744
|
+
},
|
|
745
|
+
video}`
|
|
746
|
+
return fetchSanity(query, false);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Fetch related lessons for a specific lesson by RailContent ID and type.
|
|
751
|
+
* @param {string} railContentId - The RailContent ID of the current lesson.
|
|
752
|
+
* @param {string} brand - The current brand.
|
|
611
753
|
* @returns {Promise<Array<Object>|null>} - The fetched related lessons data or null if not found.
|
|
612
754
|
*/
|
|
613
|
-
async function fetchRelatedLessons(
|
|
614
|
-
let sort = 'published_on'
|
|
615
|
-
if (type == 'rhythmic-adventures-of-captain-carson' ||
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
755
|
+
async function fetchRelatedLessons(railContentId, brand) {
|
|
756
|
+
// let sort = 'published_on'
|
|
757
|
+
// if (type == 'rhythmic-adventures-of-captain-carson' ||
|
|
758
|
+
// type == 'diy-drum-experiments' ||
|
|
759
|
+
// type == 'in-rhythm') {
|
|
760
|
+
// sort = 'sort';
|
|
761
|
+
// }
|
|
620
762
|
//TODO: Implement $this->contentService->getFiltered
|
|
621
|
-
const query = `*[
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
difficulty,
|
|
628
|
-
difficulty_string,
|
|
629
|
-
web_url_path,
|
|
630
|
-
published_on
|
|
631
|
-
} | order(published_on asc)[0...5]`
|
|
632
|
-
return fetchSanity(query);
|
|
763
|
+
const query = `*[railcontent_id == ${railContentId} && brand == "${brand}" && references(*[_type=='permission']._id)]{
|
|
764
|
+
"related_lessons" : array::unique([
|
|
765
|
+
...(*[_type=="song" && brand == "${brand}" && references(^.artist->_id)]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->}[0...11]),
|
|
766
|
+
...(*[_type=="song" && brand == "${brand}" && references(^.genre[]->_id)]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->}[0...11])
|
|
767
|
+
])|order(published_on, railcontent_id)[0...11]}`;
|
|
768
|
+
return fetchSanity(query, false);
|
|
633
769
|
}
|
|
634
770
|
|
|
635
771
|
/**
|
|
@@ -650,7 +786,7 @@ async function fetchPackAll(railcontentId) {
|
|
|
650
786
|
web_url_path,
|
|
651
787
|
published_on
|
|
652
788
|
} | order(published_on asc)[0...5]`
|
|
653
|
-
return fetchSanity(query);
|
|
789
|
+
return fetchSanity(query, true);
|
|
654
790
|
}
|
|
655
791
|
|
|
656
792
|
/**
|
|
@@ -665,9 +801,10 @@ async function fetchPackChildren(railcontentId) {
|
|
|
665
801
|
/**
|
|
666
802
|
* Fetch data from the Sanity API based on a provided query.
|
|
667
803
|
* @param {string} query - The GROQ query to execute against the Sanity API.
|
|
804
|
+
* @param {boolean} isList - Whether to return an array or single result
|
|
668
805
|
* @returns {Promise<Object|null>} - The first result from the query, or null if an error occurs or no results are found.
|
|
669
806
|
*/
|
|
670
|
-
async function fetchSanity(query, isList
|
|
807
|
+
async function fetchSanity(query, isList) {
|
|
671
808
|
// Check the config object before proceeding
|
|
672
809
|
if (!checkConfig(globalConfig)) {
|
|
673
810
|
return null;
|
|
@@ -678,18 +815,19 @@ async function fetchSanity(query, isList = false) {
|
|
|
678
815
|
}
|
|
679
816
|
|
|
680
817
|
const encodedQuery = encodeURIComponent(query);
|
|
681
|
-
const
|
|
818
|
+
const api = globalConfig.useCachedAPI ? 'apicdn' : 'api'
|
|
819
|
+
const url = `https://${globalConfig.projectId}.${api}.sanity.io/v${globalConfig.version}/data/query/${globalConfig.dataset}?query=${encodedQuery}`;
|
|
682
820
|
const headers = {
|
|
683
821
|
'Authorization': `Bearer ${globalConfig.token}`,
|
|
684
822
|
'Content-Type': 'application/json'
|
|
685
823
|
};
|
|
686
824
|
|
|
687
825
|
try {
|
|
688
|
-
const response = await fetch(url, {
|
|
826
|
+
const response = await fetch(url, {headers});
|
|
689
827
|
const result = await response.json();
|
|
690
828
|
if (result.result) {
|
|
691
829
|
if (globalConfig.debug) {
|
|
692
|
-
console.log("fetchSanity Results:", result
|
|
830
|
+
console.log("fetchSanity Results:", result);
|
|
693
831
|
}
|
|
694
832
|
return isList ? result.result : result.result[0];
|
|
695
833
|
} else {
|
|
@@ -734,13 +872,14 @@ function checkConfig(config) {
|
|
|
734
872
|
|
|
735
873
|
|
|
736
874
|
//Main
|
|
737
|
-
|
|
875
|
+
module.exports = {
|
|
738
876
|
initializeSanityService,
|
|
739
877
|
fetchSongById,
|
|
740
878
|
fetchArtists,
|
|
879
|
+
fetchSongArtistCount,
|
|
741
880
|
fetchRelatedSongs,
|
|
742
881
|
fetchAllSongs,
|
|
743
|
-
|
|
882
|
+
fetchSongFilterOptions,
|
|
744
883
|
fetchSongCount,
|
|
745
884
|
fetchWorkouts,
|
|
746
885
|
fetchNewReleases,
|
|
@@ -748,11 +887,13 @@ export {
|
|
|
748
887
|
fetchByRailContentId,
|
|
749
888
|
fetchByRailContentIds,
|
|
750
889
|
fetchAll,
|
|
890
|
+
fetchAllFilterOptions,
|
|
751
891
|
fetchMethodNextLesson,
|
|
752
892
|
fetchMethodChildren,
|
|
753
893
|
fetchNextPreviousLesson,
|
|
754
894
|
fetchRelatedLessons,
|
|
755
895
|
fetchPackAll,
|
|
756
896
|
fetchPackChildren,
|
|
897
|
+
fetchLessonContent
|
|
757
898
|
};
|
|
758
899
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const {
|
|
2
|
+
initializeSanityService,
|
|
3
|
+
fetchSongById,
|
|
4
|
+
fetchArtists,
|
|
5
|
+
fetchSongArtistCount,
|
|
6
|
+
fetchRelatedSongs,
|
|
7
|
+
fetchAllSongs,
|
|
8
|
+
fetchSongFilterOptions,
|
|
9
|
+
fetchSongCount,
|
|
10
|
+
fetchWorkouts,
|
|
11
|
+
fetchNewReleases,
|
|
12
|
+
fetchUpcomingEvents,
|
|
13
|
+
fetchByRailContentId,
|
|
14
|
+
fetchByRailContentIds,
|
|
15
|
+
fetchAll,
|
|
16
|
+
fetchAllFilterOptions,
|
|
17
|
+
fetchMethodNextLesson,
|
|
18
|
+
fetchMethodChildren,
|
|
19
|
+
fetchNextPreviousLesson,
|
|
20
|
+
fetchRelatedLessons,
|
|
21
|
+
fetchPackAll,
|
|
22
|
+
fetchPackChildren,
|
|
23
|
+
fetchLessonContent
|
|
24
|
+
} = require('../src/index.js');
|
|
25
|
+
|
|
26
|
+
describe('Sanity Queries', function () {
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
const config = {
|
|
29
|
+
token: process.env.SANITY_API_TOKEN,
|
|
30
|
+
projectId: process.env.SANITY_PROJECT_ID,
|
|
31
|
+
dataset: process.env.SANITY_DATASET,
|
|
32
|
+
useCachedAPI: process.env.SANITY_USE_CACHED_API || true,
|
|
33
|
+
version: '2021-06-07',
|
|
34
|
+
debug: process.env.DEBUG || false
|
|
35
|
+
};
|
|
36
|
+
initializeSanityService(config);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('fetchSongById', async () => {
|
|
40
|
+
const id = 380094;
|
|
41
|
+
const response = await fetchSongById(id);
|
|
42
|
+
expect(response.railcontent_id).toBe(id);
|
|
43
|
+
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('fetchArtists', async () => {
|
|
47
|
+
const response = await fetchArtists('drumeo');
|
|
48
|
+
const artistNames = response.map((x) => x.name);
|
|
49
|
+
expect(artistNames).toContain("Arctic Monkeys");
|
|
50
|
+
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('fetchSongArtistCount', async () => {
|
|
54
|
+
const response = await fetchSongArtistCount('drumeo');
|
|
55
|
+
console.log(response);
|
|
56
|
+
expect(response).toBeGreaterThan(1000);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('fetchByRailContentId', async () => {
|
|
60
|
+
const id = 380094;
|
|
61
|
+
const response = await fetchByRailContentId(id);
|
|
62
|
+
expect(response.railcontent_id).toBe(id);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('fetchByRailContentIds', async () => {
|
|
66
|
+
const id = 380094;
|
|
67
|
+
const id2 = 402204;
|
|
68
|
+
const response = await fetchByRailContentIds([id, id2]);
|
|
69
|
+
const returnedIds = response.map((x) => x.railcontent_id);
|
|
70
|
+
expect(returnedIds).toContain(id);
|
|
71
|
+
expect(returnedIds).toContain(id2);
|
|
72
|
+
expect(returnedIds.length).toBe(2);
|
|
73
|
+
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('fetchLessonContent', async () => {
|
|
77
|
+
const id = 380094;
|
|
78
|
+
const response = await fetchLessonContent(id);
|
|
79
|
+
expect(response.railcontent_id).toBe(id);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// test('fetchRelatedLessons', async () => {
|
|
83
|
+
// const id = 380094;
|
|
84
|
+
// const response = await fetchRelatedLessons(id, 'singeo', 'song');
|
|
85
|
+
// console.log(response.related_lessons[0]);
|
|
86
|
+
// expect(response.related_lessons[0]).toBe(id);
|
|
87
|
+
// });
|
|
88
|
+
});
|