vtb-appit 0.0.60 → 0.0.62

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.
Files changed (2) hide show
  1. package/appit.js +175 -16
  2. package/package.json +1 -1
package/appit.js CHANGED
@@ -137,6 +137,7 @@ class Appit {
137
137
 
138
138
  async places()
139
139
  {
140
+ let placesHistory = {};
140
141
  const places = this.transformer.places();
141
142
  if(places && places.length) {
142
143
  for(let i = 0; i < places.length; i++) {
@@ -152,14 +153,26 @@ class Appit {
152
153
  let id = this.findId('places', places[i].object_id);
153
154
  if(!id) {
154
155
  let model = await this.createPlace({...place, ...images, excursion_id: this.history.excursionId});
155
- this.history.places[places[i].object_id] = model.data.id;
156
+ placesHistory[places[i].object_id] = model.data.id;
156
157
  } else {
157
- await this.updatePlace(id, {...place, ...images, excursion_id: this.history.excursionId});
158
+ let model = await this.updatePlace(id, {...place, ...images, excursion_id: this.history.excursionId});
159
+ placesHistory[places[i].object_id] = model.data.id;
158
160
  }
159
161
  }
160
162
  }
163
+ console.log(Object.values(this.history.places));
164
+ console.log(Object.values(placesHistory));
165
+ let nonExisting = this.difference(Object.values(this.history.places), Object.values(placesHistory));
166
+ console.log(nonExisting);
167
+ for(let i = 0; i < nonExisting.length; i++) {
168
+ await this.deletePlace(nonExisting[i]);
169
+ }
170
+
171
+ this.history.places = placesHistory;
161
172
  }
162
173
 
174
+
175
+
163
176
  async findOrCreateCategory(title)
164
177
  {
165
178
  let category_id = null;
@@ -219,6 +232,7 @@ class Appit {
219
232
 
220
233
  async organizations()
221
234
  {
235
+ let organizationsHistory = {};
222
236
  const organizations = this.transformer.organizations();
223
237
  if(organizations) {
224
238
  for(let i = 0; i < organizations.length; i++) {
@@ -226,12 +240,20 @@ class Appit {
226
240
  let id = this.findId('organizations', organizations[i].object_id);
227
241
  if(!id) {
228
242
  let model = await this.createOrganization(organizations[i]);
229
- this.history.organizations[organizations[i].object_id] = model.data.id;
243
+ organizationsHistory[organizations[i].object_id] = model.data.id;
230
244
  } else {
231
- await this.updateOrganization(id, organizations[i]);
245
+ let model = await this.updateOrganization(id, organizations[i]);
246
+ organizationsHistory[organizations[i].object_id] = model.data.id;
232
247
  }
233
248
  }
234
249
  }
250
+
251
+ let nonExisting = this.difference(Object.values(this.history.organizations), Object.values(organizationsHistory));
252
+ for(let i = 0; i < nonExisting.length; i++) {
253
+ await this.deleteOrganization(nonExisting[i]);
254
+ }
255
+
256
+ this.history.organizations = organizationsHistory;
235
257
  }
236
258
 
237
259
  async notifications()
@@ -256,6 +278,7 @@ class Appit {
256
278
 
257
279
  async members()
258
280
  {
281
+ let memberHistory = {};
259
282
  const participants = this.transformer.participants();
260
283
  if(participants && participants.length) {
261
284
  for(let i = 0; i < participants.length; i++) {
@@ -264,34 +287,57 @@ class Appit {
264
287
  let id = this.findId('members', participants[i].id);
265
288
  if(!id) {
266
289
  let model = await this.createMember({...participant, excursion_id: this.history.excursionId});
267
- this.history.members[participants[i].id] = model.data.id;
290
+ memberHistory[participants[i].id] = model.data.id;
268
291
  } else {
269
- await this.updateMember(id, {...participant, excursion_id: this.history.excursionId});
292
+ let model = await this.updateMember(id, {...participant, excursion_id: this.history.excursionId});
293
+ memberHistory[participants[i].id] = model.data.id;
270
294
  }
271
295
  }
272
296
  }
297
+
298
+ let nonExisting = this.difference(Object.values(this.history.members), Object.values(memberHistory));
299
+ for(let i = 0; i < nonExisting.length; i++) {
300
+ await this.deleteMember(nonExisting[i]);
301
+ }
302
+
303
+ this.history.members = memberHistory;
273
304
  }
274
305
 
275
306
  async travelInfo()
276
307
  {
308
+ let travelInfoHistory = {};
277
309
  const travelInfo = this.transformer.travelInfo();
278
310
  if(travelInfo && travelInfo.length) {
279
311
  for(let i = 0; i < travelInfo.length; i++) {
280
312
  let info = this.clean(JSON.parse(JSON.stringify(travelInfo[i])));
281
313
 
282
314
  let id = this.findId('travels', travelInfo[i].object_id);
315
+
316
+ if(info.category_id && typeof info.category_id === 'string') {
317
+ info.category_id = await this.findOrCreateCategory(info.category_id);
318
+ }
319
+
283
320
  if(!id) {
284
321
  let model = await this.createTravel({...info, excursion_id: this.history.excursionId});
285
- this.history.travels[travelInfo[i].object_id] = model.data.id;
322
+ travelInfoHistory[travelInfo[i].object_id] = model.data.id;
286
323
  } else {
287
- await this.updateTravel(id, {...info, excursion_id: this.history.excursionId});
324
+ let model = await this.updateTravel(id, {...info, excursion_id: this.history.excursionId});
325
+ travelInfoHistory[travelInfo[i].object_id] = model.data.id;
288
326
  }
289
327
  }
290
328
  }
329
+
330
+ let nonExisting = this.difference(Object.values(this.history.travels), Object.values(travelInfoHistory));
331
+ for(let i = 0; i < nonExisting.length; i++) {
332
+ await this.deleteTravel(nonExisting[i]);
333
+ }
334
+
335
+ this.history.travels = travelInfoHistory;
291
336
  }
292
337
 
293
338
  async accommodations()
294
339
  {
340
+ let accommodationHistory = {};
295
341
  const accommodations = this.transformer.accommodations();
296
342
  if(accommodations && accommodations.length) {
297
343
  for(let i = 0; i < accommodations.length; i++) {
@@ -304,16 +350,25 @@ class Appit {
304
350
  let id = this.findId('accommodations', accommodations[i].object_id);
305
351
  if(!id) {
306
352
  let model = await this.createAccommodation({...accommodation, ...images, labels: labels, excursion_id: this.history.excursionId});
307
- this.history.accommodations[accommodations[i].object_id] = model.data.id;
353
+ accommodationHistory[accommodations[i].object_id] = model.data.id;
308
354
  } else {
309
- await this.updateAccommodation(id, {...accommodation, ...images, labels: labels, excursion_id: this.history.excursionId});
355
+ let model = await this.updateAccommodation(id, {...accommodation, ...images, labels: labels, excursion_id: this.history.excursionId});
356
+ accommodationHistory[accommodations[i].object_id] = model.data.id;
310
357
  }
311
358
  }
312
359
  }
360
+
361
+ let nonExisting = this.difference(Object.values(this.history.accommodations), Object.values(accommodationHistory));
362
+ for(let i = 0; i < nonExisting.length; i++) {
363
+ await this.deleteAccommodation(nonExisting[i]);
364
+ }
365
+
366
+ this.history.accommodations = accommodationHistory;
313
367
  }
314
368
 
315
369
  async destinations()
316
370
  {
371
+ let destinationHistory = {};
317
372
  const destinations = await this.transformer.destinations();
318
373
  if(destinations && destinations.length) {
319
374
  for(let i = 0; i < destinations.length; i++) {
@@ -326,16 +381,25 @@ class Appit {
326
381
  let id = this.findId('destinations', destinations[i].object_id);
327
382
  if(!id) {
328
383
  let model = await this.createDestination({...destination, ...images, labels: labels, excursion_id: this.history.excursionId});
329
- this.history.destinations[destinations[i].object_id] = model.data.id;
384
+ destinationHistory[destinations[i].object_id] = model.data.id;
330
385
  } else {
331
- await this.updateDestination(id, {...destination, ...images, labels: labels, excursion_id: this.history.excursionId});
386
+ let model = await this.updateDestination(id, {...destination, ...images, labels: labels, excursion_id: this.history.excursionId});
387
+ destinationHistory[destinations[i].object_id] = model.data.id;
332
388
  }
333
389
  }
334
390
  }
391
+
392
+ let nonExisting = this.difference(Object.values(this.history.destinations), Object.values(destinationHistory));
393
+ for(let i = 0; i < nonExisting.length; i++) {
394
+ await this.deleteDestination(nonExisting[i]);
395
+ }
396
+
397
+ this.history.destinations = destinationHistory;
335
398
  }
336
399
 
337
400
  async schemes()
338
401
  {
402
+ const schemeHistory = {};
339
403
  const schemes = this.transformer.schemes(this.history);
340
404
  if(schemes && schemes.length) {
341
405
  for(let i = 0; i < schemes.length; i++) {
@@ -345,16 +409,25 @@ class Appit {
345
409
  let id = this.findId('schemes', schemes[i].object_id);
346
410
  if(!id) {
347
411
  let model = await this.createScheme({...scheme, labels: labels, excursion_id: this.history.excursionId});
348
- this.history.schemes[schemes[i].object_id] = model.data.id;
412
+ schemeHistory[schemes[i].object_id] = model.data.id;
349
413
  } else {
350
- await this.updateScheme(id, {...scheme, labels: labels, excursion_id: this.history.excursionId});
414
+ let model = await this.updateScheme(id, {...scheme, labels: labels, excursion_id: this.history.excursionId});
415
+ schemeHistory[schemes[i].object_id] = model.data.id;
351
416
  }
352
417
  }
353
418
  }
419
+
420
+ let nonExisting = this.difference(Object.values(this.history.schemes), Object.values(schemeHistory));
421
+ for(let i = 0; i < nonExisting.length; i++) {
422
+ await this.deleteScheme(nonExisting[i]);
423
+ }
424
+
425
+ this.history.schemes = schemeHistory;
354
426
  }
355
427
 
356
428
  async contacts()
357
429
  {
430
+ let contactHistory = {};
358
431
  const contacts = this.transformer.contacts();
359
432
  if(contacts && contacts.length) {
360
433
  for(let i = 0; i < contacts.length; i++) {
@@ -363,12 +436,20 @@ class Appit {
363
436
  let id = this.findId('contacts', contacts[i].object_id);
364
437
  if(!id) {
365
438
  let model = await this.createContact({...contact, excursion_id: this.history.excursionId});
366
- this.history.contacts[contacts[i].object_id] = model.data.id;
439
+ contactHistory[contacts[i].object_id] = model.data.id;
367
440
  } else {
368
- await this.updateContact(id, {...contact, excursion_id: this.history.excursionId});
441
+ let model = await this.updateContact(id, {...contact, excursion_id: this.history.excursionId});
442
+ contactHistory[contacts[i].object_id] = model.data.id;
369
443
  }
370
444
  }
371
445
  }
446
+
447
+ let nonExisting = this.difference(Object.values(this.history.contacts), Object.values(contactHistory));
448
+ for(let i = 0; i < nonExisting.length; i++) {
449
+ await this.deleteContact(nonExisting[i]);
450
+ }
451
+
452
+ this.history.contacts = contactHistory;
372
453
  }
373
454
 
374
455
  getLabels(object)
@@ -586,6 +667,16 @@ class Appit {
586
667
  return true;
587
668
  }
588
669
 
670
+ difference(original, current)
671
+ {
672
+ let difference = [];
673
+ original.forEach(id => {
674
+ if(current.indexOf(id) === -1)
675
+ difference.push(id);
676
+ });
677
+ return difference;
678
+ }
679
+
589
680
  async createDestination(data)
590
681
  {
591
682
  return await this.request('POST', `destinations`, data);
@@ -596,6 +687,11 @@ class Appit {
596
687
  return await this.request('PUT', `destinations/${id}`, data);
597
688
  }
598
689
 
690
+ async updateDestination(id)
691
+ {
692
+ return await this.request('DELETE', `destinations/${id}`);
693
+ }
694
+
599
695
  async createOrganization(data)
600
696
  {
601
697
  return await this.request('POST', `organizations`, data);
@@ -606,6 +702,15 @@ class Appit {
606
702
  return await this.request('PUT', `organizations/${id}`, data);
607
703
  }
608
704
 
705
+ async deleteOrganization(id)
706
+ {
707
+ try {
708
+ return await this.request('DELETE', `organizations/${id}`);
709
+ } catch {
710
+ console.log(`deleteOrganization failed: ${id}`);
711
+ }
712
+ }
713
+
609
714
  async createNotification(data)
610
715
  {
611
716
  return await this.request('POST', `notifications`, data);
@@ -626,6 +731,15 @@ class Appit {
626
731
  return await this.request('PUT', `members/${id}`, data);
627
732
  }
628
733
 
734
+ async deleteMember(id)
735
+ {
736
+ try {
737
+ return await this.request('DELETE', `members/${id}`);
738
+ } catch {
739
+ console.log(`deleteMember failed: ${id}`);
740
+ }
741
+ }
742
+
629
743
  async createExcursion(data)
630
744
  {
631
745
  return await this.request('POST', `excursions`, data);
@@ -650,6 +764,15 @@ class Appit {
650
764
  }
651
765
  }
652
766
 
767
+ async deleteTravel(id)
768
+ {
769
+ try {
770
+ return await this.request('DELETE', `travels/${id}`);
771
+ } catch {
772
+ console.log(`deleteTravel failed: ${id}`);
773
+ }
774
+ }
775
+
653
776
  async createScheme(data)
654
777
  {
655
778
  return await this.request('POST', `schemes`, data);
@@ -664,6 +787,15 @@ class Appit {
664
787
  }
665
788
  }
666
789
 
790
+ async deleteScheme(id)
791
+ {
792
+ try {
793
+ return await this.request('DELETE', `schemes/${id}`);
794
+ } catch {
795
+ console.log(`deleteScheme failed: ${id}`);
796
+ }
797
+ }
798
+
667
799
  async createContact(data)
668
800
  {
669
801
  return await this.request('POST', `contacts`, data);
@@ -678,6 +810,15 @@ class Appit {
678
810
  }
679
811
  }
680
812
 
813
+ async deleteContact(id)
814
+ {
815
+ try {
816
+ return await this.request('DELETE', `contacts/${id}`);
817
+ } catch {
818
+ console.log(`deleteContact failed: ${id}`);
819
+ }
820
+ }
821
+
681
822
  async createAccommodation(data)
682
823
  {
683
824
  return await this.request('POST', `accommodations`, data);
@@ -692,6 +833,15 @@ class Appit {
692
833
  }
693
834
  }
694
835
 
836
+ async deleteAccommodation(id)
837
+ {
838
+ try {
839
+ return await this.request('DELETE', `accommodations/${id}`);
840
+ } catch {
841
+ console.log(`deleteAccommodation failed: ${id}`);
842
+ }
843
+ }
844
+
695
845
  async createPlace(data)
696
846
  {
697
847
  return await this.request('POST', `places`, data);
@@ -706,6 +856,15 @@ class Appit {
706
856
  }
707
857
  }
708
858
 
859
+ async deletePlace(id)
860
+ {
861
+ try {
862
+ return await this.request('DELETE', `places/${id}`);
863
+ } catch {
864
+ console.log(`deletePlace failed: ${id}`);
865
+ }
866
+ }
867
+
709
868
  async createFlight(data)
710
869
  {
711
870
  return await this.request('POST', `flights`, data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtb-appit",
3
- "version": "0.0.60",
3
+ "version": "0.0.62",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {