inconsistent state fix
This commit is contained in:
@@ -13,9 +13,11 @@ constants.amazonResultCodes = {
|
||||
constants.apiResultCodes = {
|
||||
genericError : -1,
|
||||
ok:0,
|
||||
amazonError:1,
|
||||
databaseError:2,
|
||||
IDLengthError:3,
|
||||
amazonError:1, //amazon api works, but error is some of the amazonResultCodes
|
||||
amazonFail:2, //amazon api doesn't work
|
||||
databaseError:3,
|
||||
noSkill:4,
|
||||
inconsistentState:5,
|
||||
}
|
||||
|
||||
constants.skillIDLength = 24;
|
||||
|
||||
@@ -33,60 +33,75 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
|
||||
|
||||
console.log ('id = ' + id);
|
||||
|
||||
//TODO : Fix inconsistency ! If skill is sent to amazon and accepted, but
|
||||
//fails in database (ID doesn't exist)
|
||||
if (id.length === constants.skillIDLength) {
|
||||
if (updateOnAmazon) {
|
||||
amazonHelper
|
||||
.updateSkill (skill)
|
||||
.then (amazonResult => {
|
||||
console.log ('Amazon : ' + amazonResult);
|
||||
if (
|
||||
amazonResult === constants.amazonResultCodes.ok ||
|
||||
amazonResult === constants.amazonResultCodes.accepted
|
||||
) {
|
||||
//Skill uploaded, it's ok to update databaseI
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
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
|
||||
amazonHelper
|
||||
.updateSkill (skill)
|
||||
.then (amazonResult => {
|
||||
if (
|
||||
amazonResult === constants.amazonResultCodes.ok ||
|
||||
amazonResult === constants.amazonResultCodes.accepted
|
||||
) {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
} else {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.amazonError,
|
||||
message: amazonResult,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.amazonFail,
|
||||
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.amazonError,
|
||||
message: amazonResult,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.amazonError,
|
||||
message: 'unknown',
|
||||
});
|
||||
});
|
||||
} 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 (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 {
|
||||
res.json ({result: constants.IDLengthError, message: ''});
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.databaseError,
|
||||
message: '',
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user