Cleaned up some routes

This commit is contained in:
Wyatt Johnson
2017-02-21 15:24:44 -07:00
parent 17813e23a9
commit d7a6bf9e1f
6 changed files with 38 additions and 25 deletions
+4 -10
View File
@@ -102,15 +102,13 @@ router.put('/password/reset', (req, res, next) => {
UsersService
.verifyPasswordResetToken(token)
.then(user => {
.then((user) => {
return UsersService.changePassword(user.id, password);
})
.then(() => {
res.status(204).end();
})
.catch(error => {
console.error(error);
.catch(() => {
next(authorization.ErrNotAuthorized);
});
});
@@ -121,12 +119,8 @@ router.put('/username', authorization.needed(), (req, res, next) => {
.then(() => {
res.status(204).end();
})
.catch(error => {
if (error.code === 11000) {
next(errors.ErrUsernameTaken);
} else {
next(error);
}
.catch((err) => {
next(err);
});
});
+16 -9
View File
@@ -79,7 +79,8 @@ router.get('/:asset_id', (req, res, next) => {
.findById(req.params.asset_id)
.then((asset) => {
if (!asset) {
return res.status(404).end();
res.status(404).end();
return;
}
res.json(asset);
@@ -97,15 +98,17 @@ router.post('/:asset_id/scrape', (req, res, next) => {
.findById(req.params.asset_id)
.then((asset) => {
if (!asset) {
return res.status(404).end();
res.status(404).end();
return;
}
return scraper.create(asset);
})
.then((job) => {
return scraper
.create(asset)
.then((job) => {
// Send the job back for monitoring.
res.status(201).json(job);
// Send the job back for monitoring.
res.status(201).json(job);
});
})
.catch((err) => {
next(err);
@@ -117,8 +120,12 @@ router.put('/:asset_id/settings', (req, res, next) => {
// Override the settings for the asset.
AssetsService
.overrideSettings(req.params.asset_id, req.body)
.then(() => res.status(204).end())
.catch((err) => next(err));
.then(() => {
res.status(204).end();
})
.catch((err) => {
next(err);
});
});
router.put('/:asset_id/status', (req, res, next) => {
+2 -1
View File
@@ -5,7 +5,8 @@ const router = express.Router();
router.get('/', (req, res, next) => {
SettingsService
.retrieve().then((settings) => {
.retrieve()
.then((settings) => {
res.json(settings);
})
.catch((err) => {
+1 -1
View File
@@ -43,7 +43,7 @@ router.post('/', (req, res, next) => {
res.status(204).end();
})
.catch((err) => {
return next(err);
next(err);
});
});
+1 -1
View File
@@ -152,7 +152,7 @@ router.post('/:user_id/actions', authorization.needed(), (req, res, next) => {
.then((action) => {
// Set the user status to "pending" for review by moderators
if (action_type.slice(0, 4) === 'FLAG') {
if (action_type === 'FLAG') {
return UsersService.setStatus(req.params.user_id, 'PENDING')
.then(() => action);
} else {
+14 -3
View File
@@ -686,9 +686,20 @@ module.exports = class UsersService {
canEditName: false,
status: 'PENDING'
}
}).then((result) => {
return result.nModified > 0 ? result :
Promise.reject(errors.ErrPermissionUpdateUsername);
})
.then((result) => {
if (result.nModified <= 0) {
return Promise.reject(errors.ErrPermissionUpdateUsername);
}
return result;
})
.catch((err) => {
if (err.code === 11000) {
throw errors.ErrUsernameTaken;
}
throw err;
});
}
};