Skip to content

Commit 8a5cf45

Browse files
committed
migrate usage of floating prefix definitions
1 parent e6b33e2 commit 8a5cf45

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

packages/db/src/impl/common/BaseUserDB.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ Currently logged-in as ${this._username}.`
172172
const id = row.id;
173173
// Delete user progress data but preserve core user documents
174174
return (
175-
id.startsWith(cardHistoryPrefix) || // Card interaction history
176-
id.startsWith(REVIEW_PREFIX) || // Scheduled reviews
175+
id.startsWith(DocTypePrefixes[DocType.CARDRECORD]) || // Card interaction history
176+
id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD]) || // Scheduled reviews
177177
id === BaseUser.DOC_IDS.COURSE_REGISTRATIONS || // Course registrations
178178
id === BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS || // Classroom registrations
179179
id === BaseUser.DOC_IDS.CONFIG // User config
@@ -372,8 +372,11 @@ Currently logged-in as ${this._username}.`
372372
);
373373
return reviews.rows
374374
.filter((r) => {
375-
if (r.id.startsWith(REVIEW_PREFIX)) {
376-
const date = moment.utc(r.id.substr(REVIEW_PREFIX.length), REVIEW_TIME_FORMAT);
375+
if (r.id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD])) {
376+
const date = moment.utc(
377+
r.id.substr(DocTypePrefixes[DocType.SCHEDULED_CARD].length),
378+
REVIEW_TIME_FORMAT
379+
);
377380
if (targetDate.isAfter(date)) {
378381
if (course_id === undefined || r.doc!.courseId === course_id) {
379382
return true;
@@ -814,7 +817,7 @@ Currently logged-in as ${this._username}.`
814817
* @param course_id optional specification of individual course
815818
*/
816819
async getSeenCards(course_id?: string) {
817-
let prefix = cardHistoryPrefix;
820+
let prefix = DocTypePrefixes[DocType.CARDRECORD];
818821
if (course_id) {
819822
prefix += course_id;
820823
}
@@ -824,8 +827,8 @@ Currently logged-in as ${this._username}.`
824827
// const docs = await this.localDB.allDocs({});
825828
const ret: PouchDB.Core.DocumentId[] = [];
826829
docs.rows.forEach((row) => {
827-
if (row.id.startsWith(cardHistoryPrefix)) {
828-
ret.push(row.id.substr(cardHistoryPrefix.length));
830+
if (row.id.startsWith(DocTypePrefixes[DocType.CARDRECORD])) {
831+
ret.push(row.id.substr(DocTypePrefixes[DocType.CARDRECORD].length));
829832
}
830833
});
831834
return ret;
@@ -838,7 +841,7 @@ Currently logged-in as ${this._username}.`
838841
async getHistory() {
839842
const cards = await filterAllDocsByPrefix<CardHistory<CardRecord>>(
840843
this.remoteDB,
841-
cardHistoryPrefix,
844+
DocTypePrefixes[DocType.CARDRECORD],
842845
{
843846
include_docs: true,
844847
attachments: false,

packages/db/src/impl/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type {
1111
} from './types';
1212
export { BaseUser } from './BaseUserDB';
1313
export {
14-
REVIEW_PREFIX,
1514
REVIEW_TIME_FORMAT,
1615
hexEncode,
1716
filterAllDocsByPrefix,

packages/db/src/impl/couch/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ENV } from '@db/factory';
2-
import { DocType, GuestUsername, log, SkuilderCourseData } from '../../core/types/types-legacy';
2+
import {
3+
DocType,
4+
DocTypePrefixes,
5+
GuestUsername,
6+
log,
7+
SkuilderCourseData,
8+
} from '../../core/types/types-legacy';
39
// import { getCurrentUser } from '../../stores/useAuthStore';
410
import moment, { Moment } from 'moment';
511
import { logger } from '@db/util/logger';
@@ -155,7 +161,6 @@ export async function getRandomCards(courseIDs: string[]) {
155161
}
156162
}
157163

158-
export const REVIEW_PREFIX: string = 'card_review_';
159164
export const REVIEW_TIME_FORMAT: string = 'YYYY-MM-DD--kk:mm:ss-SSS';
160165

161166
export function getCouchUserDB(username: string): PouchDB.Database {
@@ -191,7 +196,7 @@ export function scheduleCardReview(review: {
191196
const now = moment.utc();
192197
logger.info(`Scheduling for review in: ${review.time.diff(now, 'h') / 24} days`);
193198
void getCouchUserDB(review.user).put<ScheduledCard>({
194-
_id: REVIEW_PREFIX + review.time.format(REVIEW_TIME_FORMAT),
199+
_id: DocTypePrefixes[DocType.SCHEDULED_CARD] + review.time.format(REVIEW_TIME_FORMAT),
195200
cardId: review.card_id,
196201
reviewTime: review.time,
197202
courseId: review.course_id,

0 commit comments

Comments
 (0)