-
Notifications
You must be signed in to change notification settings - Fork 0
/
podio.js
147 lines (124 loc) · 3.87 KB
/
podio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var Promise = require("bluebird");
var rest = require('restler');
var util = require('util');
var moment = require('moment');
var Podio = function(apiKey, appName, callbackUrl, code) {
this.apiKey = apiKey;
this.appName = appName;
this.callbackUrl = callbackUrl;
this.code = code;
};
Podio.prototype = {
authorize: function(access_token) {
var def = Promise.defer();
if (access_token) {
this.access_token = access_token;
def.resolve(this.access_token);
return def.promise;
}
var self = this;
rest.post('https://podio.com/oauth/token', {
data: {
grant_type: 'authorization_code',
client_id: this.appName,
redirect_uri: this.callbackUrl,
client_secret: this.apiKey,
code: this.code
}
}).on('complete', (result) => {
self.authSettings = result;
self.access_token = result.access_token;
def.resolve(self.access_token);
}).on('error', () => {
def.reject();
});
return def.promise;
},
getResource: function (resource) {
var def = Promise.defer();
rest.get('https://api.podio.com' + resource, {
headers: {
Authorization: util.format('OAuth2 %s', this.access_token)
}
}).on('complete', (result) => {
def.resolve(result);
}).on('error', () => def.reject());
return def.promise;
},
getCalendar: function(fromDate, toDate) {
return this.getResource(
util.format('/calendar?date_from=%s&date_to=%s&priority=%s&tasks=%s',
moment(fromDate).format('YYYY-MM-DD'),
moment(toDate).format('YYYY-MM-DD'),
'5',
'false'
));
return def.promise;
},
getLinkedCalendar: function(linked_account_id, fromDate, toDate) {
return this.getResource(util.format('/calendar/linked_account/%s?date_from=%s&date_to=%s',
linked_account_id,
moment(fromDate).format('YYYY-MM-DD'),
moment(toDate).format('YYYY-MM-DD')
));
},
getAllCalendars: function(fromDate, toDate) {
var self = this;
return this.getLinkedAccounts().then((linkedAccounts) => {
if (linkedAccounts.error) { return; }
return Promise.all(
linkedAccounts.map(
(link) => self.getLinkedCalendar( link.linked_account_id, fromDate, toDate )
).concat([self.getCalendar(fromDate, toDate)])
).then((cResult) => cResult.reduce((a,b) => a.concat(b)));
});
},
getLinkedAccounts: function() {
return this.getResource('/linked_account?capability=calendar');
},
getOrganizations: function () {
return this.getResource('/org/');
},
getWorkspaces: function (org, user_id) {
return this.getResource('/org/' + org.org_id + '/member/' + user_id + '/space_member/');
},
getWorkspacesForallOrgs: function (user_id) {
var self = this;
return this.getOrganizations().then((orgs) => {
if (orgs.error) { return; }
var allSpaces = [];
orgs.forEach((org) => { allSpaces = allSpaces.concat(
org.spaces.map((s) => {return { name: s.name, space_id: s.space_id } } )
) });
return allSpaces;
});
},
getApps: function(space_id) {
return this.getResource('/app/space/' + space_id + '/');
},
getRoomApp: function(space_id) {
return this.getApps(space_id).then((apps) => {
var roomApps = apps.filter((a) => a.config.name === "Rooms");
return roomApps.length ? roomApps[0] : [];
});
},
mapRoomItem: function(roomItem) {
var newRoom = {};
roomItem.fields.forEach(field => {
newRoom[field.label] = field.values[0].value;
});
return newRoom;
},
getRooms: function (space_id) {
var self = this;
return this.getRoomApp(space_id).then((roomApp) => {
return self.getResource('/item/app/' + roomApp.app_id).then((roomItems) => {
return roomItems.items.map(self.mapRoomItem);
});
});
},
getUserInfo: function(user_id) {
return this.getResource('/user/status');
},
};
module.exports = Podio;