From de0a4f3176adbf65c902d8b01f15ecde9f973618 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Fri, 19 Jan 2018 22:54:50 +0100 Subject: [PATCH] fix reverting skill on failed update --- backend/config/constants.js | 2 +- backend/controllers/skill.js | 86 +++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/backend/config/constants.js b/backend/config/constants.js index c131195..b60f24b 100644 --- a/backend/config/constants.js +++ b/backend/config/constants.js @@ -17,7 +17,7 @@ constants.apiResultCodes = { AMAZON_FAIL:2, //amazon api doesn't work DATABASE_ERROR:3, NO_SKILL:4, - INCONSISTEN_STATE:5, + INCONSISTENT_STATE:5, } constants.HTTPResultCodes = { diff --git a/backend/controllers/skill.js b/backend/controllers/skill.js index 6cc36e7..a0c2f5f 100644 --- a/backend/controllers/skill.js +++ b/backend/controllers/skill.js @@ -41,6 +41,7 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => { .then (() => { //Ok, done, now update skill on Amazon (if needed) if (updateOnAmazon) { + //We need to update skill on Amazon amazonHelper .updateSkill (skill) .then (amazonResult => { @@ -51,45 +52,78 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => { res.json ({result: constants.apiResultCodes.OK, message: ''}); alexa.updateModel (); } else { - res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({ - result: constants.apiResultCodes.AMAZON_ERROR, - message: amazonResult, - }); + //Update on amazon failed, revert changes in database and send error to user + databaseHelper + .updateSkill (id, currentSkillState) + .then (() => { + res + .status ( + constants.HTTPResultCodes.INTERNAL_SERVER_ERROR + ) + .json ({ + result: constants.apiResultCodes.AMAZON_ERROR, + message: amazonResult, + }); + }) + .catch (() => { + //This should never happen, something is seriously wrong, like no database connection + res + .status ( + constants.HTTPResultCodes.INTERNAL_SERVER_ERROR + ) + .json ({ + result: constants.apiResultCodes.INCONSISTENT_STATE, + message: '', + }); + }); } }) .catch (e => { - res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({ - result: constants.apiResultCodes.AMAZON_FAIL, - message: e, - }); + //Update on amazon failed, revert changes in database and send error to user + databaseHelper + .updateSkill (id, currentSkillState) + .then (() => { + res + .status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR) + .json ({ + result: constants.apiResultCodes.AMAZON_FAIL, + message: e, + }); + }) + .catch (() => { + //This should never happen, something is seriously wrong, like no database connection + res + .status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR) + .json ({ + result: constants.apiResultCodes.INCONSISTENT_STATE, + message: '', + }); + }); }); - }else{ + } else { + //No need to update on Amazon, tell to user it's ok res.json ({result: constants.apiResultCodes.OK, message: ''}); alexa.updateModel (); } }) .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 (() => { - //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: '', - }); - }); + //Update in database didn't go well, no need to revert since it failed to write in the first place + //just send error to user + 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.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({result: constants.apiResultCodes.NO_SKILL, message: ''}); + res + .status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR) + .json ({result: constants.apiResultCodes.NO_SKILL, message: ''}); }); });