Skip to content

Commit 253f617

Browse files
committed
use CourseLookup
1 parent 83713a5 commit 253f617

File tree

1 file changed

+20
-45
lines changed

1 file changed

+20
-45
lines changed

packages/express/src/client-requests/course-requests.ts

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AsyncProcessQueue from '../utils/processQueue.js';
77
import nano from 'nano';
88
import { Status } from '@vue-skuilder/common';
99
import logger from '@/logger.js';
10+
import { CourseLookup } from '@vue-skuilder/db';
1011

1112
/**
1213
* Fake fcn to allow usage in couchdb map fcns which, after passing
@@ -16,13 +17,6 @@ function emit(key?: unknown, value?: unknown) {
1617
return [key, value];
1718
}
1819

19-
export const COURSE_DB_LOOKUP = 'coursedb-lookup';
20-
const courseHasher = new hashids(
21-
COURSE_DB_LOOKUP,
22-
6,
23-
'abcdefghijkmnopqrstuvwxyz23456789'
24-
);
25-
2620
function getCourseDBName(courseID: string): string {
2721
return `coursedb-${courseID}`;
2822
}
@@ -161,45 +155,20 @@ const courseDBDesignDocs: { _id: string }[] = [
161155
];
162156

163157
export async function initCourseDBDesignDocInsert(): Promise<void> {
164-
const lookup = await useOrCreateDB(COURSE_DB_LOOKUP);
165-
lookup.list((err, body) => {
166-
if (!err) {
167-
body.rows.forEach((courseDoc) => {
168-
courseDBDesignDocs.forEach((dDoc) => {
169-
insertDesignDoc(getCourseDBName(courseDoc.id), dDoc);
170-
});
171-
});
172-
}
158+
const courses = await CourseLookup.allCourses();
159+
courses.forEach((c) => {
160+
courseDBDesignDocs.forEach((dd) => {
161+
insertDesignDoc(getCourseDBName(c._id), dd);
162+
});
173163
});
174164
}
175165

176166
type CourseConfig = CreateCourse['data'];
177167

178168
async function createCourse(cfg: CourseConfig): Promise<any> {
179-
const lookup = await useOrCreateDB(COURSE_DB_LOOKUP);
180-
181-
const lookupInsert = await lookup.insert({
182-
...cfg,
183-
} as nano.MaybeDocument);
184-
185-
if (!lookupInsert.ok) {
186-
return {
187-
courseID: '',
188-
ok: false,
189-
status: Status.error,
190-
};
191-
}
169+
cfg.courseID = await CourseLookup.add(cfg.name);
192170

193-
const courseID = lookupInsert.id;
194-
195-
cfg.courseID = courseID;
196-
await lookup.insert({
197-
...cfg,
198-
_id: lookupInsert.id,
199-
_rev: lookupInsert.rev,
200-
} as nano.MaybeDocument);
201-
202-
const courseDBName: string = getCourseDBName(courseID);
171+
const courseDBName: string = getCourseDBName(cfg.courseID);
203172
const dbCreation = await CouchDB.db.create(courseDBName);
204173

205174
if (dbCreation.ok) {
@@ -211,13 +180,19 @@ async function createCourse(cfg: CourseConfig): Promise<any> {
211180
...cfg,
212181
})
213182
.catch((e) => {
214-
logger.error(`Error inserting CourseConfig for course ${courseID}:`, e);
183+
logger.error(
184+
`Error inserting CourseConfig for course ${cfg.courseID}:`,
185+
e
186+
);
215187
});
216188

217189
// insert the tags, elo, etc view docs
218190
courseDBDesignDocs.forEach((doc) => {
219191
courseDB.insert(doc).catch((e) => {
220-
logger.error(`Error inserting design doc for course ${courseID}:`, e);
192+
logger.error(
193+
`Error inserting design doc for course ${cfg.courseID}:`,
194+
e
195+
);
221196
});
222197
});
223198

@@ -235,7 +210,7 @@ async function createCourse(cfg: CourseConfig): Promise<any> {
235210

236211
courseDB.insert(secObj as nano.MaybeDocument, '_security').catch((e) => {
237212
logger.error(
238-
`Error inserting security object for course ${courseID}:`,
213+
`Error inserting security object for course ${cfg.courseID}:`,
239214
e
240215
);
241216
});
@@ -244,12 +219,12 @@ async function createCourse(cfg: CourseConfig): Promise<any> {
244219

245220
// follow the course so that user-uploaded content goes through
246221
// post-processing
247-
postProcessCourse(courseID);
222+
postProcessCourse(cfg.courseID);
248223

249224
return {
250-
ok: lookupInsert.ok && dbCreation.ok,
225+
ok: dbCreation.ok,
251226
status: 'ok',
252-
courseID: courseID,
227+
courseID: cfg.courseID,
253228
};
254229
}
255230

0 commit comments

Comments
 (0)