Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public error(error: Error, request: Request, response: Response, next: NextFunction) {
// in case the controllers throw invalid http error
if (!(error instanceof HttpError)) {
const internalError = new InternalServerError('Internal Server Error');
return response.status(500).send(internalError);
}
return response.send(error);
}
}
}
}
if (user.role !== 'teacher' && user.role !== 'student') {
throw new BadRequestError('You can only sign up as student or teacher');
}
if (user.role === 'teacher' && (typeof user.email !== 'string' || !user.email.match(config.teacherMailRegex))) {
throw new BadRequestError(errorCodes.errorCodes.mail.noTeacher.code);
}
const newUser = new User(user);
const savedUser = await newUser.save();
// User can now match a whitelist.
await this.addWhitelistedUserToCourses(savedUser);
try {
emailService.sendActivation(savedUser);
} catch (err) {
throw new InternalServerError(errorCodes.errorCodes.mail.notSend.code);
}
}
private static getClassMappingForUnit(unit: IUnit): any {
const hasNoProgressClass = Object.keys(this.classMappings).indexOf(unit.type) === -1 ;
if (hasNoProgressClass) {
throw new InternalServerError(`No classmapping for type ${unit.type} available`);
}
return this.classMappings[unit.type];
}
async duplicateUnit(@Param('id') id: string, @Body() data: any, @Req() request: Request) {
const courseId = data.courseId;
const lectureId = data.lectureId;
try {
const unitModel: IUnitModel = await Unit.findById(id);
const exportedUnit: IUnit = await unitModel.exportJSON();
return Unit.schema.statics.importJSON(exportedUnit, courseId, lectureId);
} catch (err) {
const newError = new InternalServerError('Failed to duplicate unit');
newError.stack += '\nCaused by: ' + err.message + '\n' + err.stack;
throw newError;
}
}
try {
const savedLecture = await new Lecture(lecture).save();
const course = await Course.findById(courseId);
course.lectures.push(savedLecture);
await course.save();
for (const unit of units) {
const unitTypeClass = UnitClassMapper.getMongooseClassForUnit(unit);
await unitTypeClass.schema.statics.importJSON(unit, courseId, savedLecture._id);
}
const newLecture: ILectureModel = await Lecture.findById(savedLecture._id);
return newLecture.toObject();
} catch (err) {
const newError = new InternalServerError('Failed to import lecture');
newError.stack += '\nCaused by: ' + err.message + '\n' + err.stack;
throw newError;
}
};
public getByDate(@Param('date') date: string) {
try {
validateDate(date);
return this.getData(date);
} catch (error) {
logger.error(error);
return new InternalServerError(error.message);
}
}
}
.catch((err) => {
throw new InternalServerError('Could not send E-Mail');
});
}
.catch((err: Error) => {
const newError = new InternalServerError('Failed to import task');
newError.stack += '\nCaused by: ' + err.message + '\n' + err.stack;
throw newError;
});
}
throw new HttpError(503, errorCodes.errorCodes.user.retryAfter.code);
}
const existingUser = await User.findOne({email: email});
if (existingUser && existingUser.uid !== uid) {
throw new BadRequestError(errorCodes.errorCodes.mail.duplicate.code);
}
user.authenticationToken = undefined;
user.email = email;
const savedUser = await user.save();
try {
await emailService.resendActivation(savedUser);
} catch (err) {
throw new InternalServerError(err.toString());
}
}
async duplicateLecture(@Param('id') id: string, @Body() data: any, @Req() request: Request) {
const courseId = data.courseId;
try {
const lectureModel: ILectureModel = await Lecture.findById(id);
const exportedLecture: ILecture = await lectureModel.exportJSON();
return Lecture.schema.statics.importJSON(exportedLecture, courseId);
} catch (err) {
const newError = new InternalServerError('Failed to duplicate lecture');
newError.stack += '\nCaused by: ' + err.message + '\n' + err.stack;
throw newError;
}
}