code improvements

This commit is contained in:
GotPPay
2018-01-16 16:56:56 +01:00
parent 0607f1e98d
commit cd041c0131
4 changed files with 65 additions and 73 deletions

View File

@@ -8,7 +8,7 @@ var alexa = require ('../components/alexa');
router.get ('/:id', async (req, res, next) => {
const id = req.params.id;
if (id.length !== constants.skillIDLength) {
if (id.length !== constants.SKILL_ID_LENGTH) {
res.json ([]);
} else {
databaseHelper
@@ -31,78 +31,66 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
delete skill.updateOnAmazon;
delete skill._id;
console.log ('id = ' + id);
if (updateOnAmazon) {
//First get current skill from DB
databaseHelper
.getSkill (id)
.then (skillInDB => {
//Now let's update skill in DB
databaseHelper
.updateSkill (id, skill)
.then (() => {
//Ok, done, now update skill on Amazon
//First get current skill from DB
databaseHelper
.getSkill (id)
.then (currentSkillState => {
//Now let's update skill in DB
databaseHelper
.updateSkill (id, skill)
.then (() => {
//Ok, done, now update skill on Amazon (if needed)
if (updateOnAmazon) {
amazonHelper
.updateSkill (skill)
.then (amazonResult => {
if (
amazonResult === constants.amazonResultCodes.ok ||
amazonResult === constants.amazonResultCodes.accepted
amazonResult === constants.amazonResultCodes.OK ||
amazonResult === constants.amazonResultCodes.ACCEPTED
) {
res.json ({result: constants.apiResultCodes.ok, message: ''});
res.json ({result: constants.apiResultCodes.OK, message: ''});
alexa.updateIntentsJSON ();
} else {
res.json ({
result: constants.apiResultCodes.amazonError,
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.AMAZON_ERROR,
message: amazonResult,
});
}
})
.catch (e => {
res.json ({
result: constants.apiResultCodes.amazonFail,
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.AMAZON_FAIL,
message: e,
});
});
})
.catch (() => {
//Update in database didn't go well, revert changes
databaseHelper
.updateSkill (id, skillInDB)
.then (() => {
res.json ({
result: constants.apiResultCodes.databaseError,
message: '',
});
})
.catch (() => {
//This should never happen, something is seriously wrong, like no database connection
res.json ({
result: constants.apiResultCodes.inconsistentState,
message: '',
});
}else{
res.json ({result: constants.apiResultCodes.OK, message: ''});
alexa.updateIntentsJSON ();
}
})
.catch (() => {
//Update in database didn't go well, revert changes
databaseHelper
.updateSkill (id, currentSkillState)
.then (() => {
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.DATABASE_ERROR,
message: '',
});
});
})
.catch (e => {
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
res.json ({result: constants.apiResultCodes.noSkill, message: ''});
});
} else {
databaseHelper
.updateSkill (id, skill)
.then (result => {
res.json ({result: constants.apiResultCodes.ok, message: ''});
alexa.updateIntentsJSON ();
})
.catch (e => {
res.json ({
result: constants.apiResultCodes.databaseError,
message: '',
})
.catch (() => {
//This should never happen, something is seriously wrong, like no database connection
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.INCONSISTEN_STATE,
message: '',
});
});
});
});
}
})
.catch (e => {
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
});
});
module.exports = router;