-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.js
179 lines (165 loc) · 6.02 KB
/
setup.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const gonebusy = require('gonebusy-nodejs-client');
const request = require("request");
const Promise = require('bluebird').Promise;
const fs = require('fs');
gonebusy.Configuration.currentEnvironment = 'sandbox';
const { ServicesController, ResourcesController, SchedulesController } = gonebusy;
const AUTH_TOKEN = process.argv[2];
const TEAR_DOWN = process.argv[3];
const authorization = `Token ${AUTH_TOKEN}`;
const createResources = () => {
const staffMembers = [
{ name: 'Phillip Fry', type: 'staff' },
{ name: 'Sarah Belmoris', type: 'staff' },
{ name: 'Selena Yamada', type: 'staff' },
{ name: 'James Hunter', type: 'staff' }
];
const promises = [];
staffMembers.forEach(staffMember => {
promises.push(new Promise((resolve, reject) => {
ResourcesController.createResource(authorization, staffMember).then((success) => {
resolve(success.resource.id);
}).catch((error) => {
console.log(`error in creating resource ${staffMember}: ${error.errorMessage}`);
reject();
});
}));
});
return promises;
};
const deleteResources = (resourceIds) => {
resourceIds.forEach((resourceId) => {
ResourcesController.deleteResourceById(authorization, resourceId).catch((error) => {
console.log(error.errorMessage);
console.log(`Error in deleting resource: ${resourceId}`);
});
});
};
const createService = (resourceIds) => {
const createServiceBody = {
name: 'LSAT Tutoring',
description: 'LSAT Tutoring service',
duration: 60,
max_duration: 90,
resources: resourceIds.join(',')
};
return new Promise((resolve, reject) => {
ServicesController.createService(authorization, createServiceBody).then((success) => {
const { id, ownerId, schedules } = success.service;
console.log(`service id: ${id}`);
console.log(`user id: ${ownerId}`);
resolve({scheduleIds: schedules, userId: ownerId, serviceId: id});
}).catch((error) => {
console.log(`error in creating service: ${error.errorMessage}`);
reject();
});
});
};
const deleteService = (serviceId) => {
ServicesController.deleteServiceById(authorization,serviceId).catch((error) => {
console.log(error.errorMessage);
console.log(`error in deleting service: ${serviceId}`);
});
};
const createSchedules = (scheduleIds) => {
const resourceSchedules = [
{
start_date: '2017-03-26',
start_time: '18:00',
end_time: '21:00',
days: 'monday,tuesday',
recurs_by: 'weekly'
},
{
start_date: '2017-03-26',
start_time: '20:00',
end_time: '21:00',
days: 'wednesday,friday',
recurs_by: 'weekly'
},
{
start_date: '2017-03-26',
start_time: '16:00',
end_time: '21:00',
days: 'friday',
recurs_by: 'weekly'
},
{
start_date: '2017-03-26',
start_time: '18:00',
end_time: '21:00',
days: 'thursday',
recurs_by: 'weekly'
}
];
const promises = [];
for (let i = 0; i < scheduleIds.length; i++) {
const id = scheduleIds[i];
const createScheduleTimeWindowBody = resourceSchedules[i];
promises.push(new Promise((resolve, reject) => {
SchedulesController.createScheduleTimeWindow(authorization, id, createScheduleTimeWindowBody)
.then((success) => (resolve()))
.catch((error) => {
console.log(error.errorMessage);
console.log(`error in creating schedule for ${scheduleIds[i]}`);
reject();
});
}));
}
return promises;
};
const deleteSchedules = (scheduleIds) => {
scheduleIds.forEach(scheduleId => {
SchedulesController.deleteScheduleById(authorization, scheduleId).catch((error) => {
console.log(error.errorMessage);
console.log(`error in deleting schedule: ${scheduleId}`);
});
});
};
if (!AUTH_TOKEN) {
console.log('please provide your API token');
return;
}
if (TEAR_DOWN === 'teardown') {
ServicesController.getServices(authorization).then((success) => {
const services = success.services;
services.forEach(({ resources, schedules, id}) => {
console.log(`deleting service: ${id}`);
console.log(`deleting schedules: ${schedules} for service ${id}`);
deleteSchedules(schedules);
console.log(`deleting resources: ${resources} for service ${id}`);
deleteResources(resources);
deleteService(id);
console.log(`deleted service: ${id}`)
});
}).catch((error) => {
console.log('error in retrieving services');
});
return;
}
console.log('creating resources...');
const resourcePromises = createResources();
Promise.all(resourcePromises).then((resourceIds) => {
if (resourceIds.length) {
console.log('creating your service');
const serviceResponse = createService(resourceIds);
serviceResponse.then(({scheduleIds, userId, serviceId}) => {
if (scheduleIds.length) {
console.log('creating schedules');
const schedulesPromises = createSchedules(scheduleIds);
Promise.all(schedulesPromises).then(() => {
console.log('writing .env file');
fs.writeFile(
'.env',
`API_KEY=${AUTH_TOKEN}\nSERVICE_ID=${serviceId}\nUSER_ID=${userId}\nPORT=4000`,
(error) => {
if (error) {
console.log('failed to write to .env file');
}
console.log('success');
});
})
}
});
}
});