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