-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
979 lines (911 loc) · 33.9 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
const constants = require('./utils/constants');
const R = require('ramda');
const Promise = require('bluebird');
const assert = require('assert');
const moment = require('moment');
const jwt = require('jsonwebtoken');
const wildcardMatch = require('./utils/wildcardMatch');
const { translateProduct } = require('./resolvers/product');
const { translateAvailability } = require('./resolvers/availability');
const { translateBooking } = require('./resolvers/booking');
const ORIGIN_COUNTRIES =
`Australia,New Zealand,United Kingdom,U.S. United States,------------------,Albania,Algeria,American Samoa,Andorra,Angola,Anguilla,Antarctica,Antigua And Barbuda,Argentina,Armenia,Aruba,Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bermuda,Bhutan,Bolivia,Bosnia and Herzegowina,Botswana,Bouvet Island,Brazil,Brunei Darussalam,Bulgaria,Burkina Faso,Burma,Burundi,Cambodia,Cameroon,Canada,Cape Verde,Cayman Islands,Central African Republic,Chad,Chile,Christmas Island,Cocos (Keeling) Islands,Colombia,Comoros,Congo,Cook Islands,Costa Rica,Cote dIvoire,Croatia,Cyprus,Czech Republic,Denmark,Djibouti,Dominica,Dominican Republic,East Timor,Ecuador,Egypt,El Salvador,England,Equatorial Guinea,Eritrea,Espana,Estonia,Ethiopia,Falkland Islands,Faroe Islands,Fiji,Finland,France,French Guiana,French Polynesia,Gabon,Gambia,Georgia,Germany,Ghana,Gibraltar,Great Britain,Greece,Greenland,Grenada,Guadeloupe,Guam,Guatemala,Guinea,Guinea-Bissau,Guyana,Haiti,Honduras,Hong Kong,Hungary,Iceland,India,Indonesia,Ireland,Israel,Italy,Jamaica,Japan,Jordan,Kazakhstan,Kenya,Kiribati,Korea (South),Korea - Republic of,Kuwait,Kyrgyzstan,Latvia,Lebanon,Lesotho,Liberia,Liechtenstein,Lithuania,Luxembourg,Macau,Macedonia,Madagascar,Malawi,Malaysia,Maldives,Mali,Malta,Marshall Islands,Martinique,Mauritania,Mauritius,Mayotte,Mexico,Moldova - Republic of,Monaco,Mongolia,Montserrat,Morocco,Mozambique,Myanmar,Namibia,Nauru,Nepal,Netherlands,Netherlands Antilles,New Caledonia,Nicaragua,Niger,Nigeria,Niue,Norfolk Island,Northern Ireland,Northern Mariana Islands,Norway,Oman,Pakistan,Palau,Panama,Papua New Guinea,Paraguay,Peru,Philippines,Pitcairn,Poland,Portugal,Puerto Rico,Qatar,Reunion,Romania,Russia,Russian Federation,Rwanda,Saint Kitts and Nevis,Saint Lucia,Samoa (Independent),San Marino,Sao Tome and Principe,Saudi Arabia,Scotland,Senegal,Seychelles,Sierra Leone,Singapore,Slovakia,Slovenia,Solomon Islands,Somalia,South Africa,South Korea,Spain,Sri Lanka,St. Helena,St. Pierre and Miquelon,Suriname,Swaziland,Sweden,Switzerland,Taiwan,Tajikistan,Tanzania,Thailand,Togo,Tokelau,Tonga,Trinidad,Triniad and Tobago,Tunisia,Turkey,Turkmenistan,Tuvalu,Uganda,Ukraine,United Arab Emirates,Uruguay,Uzbekistan,Vanuatu,Venezuela,Viet Nam,Virgin Islands (British),Virgin Islands (U.S.),Wales,Western Sahara,Yemen,Zambia,Zimbabwe`.
split(`,`,)
const CUSTOM_FIELD_IDS = {
EBIKE_COUNT: 1,
BABYSEATS_COUNT: 2,
TRAILALONGS_COUNT: 3,
KIDDIECARRIER_COUNT: 4,
SMALLKIDSBIKE_COUNT: 5,
LARGEKIDSBIKE_COUNT: 6,
ORIGINCOUNTRY: 7,
TRAVELAGENCY: 8,
FAMILS: 9,
SEND_EMAIL_TO_PARTNER: 10,
SEND_EMAIL_TO_GUEST: 11
}
const EQUIPMENT_FIELD_IDS = {
EBIKE: 1001,
BABYSEATS: 1002,
TRAILALONGS: 1003,
KIDDIECARRIER: 1004,
SMALLKIDSBIKE: 1005,
LARGEKIDSBIKE: 1006,
}
const CONCURRENCY = 3; // is this ok ?
const isNilOrEmpty = R.either(R.isNil, R.isEmpty);
const getHeaders = ({
apiKey,
}) => ({
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
});
class Plugin {
constructor(params) { // we get the env variables from here
Object.entries(params).forEach(([attr, value]) => {
this[attr] = value;
});
this.tokenTemplate = () => ({
endpoint: {
type: 'text',
regExp: /^(?!mailto:)(?:(?:http|https|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:(\/|\?|#)[^\s]*)?$/i,
default: 'https://bmsstage.bonzabiketours.com:3001/octo/v1',
description: 'The Bonza API endpoint URL',
},
apiKey: {
type: 'text',
regExp: /[0-9a-f]{48}/,
description: 'The authentication key for Bonza API endpoint',
},
bookingPartnerId: {
type: 'text',
regExp: /^\d+$/,
description: 'The Bonza booking partner id',
}
});
this.errorPathsAxiosErrors = () => ([ // axios triggered errors
['response', 'data', 'errorMessage'],
]);
this.errorPathsAxiosAny = () => ([]); // 200's that should be errors
}
async validateToken({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
})
{
// console.log("API KEY in plugin : " + apiKey);
// console.log("ENDPOINT in plugin : " + endpoint);
// console.log("BOOKIGN PARTNER ID in plugin : " + bookingPartnerId);
const url = `${endpoint}/products`;
const headers = getHeaders({
apiKey: apiKey,
});
try {
const suppliers = R.path(['data'], await axios({
method: 'get',
url,
headers,
}));
return Array.isArray(suppliers) && suppliers.length > 0;
} catch (err) {
console.log("ERR: " + err);
return false;
}
}
async searchProducts({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
payload,
typeDefsAndQueries: {
productTypeDefs,
productQuery,
},
}) {
let url = `${endpoint}/products`;
console.log("URL: " + url);
if (!isNilOrEmpty(payload)) {
if (payload.productId) {
console.log("payload.productId: " + payload.productId);
url = `${url}/${payload.productId}`;
}
}
console.log("URL: " + url);
const headers = getHeaders({
apiKey: apiKey,
});
let results = R.pathOr([], ['data'], await axios({
method: 'get',
url,
headers,
}));
if (!Array.isArray(results)) results = [results];
let products = await Promise.map(results, async product => {
return translateProduct({
rootValue: product,
typeDefs: productTypeDefs,
query: productQuery,
});
});
console.log("dynamic extra filtering");
// dynamic extra filtering
if (!isNilOrEmpty(payload)) {
const extraFilters = R.omit(['productId'], payload);
if (Object.keys(extraFilters).length > 0) {
products = products.filter(
product => Object.entries(extraFilters).every(
([key, value]) => {
if (typeof value === 'string') return wildcardMatch(value, product[key]);
return true;
},
),
);
}
}
return ({ products });
}
// async searchQuote({
// token: {
// endpoint,
// apiKey,
// bookingPartnerId,
// },
// payload: {
// productIds,
// optionIds,
// },
// }) {
// return { quote: [] };
// }
async searchAvailability({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
// ONLY add payload key when absolutely necessary
payload: {
productIds,
optionIds,
startDate,
endDate,
dateFormat,
units
},
typeDefsAndQueries: {
availTypeDefs,
availQuery,
},
}) {
assert(this.jwtKey, 'JWT secret should be set');
assert(
productIds.length === productIds.length,
'mismatched productIds/options length',
);
assert(
optionIds.length === units.length,
'mismatched options/units length',
);
assert(productIds.every(Boolean), 'some invalid productId(s)');
// assert(optionIds.every(Boolean), 'some invalid optionId(s)');
console.log("START DATE BEFORE: " + startDate);
let todayDate = Date.now();
if (Date(startDate) < todayDate) {
startDate = todayDate.toString();
}
console.log("START DATE AFTER: " + startDate);
const localDateStart = moment(startDate, dateFormat).format('YYYY-MM-DD');
const localDateEnd = moment(endDate, dateFormat).format('YYYY-MM-DD');
const headers = getHeaders({
apiKey: apiKey,
});
const url = `${endpoint}/availability/calendar`;
let availability = (
await Promise.map(productIds, async (productId, ix) => {
const data = {
productId,
localDateStart,
localDateEnd,
};
return R.path(['data'], await axios({
method: 'get',
url,
data,
headers,
}));
}, { concurrency: CONCURRENCY })
);
availability = await Promise.map(availability,
(avails, ix) => {
return Promise.map(avails,
avail => translateAvailability({
typeDefs: availTypeDefs,
query: availQuery,
rootValue: avail,
variableValues: {
productId: productIds[ix],
optionId: optionIds[ix],
// currency,
unitsWithQuantity: units[ix],
jwtKey: this.jwtKey,
},
}),
);
},
);
return { availability };
}
async availabilityCalendar({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
// ONLY add payload key when absolutely necessary
payload: {
productIds,
startDate,
endDate,
dateFormat,
units
},
typeDefsAndQueries: {
availTypeDefs,
availQuery,
},
}) {
try {
console.log("availabilityCalendar called");
assert(this.jwtKey, 'JWT secret should be set');
assert(
productIds.length === productIds.length,
'mismatched productIds/options length',
);
// assert(
// optionIds.length === units.length,
// 'mismatched options/units length',
// );
assert(productIds.every(Boolean), 'some invalid productId(s)');
// assert(optionIds.every(Boolean), 'some invalid optionId(s)');
console.log("AC: START DATE BEFORE: " + startDate);
let todayDate = moment(new Date(), dateFormat).format('YYYY-MM-DD');
startDate = moment(startDate, dateFormat).format('YYYY-MM-DD');
console.log("AC: TodayDATE: " + todayDate.toString());
console.log("AC: startDate: " + startDate.toString());
if (startDate < todayDate) {
startDate = todayDate.toString();
}
console.log("AC: START DATE AFTER: " + startDate);
const localDateStart = startDate;
const localDateEnd = moment(endDate, dateFormat).format('YYYY-MM-DD');
// console.log("AC: END DATE: " + localDateEnd);
const headers = getHeaders({
apiKey: apiKey,
});
const url = `${endpoint}/availability/calendar`;
const availability = (
await Promise.map(productIds, async (productId, ix) => {
// console.log("PRODUCT ID: " + productId);
const data = {
productId,
// optionId: optionIds[ix],
localDateStart,
localDateEnd,
// units is required here to get the total pricing for the calendar
//units: units[ix].map(u => ({ id: u.unitId, quantity: u.quantity })),
};
const result = await axios({
method: 'get',
url,
data,
headers,
});
return Promise.map(result.data, avail => translateAvailability({
rootValue: avail,
typeDefs: availTypeDefs,
query: availQuery,
}))
}, { concurrency: CONCURRENCY })
);
return { availability };
} catch (err) {
console.log("ERR: " + err);
return false;
}
}
async createBooking({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
// ONLY add payload key when absolutely necessary
payload: {
availabilityKey,
holder,
notes,
reference,
customFieldValues,
// settlementMethod,
rebookingId
},
token,
typeDefsAndQueries,
typeDefsAndQueries: {
bookingTypeDefs,
bookingQuery,
},
}) {
assert(availabilityKey, 'an availability code is required !');
assert(R.path(['name'], holder), 'a holder\' first name is required');
assert(R.path(['surname'], holder), 'a holder\' surname is required');
assert(R.path(['emailAddress'], holder), 'a holder\' email is required');
let uiEndPoint = "https://bms.bonzabiketours.com/";
if (endpoint.includes("bmsstage")) {
uiEndPoint = "https://bmsstage.bonzabiketours.com/";
}
const inputDataForBooking = await jwt.verify(availabilityKey, this.jwtKey);
console.log("OPTION ID: " + inputDataForBooking.optionId);
let isFamilyBooking = false;
if (constants.BOOKING_TYPE.FAMILY == inputDataForBooking.optionId) {
isFamilyBooking = true;
}
console.log("Family Booking: " + isFamilyBooking);
// Get Equipment Details
console.log("customFieldValues: " + JSON.stringify(customFieldValues));
// Example:
// [
// {"field":{"id":7,"title":"Entry Origin Country","subtitle":"Enter the traveler's country of origin",
// "type":"short","isPerUnitItem":false},"value":"USA"},
// {"field":{"id":8,"title":"Entry Travel Agency","subtitle":"Enter the travel agency name",
// "type":"short","isPerUnitItem":false},"value":"BONZA PARTNER USA"}
// {"field":{"id":9,"title":"Is it Famil?","subtitle":"Entry whether this is a famil booking",
// "type":"yes-no","isPerUnitItem":false},"value":false}
// {"field":{"id":"EBIKE_COUNT","title":"Enter e-Bike(s) Required","subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"1"},
// {"field":{"id":"TRAILALONGS_COUNT","title":"Enter Trail Alongs(s) Required","subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"2"},
// {"field":{"id":"KIDDIECARRIER_COUNT","title":"Enter Kiddie Carrier(s) Required","subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"3"},
// {"field":{"id":"SMALLKIDSBIKE_COUNT","title":"Enter Small Kids Bike(s) Required","subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"4"},
//{"field":{"id":"LARGEKIDSBIKE_COUNT","title":"Enter Large Kids Bike(s) Required","subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"5"},
// {"field":{"id":"BABYSEATS_COUNT","title":"Enter Baby Seat(s) Required", "subtitle":"Enter the equipment you need",
// "type":"count","isPerUnitItem":false},"value":"6"}
// ]
let eBikeCount = 0;
let babySeatCount = 0;
let kiddieCarrierCount = 0;
let trailAlongsCount = 0;
let smallKidsBikeCount = 0;
let largeKidsBikeCount = 0;
let adult_equipment = [];
let kid_equipments = [];
let infant_equipment = [];
let originCountry = "";
let travelAgency = "";
let famils = 0;
let sendEmailToPartner = 0;
let sendEmailToGuest = 0;
if (customFieldValues && customFieldValues.length) {
console.log("Len: " + customFieldValues.length);
customFieldValues.forEach(function (unit, d) {
console.log('unit.value: ', unit.value);
switch(parseInt(unit.field.id)) {
case CUSTOM_FIELD_IDS.ORIGINCOUNTRY:
originCountry = !isNilOrEmpty(unit.value) ? ORIGIN_COUNTRIES[unit.value] : "";
break;
case CUSTOM_FIELD_IDS.TRAVELAGENCY:
travelAgency = !isNilOrEmpty(unit.value) ? unit.value : "";
break;
case CUSTOM_FIELD_IDS.FAMILS:
let booleanFamils = !isNilOrEmpty(unit.value) ? unit.value : "";
famils = booleanFamils === true ? 1 : 0;
break;
case CUSTOM_FIELD_IDS.SEND_EMAIL_TO_PARTNER:
let booleanEmailToPartner = !isNilOrEmpty(unit.value) ? unit.value : "";
sendEmailToPartner = booleanEmailToPartner === true ? 1 : 0;
break;
case CUSTOM_FIELD_IDS.SEND_EMAIL_TO_GUEST:
let booleanEmailToGuest = !isNilOrEmpty(unit.value) ? unit.value : "";
sendEmailToGuest = booleanEmailToGuest === true ? 1 : 0;
break;
}
})
const unitItemCFV = customFieldValues.filter(o => (!R.isNil(o.value) && (o.value > 0) && !o.field.isPerUnitItem));
console.log("unitItemCFV: " + JSON.stringify(unitItemCFV));
unitItemCFV.forEach(function (unit, d) {
console.log('unit.field: ', unit.field.title);
let count = parseInt(unit.value);
console.log('Count : ', count);
switch(parseInt(unit.field.id)) {
case CUSTOM_FIELD_IDS.EBIKE_COUNT:
eBikeCount = count;
adult_equipment.push({
id : EQUIPMENT_FIELD_IDS.EBIKE,
count: count
});
break;
case CUSTOM_FIELD_IDS.BABYSEATS_COUNT:
babySeatCount = count;
infant_equipment.push({
id : EQUIPMENT_FIELD_IDS.BABYSEATS,
count: count
});
break;
case CUSTOM_FIELD_IDS.KIDDIECARRIER_COUNT:
kiddieCarrierCount = count;
kid_equipments.push ({
id : EQUIPMENT_FIELD_IDS.KIDDIECARRIER,
count: count
});
break;
case CUSTOM_FIELD_IDS.TRAILALONGS_COUNT:
trailAlongsCount = count;
kid_equipments.push ({
id : EQUIPMENT_FIELD_IDS.TRAILALONGS,
count: count
});
break;
case CUSTOM_FIELD_IDS.SMALLKIDSBIKE_COUNT:
smallKidsBikeCount = count;
kid_equipments.push ({
id : EQUIPMENT_FIELD_IDS.SMALLKIDSBIKE,
count: count
});
break;
case CUSTOM_FIELD_IDS.LARGEKIDSBIKE_COUNT:
largeKidsBikeCount = count;
kid_equipments.push ({
id : EQUIPMENT_FIELD_IDS.LARGEKIDSBIKE,
count: count
});
break;
}
})
}
// SAMPLE value of inputDataForBooking
console.log("inputDataForBooking: " + JSON.stringify(inputDataForBooking));
// {"productId":"11","tourDate":"2024-06-21",
console.log("BEFORE UPDATE unitItems : " + JSON.stringify(inputDataForBooking.unitItems));
// NOTE: Remember that unitItems return DUPLICATES as shown below. Example:
// [{"unitId":"ADULT","noOfPax":1},{"unitId":"CHILD","noOfPax":4},{"unitId":"CHILD","noOfPax":4},
// {"unitId":"CHILD","noOfPax":4},{"unitId":"CHILD","noOfPax":4},{"unitId":"INFANT","noOfPax":1}]
// Remove duplicates
const uniqueUnitItems = inputDataForBooking.unitItems.filter((obj1, i, arr) =>
arr.findIndex(obj2 => (obj2.unitId === obj1.unitId)) === i
)
console.log("uniqueUnitItems : " + JSON.stringify(uniqueUnitItems));
let noOfAdults = 0;
let noOfChildren = 0;
let noOfInfants = 0;
let noOfAdditionalPax = {};
let familyUnit = {};
// Get Pax Count and also update family & equipments
uniqueUnitItems.map(unitItem => {
let paxCount = parseInt(R.path(['noOfPax'], unitItem));
let unitId = String(R.path(['unitId'], unitItem));
console.log("unitItem: " + JSON.stringify(unitItem));
console.log("unitId: " + unitId);
console.log("Pax Count: " + paxCount);
switch (unitId) {
case constants.UNIT_IDS.ADULT:
noOfAdults = paxCount;
console.log('adult_equipment : ', adult_equipment);
unitItem.equipments = adult_equipment;
break;
case constants.UNIT_IDS.CHILD:
noOfChildren = paxCount;
unitItem.equipments = kid_equipments;
break;
case constants.UNIT_IDS.INFANT:
noOfInfants = paxCount;
unitItem.equipments = infant_equipment;
// Family Booking Related
noOfAdditionalPax.noOfBabies = paxCount;
break;
case constants.UNIT_IDS.FAMILY:
noOfAdults += paxCount * 2;
noOfChildren += paxCount * 2;
familyUnit.unitId = unitId;
familyUnit.noOfPax = paxCount;
break;
case constants.UNIT_IDS.FAMILY_ADD_ADULT:
noOfAdults += paxCount;
noOfAdditionalPax.noOfAdditionalAdults = paxCount;
break;
case constants.UNIT_IDS.FAMILY_ADD_CHILD:
noOfChildren += paxCount;
noOfAdditionalPax.noOfAdditionalChildren = paxCount;
break;
}
});
// Assert validations for equipment
if (noOfAdults < eBikeCount) {
assert('','e-Bikes are only available for adults at this time. The number of e-bikes you have added to the booking exceed the number of adults in the booking.');
}
if (noOfInfants < babySeatCount) {
assert('',"baby seats are only available for Infants at this time. The number of baby seats you add to the booking can't be more than the number of infants in the booking.");
}
if (noOfChildren < (kiddieCarrierCount + trailAlongsCount + smallKidsBikeCount + largeKidsBikeCount)) {
assert('',"kids equipments are only available for kids at this time. The number of kid equipments you add to the booking can't be more than the number of kids (excluding infants) in the booking.");
}
// Input data validated, including equipment count. proceed to create the booking
let dataForBooking = {};
dataForBooking.productId = inputDataForBooking.productId;
dataForBooking.tourDate = inputDataForBooking.tourDate;
dataForBooking.originCountry = originCountry;
dataForBooking.travelAgency = travelAgency;
dataForBooking.famils = famils;
if (isFamilyBooking) {
// add additional pax
familyUnit.noOfAdditionalPax = noOfAdditionalPax;
// add family equipments
console.log("adult_equipment : " + JSON.stringify(adult_equipment));
console.log("kid_equipments : " + JSON.stringify(kid_equipments));
console.log("infant_equipment : " + JSON.stringify(infant_equipment));
familyUnit.equipments = adult_equipment.concat(kid_equipments, infant_equipment);
// add family units
console.log("familyUnit : " + JSON.stringify(familyUnit));
dataForBooking.unitItems = [familyUnit];
} else {
dataForBooking.unitItems = uniqueUnitItems;
}
console.log("AFTER UPDATE (data for create booking) : " + JSON.stringify(dataForBooking));
const headers = getHeaders({
apiKey: apiKey,
});
const urlForCreateBooking = `${endpoint}/bookings`;
let booking = R.path(['data'], await axios({
method: 'post',
url: urlForCreateBooking,
data: {
// settlementMethod,
travelerFirstname: `${holder.name}`,
travelerLastname: `${holder.surname}`,
email: R.path(['emailAddress'], holder),
phone: R.pathOr('', ['phone'], holder),
bookingRefID: reference,
bookingPartnerId: bookingPartnerId,
...R.omit(['iat', 'currency'], dataForBooking),
// notes,
},
headers,
}));
const dataForConfirmBooking = {
// locales: R.pathOr(null, ['locales'], holder),
// country: R.pathOr('', ['country'], holder),
paymentType: "Invoice",
bookingPartnerId: bookingPartnerId,
bookingRefID: reference,
sendEmailToPartner: sendEmailToPartner,
sendEmailToGuest: sendEmailToGuest
// settlementMethod,
};
booking = R.path(['data'], await axios({
method: 'post',
url: `${endpoint}/bookings/${booking.orderUUID}/confirm`,
data: dataForConfirmBooking,
headers,
}));
// console.log("booking: " + JSON.stringify(booking));
// console.log("uiEndPoint: " + uiEndPoint);
// Get the booking
let newBooking = R.path(['data'], await axios({
method: 'get',
url: `${endpoint}/bookings/${booking.id}`,
data: dataForConfirmBooking,
headers,
}));
return ({
booking: await translateBooking({
rootValue: {
...newBooking,
uiEndPoint
},
typeDefs: bookingTypeDefs,
query: bookingQuery,
})
});
}
async cancelBooking({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
// ONLY add payload key when absolutely necessary
payload: {
bookingId,
id,
reason,
},
typeDefsAndQueries,
token,
typeDefsAndQueries: {
bookingTypeDefs,
bookingQuery,
},
}) {
assert(!isNilOrEmpty(bookingId) || !isNilOrEmpty(id), 'Invalid booking id');
const headers = getHeaders({
apiKey: apiKey,
});
const url = `${endpoint}/bookings/${bookingId || id}/cancel`;
const booking = R.path(['data'], await axios({
method: 'post',
url,
data: { reason },
headers,
}));
const { products: [product] } = await this.searchProducts({
axios,
typeDefsAndQueries,
token,
payload: {
productId: booking.productId,
}
});
return ({
cancellation: await translateBooking({
rootValue: {
...booking,
product,
option: product.options.find(o => o.optionId === booking.optionId),
},
typeDefs: bookingTypeDefs,
query: bookingQuery,
})
});
}
async searchBooking({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
},
// ONLY add payload key when absolutely necessary
payload: {
bookingRefId,
bookingId,
name,
purchaseDateStart,
purchaseDateEnd,
travelDateStart,
travelDateEnd,
dateFormat,
},
typeDefsAndQueries,
token,
typeDefsAndQueries: {
bookingTypeDefs,
bookingQuery,
},
}) {
assert(
!isNilOrEmpty(bookingId)
|| !isNilOrEmpty(bookingRefId)
|| !isNilOrEmpty(name)
|| !(isNilOrEmpty(travelDateStart) && isNilOrEmpty(travelDateEnd) && isNilOrEmpty(dateFormat))
|| !(isNilOrEmpty(purchaseDateStart) && isNilOrEmpty(purchaseDateEnd) && isNilOrEmpty(dateFormat)),
'at least one parameter is required',
);
const headers = getHeaders({
apiKey: apiKey,
});
let uiEndPoint = "https://bms.bonzabiketours.com/";
if (endpoint.includes("bmsstage")) {
uiEndPoint = "https://bmsstage.bonzabiketours.com/";
}
const bookingsFound = await (async () => {
if (!isNilOrEmpty(bookingId)) {
console.log("BookingID: calling search by URL");
let url = `${endpoint}/bookings/${bookingId}`;
return [R.path(['data'], await axios({
method: 'get',
url,
headers,
}))]
} else if (!isNilOrEmpty(bookingRefId)) {
console.log("BookingRefId: calling search by Booking Ref ID");
let url = `${endpoint}/bookings?bookingRefId=${bookingRefId}`;
return R.path(['data', 'bookings'], await axios({
method: 'get',
url,
headers,
}));
} else {
let url = `${endpoint}/bookings?`;
let lastNameFilter = false;
let travelDateFilter = false;
if (!isNilOrEmpty(name)) {
console.log("Name: calling search by URL");
url += `lastName=${name}`;
lastNameFilter = true;
}
// console.log("travelDateStart: [" + travelDateStart + "]");
// console.log("travelDateEnd: [" + travelDateEnd + "]");
// console.log("purchaseDateStart: [" + purchaseDateStart + "]");
// console.log("purchaseDateEnd: [" + purchaseDateEnd + "]");
// console.log("dateFormat: [" + dateFormat + "]");
if (!(isNilOrEmpty(travelDateStart) && isNilOrEmpty(travelDateEnd)) && !isNilOrEmpty(dateFormat)) {
const localDateStart = moment(travelDateStart, dateFormat).format('YYYY-MM-DD');
const localDateEnd = moment(travelDateEnd, dateFormat).format('YYYY-MM-DD');
console.log("TravelDate: calling search by URL");
travelDateFilter = true;
if (lastNameFilter && !isNilOrEmpty(localDateStart) && !isNilOrEmpty(localDateEnd)) {
url += `&tourDateFrom=${encodeURIComponent(localDateStart)}&tourDateTo=${encodeURIComponent(localDateEnd)}`
} else {
url += `tourDateFrom=${encodeURIComponent(localDateStart)}&tourDateTo=${encodeURIComponent(localDateEnd)}`;
}
}
if (!(isNilOrEmpty(purchaseDateStart) && isNilOrEmpty(purchaseDateEnd)) && !isNilOrEmpty(dateFormat)) {
const localDateStart = moment(purchaseDateStart, dateFormat).format('YYYY-MM-DD');
const localDateEnd = moment(purchaseDateEnd, dateFormat).format('YYYY-MM-DD');
console.log("PurchaseDate: calling search by URL");
if (lastNameFilter || travelDateFilter) {
url += `&purchaseDateFrom=${encodeURIComponent(localDateStart)}&purchaseDateTo=${encodeURIComponent(localDateEnd)}`;
}
else {
url += `purchaseDateFrom=${encodeURIComponent(localDateStart)}&purchaseDateTo=${encodeURIComponent(localDateEnd)}`;
}
}
return R.path(['data', 'bookings'], await axios({
method: 'get',
url,
headers,
}));
}
})();
return (
console.log("bookingsFound: " + JSON.stringify(bookingsFound)),
{
bookings: await Promise.map(bookingsFound, async booking => {
console.log("booking raw: " + JSON.stringify(booking));
return translateBooking({
rootValue: {
...booking,
uiEndPoint
// product,
// option: product.options.find(o => o.optionId === booking.optionId),
},
typeDefs: bookingTypeDefs,
query: bookingQuery,
});
})
});
}
async getCreateBookingFields({
axios,
token: {
endpoint,
apiKey,
bookingPartnerId,
octoEnv,
acceptLanguage,
resellerId,
},
query: {
productId,
unitsSelected,
date,
dateFormat
},
}) {
const headers = getHeaders({
apiKey: apiKey,
endpoint,
octoEnv,
acceptLanguage,
resellerId,
});
const addCustomField = (id, title, subtitle, type) => {
customFieldsToShow.push ({
id: id,
title: title,
subtitle: subtitle,
type: type,
isPerUnitItem: false,
})
}
const getEquipmentCountField = (id, name) => {
customFieldsToShow.push ({
id: id,
title: `Enter ${name}(s) Required`,
subtitle: 'Enter the number of equipment(s) you need',
// TODO (Sachin): This has to be an option with max value based on inventory
type: 'count',
isPerUnitItem: false,
})
}
const getAdultEquipmentFields = () => {
getEquipmentCountField(CUSTOM_FIELD_IDS.EBIKE_COUNT, constants.LABELS.EBIKE);
}
const getChildEquipmentFields = () => {
getEquipmentCountField(CUSTOM_FIELD_IDS.TRAILALONGS_COUNT, constants.LABELS.TRAIL_ALONG);
getEquipmentCountField(CUSTOM_FIELD_IDS.KIDDIECARRIER_COUNT, constants.LABELS.KIDDIE_CARRIER);
getEquipmentCountField(CUSTOM_FIELD_IDS.SMALLKIDSBIKE_COUNT, constants.LABELS.SMALL_KIDS_BIKE);
getEquipmentCountField(CUSTOM_FIELD_IDS.LARGEKIDSBIKE_COUNT, constants.LABELS.LARGE_KIDS_BIKE);
}
const getInfantEquipmentFields = () => {
getEquipmentCountField(CUSTOM_FIELD_IDS.BABYSEATS_COUNT, constants.LABELS.BABY_SEAT);
}
console.log("productId : " + productId);
console.log("date : " + date);
console.log("dateFormat : " + dateFormat);
// EXAMPLE:
// unitsSelected : [{"unitId":"ADULT","quantity":1},{"unitId":"CHILD"},{"unitId":"INFANT"}]
let selectedUnits = JSON.parse(unitsSelected);
console.log("Selected Units: " + JSON.stringify(selectedUnits));
console.log("Len: " + selectedUnits.length);
let customFieldsToShow = [];
// The custom field's type. Supported types: yes-no, short, long, count, and extended-option.
let index = 0;
customFieldsToShow.push ({
id: CUSTOM_FIELD_IDS.ORIGINCOUNTRY,
title: "Entry Origin Country",
subtitle: "Enter the traveler's country of origin",
type: "extended-option",
isPerUnitItem: false,
options: ORIGIN_COUNTRIES.map(name => ({
value: index++,
label: name,
})),
})
addCustomField(CUSTOM_FIELD_IDS.TRAVELAGENCY, "Entry Travel Agency", "Enter the travel agency name", "short");
addCustomField(CUSTOM_FIELD_IDS.FAMILS, "Is this a famil booking?", "Entry whether this is a famil booking", "yes-no");
addCustomField(CUSTOM_FIELD_IDS.SEND_EMAIL_TO_PARTNER, "Send confirmation email to partner?", "Entry whether your want to send booking cofirmation email to partner.", "yes-no");
addCustomField(CUSTOM_FIELD_IDS.SEND_EMAIL_TO_GUEST, "Send confirmation email to guest?", "Entry whether your want to send booking cofirmation email to guest.", "yes-no");
selectedUnits.forEach(function (unit, d) {
console.log('unit.unitId: ', String(unit.unitId));
console.log('unit.quantity: ', unit.quantity);
switch (String(unit.unitId)) {
case constants.UNIT_IDS.ADULT:
getAdultEquipmentFields();
break;
case constants.UNIT_IDS.CHILD:
if (unit.quantity == undefined) {
return {
fields: [],
customFields: []
};
}
getChildEquipmentFields();
break;
case constants.UNIT_IDS.INFANT:
if (unit.quantity == undefined) {
return {
fields: [],
customFields: []
};
}
getInfantEquipmentFields();
break;
case constants.UNIT_IDS.FAMILY:
// Family is 2 Adults and 2 Childred
getAdultEquipmentFields();
getChildEquipmentFields();
break;
};
})
console.log("customFieldsToShow : " + customFieldsToShow.toString());
return {
fields: [],
customFields: customFieldsToShow
}
}
}
module.exports = Plugin;