@@ -37,9 +37,9 @@ function randIntWeightedTowardZero(n: number) {
3737}
3838
3939export class CourseDB implements StudyContentSource {
40- private log ( msg : string ) : void {
41- log ( `CourseLog: ${ this . id } \n ${ msg } ` ) ;
42- }
40+ // private log(msg: string): void {
41+ // log(`CourseLog: ${this.id}\n ${msg}`);
42+ // }
4343
4444 private db : PouchDB . Database ;
4545 private id : string ;
@@ -95,7 +95,7 @@ export class CourseDB implements StudyContentSource {
9595 contentSourceID : this . id ,
9696 cardID : r . cardId ,
9797 courseID : r . courseId ,
98- qualifiedID : r . courseId + '-' + r . cardId ,
98+ qualifiedID : ` ${ r . courseId } - ${ r . cardId } ` ,
9999 reviewID : r . _id ,
100100 status : 'review' ,
101101 } ;
@@ -150,10 +150,15 @@ export class CourseDB implements StudyContentSource {
150150 const ret : CourseElo [ ] = [ ] ;
151151 docs . rows . forEach ( ( r ) => {
152152 // [ ] remove these ts-ignore directives.
153- if ( r . doc && r . doc . elo ) {
154- ret . push ( toCourseElo ( r . doc . elo ) ) ;
153+ if ( isSuccessRow ( r ) ) {
154+ if ( r . doc && r . doc . elo ) {
155+ ret . push ( toCourseElo ( r . doc . elo ) ) ;
156+ } else {
157+ console . warn ( 'no elo data for card: ' + r . id ) ;
158+ ret . push ( blankCourseElo ( ) ) ;
159+ }
155160 } else {
156- console . warn ( 'no elo data for card: ' + r . id ) ;
161+ console . warn ( 'no elo data for card: ' + JSON . stringify ( r ) ) ;
157162 ret . push ( blankCourseElo ( ) ) ;
158163 }
159164 } ) ;
@@ -204,13 +209,17 @@ export class CourseDB implements StudyContentSource {
204209 } ) ;
205210 const ret : { [ card : string ] : string [ ] } = { } ;
206211 cards . rows . forEach ( ( r ) => {
207- ret [ r . id ] = r . doc ! . id_displayable_data ;
212+ if ( isSuccessRow ( r ) ) {
213+ ret [ r . id ] = r . doc ! . id_displayable_data ;
214+ }
208215 } ) ;
209216
210217 await Promise . all (
211218 cards . rows . map ( ( r ) => {
212219 return async ( ) => {
213- ret [ r . id ] = r . doc ! . id_displayable_data ;
220+ if ( isSuccessRow ( r ) ) {
221+ ret [ r . id ] = r . doc ! . id_displayable_data ;
222+ }
214223 } ;
215224 } )
216225 ) ;
@@ -283,7 +292,7 @@ export class CourseDB implements StudyContentSource {
283292 return {
284293 courseID : this . id ,
285294 cardID : split [ 1 ] ,
286- qualifiedID : c ,
295+ qualifiedID : ` ${ split [ 0 ] } - ${ split [ 1 ] } ` ,
287296 contentSourceType : 'course' ,
288297 contentSourceID : this . id ,
289298 status : 'new' ,
@@ -431,7 +440,14 @@ export async function getCourseQuestionTypes(courseID: string) {
431440
432441export async function getCourseConfig ( courseID : string ) {
433442 const config = await getCourseConfigs ( [ courseID ] ) ;
434- return config . rows [ 0 ] . doc ;
443+ let first = config . rows [ 0 ] ;
444+ if ( ! first ) {
445+ throw new Error ( `Course config not found for course ID: ${ courseID } ` ) ;
446+ } else if ( isSuccessRow ( first ) ) {
447+ return first . doc ;
448+ } else {
449+ throw new Error ( `Course config not found for course ID: ${ courseID } ` ) ;
450+ }
435451}
436452
437453// todo: this is actually returning full tag docs now.
@@ -576,3 +592,30 @@ export async function getCourseConfigs(ids: string[]) {
576592 keys : ids ,
577593 } ) ;
578594}
595+
596+ function isSuccessRow < T > (
597+ row :
598+ | {
599+ key : PouchDB . Core . DocumentKey ;
600+ error : 'not_found' ;
601+ }
602+ | {
603+ doc ?: PouchDB . Core . ExistingDocument < PouchDB . Core . AllDocsMeta & T > | null | undefined ;
604+ id : PouchDB . Core . DocumentId ;
605+ key : PouchDB . Core . DocumentKey ;
606+ value : {
607+ rev : PouchDB . Core . RevisionId ;
608+ deleted ?: boolean | undefined ;
609+ } ;
610+ }
611+ ) : row is {
612+ doc ?: PouchDB . Core . ExistingDocument < PouchDB . Core . AllDocsMeta & T > | null | undefined ;
613+ id : PouchDB . Core . DocumentId ;
614+ key : PouchDB . Core . DocumentKey ;
615+ value : {
616+ rev : PouchDB . Core . RevisionId ;
617+ deleted ?: boolean | undefined ;
618+ } ;
619+ } {
620+ return 'doc' in row && row . doc !== null && row . doc !== undefined ;
621+ }
0 commit comments