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 UsersService
.verifyPasswordResetToken(token) .verifyPasswordResetToken(token)
.then(user => { .then((user) => {
return UsersService.changePassword(user.id, password); return UsersService.changePassword(user.id, password);
}) })
.then(() => { .then(() => {
res.status(204).end(); res.status(204).end();
}) })
.catch(error => { .catch(() => {
console.error(error);
next(authorization.ErrNotAuthorized); next(authorization.ErrNotAuthorized);
}); });
}); });
@@ -121,12 +119,8 @@ router.put('/username', authorization.needed(), (req, res, next) => {
.then(() => { .then(() => {
res.status(204).end(); res.status(204).end();
}) })
.catch(error => { .catch((err) => {
if (error.code === 11000) { next(err);
next(errors.ErrUsernameTaken);
} else {
next(error);
}
}); });
}); });
+16 -9
View File
@@ -79,7 +79,8 @@ router.get('/:asset_id', (req, res, next) => {
.findById(req.params.asset_id) .findById(req.params.asset_id)
.then((asset) => { .then((asset) => {
if (!asset) { if (!asset) {
return res.status(404).end(); res.status(404).end();
return;
} }
res.json(asset); res.json(asset);
@@ -97,15 +98,17 @@ router.post('/:asset_id/scrape', (req, res, next) => {
.findById(req.params.asset_id) .findById(req.params.asset_id)
.then((asset) => { .then((asset) => {
if (!asset) { if (!asset) {
return res.status(404).end(); res.status(404).end();
return;
} }
return scraper.create(asset); return scraper
}) .create(asset)
.then((job) => { .then((job) => {
// Send the job back for monitoring. // Send the job back for monitoring.
res.status(201).json(job); res.status(201).json(job);
});
}) })
.catch((err) => { .catch((err) => {
next(err); next(err);
@@ -117,8 +120,12 @@ router.put('/:asset_id/settings', (req, res, next) => {
// Override the settings for the asset. // Override the settings for the asset.
AssetsService AssetsService
.overrideSettings(req.params.asset_id, req.body) .overrideSettings(req.params.asset_id, req.body)
.then(() => res.status(204).end()) .then(() => {
.catch((err) => next(err)); res.status(204).end();
})
.catch((err) => {
next(err);
});
}); });
router.put('/:asset_id/status', (req, res, next) => { 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) => { router.get('/', (req, res, next) => {
SettingsService SettingsService
.retrieve().then((settings) => { .retrieve()
.then((settings) => {
res.json(settings); res.json(settings);
}) })
.catch((err) => { .catch((err) => {
+1 -1
View File
@@ -43,7 +43,7 @@ router.post('/', (req, res, next) => {
res.status(204).end(); res.status(204).end();
}) })
.catch((err) => { .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) => { .then((action) => {
// Set the user status to "pending" for review by moderators // 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') return UsersService.setStatus(req.params.user_id, 'PENDING')
.then(() => action); .then(() => action);
} else { } else {
+14 -3
View File
@@ -686,9 +686,20 @@ module.exports = class UsersService {
canEditName: false, canEditName: false,
status: 'PENDING' status: 'PENDING'
} }
}).then((result) => { })
return result.nModified > 0 ? result : .then((result) => {
Promise.reject(errors.ErrPermissionUpdateUsername); if (result.nModified <= 0) {
return Promise.reject(errors.ErrPermissionUpdateUsername);
}
return result;
})
.catch((err) => {
if (err.code === 11000) {
throw errors.ErrUsernameTaken;
}
throw err;
}); });
} }
}; };