Created
January 11, 2018 10:15
-
-
Save shmuelgutman/4ed310d9faa8cf5464e7dd4b0dde47f7 to your computer and use it in GitHub Desktop.
Simple demostration to create reource and project in timkekit.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
config = require('config'), | |
axios = require('axios'); | |
const request = axios.create({ | |
baseURL: 'https://api.timekit.io/v2/', | |
headers: {'Timekit-App': config.get('timekit.master_app.slug')}, | |
auth: {username: config.get('timekit.username'), password: config.get('timekit.token')} | |
}); | |
var timekit = { | |
createResource(email, name){ | |
return request.post('resources', { | |
email: email, | |
name: name, | |
timezone: config.get('timekit.default_timezone') | |
}) | |
.then(({data}) => data.data); | |
}, | |
createWidgetToken(resource, name){ | |
return request.post('credentials', { | |
type: 'client-token', | |
scopes: 'widget', | |
description: 'BookingJS for ' + name, | |
}, {auth: {username: resource.email, password: resource.api_token}}) | |
.then(({data}) => data.data); | |
}, | |
createProject(name, slug, resource){ | |
return request.post('projects', { | |
name: name, | |
slug: slug, | |
config: { | |
calendar: resource.calendars[0].id, | |
email: resource.email, | |
apiToken: resource.api_token, | |
app: config.get('timekit.master_app.slug'), | |
name: '' | |
} | |
}, {auth: {username: resource.email, password: resource.api_token}}) | |
.then(({data}) => data.data); | |
} | |
}; | |
exports.create = function(email, brokerName, agencyNameSlug, agencyName){ | |
return Promise.resolve() | |
.then(() => { | |
return timekit.createResource(email, brokerName) | |
}) | |
.then((resource) => { | |
return timekit.createWidgetToken(resource, brokerName) | |
.then(widget => ({resource, widget})); | |
}) | |
.then(({resource, widget}) => { | |
return timekit.createProject(agencyName, agencyNameSlug, resource) | |
.then(project => ({resource, widget, project})); | |
}) | |
.catch(err => Promise.reject(new Error(err))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment