vtb-appit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ const https = require('https');
2
+ const fs = require("fs");
3
+ const pth = require("path");
4
+
5
+ class AppitBaseTransformer {
6
+ constructor()
7
+ {
8
+ const data = fs.readFileSync(pth.join(__dirname, '../../assets/travelplan.json'), {encoding: 'utf8' });
9
+ this.travelplan = JSON.parse(data);
10
+ }
11
+
12
+ participants()
13
+ {
14
+ return false;
15
+ }
16
+
17
+ excursion()
18
+ {
19
+ return false;
20
+ }
21
+
22
+ places()
23
+ {
24
+ return false;
25
+ }
26
+
27
+ organizations()
28
+ {
29
+ return false;
30
+ }
31
+
32
+ travelInfo()
33
+ {
34
+ return false;
35
+ }
36
+
37
+ accommodations()
38
+ {
39
+ return false;
40
+ }
41
+
42
+ async mediaToBase64(media)
43
+ {
44
+ let promises = [];
45
+
46
+ promises.push(new Promise((resolve, reject) => {
47
+ for(let i = 0; i < media.length; i++) {
48
+ const req = https.request(media[i].url, res => {
49
+ let chunks = [];
50
+ res.on('data', chunk => {
51
+ chunks.push(chunk);
52
+ })
53
+
54
+ res.on('end', () => {
55
+ const buffer = Buffer.concat(chunks).toString('base64');
56
+ let base64Data = 'data:' + res.headers['content-type'] + ';base64,';
57
+ base64Data += buffer;
58
+
59
+ let frags = media[i].url.split('/');
60
+
61
+ resolve({
62
+ title: '',
63
+ name: frags[frags.length - 1],
64
+ content: base64Data
65
+ });
66
+ })
67
+ });
68
+
69
+ req.end();
70
+ }
71
+ }));
72
+
73
+ return Promise.all(promises);
74
+ }
75
+ }
76
+
77
+ module.exports = { AppitBaseTransformer };
package/appit.js ADDED
@@ -0,0 +1,370 @@
1
+ const fs = require("fs");
2
+ const pth = require("path");
3
+ const https = require('https');
4
+
5
+ class Appit {
6
+
7
+ constructor(transformer)
8
+ {
9
+ this.transformer = transformer;
10
+
11
+ this.historyPath = pth.join(__dirname, '../../assets/history.json');
12
+
13
+ this.hostname = 'portal.appit4travel.com';
14
+ this.pathPrefix = '/api/v1/';
15
+ this.token;
16
+
17
+ this.history();
18
+ }
19
+
20
+ history()
21
+ {
22
+ this.history = {
23
+ excursionId: 861,//todo: delete this!
24
+ members: {},
25
+ organizations: {},
26
+ travels: {},
27
+ places: {},
28
+ accommodation: {}
29
+ };
30
+
31
+ try {
32
+ const historyFile = fs.readFileSync(this.historyPath, {encoding: 'utf8'});
33
+ history = JSON.parse(historyFile);
34
+ } catch(e) {
35
+ console.log(e);
36
+ }
37
+ }
38
+
39
+ async exec()
40
+ {
41
+ if(!await this.login()) return false;
42
+
43
+ await this.excursion();
44
+ await this.places();
45
+ await this.organizations();
46
+ await this.members();
47
+ await this.travelInfo();
48
+ await this.accommodations();
49
+
50
+ fs.writeFileSync(this.historyPath, JSON.stringify(this.history));
51
+ }
52
+
53
+ async excursion()
54
+ {
55
+ const excursion = this.transformer.excursion();
56
+ const images = await this.getImages(excursion.images, 'original/lg');
57
+ delete excursion.images;
58
+
59
+ if(!this.history.excursionId) {
60
+ let model = await this.createExcursion({...excursion, ...images, workspace_id: this.transformer.workspace_id});
61
+ this.history.excursionId = model.id;
62
+ } else {
63
+ await this.updateExcursion(this.history.excursionId, {...excursion, ...images, workspace_id: this.transformer.workspace_id});
64
+ }
65
+ }
66
+
67
+ async places()
68
+ {
69
+ const places = this.transformer.places();
70
+ if(places && places.length) {
71
+ const formattedPlaces = await this.formatPlaces(places);
72
+ for(let i = 0; i < formattedPlaces.length; i++) {
73
+ let formattedPlace = JSON.parse(JSON.stringify(formattedPlaces[i]));
74
+ delete formattedPlace.vtb_object_id;
75
+
76
+ let id = this.findId('places', formattedPlaces[i].vtb_object_id);
77
+ if(!id) {
78
+ let model = await this.createPlace({...formattedPlace, excursion_id: this.history.excursionId});
79
+ this.history.places[formattedPlaces[i].vtb_object_id] = model.data.id;
80
+ } else {
81
+ await this.updatePlace(id, {...formattedPlace, excursion_id: this.history.excursionId});
82
+ }
83
+ }
84
+ }
85
+ }
86
+
87
+ async organizations()
88
+ {
89
+ const organizations = this.transformer.organizations();
90
+ if(organizations) {
91
+ for(let i = 0; i < organizations.length; i++) {
92
+ organizations[i].excursion_id = this.history.excursionId;
93
+ let id = this.findId('organizations', organizations[i].vtb_object_id);
94
+ if(!id) {
95
+ let model = await this.createOrganization(organizations[i]);
96
+ this.history.organizations[organizations[i].vtb_object_id] = model.data.id;
97
+ } else {
98
+ await this.updateOrganization(id, organizations[i]);
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ async members()
105
+ {
106
+ const participants = this.transformer.participants();
107
+ if(participants && participants.length) {
108
+ for(let i = 0; i < participants.length; i++) {
109
+ let participant = JSON.parse(JSON.stringify(participants[i]));
110
+ delete participant.id;
111
+
112
+ let id = this.findId('members', participants[i].id);
113
+ if(!id) {
114
+ let model = await this.createMember({...participant, excursion_id: this.history.excursionId});
115
+ this.history.members[participants[i].id] = model.data.id;
116
+ } else {
117
+ await this.updateMember(id, {...participant, excursion_id: this.history.excursionId});
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ async travelInfo()
124
+ {
125
+ const travelInfo = this.transformer.travelInfo();
126
+ if(travelInfo && travelInfo.length) {
127
+ for(let i = 0; i < travelInfo.length; i++) {
128
+ let info = JSON.parse(JSON.stringify(travelInfo[i]));
129
+ delete info.vtbObjectId;
130
+
131
+ let id = this.findId('travels', travelInfo[i].vtb_object_id);
132
+ if(!id) {
133
+ let model = await this.createTravel({...info, excursion_id: this.history.excursionId});
134
+ this.history.travels[travelInfo[i].vtb_object_id] = model.data.id;
135
+ } else {
136
+ await this.updateTravel(id, {...info, excursion_id: this.history.excursionId});
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ async accommodations()
143
+ {
144
+ const accommodations = this.transformer.accommodations();
145
+ console.log(accommodations);
146
+ console.log('abc');
147
+ // if(accommodations && accommodations.length) {
148
+ // for(let i = 0; i < accommodations.length; i++) {
149
+ // let accommodation = JSON.parse(JSON.stringify(accommodations[i]));
150
+ // delete accommodation.vtbObjectId;
151
+
152
+ // let id = this.findId('accommodations', accommodations[i].vtb_object_id);
153
+ // if(!id) {
154
+ // let model = await appit.createAccommodation({...accommodation, excursion_id: this.history.excursionId});
155
+ // this.history.accommodations[accommodations[i].vtb_object_id] = model.data.id;
156
+ // } else {
157
+ // await appit.updateAccommodation(id, {...accommodation, excursion_id: history.excursionId});
158
+ // }
159
+ // }
160
+ // }
161
+ }
162
+
163
+ async formatPlaces(places)
164
+ {
165
+ let result = [];
166
+
167
+ for(let i = 0; i < places.length; i++) {
168
+ let images = {};
169
+
170
+ if(places[i].media.length)
171
+ images = await this.getImages(places[i].media, 'original/lg');
172
+
173
+ result.push({
174
+ title : this.encode(places[i].title),
175
+ content : this.encode(places[i].additionalText),
176
+ category_id : places[i].category_id,
177
+ vtb_object_id : places[i].vtbObjectId,
178
+ ...images
179
+ });
180
+ };
181
+
182
+ return result;
183
+ }
184
+
185
+ getImages(media, crop)
186
+ {
187
+ const result = {
188
+ file_size: [],
189
+ file_mime_type: [],
190
+ file_name: [],
191
+ file_path: [],
192
+ file_active: [],
193
+ file_type: []
194
+ };
195
+
196
+ return new Promise((resolve, reject) => {
197
+ media.forEach(image => {
198
+ const regex = (image.url.match(/amazonaws\.com/)) ? /\.com\/(.*?)\/((?:.*?)\/(?:.*?))\//g : /\.(?:nl|com|travel|io)\.?\/((?:.*?)\/(?:.*?))\//g;
199
+ const match = regex.exec(image.url);
200
+
201
+ if(match) {
202
+ let replace = (match.length == 3) ? match[2] : match[1];
203
+ image.url = image.url.replace(replace, crop);
204
+ }
205
+
206
+ const req = https.request(image.url, res => {
207
+ let body = '';
208
+ res.on('data', chunk => {
209
+ body += chunk;
210
+ })
211
+
212
+ res.on('end', () => {
213
+ const frags = image.url.split('/');
214
+ result.file_name.push(frags[frags.length - 1]);
215
+ result.file_path.push(image.url);
216
+ result.file_mime_type.push(res.headers['content-type']);
217
+ result.file_size.push(res.headers['content-length']);
218
+ result.file_active.push(1)
219
+ result.file_type.push(1);
220
+
221
+ resolve(result);
222
+ })
223
+ });
224
+ req.end();
225
+ });
226
+ })
227
+ }
228
+
229
+ request(method, path, data)
230
+ {
231
+ const payload = (data) ? JSON.stringify(data) : false;
232
+
233
+ return new Promise((resolve, reject) => {
234
+ const options = {
235
+ hostname: this.hostname,
236
+ path: pth.join(this.pathPrefix, path),
237
+ method: method,
238
+ }
239
+
240
+ if(payload) {
241
+ options.headers = {
242
+ 'Content-Type': 'application/json',
243
+ 'Content-Length': payload.length
244
+ };
245
+ }
246
+
247
+ if(this.token) {
248
+ options.headers.Authorization = `Bearer ${this.token}`;
249
+ }
250
+
251
+ const req = https.request(options, res => {
252
+ let result = '';
253
+ res.on('data', chunk => {
254
+ result += chunk;
255
+ })
256
+
257
+ res.on('end', () => {
258
+ resolve(JSON.parse(result))
259
+ })
260
+ });
261
+
262
+ req.on('error', error => {
263
+ console.error(error);
264
+
265
+ reject(false);
266
+ });
267
+
268
+
269
+ if(payload) {
270
+ req.write(payload)
271
+ }
272
+
273
+ req.end();
274
+ });
275
+ }
276
+
277
+ findId(key, id) {
278
+ if(this.history.hasOwnProperty(key) && this.history[key].hasOwnProperty(id)) {
279
+ return this.history[key][id];
280
+ }
281
+
282
+ return false;
283
+ }
284
+
285
+ encode(text)
286
+ {
287
+ if(!text) return '';
288
+
289
+ return text.replace('ë', '&euml;').replace('é', '&eacute;').replace('ï', '&iuml;').replace('ñ', '&ntilde;').replace('±', '&plusmn;');
290
+ }
291
+
292
+ async login()
293
+ {
294
+ let crediantials = await this.request('POST', 'login', {
295
+ email: this.transformer.email,
296
+ password: this.transformer.password
297
+ });
298
+
299
+ if(!crediantials || !crediantials.data) {
300
+ console.log('***** Incorrect login credentials *****');
301
+ return false;
302
+ }
303
+
304
+ this.token = crediantials.data.token;
305
+
306
+ return true;
307
+ }
308
+
309
+ async createOrganization(data)
310
+ {
311
+ return await this.request('POST', `organizations`, data);
312
+ }
313
+
314
+ async updateOrganization(id, data)
315
+ {
316
+ return await this.request('PUT', `organizations/${id}`, data);
317
+ }
318
+
319
+ async createMember(data)
320
+ {
321
+ return await this.request('POST', `members`, data);
322
+ }
323
+
324
+ async updateMember(id, data)
325
+ {
326
+ return await this.request('PUT', `members/${id}`, data);
327
+ }
328
+
329
+ async createExcursion(data)
330
+ {
331
+ return await this.request('POST', `excursions`, data);
332
+ }
333
+
334
+ async updateExcursion(id, data)
335
+ {
336
+ return await this.request('PUT', `excursions/${id}`, data);
337
+ }
338
+
339
+ async createTravel(data)
340
+ {
341
+ return await this.request('POST', `travels`, data);
342
+ }
343
+
344
+ async updateTravel(id, data)
345
+ {
346
+ return await this.request('PUT', `travels/${id}`, data);
347
+ }
348
+
349
+ async createAccommodation(data)
350
+ {
351
+ return await this.request('POST', `accommodations`, data);
352
+ }
353
+
354
+ async updateAccommodation(id, data)
355
+ {
356
+ return await this.request('PUT', `accommodations/${id}`, data);
357
+ }
358
+
359
+ async createPlace(data)
360
+ {
361
+ return await this.request('POST', `places`, data);
362
+ }
363
+
364
+ async updatePlace(id, data)
365
+ {
366
+ return await this.request('PUT', `places/${id}`, data);
367
+ }
368
+ }
369
+
370
+ module.exports = { Appit };
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ exports.Appit = require('./appit').Appit;
2
+ exports.AppitBaseTransformer = require('./appit-base-transformer').AppitBaseTransformer;
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "vtb-appit",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "dependencies": {
11
+
12
+ }
13
+ }
14
+