Files

146 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

let express = require ('express'), router = express.Router ();
2018-01-13 14:57:41 +01:00
const constants = require ('../config/constants');
let databaseHelper = require ('../helpers/database');
let amazonHelper = require ('../helpers/amazon');
let skillValidator = require('../helpers/skillValidator');
let bodyParser = require ('body-parser');
let alexa = require ('../models/alexa');
2018-01-13 14:57:41 +01:00
router.get ('/:id', async (req, res, next) => {
const id = req.params.id;
2018-01-16 16:56:56 +01:00
if (id.length !== constants.SKILL_ID_LENGTH) {
2018-01-13 14:57:41 +01:00
res.json ([]);
} else {
databaseHelper
.getSkill (id)
.then (result => {
res.json (result);
})
.catch (err => {
res.json ([]);
});
}
});
2018-01-15 16:06:37 +01:00
router.put ('/:id', bodyParser.json (), async (req, res, next) => {
2018-01-13 14:57:41 +01:00
let id = req.params.id;
let dataFromWeb = JSON.stringify (req.body);
let skill = JSON.parse (dataFromWeb);
let updateOnAmazon = skill.updateOnAmazon;
delete skill.updateOnAmazon;
delete skill._id;
2018-01-29 23:23:36 +01:00
//Validate skill
if (!skillValidator.validateSkill(skill)){
//skill not valid
res
.status (
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
)
.json ({
result: constants.apiResultCodes.INVALID_SKILL,
message: '',
});
return;
}
2018-01-16 16:56:56 +01:00
//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) {
2018-01-19 22:54:50 +01:00
//We need to update skill on Amazon
2018-01-16 00:40:03 +01:00
amazonHelper
.updateSkill (skill)
.then (amazonResult => {
if (
2018-01-16 16:56:56 +01:00
amazonResult === constants.amazonResultCodes.OK ||
amazonResult === constants.amazonResultCodes.ACCEPTED
2018-01-16 00:40:03 +01:00
) {
2018-01-16 16:56:56 +01:00
res.json ({result: constants.apiResultCodes.OK, message: ''});
2018-01-18 21:33:44 +01:00
alexa.updateModel ();
2018-01-16 00:40:03 +01:00
} else {
2018-01-19 22:54:50 +01:00
//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: '',
});
});
2018-01-16 00:40:03 +01:00
}
2018-01-13 14:57:41 +01:00
})
.catch (e => {
2018-01-19 22:54:50 +01:00
//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: '',
});
});
2018-01-16 00:40:03 +01:00
});
2018-01-19 22:54:50 +01:00
} else {
//No need to update on Amazon, tell to user it's ok
2018-01-16 16:56:56 +01:00
res.json ({result: constants.apiResultCodes.OK, message: ''});
2018-01-18 21:33:44 +01:00
alexa.updateModel ();
2018-01-16 16:56:56 +01:00
}
})
.catch (() => {
2018-01-19 22:54:50 +01:00
//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: '',
});
2018-01-16 00:40:03 +01:00
});
2018-01-16 16:56:56 +01:00
})
.catch (e => {
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
2018-01-19 22:54:50 +01:00
res
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
.json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
2018-01-16 16:56:56 +01:00
});
2018-01-13 14:57:41 +01:00
});
module.exports = router;