Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async deleteAddress(userObj, addressId) {
let result = {};
try {
// find the user first
let user = await User.findOne({email: userObj.email}).exec();
// If user is not authenticated or the user does not exist in system - stop
// If so do not continue further
if (!user) {
result = {httpStatus: httpStatus.UNAUTHORIZED, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.UNAUTHORIZED)};
return result;
}
// Remove the requested address from user
user = await User.findOneAndUpdate({'addresses._id': addressId},
{$pull: { addresses: { _id: addressId}}},
{new: true}
).exec()
// Return the proper response depending on whether save was successful
result = user ? {httpStatus: httpStatus.OK, status: "successful", responseData: user.addresses} : {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.BAD_REQUEST)};
return result;
}
catch(err) {
logger.error("Error in deleteAddress Service", {meta: err});
result = {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: err};
let token = await cryptoGen.generateRandomToken();
// If an associated user with the email wasn't found, and a token not generated, stop here
if (!(user && token)) {
result = {httpStatus: httpStatus.NOT_FOUND, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.NOT_FOUND)};
return result;
}
// Generate password reset token and save it
user.passwordResetToken = token;
user.passwordResetExpires = Date.now() + config.get('token_validity.password_reset_token_valid_for');
user = await user.save();
// If the user was not properly saved, stop here and return failure
if (!user) {
result = {httpStatus: httpStatus.INTERNAL_SERVER_ERROR, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.INTERNAL_SERVER_ERROR)};
return result;
}
// If we have gotten this far, return success
emailService.emailPasswordResetInstructions(user.email, user.name, user.passwordResetToken);
result = {httpStatus: httpStatus.OK, status: "successful", responseData: true};
return result;
}
catch(err) {
logger.error("Error in forgotPassword Service", {meta: err});
result = {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: err};
return result;
}
},
async getCart(userObj, returnAllProductProps=false, save=true) {
let result = {};
try {
let user;
// find the user first and populate while searching
if (returnAllProductProps) {
user = await User.findOne({email: userObj.email}).populate({path: 'cart.items.product', select: '-__v', populate: [{path: 'category', select: '_id category subcategory'}, {path: 'tariff', select: '_id name rates'}]}).exec();
}
else {
user = await User.findOne({email: userObj.email}).populate('cart.items.product', '_id name brand store weight price thumbnailUrls').exec();
}
// If the user is not found, its most likely they are not authenticated and don't have user info under session
if (!user) {
result = {httpStatus: httpStatus.UNAUTHORIZED, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.UNAUTHORIZED)};
return result;
}
// If the user doesn't have a cart set up yet, initialize it
if (!user.cart) {
user.cart = this.returnFreshInitializedCart();
result = { httpStatus: httpStatus.OK, status: "successful", responseData: user.cart };
return result;
};
// Remove any existing items from cart which were not found in catalog during population
_.remove(user.cart.items, (item) => {
// Returns truthy if product does not have a truthy value
return !item.product;
})
app.use((err, req, res, next) => {
console.error(req.path)
console.error(err.stack)
const status = err.statusCode || 500
res
.status(status)
.send(status === 500 ? HttpStatus.getStatusText(status) : err.message)
})
function createError (code, msg) {
const err = new Error(msg || HttpStatus.getStatusText(code))
err.statusCode = code
return err
}
router.get('/:deviceId/off', (req, res) => {
const id = req.params.deviceId;
const state = store.getState();
if (!state[id]) {
return res.status(httpStatus.NOT_FOUND).send(httpStatus.getStatusText(httpStatus.NOT_FOUND));
}
store.dispatch(turnOff(id));
res.send(`DEVICE WITH ID: ${id} TURNED OFF`);
});
constructor(message?: string, public readonly statusCode: number = INTERNAL_SERVER_ERROR) {
super();
this.statusCodeText = getStatusText(statusCode);
this.message = message || getStatusText(statusCode);
}
}
return {
code: err.output.statusCode,
message: err.output.payload.message || err.output.payload.error
};
}
if (err.isCustom) {
return {
code: err.statusCode,
message: err.message
};
}
return {
code: HttpStatus.INTERNAL_SERVER_ERROR,
message: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR)
};
}