Skip to content

Commit

Permalink
Merge pull request #73 from railroadmedia/BE-345-related-lessons
Browse files Browse the repository at this point in the history
Be 345 related lessons
  • Loading branch information
roxanariza authored Oct 9, 2024
2 parents 5bf1a1b + 3a05ced commit 9c02ace
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/contentMetaData.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ const contentMetadata = {
}
};

const typeWithSortOrder = ['in-rhythm', 'diy-drum-experiments', 'rhythmic-adventures-of-captain-carson'];
function processMetadata(brand, type, withFilters = false)
{
let brandMetaData = contentMetadata[brand]?.[type];
Expand Down Expand Up @@ -1099,4 +1100,5 @@ function processMetadata(brand, type, withFilters = false)

module.exports = {
processMetadata,
typeWithSortOrder
}
23 changes: 11 additions & 12 deletions src/services/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {

import {
processMetadata,
typeWithSortOrder
} from "../contentMetaData";

import {globalConfig} from "./config";
Expand Down Expand Up @@ -966,19 +967,17 @@ export async function fetchLessonContent(railContentId) {
* @returns {Promise<Array<Object>|null>} - The fetched related lessons data or null if not found.
*/
export async function fetchRelatedLessons(railContentId, brand) {
// let sort = 'published_on'
// if (type == 'rhythmic-adventures-of-captain-carson' ||
// type == 'diy-drum-experiments' ||
// type == 'in-rhythm') {
// sort = 'sort';
// }
//TODO: Implement $this->contentService->getFiltered
const query = `*[railcontent_id == ${railContentId} && brand == "${brand}" && references(*[_type=='permission']._id)]{
const query = `*[railcontent_id == ${railContentId} && brand == "${brand}" && references(*[_type=='permission']._id)]{
_type, parent_type, railcontent_id,
"related_lessons" : array::unique([
...(*[_type=="song" && brand == "${brand}" && references(^.artist->_id)]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,}[0...11]),
...(*[_type=="song" && brand == "${brand}" && references(^.genre[]->_id)]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,}[0...11])
])|order(published_on, railcontent_id)[0...11]}`;
return fetchSanity(query, false);
...(*[references(^._id)][0].child[]->{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,_type}),
...(*[_type=="song" && _type==^._type && brand == "${brand}" && references(^.artist->_id) && railcontent_id !=${railContentId}]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,_type}|order(published_on desc, title asc)[0...11]),
...(*[_type=="song" && _type==^._type && brand == "${brand}" && references(^.genre[]->_id) && railcontent_id !=${railContentId}]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,_type}|order(published_on desc, title asc)[0...11]),
...(*[_type==^._type && _type in ${JSON.stringify(typeWithSortOrder)} && brand == "${brand}" && railcontent_id !=${railContentId}]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,_type, sort}|order(sort asc, title asc)[0...11]),
...(*[_type==^._type && !(_type in ${JSON.stringify(typeWithSortOrder)}) && brand == "${brand}" && railcontent_id !=${railContentId}]{_id, "id":railcontent_id, published_on, title, "thumbnail_url":thumbnail.asset->url, difficulty_string, railcontent_id, artist->,"permission_id": permission[]->railcontent_id,_type}|order(published_on desc, title asc)[0...11]),
])[0...11]}`;

return fetchSanity(query, false);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions test/sanityQueryService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,44 @@ describe('Sanity Queries', function () {
expect(isMatch).toBeTruthy();
});

test('fetchRelatedLessons-quick-tips', async () => {
const id = 406213;
const response = await fetchRelatedLessons(id, 'singeo');
log(response);
const relatedLessons = response.related_lessons;
expect(Array.isArray(relatedLessons)).toBe(true);
relatedLessons.forEach(lesson => {
expect(lesson._type).toBe('quick-tips');
});
});

test('fetchRelatedLessons-in-rhythm', async () => {
const id = 236677;
const response = await fetchRelatedLessons(id, 'drumeo');
log(response);
const relatedLessons = response.related_lessons;
let episode = 0;
expect(Array.isArray(relatedLessons)).toBe(true);
relatedLessons.forEach(lesson => {
expect(lesson._type).toBe('in-rhythm');
expect(lesson.sort).toBeGreaterThan(episode);
episode = lesson.sort;
});
});

test('fetchRelatedLessons-child', async () => {
const id = 362278;
const course = await fetchByRailContentId(362277, 'course');
const lessonIds = course.lessons.map((doc) => doc.id);
const response = await fetchRelatedLessons(id, 'drumeo');
log(response.related_lessons);
const relatedLessons = response.related_lessons;
expect(Array.isArray(relatedLessons)).toBe(true);
expect(relatedLessons.some(
lesson => lessonIds.includes(lesson.id)
)).toBe(true);
},10000);

test('fetchChildren', async () => {
// complement test to fetchParentByRailContentId
const id = 191338; ////https://web-staging-one.musora.com/admin/studio/publishing/structure/play-along;play-along_191338
Expand Down

0 comments on commit 9c02ace

Please sign in to comment.