Skip to content

Commit 769b0d5

Browse files
committed
import smudging, etc, toward build
1 parent c626c16 commit 769b0d5

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

packages/db/src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
// This will be populated with interface definitions extracted from the Vue package
33
export * from './types';
44
export * from './interfaces';
5+
export * from './types-legacy';

packages/db/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
// Re-export everything from the core and pouch implementations
22
export * from './core';
33
export * from './pouch';
4-
5-
// Default exports - currently PouchDB implementation
6-
export * from './pouch';

packages/db/src/core/contentSource.ts renamed to packages/db/src/pouch/contentSource.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StudentClassroomDB } from './classroomDB';
22
import { CourseDB } from './courseDB';
3-
import { ScheduledCard } from './userDB';
3+
import { ScheduledCard, User } from './userDB';
44

55
export type StudySessionFailedItem = StudySessionFailedNewItem | StudySessionFailedReviewItem;
66

@@ -48,11 +48,16 @@ export interface StudyContentSource {
4848
getNewCards(n?: number): Promise<StudySessionNewItem[]>;
4949
}
5050

51-
export async function getStudySource(source: ContentSourceID): Promise<StudyContentSource> {
51+
export async function getStudySource(
52+
source: ContentSourceID,
53+
user: User
54+
): Promise<StudyContentSource> {
5255
if (source.type === 'classroom') {
5356
return await StudentClassroomDB.factory(source.id);
5457
} else {
5558
// if (source.type === 'course') - removed so tsc is certain something returns
56-
return new CourseDB(source.id);
59+
return new CourseDB(source.id, async () => {
60+
return user;
61+
});
5762
}
5863
}

packages/db/src/pouch/courseDB.ts

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function randIntWeightedTowardZero(n: number) {
3737
}
3838

3939
export 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

432441
export 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+
}

packages/db/src/pouch/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,10 @@ export function getStartAndEndKeys(key: string) {
225225
endkey: key + '\ufff0',
226226
};
227227
}
228+
229+
//////////////////////
230+
// Package exports
231+
//////////////////////
232+
233+
export * from './userDB';
234+
export * from './courseDB';

0 commit comments

Comments
 (0)