Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/db/src/core/types/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { Status } from '@vue-skuilder/common';
export interface DataLayerResult {
status: Status;
message: string;
id?: string;
}
31 changes: 25 additions & 6 deletions packages/db/src/impl/pouch/courseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ export async function addNote55(
});

if (result.ok) {
// create cards
await createCards(courseID, dataShapeId, result.id, tags, elo);
try {
// create cards
await createCards(courseID, dataShapeId, result.id, tags, elo);
} catch (error) {
console.error(
`[addNote55] Failed to create cards for note ${result.id}: ${
error instanceof Error ? error.message : String(error)
}`
);
// Add info to result to indicate card creation failed
(result as any).cardCreationFailed = true;
(result as any).cardCreationError = error instanceof Error ? error.message : String(error);
}
} else {
console.log(`Error adding note: ${result}`);
console.error(`[addNote55] Error adding note. Result: ${JSON.stringify(result)}`);
}

return result;
}

export async function createCards(
async function createCards(
courseID: string,
datashapeID: PouchDB.Core.DocumentId,
noteID: PouchDB.Core.DocumentId,
Expand All @@ -66,8 +77,16 @@ export async function createCards(
}
}

if (questionViewTypes.length === 0) {
const errorMsg = `No questionViewTypes found for datashapeID: ${datashapeID} in course config. Cards cannot be created.`;
console.error(errorMsg);
throw new Error(errorMsg);
}

let createdCards = 0;
for (const questionView of questionViewTypes) {
createCard(questionView, courseID, dsDescriptor, noteID, tags, elo);
await createCard(questionView, courseID, dsDescriptor, noteID, tags, elo);
createdCards++;
}
}

Expand All @@ -86,7 +105,7 @@ async function createCard(
for (const rQ of cfg.questionTypes) {
if (rQ.name === questionViewName) {
for (const view of rQ.viewList) {
addCard(
await addCard(
courseID,
dsDescriptor.course,
[noteID],
Expand Down
18 changes: 16 additions & 2 deletions packages/db/src/impl/pouch/courseDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,28 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
try {
const resp = await addNote55(this.id, codeCourse, shape, data, author, tags, uploads, elo);
if (resp.ok) {
// Check if card creation failed (property added by addNote55)
if ((resp as any).cardCreationFailed) {
console.warn(
`[courseDB.addNote] Note added but card creation failed: ${
(resp as any).cardCreationError
}`
);
return {
status: Status.error,
message: `Note was added but no cards were created: ${(resp as any).cardCreationError}`,
id: resp.id,
};
}
return {
status: Status.ok,
message: '',
id: resp.id,
};
} else {
return {
status: Status.error,
message: 'Unexpected ',
message: 'Unexpected error adding note',
};
}
} catch (e) {
Expand All @@ -384,7 +398,7 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
);
return {
status: Status.error,
message: `Error adding note to course. ${(e as PouchError).reason}`,
message: `Error adding note to course. ${(e as PouchError).reason || err.message}`,
};
}
}
Expand Down
Loading