Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-stephenson-/e0200233663b2cf1ce1d0a5a33559372 to your computer and use it in GitHub Desktop.
Save mark-stephenson-/e0200233663b2cf1ce1d0a5a33559372 to your computer and use it in GitHub Desktop.
Wit, undefined .then
Index.js (custom action)
-------------------------
activate_schedule({sessionId, context, entities}) {
return new Promise(function(resolve, reject) {
LevelUp.set_activation_state(sessions[sessionId].fbid, true)
.then(user_schedule => {
console.log(user_schedule);
// if(user_schedule.schedule_on){
// context.schedule_active= true;
// delete context.schedule_inactive;
// }else {
// delete context.schedule_active;
// context.schedule_inactive = true;
// }
return resolve(context);
}
);
});
#LevelUp.js
-----------
LevelUp.prototype.get_user = function (fbid) {
return fetch(config.LUP_API_ENDPOINT + '/users?fb_id=' + fbid, {
method: 'GET',
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json',
'Authorization' : 'Token token=' + config.LUP_API_TOKEN
}
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
LevelUp.prototype.set_activation_state = function (fbid, state) {
this.get_user(fbid)
.then(user_profile => {
const body = JSON.stringify({schedule:{schedule_on: state}});
return fetch(config.LUP_API_ENDPOINT + '/schedules/' + user_profile.user.schedule.id, {
method: 'PUT',
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json',
'Authorization' : 'Token token=' + config.LUP_API_TOKEN
},
body
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
console.log('set_activation_state', json);
return json;
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment