( courseId: number, )
| 270 | //TODO: add a cache here |
| 271 | |
| 272 | export const getFullCourseContent = async ( |
| 273 | courseId: number, |
| 274 | ): Promise<FullCourseContent[]> => { |
| 275 | // const value = cache.get('getFullCourseContent', [ |
| 276 | // courseId.toString(), |
| 277 | // ]); |
| 278 | // if (value) { |
| 279 | // return value; |
| 280 | // } |
| 281 | const session = await getServerSession(authOptions); |
| 282 | if (!session?.user) { |
| 283 | return []; |
| 284 | } |
| 285 | const contents = await getAllContent(); |
| 286 | const courseContent = await getRootCourseContent(courseId); |
| 287 | const videoProgress = await getVideoProgressForUser(session?.user?.id); |
| 288 | const bookmarkData = await getBookmarkData(); |
| 289 | const contentMap = new Map<string, FullCourseContent>( |
| 290 | contents.map((content: any) => [ |
| 291 | content.id, |
| 292 | { |
| 293 | ...content, |
| 294 | children: [], |
| 295 | bookmark: bookmarkData.find((x) => x.contentId === content.id), |
| 296 | videoProgress: |
| 297 | content.type === 'video' |
| 298 | ? { |
| 299 | duration: videoProgress.find((x) => x.contentId === content.id) |
| 300 | ?.currentTimestamp, |
| 301 | markAsCompleted: videoProgress.find( |
| 302 | (x) => x.contentId === content.id, |
| 303 | )?.markAsCompleted, |
| 304 | videoFullDuration: content.VideoMetadata?.duration, |
| 305 | } |
| 306 | : null, |
| 307 | }, |
| 308 | ]), |
| 309 | ); |
| 310 | |
| 311 | const rootContents: FullCourseContent[] = []; |
| 312 | |
| 313 | contents |
| 314 | .sort((a, b) => (a.id < b.id ? -1 : 1)) |
| 315 | .forEach((content: any) => { |
| 316 | if (content.parentId) { |
| 317 | contentMap |
| 318 | .get(content.parentId) |
| 319 | ?.children?.push(contentMap.get(content.id)!); |
| 320 | } else if (courseContent.find((x: any) => x.contentId === content.id)) { |
| 321 | rootContents.push(contentMap.get(content.id)!); |
| 322 | } |
| 323 | }); |
| 324 | |
| 325 | await cache.set('getFullCourseContent', [courseId.toString()], rootContents); |
| 326 | return rootContents; |
| 327 | }; |
| 328 | |
| 329 | export const getCourseContent = async ( |
no test coverage detected