Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function createUser(args: ICreateUserArgs): Promise> {
validate(args, createUserValidation)
const existingUser: User = await UserModel.findByEmail(args.email)
// If user is not unique, return error
if (existingUser) {
throw new APIError('Email address is already in use', httpStatus.CONFLICT)
}
// If email is unique, create account
let user = new UserModel({
email: args.email.toLowerCase()
})
user = await user.save()
// User has no password yet and must set one in the verification link, sent in the pre-save
return user
}
menu.updated_at = new Date();
const update = await menusch.findByIdAndUpdate(
menu._id,
{
$set: menu,
},
{ new: true },
);
return otherHelper.sendResponse(res, httpStatus.OK, true, update, null, menuConfig.save, null);
} else {
const checkIf = await menusch.findOne({ key: menu.key, is_deleted: false });
if (checkIf) {
const error = { key: 'Key already exists!!' };
return otherHelper.sendResponse(res, httpStatus.CONFLICT, false, null, error, null, null);
}
menu.added_by = req.user.id;
menu.added_at = new Date();
const newMenu = new menusch(menu);
const MenuSave = await newMenu.save();
// const data = await menuControl(req, res, next);
return otherHelper.sendResponse(res, httpStatus.OK, true, MenuSave, null, menuConfig.save, null);
}
} catch (err) {
next(err);
}
};
userController.RegisterFromAdmin = async (req, res, next) => {
try {
const user = await users.findOne({ email: req.body.email, is_deleted: false });
if (user) {
errors.email = 'Email already exists';
const data = { email: req.body.email };
return otherHelper.sendResponse(res, httpStatus.CONFLICT, false, data, errors, errors.email, null);
} else {
if (req.file) {
req.file.destination =
req.file.destination
.split('\\')
.join('/')
.split('server/')[1] + '/';
req.file.path = req.file.path
.split('\\')
.join('/')
.split('server/')[1];
req.body.image = req.file;
}
const { name, email, password, date_of_birth, bio, location, phone, description, is_active, email_verified, roles, image, company_name, company_location, company_established, company_phone_no } = req.body;
const avatar = gravatar.url(email, { s: '200', r: 'pg', d: 'mm' });
const newUser = new User({ name, email, avatar, password, date_of_birth, bio, description, email_verified, is_active, roles, image, location, phone, company_name, company_location, company_established, company_phone_no });
checkDuplicateEmail(error) {
if (error.code === 11000 && (error.name === 'BulkWriteError' || error.name === 'MongoError')) {
return new APIError({
message: 'Validation Error',
errors: [{
field: 'email',
location: 'body',
messages: ['"email" already exists'],
}],
status: httpStatus.CONFLICT,
isPublic: true,
stack: error.stack,
});
}
return error;
},
var _conflict = function(req, err) {
var statusCode = httpStatus.CONFLICT;
var title = 'Conflict';
var message = 'There already exists a \'' + _getMessageProperty(err) +
'\' object with these properties.';
var devMessage = '';
return _error(req, err, statusCode, title, message, devMessage);
};
const numUsersInTenant = await auth0.countUsers(tenant);
const tenantPolicy = await store.getClassTenant(tenant);
if (numUsersInTenant >= tenantPolicy.maxUsers) {
return res.status(httpstatus.CONFLICT)
.json({ error : 'Class already has maximum allowed number of students' });
}
try {
const newstudent = await auth0.createStudent(tenant, req.body.username);
return res.status(httpstatus.CREATED)
.json(newstudent);
}
catch (err) {
if (userAlreadyExists(err)) {
return res.status(httpstatus.CONFLICT).json({ error : 'There is already a student with that username' });
}
log.error({ err }, 'Failed to create student account');
let statusCode = httpstatus.INTERNAL_SERVER_ERROR;
let errObj = { error : 'Failed to create new account' };
if (err.response && err.response.body && err.response.body.statusCode) {
statusCode = err.response.body.statusCode;
}
if (err.error) {
errObj = err.error;
}
return res.status(statusCode).json(errObj);
}
function userAlreadyExists(err: any) {
return err && err.response && err.response.body &&
(
(err.response.body.statusCode === httpstatus.CONFLICT &&
err.response.body.message === 'The user already exists.')
||
(err.response.body.statusCode === httpstatus.BAD_REQUEST &&
err.response.body.message === 'The username provided is in use already.')
);
}
function passwordRejected(err: any) {
exports.mongoErrorHandler = (error) => {
if ((error.name === 'BulkWriteError' || error.name === 'MongoError') && error.code === 11000) {
return new APIError({
message: 'Unique field validation Error',
status: httpStatus.CONFLICT
});
}
if (error.name === 'ValidationError') {
return new APIError({
message: 'Required field not provided',
status: httpStatus.BAD_REQUEST
});
}
if (error.name === 'CastError') {
return new APIError({
message: 'Invalid data sent',
status: httpStatus.BAD_REQUEST
})
}
checkDuplicateEmail(error) {
if (error.name === 'MongoError' && error.code === 11000) {
return new APIError({
message: 'Validation Error',
errors: [{
field: 'email',
location: 'body',
messages: ['"email" already exists'],
}],
status: httpStatus.CONFLICT,
isPublic: true,
stack: error.stack,
});
}
return error;
},
};
registrationValidation.duplicateValidation = async (req, res, next) => {
const filter = { RegistrationNo: req.body.RegistrationNo };
if (req.body._id) {
filter._id = { $ne: req.body._id };
}
let dstatus = await registrationModel.findOne(filter);
if (dstatus != null) {
let errors = { RegistrationNo: registrationConfig.validationMessage.registrationNoexists };
return otherHelper.sendResponse(res, HttpStatus.CONFLICT, false, null, errors, 'Validation Error.', null);
} else {
return next();
}
};