Skip to content

Commit

Permalink
fix: handle organization items as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Dec 19, 2024
1 parent 5fb15c2 commit 87a2a96
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions apps/api/src/scorm/services/scorm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ import { ScormRepository } from "../repositories/scorm.repository";

import type { UUIDType } from "src/common";

interface ScormChapter {
type ScormChapter = {
title: string;
identifier: string;
displayOrder: number;
lessons: ScormLesson[];
}
};

interface ScormLesson {
type ScormLesson = {
title: string;
identifier: string;
href: string;
type: string;
displayOrder: number;
isQuiz: boolean;
}
};

@Injectable()
export class ScormService {
Expand Down Expand Up @@ -417,7 +417,6 @@ export class ScormService {
const organization = manifest.manifest.organizations[0].organization[0];
const resources = manifest.manifest.resources[0].resource;

// Map to quickly lookup resources
const resourceMap = new Map(
resources.map((resource: any) => [
resource.$.identifier,
Expand All @@ -429,28 +428,45 @@ export class ScormService {
]),
);

// Parse chapters (scorm folders)
return organization.item.map((chapterItem: any, chapterIndex: number) => {
const lessons = chapterItem.item.map((lessonItem: any, lessonIndex: number) => {
const items = Array.isArray(organization.item) ? organization.item : [organization.item];

return items.map((chapterItem: any, chapterIndex: number) => {
const subItems = chapterItem.item
? Array.isArray(chapterItem.item)
? chapterItem.item
: [chapterItem.item]
: [
{
$: {
identifier: chapterItem.$.identifier,
identifierref: chapterItem.$.identifierref,
},
title: chapterItem.title,
},
];

const lessons = subItems.map((lessonItem: any, lessonIndex: number) => {
const resourceId = lessonItem.$.identifierref;
const resource = resourceMap.get(resourceId);
const lessonTitle = lessonItem.title[0];
const lessonTitle = Array.isArray(lessonItem.title)
? lessonItem.title[0]
: lessonItem.title;
const isQuiz = lessonTitle.toLowerCase().includes("quiz");

return {
title: lessonTitle,
identifier: lessonItem.$.identifier,
// @ts-expect-error there is no way to type resource properly
// @ts-expect-error tet
href: resource?.href || "",
// @ts-expect-error there is no way to type resource properly
// @ts-expect-error tet
type: isQuiz ? LESSON_TYPES.quiz : this.determineLessonType(resource?.href || ""),
displayOrder: lessonIndex + 1,
isQuiz,
};
});

return {
title: chapterItem.title[0],
title: Array.isArray(chapterItem.title) ? chapterItem.title[0] : chapterItem.title,
identifier: chapterItem.$.identifier,
displayOrder: chapterIndex + 1,
lessons,
Expand Down

0 comments on commit 87a2a96

Please sign in to comment.