Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
}
if (config.enableCreatedAt) {
for (const document of payload) {
document.createdAt = new Date()
}
}
let data
try {
data = await model.create(payload)
} catch (err) {
Log.error(err)
if (err.code === 11000) {
throw Boom.conflict('There was a duplicate key error.')
} else {
throw Boom.badImplementation(
'There was an error creating the resource.'
)
}
}
// EXPL: rather than returning the raw "create" data, we filter the data through a separate query
const attributes = QueryHelper.createAttributesFilter({}, model, Log)
data = data.map(item => {
return item._id
})
const result = await model
.find()
Log
)
}
if (config.enableUpdatedAt) {
payload.updatedAt = new Date()
}
let result
try {
result = await model.findByIdAndUpdate(_id, payload, {
runValidators: config.enableMongooseRunValidators
})
} catch (err) {
Log.error(err)
if (err.code === 11000) {
throw Boom.conflict('There was a duplicate key error.')
} else {
throw Boom.badImplementation(
'There was an error updating the resource.'
)
}
}
if (result) {
const attributes = QueryHelper.createAttributesFilter({}, model, Log)
result = await model.findOne({ _id: result._id }, attributes).lean()
try {
if (
model.routeOptions &&
model.routeOptions.update &&
model.routeOptions.update.post
const user = await User.findByUsername(request.payload.username);
if (!user) {
throw Boom.notFound('User not found.');
}
if (user.roles.account &&
user.roles.account.id !== request.params.id) {
throw Boom.conflict('User is linked to an account. Unlink first.');
}
if (request.pre.account.user &&
request.pre.account.user.id !== `${user._id}`) {
throw Boom.conflict('Account is linked to a user. Unlink first.');
}
return user;
}
}]
method: async function (request, h) {
const user = await User.findByUsername(request.payload.username);
if (user) {
throw Boom.conflict('Username already in use.');
}
return h.continue;
}
}, {
const user = await User.findByUsername(request.payload.username);
if (!user) {
throw Boom.notFound('User not found.');
}
if (user.roles.admin &&
user.roles.admin.id !== request.params.id) {
throw Boom.conflict('User is linked to an admin. Unlink first.');
}
if (request.pre.admin.user &&
request.pre.admin.user.id !== `${user._id}`) {
throw Boom.conflict('Admin is linked to a user. Unlink first.');
}
return user;
}
}
method: async function (request, h) {
const user = await User.findByEmail(request.payload.email);
if (user) {
throw Boom.conflict('Email already in use.');
}
return h.continue;
}
}
method: async function (request, h) {
const user = await User.findByUsername(request.payload.username);
if (!user) {
throw Boom.notFound('User not found.');
}
if (user.roles.account &&
user.roles.account.id !== request.params.id) {
throw Boom.conflict('User is linked to an account. Unlink first.');
}
if (request.pre.account.user &&
request.pre.account.user.id !== `${user._id}`) {
throw Boom.conflict('Account is linked to a user. Unlink first.');
}
return user;
}
}]
register: async (req, res, next) => {
const { name, email, password } = req.body;
try {
const user = await service.create(name, email, password);
return res.status(201).json({ success: true, user });
} catch (error) {
switch (error.name) {
case 'EmailIsTakenError':
return next(boom.conflict(error.message));
default:
return next(boom.internal(error));
}
}
},
login: async (req, res, next) => {
method: async function (request, h) {
const user = await User.findByUsername(request.payload.username);
if (!user) {
throw Boom.notFound('User not found.');
}
if (user.roles.admin &&
user.roles.admin.id !== request.params.id) {
throw Boom.conflict('User is linked to an admin. Unlink first.');
}
if (request.pre.admin.user &&
request.pre.admin.user.id !== `${user._id}`) {
throw Boom.conflict('Admin is linked to a user. Unlink first.');
}
return user;
}
}