code improvements
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
var config = {};
|
||||
|
||||
config.dbURL = 'mongodb://localhost:27017/tellall';
|
||||
config.DB_URL = 'mongodb://localhost:27017/tellall';
|
||||
config.PORT = 5000;
|
||||
|
||||
config.TOKEN = 'Atza|IwEBIBe6gDqrrowEEav6N-_6s4NztYeP3oG8PGWmu8ZiZw6lbOh3wNla3TK6pY-VEpT1d8an-dVf_n3kXJzVFsNo_4xBfZyFHGoCTDTFjs3yBRul4PVdBOhwwiH3-sgRLcUofZbe2oE06GmTcbfYtaStfXpQI5dfpldfnsJg_CvhSA6AHb_snJT3F6lyXzbV076d_3cYUMJxFldJGnYcviNHHxjjmuQTD06hhGzCbAxxe9eBmkuopRsNfyedLT2UlKP_ublah9CUGA3AdIX_3Iuke82jMwGnNl9gv7pbaDNEjAbj7IQSl3B08uuREtJq-oTBOjALNXRvFxTJmQjZwXNf9eHC7fSHJDdEPdZQU0AcffRQObAyAkUuL6Jv39OHzhb3Q64-zzoyODqnJyLP5SQZ2JVF53Kc_cTBqjIc9pXljqe7yEVk6JDs7q1zKbBibx_AQm57TO79IzWyLBzBMlYL5HdTsqEfRzLeDw2tws-hGMgkx2HWfdbYnmf5Qb4SyIhzvmmdfPLg3MVKTxjIBu1rx0xf3n0PLZP1EO6jsJPoMRPg77Gm4oit5Zp6s37ek3A3Vxh-ntoASpkrkxGTG9kVtRNt';
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
const constants = {};
|
||||
|
||||
constants.amazonResultCodes = {
|
||||
ok:200,
|
||||
accepted:202,
|
||||
badRequest:400,
|
||||
unauthorized:401,
|
||||
notFound:404,
|
||||
conflict:409,
|
||||
payloadTooLarge:413
|
||||
OK:200,
|
||||
ACCEPTED:202,
|
||||
BAD_REQUEST:400,
|
||||
UNAUTHORIZED:401,
|
||||
NOT_FOUND:404,
|
||||
CONFLICT:409,
|
||||
PAYLOAD_TOO_LARGE:413
|
||||
}
|
||||
|
||||
constants.apiResultCodes = {
|
||||
genericError : -1,
|
||||
ok:0,
|
||||
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,
|
||||
GENERIC_ERROR : -1,
|
||||
OK:0,
|
||||
AMAZON_ERROR:1, //amazon api works, but error is some of the amazonResultCodes
|
||||
AMAZON_FAIL:2, //amazon api doesn't work
|
||||
DATABASE_ERROR:3,
|
||||
NO_SKILL:4,
|
||||
INCONSISTEN_STATE:5,
|
||||
}
|
||||
|
||||
constants.skillIDLength = 24;
|
||||
constants.HTTPResultCodes = {
|
||||
INTERNAL_SERVER_ERROR : 500,
|
||||
}
|
||||
|
||||
constants.SKILL_ID_LENGTH = 24;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -20,7 +20,7 @@ app.set ('view engine', 'ejs');
|
||||
app.use (require ('./middleware')); //common middleware for all requests
|
||||
app.use (require ('./controllers')); //all routes
|
||||
|
||||
MongoClient.connect (config.dbURL)
|
||||
MongoClient.connect (config.DB_URL)
|
||||
.then (database => {
|
||||
databaseHelper.initModule (database);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user