Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async createNotificationSettings(@Body() data: any) {
if (!data.user || !data.course) {
throw new BadRequestError('NotificationSettings need course and user');
}
const notificationSettings: INotificationSettingsModel =
await NotificationSettings.findOne({'user': data.user, 'course': data.course});
if (notificationSettings) {
throw new BadRequestError('NotificationSettings for user:' + data.user + ' with course: ' + data.course + ' already exist');
}
const settings: INotificationSettingsModel = await new NotificationSettings({
'user': data.user,
'course': data.course,
'notificationType': API_NOTIFICATION_TYPE_ALL_CHANGES,
'emailNotification': false
}).save();
return settings.toObject();
}
async createAdministrator(@BodyParam('username', {required: true}) username: string,
@BodyParam('password', {required: true}) password: string
) {
if (Environment.identity === 'development') {
if (await this.userService.userModel.count({username: username})) {
throw new BadRequestError('重复的用户名')
} else {
return await this.userService.userModel.create({
username: username,
password: password,
usertype: 1
})
}
} else {
throw new BadRequestError()
}
}
function validateTestArea(testArea: any) {
if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*}', 'gmi'))) {
throw new BadRequestError('The test section must contain a validate function');
}
if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*return(.|\n|\t)*}', 'gmi'))) {
throw new BadRequestError('The validate function must return something');
}
if (!testArea.match(new RegExp('validate\\(\\);', 'gmi'))) {
throw new BadRequestError('The test section must call the validate function');
}
return true;
}
protected checkPostParam(data: any) {
if (!data.lectureId) {
throw new BadRequestError('No lecture ID was submitted.');
}
if (!data.model) {
throw new BadRequestError('No unit was submitted.');
}
if (!data.model._course) {
throw new BadRequestError('Unit has no _course set');
}
}
}
async updateStudentConfig(@Param('id') id: string, @Body() config: IStudentConfig, @CurrentUser() currentUser: IUser) {
if (currentUser._id !== config.user) {
throw new BadRequestError('Student id does not match');
}
const newConfig = await StudentConfig.findByIdAndUpdate(id, config, {'new': false});
return newConfig.toObject({virtuals: true});
}
.then((existingCourse) => {
if (existingCourse) {
throw new BadRequestError(errorCodes.errorCodes.course.duplicateName.code);
}
return new Course(course).save()
.then((c) => c.toObject());
});
}
async postUnitProgress(@Body() config: IStudentConfig, @CurrentUser() currentUser: IUser) {
const result = await StudentConfig.findOne({'user': config.user});
if (result) {
throw new BadRequestError('Student config already existed');
}
if (currentUser._id !== config.user) {
throw new BadRequestError('Student id does not match');
}
const newConfig = await new StudentConfig(config).save();
return newConfig.toObject();
}
async updateWhitelistUser(@Param('id') id: string, @Body() whitelistUser: IWhitelistUser) {
let updatedWhitelistUser;
const foundWhitelistUser = await WhitelistUser.findById(id);
try {
updatedWhitelistUser = await WhitelistUser.findOneAndUpdate(
this.toMongooseObjectId(whitelistUser),
{'new': true});
} catch (err) {
throw new BadRequestError(errorCodes.whitelist.duplicateWhitelistUser.text);
}
await this.deleteUserIfFound(foundWhitelistUser);
await this.addUserIfFound(updatedWhitelistUser);
return updatedWhitelistUser ? updatedWhitelistUser.toObject() : undefined;
}
async createNotificationForStudent(@Param('id') userId: string, @Body() data: any) {
if (!data.changedCourse && !data.text) {
throw new BadRequestError('Notification needs at least the field changedCourse or text');
}
const user = await User.findById(userId);
if (!user) {
throw new BadRequestError('Could not create notification because user not found');
}
await this.createNotification(user, data.text, data.changedCourse, data.changedLecture, data.changedUnit);
return {notified: true};
}
function validateTestArea(testArea: any) {
if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*}', 'gmi'))) {
throw new BadRequestError('The test section must contain a validate function');
}
if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*return(.|\n|\t)*}', 'gmi'))) {
throw new BadRequestError('The validate function must return something');
}
if (!testArea.match(new RegExp('validate\\(\\);', 'gmi'))) {
throw new BadRequestError('The test section must call the validate function');
}
return true;
}