musora-content-services 1.0.104 → 1.0.107
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 +6 -0
- package/docs/config.js.html +1 -1
- package/docs/index.html +46 -8
- package/docs/module-Config.html +1 -1
- package/docs/module-Railcontent-Services.html +1 -1
- package/docs/module-Sanity-Services.html +26 -26
- package/docs/railcontent.js.html +13 -13
- package/docs/sanity.js.html +7 -3
- package/package.json +1 -1
- package/src/contentTypeConfig.js +21 -13
- package/src/filterBuilder.js +122 -0
- package/src/services/railcontent.js +12 -12
- package/src/services/sanity.js +205 -86
- package/test/log.js +5 -0
- package/test/sanityQueryService.test.js +240 -33
package/src/services/sanity.js
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
import {globalConfig} from "./config";
|
|
17
17
|
|
|
18
18
|
import { fetchAllCompletedStates, fetchCurrentSongComplete } from './railcontent.js';
|
|
19
|
+
import {arrayToStringRepresentation, FilterBuilder} from "../filterBuilder";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Fetch a song by its document ID from Sanity.
|
|
@@ -29,12 +30,15 @@ import { fetchAllCompletedStates, fetchCurrentSongComplete } from './railcontent
|
|
|
29
30
|
* .catch(error => console.error(error));
|
|
30
31
|
*/
|
|
31
32
|
export async function fetchSongById(documentId) {
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
const fields = getFieldsForContentType('song');
|
|
34
|
+
const filterParams = {};
|
|
35
|
+
const query = buildQuery(
|
|
36
|
+
`_type == "song" && railcontent_id == ${documentId}`,
|
|
37
|
+
filterParams,
|
|
38
|
+
fields,
|
|
39
|
+
{
|
|
40
|
+
isSingle: true,
|
|
41
|
+
});
|
|
38
42
|
return fetchSanity(query, false);
|
|
39
43
|
}
|
|
40
44
|
|
|
@@ -50,10 +54,11 @@ export async function fetchSongById(documentId) {
|
|
|
50
54
|
* .catch(error => console.error(error));
|
|
51
55
|
*/
|
|
52
56
|
export async function fetchArtists(brand) {
|
|
57
|
+
const filter = new FilterBuilder(`_type == "song" && brand == "${brand}" && references(^._id)`).buildFilter();
|
|
53
58
|
const query = `
|
|
54
59
|
*[_type == "artist"]{
|
|
55
60
|
name,
|
|
56
|
-
"lessonsCount": count(*[
|
|
61
|
+
"lessonsCount": count(*[${filter}])
|
|
57
62
|
}[lessonsCount > 0]`;
|
|
58
63
|
return fetchSanity(query, true);
|
|
59
64
|
}
|
|
@@ -254,23 +259,32 @@ export async function fetchWorkouts(brand) {
|
|
|
254
259
|
*/
|
|
255
260
|
export async function fetchNewReleases(brand, { page = 1, limit = 10, sort="-published_on" } = {}) {
|
|
256
261
|
const newTypes = getNewReleasesTypes(brand);
|
|
257
|
-
const typesString =
|
|
262
|
+
const typesString = arrayToStringRepresentation(newTypes);
|
|
258
263
|
const start = (page - 1) * limit;
|
|
259
264
|
const end = start + limit;
|
|
260
265
|
const sortOrder = getSortOrder(sort);
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
266
|
+
const filter = `_type in ${typesString} && brand == '${brand}'`;
|
|
267
|
+
const fields = `
|
|
268
|
+
"id": railcontent_id,
|
|
269
|
+
title,
|
|
270
|
+
"image": thumbnail.asset->url,
|
|
271
|
+
"artist_name": instructor[0]->name,
|
|
272
|
+
"artists": instructor[]->name,
|
|
273
|
+
difficulty,
|
|
274
|
+
difficulty_string,
|
|
275
|
+
length_in_seconds,
|
|
276
|
+
published_on,
|
|
277
|
+
"type": _type,
|
|
278
|
+
web_url_path,`;
|
|
279
|
+
const filterParams = {};
|
|
280
|
+
const query = buildQuery(
|
|
281
|
+
filter,
|
|
282
|
+
filterParams,
|
|
283
|
+
fields,
|
|
284
|
+
{
|
|
285
|
+
sortOrder: sortOrder,
|
|
286
|
+
end: end,
|
|
287
|
+
});
|
|
274
288
|
return fetchSanity(query, true);
|
|
275
289
|
}
|
|
276
290
|
|
|
@@ -291,11 +305,11 @@ export async function fetchNewReleases(brand, { page = 1, limit = 10, sort="-pub
|
|
|
291
305
|
*/
|
|
292
306
|
export async function fetchUpcomingEvents(brand, { page = 1, limit = 10 } = {}) {
|
|
293
307
|
const liveTypes = getUpcomingEventsTypes(brand);
|
|
294
|
-
const typesString =
|
|
308
|
+
const typesString = arrayToStringRepresentation(liveTypes);
|
|
295
309
|
const now = getSanityDate(new Date());
|
|
296
310
|
const start = (page - 1) * limit;
|
|
297
311
|
const end = start + limit;
|
|
298
|
-
const
|
|
312
|
+
const fields = `
|
|
299
313
|
"id": railcontent_id,
|
|
300
314
|
title,
|
|
301
315
|
"image": thumbnail.asset->url,
|
|
@@ -306,8 +320,16 @@ export async function fetchUpcomingEvents(brand, { page = 1, limit = 10 } = {})
|
|
|
306
320
|
length_in_seconds,
|
|
307
321
|
published_on,
|
|
308
322
|
"type": _type,
|
|
309
|
-
web_url_path
|
|
310
|
-
|
|
323
|
+
web_url_path,`;
|
|
324
|
+
const query = buildRawQuery(
|
|
325
|
+
`_type in ${typesString} && brand == '${brand}' && published_on > '${now}' && status == 'scheduled'`,
|
|
326
|
+
fields,
|
|
327
|
+
{
|
|
328
|
+
sortOrder: 'published_on asc',
|
|
329
|
+
start: start,
|
|
330
|
+
end: end,
|
|
331
|
+
},
|
|
332
|
+
);
|
|
311
333
|
return fetchSanity(query, true);
|
|
312
334
|
}
|
|
313
335
|
|
|
@@ -354,6 +376,7 @@ export async function fetchScheduledReleases(brand, { page = 1, limit = 10 }) {
|
|
|
354
376
|
* Fetch content by a specific Railcontent ID.
|
|
355
377
|
*
|
|
356
378
|
* @param {string} id - The Railcontent ID of the content to fetch.
|
|
379
|
+
* @param {string} contentType - The document type of content to fetch
|
|
357
380
|
* @returns {Promise<Object|null>} - A promise that resolves to the content object or null if not found.
|
|
358
381
|
*
|
|
359
382
|
* @example
|
|
@@ -362,10 +385,16 @@ export async function fetchScheduledReleases(brand, { page = 1, limit = 10 }) {
|
|
|
362
385
|
* .catch(error => console.error(error));
|
|
363
386
|
*/
|
|
364
387
|
export async function fetchByRailContentId(id, contentType) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
388
|
+
|
|
389
|
+
const query = buildRawQuery(
|
|
390
|
+
`railcontent_id == ${id} && _type == '${contentType}'`,
|
|
391
|
+
getFieldsForContentType(contentType),
|
|
392
|
+
{
|
|
393
|
+
isSingle: true,
|
|
394
|
+
},
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
return fetchSanity(query, false);
|
|
369
398
|
}
|
|
370
399
|
|
|
371
400
|
/**
|
|
@@ -382,6 +411,7 @@ export async function fetchByRailContentId(id, contentType) {
|
|
|
382
411
|
*/
|
|
383
412
|
export async function fetchByRailContentIds(ids, contentType = undefined) {
|
|
384
413
|
const idsString = ids.join(',');
|
|
414
|
+
|
|
385
415
|
const query = `*[railcontent_id in [${idsString}]]{
|
|
386
416
|
${getFieldsForContentType(contentType)}
|
|
387
417
|
}`
|
|
@@ -433,9 +463,13 @@ export async function fetchAll(brand, type, {
|
|
|
433
463
|
let config = contentTypeConfig[type] ?? {};
|
|
434
464
|
let additionalFields = config?.fields ?? [];
|
|
435
465
|
let isGroupByOneToOne = (groupBy ? config?.relationships?.[groupBy]?.isOneToOne : false) ?? false;
|
|
466
|
+
let webUrlPathType = config?.slug ?? type;
|
|
436
467
|
const start = (page - 1) * limit;
|
|
437
468
|
const end = start + limit;
|
|
438
469
|
|
|
470
|
+
// Construct the type filter
|
|
471
|
+
const typeFilter = type ? `&& _type == '${type}'` : "";
|
|
472
|
+
|
|
439
473
|
// Construct the search filter
|
|
440
474
|
const searchFilter = searchTerm
|
|
441
475
|
? groupBy !== "" ?
|
|
@@ -461,16 +495,17 @@ export async function fetchAll(brand, type, {
|
|
|
461
495
|
// Determine the group by clause
|
|
462
496
|
let query = "";
|
|
463
497
|
if (groupBy !== "" && isGroupByOneToOne) {
|
|
498
|
+
let webUrlPath = 'artists';
|
|
464
499
|
query = `
|
|
465
500
|
{
|
|
466
|
-
"total": count(*[_type == '${groupBy}' && count(*[
|
|
467
|
-
"entity": *[_type == '${groupBy}' && count(*[
|
|
501
|
+
"total": count(*[_type == '${groupBy}' && count(*[brand == '${brand}' && ^._id == ${groupBy}._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id) > 0]),
|
|
502
|
+
"entity": *[_type == '${groupBy}' && count(*[brand == '${brand}' && ^._id == ${groupBy}._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id) > 0]
|
|
468
503
|
{
|
|
469
504
|
'id': _id,
|
|
470
505
|
'type': _type,
|
|
471
506
|
name,
|
|
472
507
|
'head_shot_picture_url': thumbnail_url.asset->url,
|
|
473
|
-
web_url_path,
|
|
508
|
+
'web_url_path': '/${brand}/${webUrlPath}/'+name+'?included_fieds[]=type,${type}',
|
|
474
509
|
'all_lessons_count': count(*[_type == '${type}' && brand == '${brand}' && ^._id == ${groupBy}._ref ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id),
|
|
475
510
|
'lessons': *[_type == '${type}' && brand == '${brand}' && ^._id == ${groupBy}._ref ${searchFilter} ${includedFieldsFilter} ${progressFilter}]{
|
|
476
511
|
${fieldsString},
|
|
@@ -481,18 +516,19 @@ export async function fetchAll(brand, type, {
|
|
|
481
516
|
[${start}...${end}]
|
|
482
517
|
}`;
|
|
483
518
|
} else if (groupBy !== "") {
|
|
519
|
+
let webUrlPath = (groupBy == 'genre')?'/genres':'';
|
|
484
520
|
query = `
|
|
485
521
|
{
|
|
486
|
-
"total": count(*[_type == '${groupBy}' && count(*[
|
|
487
|
-
"entity": *[_type == '${groupBy}' && count(*[
|
|
522
|
+
"total": count(*[_type == '${groupBy}' && count(*[brand == '${brand}' && ^._id in ${groupBy}[]._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id) > 0]),
|
|
523
|
+
"entity": *[_type == '${groupBy}' && count(*[brand == '${brand}' && ^._id in ${groupBy}[]._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id) > 0]
|
|
488
524
|
{
|
|
489
525
|
'id': _id,
|
|
490
526
|
'type': _type,
|
|
491
527
|
name,
|
|
492
528
|
'head_shot_picture_url': thumbnail_url.asset->url,
|
|
493
|
-
web_url_path,
|
|
494
|
-
'all_lessons_count': count(*[
|
|
495
|
-
'lessons': *[
|
|
529
|
+
'web_url_path': select(defined(web_url_path)=> web_url_path +'?included_fieds[]=type,${type}',!defined(web_url_path)=> '/${brand}${webUrlPath}/'+name+'/${webUrlPathType}'),
|
|
530
|
+
'all_lessons_count': count(*[brand == '${brand}' && ^._id in ${groupBy}[]._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]._id),
|
|
531
|
+
'lessons': *[brand == '${brand}' && ^._id in ${groupBy}[]._ref ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}]{
|
|
496
532
|
${fieldsString},
|
|
497
533
|
${groupBy}
|
|
498
534
|
}[0...10]
|
|
@@ -503,10 +539,10 @@ export async function fetchAll(brand, type, {
|
|
|
503
539
|
} else {
|
|
504
540
|
query = `
|
|
505
541
|
{
|
|
506
|
-
"entity": *[
|
|
542
|
+
"entity": *[brand == "${brand}" ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}] | order(${sortOrder}) [${start}...${end}] {
|
|
507
543
|
${fieldsString},
|
|
508
544
|
},
|
|
509
|
-
"total": count(*[
|
|
545
|
+
"total": count(*[brand == "${brand}" ${typeFilter} ${searchFilter} ${includedFieldsFilter} ${progressFilter}])
|
|
510
546
|
}
|
|
511
547
|
`;
|
|
512
548
|
}
|
|
@@ -614,8 +650,8 @@ export async function fetchChildren(railcontentId, contentType) {
|
|
|
614
650
|
${getFieldsForContentType(contentType)}
|
|
615
651
|
},
|
|
616
652
|
}[0..1]`;
|
|
617
|
-
let parent = await fetchSanity(query,
|
|
618
|
-
return parent[
|
|
653
|
+
let parent = await fetchSanity(query, false);
|
|
654
|
+
return parent['children'] ?? [];
|
|
619
655
|
}
|
|
620
656
|
|
|
621
657
|
/**
|
|
@@ -631,8 +667,8 @@ export async function fetchParentByRailContentId(railcontentId) {
|
|
|
631
667
|
})
|
|
632
668
|
])
|
|
633
669
|
}[0...1]`;
|
|
634
|
-
let child = await fetchSanity(query,
|
|
635
|
-
return child[
|
|
670
|
+
let child = await fetchSanity(query, false);
|
|
671
|
+
return child['parents'][0] ?? [];
|
|
636
672
|
}
|
|
637
673
|
|
|
638
674
|
/**
|
|
@@ -653,9 +689,16 @@ export async function fetchMethods(brand) {
|
|
|
653
689
|
* @returns {Promise<Object|null>} - The fetched foundation data or null if not found.
|
|
654
690
|
*/
|
|
655
691
|
export async function fetchFoundation(slug) {
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
692
|
+
const filterParams = {};
|
|
693
|
+
const query = buildQuery(
|
|
694
|
+
`_type == 'foundation' && slug.current == "${slug}"`,
|
|
695
|
+
filterParams,
|
|
696
|
+
getFieldsForContentType('foundation'),
|
|
697
|
+
{
|
|
698
|
+
sortOrder: 'published_on asc',
|
|
699
|
+
isSingle: true,
|
|
700
|
+
}
|
|
701
|
+
);
|
|
659
702
|
return fetchSanity(query, false);
|
|
660
703
|
}
|
|
661
704
|
|
|
@@ -676,6 +719,7 @@ export async function fetchMethod(brand, slug) {
|
|
|
676
719
|
status,
|
|
677
720
|
title,
|
|
678
721
|
video,
|
|
722
|
+
length_in_seconds,
|
|
679
723
|
"type": _type,
|
|
680
724
|
"levels": child[]->
|
|
681
725
|
{
|
|
@@ -757,7 +801,6 @@ export async function fetchMethodPreviousNextLesson(railcontentId, methodId) {
|
|
|
757
801
|
* @returns {Promise<Array<Object>|null>} - The fetched children data or null if not found.
|
|
758
802
|
*/
|
|
759
803
|
export async function fetchMethodChildrenIds(railcontentId) {
|
|
760
|
-
//TODO: Implement getByParentId include sum XP
|
|
761
804
|
const query = `*[_type == 'learning-path' && railcontent_id == ${railcontentId}]{
|
|
762
805
|
'children': child[]-> {
|
|
763
806
|
'id': railcontent_id,
|
|
@@ -770,7 +813,7 @@ export async function fetchMethodChildrenIds(railcontentId) {
|
|
|
770
813
|
}
|
|
771
814
|
}`;
|
|
772
815
|
let allChildren = await fetchSanity(query, false);
|
|
773
|
-
return getChildrenToDepth(allChildren, 4)
|
|
816
|
+
return getChildrenToDepth(allChildren, 4);
|
|
774
817
|
}
|
|
775
818
|
|
|
776
819
|
function getChildrenToDepth(parent, depth = 1)
|
|
@@ -819,8 +862,8 @@ export async function fetchNextPreviousLesson(railcontentId) {
|
|
|
819
862
|
* .catch(error => console.error(error));
|
|
820
863
|
*/
|
|
821
864
|
export async function fetchLessonContent(railContentId) {
|
|
822
|
-
|
|
823
|
-
|
|
865
|
+
const filterParams = {};
|
|
866
|
+
const fields = `title,
|
|
824
867
|
published_on,
|
|
825
868
|
"type":_type,
|
|
826
869
|
"resources": resource,
|
|
@@ -856,8 +899,19 @@ export async function fetchLessonContent(railContentId) {
|
|
|
856
899
|
},
|
|
857
900
|
${assignmentsField}
|
|
858
901
|
video,
|
|
859
|
-
length_in_seconds
|
|
860
|
-
|
|
902
|
+
length_in_seconds,
|
|
903
|
+
mp3_no_drums_no_click_url,
|
|
904
|
+
mp3_no_drums_yes_click_url,
|
|
905
|
+
mp3_yes_drums_no_click_url,
|
|
906
|
+
mp3_yes_drums_yes_click_url,`;
|
|
907
|
+
const query = buildQuery(
|
|
908
|
+
`railcontent_id == ${railContentId}`,
|
|
909
|
+
filterParams,
|
|
910
|
+
fields,
|
|
911
|
+
{
|
|
912
|
+
isSingle: true,
|
|
913
|
+
}
|
|
914
|
+
);
|
|
861
915
|
return fetchSanity(query, false);
|
|
862
916
|
}
|
|
863
917
|
|
|
@@ -916,10 +970,16 @@ export async function fetchRelatedMethodLessons(railContentId, brand) {
|
|
|
916
970
|
*/
|
|
917
971
|
export async function fetchAllPacks(brand, sort = "-published_on", searchTerm = "") {
|
|
918
972
|
const sortOrder = getSortOrder(sort);
|
|
919
|
-
|
|
920
|
-
const
|
|
921
|
-
|
|
922
|
-
|
|
973
|
+
const filter = `_type == 'pack' && brand == '${brand}' && title match "${searchTerm}*"`
|
|
974
|
+
const filterParams = {};
|
|
975
|
+
const query = buildQuery(
|
|
976
|
+
filter,
|
|
977
|
+
filterParams,
|
|
978
|
+
getFieldsForContentType('pack'),
|
|
979
|
+
{
|
|
980
|
+
sortOrder: sortOrder,
|
|
981
|
+
}
|
|
982
|
+
);
|
|
923
983
|
return fetchSanity(query, true);
|
|
924
984
|
}
|
|
925
985
|
|
|
@@ -929,11 +989,7 @@ export async function fetchAllPacks(brand, sort = "-published_on", searchTerm =
|
|
|
929
989
|
* @returns {Promise<Array<Object>|null>} - The fetched pack content data or null if not found.
|
|
930
990
|
*/
|
|
931
991
|
export async function fetchPackAll(railcontentId) {
|
|
932
|
-
|
|
933
|
-
const query = `*[railcontent_id == ${railcontentId}]{
|
|
934
|
-
${getFieldsForContentType('pack')}
|
|
935
|
-
} | order(published_on asc)[0...1]`
|
|
936
|
-
return fetchSanity(query, false);
|
|
992
|
+
return fetchByRailContentId(railcontentId, 'pack');
|
|
937
993
|
}
|
|
938
994
|
|
|
939
995
|
export async function fetchLiveEvent(brand) {
|
|
@@ -1009,7 +1065,7 @@ export async function fetchPackChildren(railcontentId) {
|
|
|
1009
1065
|
export async function fetchChallengeOverview(id) {
|
|
1010
1066
|
// WIP
|
|
1011
1067
|
const query = `*[railcontent_id == ${id}]{
|
|
1012
|
-
${getFieldsForContentType("challenge"
|
|
1068
|
+
${getFieldsForContentType("challenge")}
|
|
1013
1069
|
"lessons": child[]->{
|
|
1014
1070
|
"id": railcontent_id,
|
|
1015
1071
|
title,
|
|
@@ -1020,7 +1076,7 @@ export async function fetchChallengeOverview(id) {
|
|
|
1020
1076
|
difficulty,
|
|
1021
1077
|
"type": _type,
|
|
1022
1078
|
}
|
|
1023
|
-
}`;
|
|
1079
|
+
} [0...1]`;
|
|
1024
1080
|
return fetchSanity(query, false);
|
|
1025
1081
|
}
|
|
1026
1082
|
|
|
@@ -1040,18 +1096,21 @@ export async function fetchCoachLessons(brand, id, {
|
|
|
1040
1096
|
page = 1,
|
|
1041
1097
|
limit = 20,
|
|
1042
1098
|
} = {}) {
|
|
1043
|
-
const fieldsString =
|
|
1099
|
+
const fieldsString = getFieldsForContentType();
|
|
1044
1100
|
const start = (page - 1) * limit;
|
|
1045
1101
|
const end = start + limit;
|
|
1046
|
-
const searchFilter = searchTerm ? `&& title match "${searchTerm}*"`: ''
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1102
|
+
const searchFilter = searchTerm ? `&& title match "${searchTerm}*"`: ''
|
|
1103
|
+
const filter = `brand == '${brand}' ${searchFilter} && references(*[_type=='instructor' && railcontent_id == ${id}]._id)`;
|
|
1104
|
+
sortOrder = getSortOrder(sortOrder);
|
|
1105
|
+
const query = buildEntityAndTotalQuery(
|
|
1106
|
+
filter,
|
|
1107
|
+
fieldsString,
|
|
1108
|
+
{
|
|
1109
|
+
sortOrder: sortOrder,
|
|
1110
|
+
start: start,
|
|
1111
|
+
end: end,
|
|
1112
|
+
},
|
|
1113
|
+
);
|
|
1055
1114
|
return fetchSanity(query, true);
|
|
1056
1115
|
}
|
|
1057
1116
|
|
|
@@ -1066,10 +1125,7 @@ export async function fetchCoachLessons(brand, id, {
|
|
|
1066
1125
|
* .catch(error => console.error(error));
|
|
1067
1126
|
*/
|
|
1068
1127
|
export async function fetchCourseOverview(id) {
|
|
1069
|
-
|
|
1070
|
-
${getFieldsForContentType("course", true)}
|
|
1071
|
-
}`
|
|
1072
|
-
return fetchSanity(query, false);
|
|
1128
|
+
return fetchByRailContentId(id, 'course');
|
|
1073
1129
|
}
|
|
1074
1130
|
|
|
1075
1131
|
/**
|
|
@@ -1090,21 +1146,23 @@ export async function fetchByReference(brand, {
|
|
|
1090
1146
|
limit = 20,
|
|
1091
1147
|
includedFields = [],
|
|
1092
1148
|
} = {}) {
|
|
1093
|
-
const fieldsString =
|
|
1149
|
+
const fieldsString = getFieldsForContentType();
|
|
1094
1150
|
const start = (page - 1) * limit;
|
|
1095
1151
|
const end = start + limit;
|
|
1096
1152
|
const searchFilter = searchTerm ? `&& title match "${searchTerm}*"`: '';
|
|
1097
1153
|
const includedFieldsFilter = includedFields.length > 0
|
|
1098
1154
|
? includedFields.join(' && ')
|
|
1099
1155
|
: "";
|
|
1100
|
-
|
|
1101
|
-
const query =
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1156
|
+
const filter = `brand == '${brand}' ${searchFilter} && references(*[${includedFieldsFilter}]._id)`;
|
|
1157
|
+
const query = buildEntityAndTotalQuery(
|
|
1158
|
+
filter,
|
|
1159
|
+
fieldsString,
|
|
1160
|
+
{
|
|
1161
|
+
sortOrder: getSortOrder(sortOrder),
|
|
1162
|
+
start: start,
|
|
1163
|
+
end: end,
|
|
1164
|
+
},
|
|
1165
|
+
);
|
|
1108
1166
|
return fetchSanity(query, true);
|
|
1109
1167
|
}
|
|
1110
1168
|
|
|
@@ -1308,3 +1366,64 @@ function checkSanityConfig(config) {
|
|
|
1308
1366
|
}
|
|
1309
1367
|
|
|
1310
1368
|
|
|
1369
|
+
function buildRawQuery(
|
|
1370
|
+
filter = '',
|
|
1371
|
+
fields = '...',
|
|
1372
|
+
{
|
|
1373
|
+
sortOrder = 'published_on desc',
|
|
1374
|
+
start = 0,
|
|
1375
|
+
end = 10,
|
|
1376
|
+
isSingle = false,
|
|
1377
|
+
}
|
|
1378
|
+
) {
|
|
1379
|
+
const sortString = sortOrder ? `order(${sortOrder})` : '';
|
|
1380
|
+
const countString = isSingle ? '[0...1]' : `[${start}...${end}]`;
|
|
1381
|
+
const query = `*[${filter}]{
|
|
1382
|
+
${fields}
|
|
1383
|
+
} | ${sortString}${countString}`
|
|
1384
|
+
return query;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
function buildQuery(
|
|
1389
|
+
baseFilter = '',
|
|
1390
|
+
filterParams = {},
|
|
1391
|
+
fields = '...',
|
|
1392
|
+
{
|
|
1393
|
+
sortOrder = 'published_on desc',
|
|
1394
|
+
start = 0,
|
|
1395
|
+
end = 10,
|
|
1396
|
+
isSingle = false,
|
|
1397
|
+
},
|
|
1398
|
+
) {
|
|
1399
|
+
const filter = new FilterBuilder(baseFilter, filterParams).buildFilter();
|
|
1400
|
+
return buildRawQuery(filter, fields, {sortOrder, start, end, isSingle});
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
function buildEntityAndTotalQuery(
|
|
1404
|
+
filter = '',
|
|
1405
|
+
fields = '...',
|
|
1406
|
+
{
|
|
1407
|
+
sortOrder = 'published_on desc',
|
|
1408
|
+
start = 0,
|
|
1409
|
+
end = 10,
|
|
1410
|
+
isSingle = false,
|
|
1411
|
+
},
|
|
1412
|
+
) {
|
|
1413
|
+
const sortString = sortOrder ? `order(${sortOrder})` : '';
|
|
1414
|
+
const countString = isSingle ? '[0...1]' : `[${start}...${end}]`;
|
|
1415
|
+
const query = `{
|
|
1416
|
+
"entity": *[${filter}] | ${sortString}${countString}
|
|
1417
|
+
{
|
|
1418
|
+
${fields}
|
|
1419
|
+
},
|
|
1420
|
+
"total": count(*[${filter}])
|
|
1421
|
+
}`;
|
|
1422
|
+
return query;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|