vtb-appit 0.0.5 → 0.0.6

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.
@@ -9,6 +9,11 @@ class AppitBaseTransformer {
9
9
  this.travelplan = JSON.parse(data);
10
10
  }
11
11
 
12
+ labels()
13
+ {
14
+ return false;
15
+ }
16
+
12
17
  participants()
13
18
  {
14
19
  return false;
@@ -39,6 +44,48 @@ class AppitBaseTransformer {
39
44
  return false;
40
45
  }
41
46
 
47
+ schemes()
48
+ {
49
+ return false;
50
+ }
51
+
52
+ encode(text)
53
+ {
54
+ if(!text) return '';
55
+
56
+ return text.replace('ë', 'ë').replace('é', 'é').replace('è', 'è').replace('ï', 'ï').replace('ñ', 'ñ').replace('±', '±');
57
+ }
58
+
59
+ labelKey(element)
60
+ {
61
+ let key = '';
62
+
63
+ if(element.TSProduct && element.TSProduct.id) {
64
+ key += element.TSProduct.id;
65
+ }
66
+
67
+ if(element.title) {
68
+ key += '_' + element.title;
69
+ }
70
+
71
+ return key.replace(/<.*?>/, '').replace(/\s/g, '-').normalize("NFD").replace(/[\u0300-\u036f]/g, '');
72
+ }
73
+
74
+ elementExtraField(element, name)
75
+ {
76
+ if(!element || !element.TSOrderline || !element.TSOrderline.extraFieldValues || !element.TSOrderline.extraFieldValues.length) return false;
77
+
78
+ let result = false;
79
+
80
+ element.TSOrderline.extraFieldValues.forEach(field => {
81
+ ; if(field.name === name) {
82
+ result = field.value;
83
+ }
84
+ });
85
+
86
+ return result;
87
+ }
88
+
42
89
  async mediaToBase64(media)
43
90
  {
44
91
  let promises = [];
package/appit.js CHANGED
@@ -13,19 +13,26 @@ class Appit {
13
13
  this.hostname = 'portal.appit4travel.com';
14
14
  this.pathPrefix = '/api/v1/';
15
15
  this.token;
16
-
16
+
17
17
  this.history();
18
18
  }
19
19
 
20
+ setHostname(hostname)
21
+ {
22
+ this.hostname = hostname;
23
+ }
24
+
20
25
  history()
21
26
  {
22
27
  this.history = {
23
- excursionId: 861,//todo: delete this!
28
+ excursionId: null,
24
29
  members: {},
25
30
  organizations: {},
26
31
  travels: {},
27
32
  places: {},
28
- accommodation: {}
33
+ accommodations: {},
34
+ schemes: {},
35
+ labels: {}
29
36
  };
30
37
 
31
38
  try {
@@ -41,24 +48,44 @@ class Appit {
41
48
  if(!await this.login()) return false;
42
49
 
43
50
  await this.excursion();
51
+ await this.labels();
44
52
  await this.places();
45
53
  await this.organizations();
46
54
  await this.members();
47
55
  await this.travelInfo();
48
56
  await this.accommodations();
57
+ await this.schemes();
49
58
 
50
59
  fs.writeFileSync(this.historyPath, JSON.stringify(this.history));
60
+
61
+ return true;
51
62
  }
52
63
 
64
+ async labels()
65
+ {
66
+ const labels = this.transformer.labels();
67
+ if(labels && labels.length) {
68
+ for(let i = 0; i < labels.length; i++) {
69
+ let id = this.findId('labels', labels[i]);
70
+ if(!id) {
71
+ let model = await this.createLabel(this.transformer.workspace_id, {label: labels[i], excursion_id: this.history.excursionId});
72
+ this.history.labels[labels[i]] = model.data.id;
73
+ } else {
74
+ await this.updateLabel(this.transformer.workspace_id, id, {label: labels[i], excursion_id: this.history.excursionId});
75
+ }
76
+ }
77
+ }
78
+ }
79
+
53
80
  async excursion()
54
81
  {
55
82
  const excursion = this.transformer.excursion();
56
- const images = await this.getImages(excursion.images, 'original/lg');
83
+ const images = (excursion.images.length) ? await this.getImages(excursion.images, 'original/lg') : {};
57
84
  delete excursion.images;
58
85
 
59
86
  if(!this.history.excursionId) {
60
87
  let model = await this.createExcursion({...excursion, ...images, workspace_id: this.transformer.workspace_id});
61
- this.history.excursionId = model.id;
88
+ this.history.excursionId = model.data.id;
62
89
  } else {
63
90
  await this.updateExcursion(this.history.excursionId, {...excursion, ...images, workspace_id: this.transformer.workspace_id});
64
91
  }
@@ -70,15 +97,15 @@ class Appit {
70
97
  if(places && places.length) {
71
98
  const formattedPlaces = await this.formatPlaces(places);
72
99
  for(let i = 0; i < formattedPlaces.length; i++) {
73
- let formattedPlace = JSON.parse(JSON.stringify(formattedPlaces[i]));
74
- delete formattedPlace.vtb_object_id;
100
+ let formattedPlace = this.clean(JSON.parse(JSON.stringify(formattedPlaces[i])));
101
+ let labels = this.getLabels(formattedPlaces[i]);
75
102
 
76
103
  let id = this.findId('places', formattedPlaces[i].vtb_object_id);
77
104
  if(!id) {
78
- let model = await this.createPlace({...formattedPlace, excursion_id: this.history.excursionId});
105
+ let model = await this.createPlace({...formattedPlace, labels: labels, excursion_id: this.history.excursionId});
79
106
  this.history.places[formattedPlaces[i].vtb_object_id] = model.data.id;
80
107
  } else {
81
- await this.updatePlace(id, {...formattedPlace, excursion_id: this.history.excursionId});
108
+ await this.updatePlace(id, {...formattedPlace, labels: labels, excursion_id: this.history.excursionId});
82
109
  }
83
110
  }
84
111
  }
@@ -106,8 +133,7 @@ class Appit {
106
133
  const participants = this.transformer.participants();
107
134
  if(participants && participants.length) {
108
135
  for(let i = 0; i < participants.length; i++) {
109
- let participant = JSON.parse(JSON.stringify(participants[i]));
110
- delete participant.id;
136
+ let participant = this.clean(JSON.parse(JSON.stringify(participants[i])));
111
137
 
112
138
  let id = this.findId('members', participants[i].id);
113
139
  if(!id) {
@@ -125,8 +151,7 @@ class Appit {
125
151
  const travelInfo = this.transformer.travelInfo();
126
152
  if(travelInfo && travelInfo.length) {
127
153
  for(let i = 0; i < travelInfo.length; i++) {
128
- let info = JSON.parse(JSON.stringify(travelInfo[i]));
129
- delete info.vtbObjectId;
154
+ let info = this.clean(JSON.parse(JSON.stringify(travelInfo[i])));
130
155
 
131
156
  let id = this.findId('travels', travelInfo[i].vtb_object_id);
132
157
  if(!id) {
@@ -144,24 +169,54 @@ class Appit {
144
169
  const accommodations = this.transformer.accommodations();
145
170
  if(accommodations && accommodations.length) {
146
171
  for(let i = 0; i < accommodations.length; i++) {
147
- // let accommodation = JSON.parse(JSON.stringify(accommodations[i]));
148
- // delete accommodation.vtbObjectId;
149
-
150
- // if(accommodation.images && accommodation.images.length) {
151
- // accommodation.images = await this.mediaToBase64(accommodation.images);
152
- // }
153
-
154
- // let id = this.findId('accommodations', accommodations[i].vtb_object_id);
155
- // if(!id) {
156
- // let model = await appit.createAccommodation({...accommodation, excursion_id: this.history.excursionId});
157
- // this.history.accommodations[accommodations[i].vtb_object_id] = model.data.id;
158
- // } else {
159
- // await appit.updateAccommodation(id, {...accommodation, excursion_id: history.excursionId});
160
- // }
172
+ let accommodation = this.clean(JSON.parse(JSON.stringify(accommodations[i])));
173
+ let labels = this.getLabels(accommodations[i]);
174
+
175
+ if(accommodation.images && accommodation.images.length) {
176
+ accommodation.images = await this.mediaToBase64(accommodation.images);
177
+ }
178
+
179
+ let id = this.findId('accommodations', accommodations[i].vtb_object_id);
180
+ if(!id) {
181
+ let model = await this.createAccommodation({...accommodation, labels: labels, excursion_id: this.history.excursionId});
182
+ this.history.accommodations[accommodations[i].vtb_object_id] = model.data.id;
183
+ } else {
184
+ await this.updateAccommodation(id, {...accommodation, labels: labels, excursion_id: this.history.excursionId});
185
+ }
186
+ }
187
+ }
188
+ }
189
+
190
+ async schemes()
191
+ {
192
+ const schemes = this.transformer.schemes();
193
+ if(schemes && schemes.length) {
194
+ for(let i = 0; i < schemes.length; i++) {
195
+ let scheme = this.clean(JSON.parse(JSON.stringify(schemes[i])));
196
+ let labels = this.getLabels(schemes[i]);
197
+
198
+ let id = this.findId('schemes', schemes[i].vtb_object_id);
199
+ if(!id) {
200
+ let model = await this.createScheme({...scheme, labels: labels, excursion_id: this.history.excursionId});
201
+ this.history.schemes[schemes[i].vtb_object_id] = model.data.id;
202
+ } else {
203
+ await this.updateScheme(id, {...scheme, labels: labels, excursion_id: this.history.excursionId});
204
+ }
161
205
  }
162
206
  }
163
207
  }
164
208
 
209
+ getLabels(object)
210
+ {
211
+ console.log(object.label);
212
+ if(object.label && this.history.labels && this.history.labels.hasOwnProperty(object.label)) {
213
+ console.log('label is!', [this.history.labels[object.label]]);
214
+ return [this.history.labels[object.label]];
215
+ }
216
+
217
+ return [];
218
+ }
219
+
165
220
  async formatPlaces(places)
166
221
  {
167
222
  let result = [];
@@ -173,8 +228,8 @@ class Appit {
173
228
  images = await this.getImages(places[i].media, 'original/lg');
174
229
 
175
230
  result.push({
176
- title : this.encode(places[i].title),
177
- content : this.encode(places[i].additionalText),
231
+ title : this.transformer.encode(places[i].title),
232
+ content : this.transformer.encode(places[i].additionalText),
178
233
  category_id : places[i].category_id,
179
234
  vtb_object_id : places[i].vtbObjectId,
180
235
  ...images
@@ -319,11 +374,21 @@ class Appit {
319
374
  return false;
320
375
  }
321
376
 
322
- encode(text)
377
+ clean(object)
323
378
  {
324
- if(!text) return '';
379
+ if(object.hasOwnProperty('vtb_object_id')) {
380
+ delete object.vtb_object_id;
381
+ }
382
+
383
+ if(object.hasOwnProperty('id')) {
384
+ delete object.id;
385
+ }
386
+
387
+ if(object.hasOwnProperty('label')) {
388
+ delete object.label;
389
+ }
325
390
 
326
- return text.replace('ë', '&euml;').replace('é', '&eacute;').replace('ï', '&iuml;').replace('ñ', '&ntilde;').replace('±', '&plusmn;');
391
+ return object
327
392
  }
328
393
 
329
394
  async login()
@@ -383,6 +448,16 @@ class Appit {
383
448
  return await this.request('PUT', `travels/${id}`, data);
384
449
  }
385
450
 
451
+ async createScheme(data)
452
+ {
453
+ return await this.request('POST', `schemes`, data);
454
+ }
455
+
456
+ async updateScheme(id, data)
457
+ {
458
+ return await this.request('PUT', `schemes/${id}`, data);
459
+ }
460
+
386
461
  async createAccommodation(data)
387
462
  {
388
463
  return await this.request('POST', `accommodations`, data);
@@ -402,6 +477,16 @@ class Appit {
402
477
  {
403
478
  return await this.request('PUT', `places/${id}`, data);
404
479
  }
480
+
481
+ async createLabel(workspace_id, data)
482
+ {
483
+ return await this.request('POST', `workspaces/${workspace_id}/labels`, data);
484
+ }
485
+
486
+ async updateLabel(workspace_id, id, data)
487
+ {
488
+ return await this.request('PUT', `workspaces/${workspace_id}/labels/${id}`, data);
489
+ }
405
490
  }
406
491
 
407
492
  module.exports = { Appit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtb-appit",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {