Skip to content

Commit 2939c51

Browse files
committed
Added endpoint and apikey parameters
1 parent b2e9733 commit 2939c51

File tree

2 files changed

+94
-54
lines changed

2 files changed

+94
-54
lines changed

index.js

+71-40
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,21 @@ class Plugin {
4444
this[attr] = value;
4545
});
4646
this.tokenTemplate = () => ({
47+
endpoint: {
48+
type: 'text',
49+
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,
50+
default: 'https://bmsstage.bonzabiketours.com:3001/octo/v1',
51+
description: 'The Bonza API endpoint URL',
52+
},
53+
apiKey: {
54+
type: 'text',
55+
regExp: /[0-9a-f]{48}/,
56+
description: 'The authentication key for Bonza API endpoint',
57+
},
4758
bookingPartnerId: {
4859
type: 'text',
4960
regExp: /^\d+$/,
50-
description: 'The booking partner id',
61+
description: 'The Bonza booking partner id',
5162
}
5263
});
5364
this.errorPathsAxiosErrors = () => ([ // axios triggered errors
@@ -57,17 +68,20 @@ class Plugin {
5768
}
5869

5970
async validateToken({
60-
axios,
61-
token: {
62-
bookingPartnerId,
63-
},
64-
})
71+
axios,
72+
token: {
73+
endpoint,
74+
apiKey,
75+
bookingPartnerId,
76+
},
77+
})
6578
{
66-
// console.log("API KEY in plugin : " + this.apiKey);
67-
// console.log("ENDPOINT in plugin : " + this.endpoint);
68-
const url = `${this.endpoint}/products`;
79+
// console.log("API KEY in plugin : " + apiKey);
80+
// console.log("ENDPOINT in plugin : " + endpoint);
81+
// console.log("BOOKIGN PARTNER ID in plugin : " + bookingPartnerId);
82+
const url = `${endpoint}/products`;
6983
const headers = getHeaders({
70-
apiKey:this.apiKey,
84+
apiKey: apiKey,
7185
});
7286
try {
7387
const suppliers = R.path(['data'], await axios({
@@ -85,15 +99,17 @@ class Plugin {
8599
async searchProducts({
86100
axios,
87101
token: {
102+
endpoint,
103+
apiKey,
88104
bookingPartnerId,
89-
},
105+
},
90106
payload,
91107
typeDefsAndQueries: {
92108
productTypeDefs,
93109
productQuery,
94110
},
95111
}) {
96-
let url = `${this.endpoint}/products`;
112+
let url = `${endpoint}/products`;
97113
console.log("URL: " + url);
98114
if (!isNilOrEmpty(payload)) {
99115
if (payload.productId) {
@@ -104,7 +120,7 @@ class Plugin {
104120

105121
console.log("URL: " + url);
106122
const headers = getHeaders({
107-
apiKey: this.apiKey,
123+
apiKey: apiKey,
108124
});
109125
let results = R.pathOr([], ['data'], await axios({
110126
method: 'get',
@@ -137,21 +153,25 @@ class Plugin {
137153
return ({ products });
138154
}
139155

140-
async searchQuote({
141-
token: {
142-
bookingPartnerId,
143-
},
144-
payload: {
145-
productIds,
146-
optionIds,
147-
},
148-
}) {
149-
return { quote: [] };
150-
}
156+
// async searchQuote({
157+
// token: {
158+
// endpoint,
159+
// apiKey,
160+
// bookingPartnerId,
161+
// },
162+
// payload: {
163+
// productIds,
164+
// optionIds,
165+
// },
166+
// }) {
167+
// return { quote: [] };
168+
// }
151169

152170
async searchAvailability({
153171
axios,
154172
token: {
173+
endpoint,
174+
apiKey,
155175
bookingPartnerId,
156176
},
157177
// ONLY add payload key when absolutely necessary
@@ -188,10 +208,10 @@ class Plugin {
188208
const localDateStart = moment(startDate, dateFormat).format('YYYY-MM-DD');
189209
const localDateEnd = moment(endDate, dateFormat).format('YYYY-MM-DD');
190210
const headers = getHeaders({
191-
apiKey: this.apiKey,
211+
apiKey: apiKey,
192212
});
193213

194-
const url = `${this.endpoint}/availability/calendar`;
214+
const url = `${endpoint}/availability/calendar`;
195215
let availability = (
196216
await Promise.map(productIds, async (productId, ix) => {
197217
const data = {
@@ -231,6 +251,8 @@ class Plugin {
231251
async availabilityCalendar({
232252
axios,
233253
token: {
254+
endpoint,
255+
apiKey,
234256
bookingPartnerId,
235257
},
236258
// ONLY add payload key when absolutely necessary
@@ -247,6 +269,8 @@ class Plugin {
247269
},
248270
}) {
249271
try {
272+
console.log("availabilityCalendar called");
273+
250274
assert(this.jwtKey, 'JWT secret should be set');
251275
assert(
252276
productIds.length === productIds.length,
@@ -273,10 +297,10 @@ class Plugin {
273297

274298
// console.log("AC: END DATE: " + localDateEnd);
275299
const headers = getHeaders({
276-
apiKey: this.apiKey,
300+
apiKey: apiKey,
277301
});
278302

279-
const url = `${this.endpoint}/availability/calendar`;
303+
const url = `${endpoint}/availability/calendar`;
280304
const availability = (
281305
await Promise.map(productIds, async (productId, ix) => {
282306
// console.log("PRODUCT ID: " + productId);
@@ -311,6 +335,8 @@ class Plugin {
311335
async createBooking({
312336
axios,
313337
token: {
338+
endpoint,
339+
apiKey,
314340
bookingPartnerId,
315341
},
316342
// ONLY add payload key when absolutely necessary
@@ -525,10 +551,10 @@ class Plugin {
525551
console.log("AFTER UPDATE (data for create booking) : " + JSON.stringify(dataForBooking));
526552

527553
const headers = getHeaders({
528-
apiKey: this.apiKey,
554+
apiKey: apiKey,
529555
});
530556

531-
const urlForCreateBooking = `${this.endpoint}/bookings`;
557+
const urlForCreateBooking = `${endpoint}/bookings`;
532558
let booking = R.path(['data'], await axios({
533559
method: 'post',
534560
url: urlForCreateBooking,
@@ -555,7 +581,7 @@ class Plugin {
555581
};
556582
booking = R.path(['data'], await axios({
557583
method: 'post',
558-
url: `${this.endpoint}/bookings/${booking.orderUUID}/confirm`,
584+
url: `${endpoint}/bookings/${booking.orderUUID}/confirm`,
559585
data: dataForConfirmBooking,
560586
headers,
561587
}));
@@ -565,7 +591,7 @@ class Plugin {
565591
// Get the booking
566592
let newBooking = R.path(['data'], await axios({
567593
method: 'get',
568-
url: `${this.endpoint}/bookings/${booking.id}`,
594+
url: `${endpoint}/bookings/${booking.id}`,
569595
data: dataForConfirmBooking,
570596
headers,
571597
}));
@@ -583,6 +609,8 @@ class Plugin {
583609
async cancelBooking({
584610
axios,
585611
token: {
612+
endpoint,
613+
apiKey,
586614
bookingPartnerId,
587615
},
588616
// ONLY add payload key when absolutely necessary
@@ -600,9 +628,9 @@ class Plugin {
600628
}) {
601629
assert(!isNilOrEmpty(bookingId) || !isNilOrEmpty(id), 'Invalid booking id');
602630
const headers = getHeaders({
603-
apiKey: this.apiKey,
631+
apiKey: apiKey,
604632
});
605-
const url = `${this.endpoint}/bookings/${bookingId || id}/cancel`;
633+
const url = `${endpoint}/bookings/${bookingId || id}/cancel`;
606634
const booking = R.path(['data'], await axios({
607635
method: 'post',
608636
url,
@@ -633,6 +661,8 @@ class Plugin {
633661
async searchBooking({
634662
axios,
635663
token: {
664+
endpoint,
665+
apiKey,
636666
bookingPartnerId,
637667
},
638668
// ONLY add payload key when absolutely necessary
@@ -663,28 +693,28 @@ class Plugin {
663693
);
664694

665695
const headers = getHeaders({
666-
apiKey: this.apiKey,
696+
apiKey: apiKey,
667697
});
668698

669699
const bookingsFound = await (async () => {
670700
if (!isNilOrEmpty(bookingId)) {
671701
console.log("BookingID: calling search by URL");
672-
let url = `${this.endpoint}/bookings/${bookingId}`;
702+
let url = `${endpoint}/bookings/${bookingId}`;
673703
return [R.path(['data'], await axios({
674704
method: 'get',
675705
url,
676706
headers,
677707
}))]
678708
} else if (!isNilOrEmpty(bookingRefId)) {
679709
console.log("BookingRefId: calling search by Booking Ref ID");
680-
let url = `${this.endpoint}/bookings?bookingRefId=${bookingRefId}`;
710+
let url = `${endpoint}/bookings?bookingRefId=${bookingRefId}`;
681711
return R.path(['data', 'bookings'], await axios({
682712
method: 'get',
683713
url,
684714
headers,
685715
}));
686716
} else {
687-
let url = `${this.endpoint}/bookings?`;
717+
let url = `${endpoint}/bookings?`;
688718
let lastNameFilter = false;
689719
let travelDateFilter = false;
690720
if (!isNilOrEmpty(name)) {
@@ -750,8 +780,9 @@ class Plugin {
750780
async getCreateBookingFields({
751781
axios,
752782
token: {
753-
apiKey,
754783
endpoint,
784+
apiKey,
785+
bookingPartnerId,
755786
octoEnv,
756787
acceptLanguage,
757788
resellerId,
@@ -764,7 +795,7 @@ class Plugin {
764795
},
765796
}) {
766797
const headers = getHeaders({
767-
apiKey: this.apiKey,
798+
apiKey: apiKey,
768799
endpoint,
769800
octoEnv,
770801
acceptLanguage,

index.test.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@ const typeDefsAndQueries = {
2727
};
2828

2929
const app = new Plugin({
30-
endpoint: process.env.ti2_bonza_endpoint,
3130
jwtKey: process.env.ti2_bonza_jwtKey,
32-
apiKey: process.env.ti2_bonza_apiKey,
3331
});
3432
const rnd = arr => arr[Math.floor(Math.random() * arr.length)];
3533

3634
describe('search tests', () => {
37-
let products;
35+
// let products;
3836
let testProduct = {
3937
productId: '11',
4038
productName: 'Sydney Classic Tour',
4139
};
4240
const token = {
43-
// apiKey: process.env.ti2_bonza_apiKey,
41+
endpoint: process.env.ti2_bonza_endpoint,
42+
apiKey: process.env.ti2_bonza_apiKey,
4443
bookingPartnerId: 181
4544
};
4645
const dateFormat = 'DD/MM/YYYY';
@@ -58,21 +57,33 @@ describe('search tests', () => {
5857
});
5958
expect(retVal).toBeTruthy();
6059
});
61-
// it('invalid token', async () => {
62-
// const retVal = await app.validateToken({
63-
// axios,
64-
// token: { apiKey: 'invalid token' },
65-
// });
66-
// expect(retVal).toBeFalsy();
67-
// });
60+
it('invalid token', async () => {
61+
const retVal = await app.validateToken({
62+
axios,
63+
token: { apiKey: 'invalid token' },
64+
});
65+
expect(retVal).toBeFalsy();
66+
});
6867
});
6968
describe('template tests', () => {
7069
let template;
7170
it('get the template', async () => {
7271
template = await app.tokenTemplate();
7372
const rules = Object.keys(template);
73+
expect(rules).toContain('endpoint');
74+
expect(rules).toContain('apiKey');
7475
expect(rules).toContain('bookingPartnerId');
7576
});
77+
it('endpoint', () => {
78+
const endpoint = template.endpoint.regExp;
79+
expect(endpoint.test('something')).toBeFalsy();
80+
expect(endpoint.test(token.endpoint)).toBeTruthy();
81+
});
82+
it('apiKey', () => {
83+
const apiKey = template.apiKey.regExp;
84+
expect(apiKey.test('asfsdf something')).toBeFalsy();
85+
expect(apiKey.test(token.apiKey)).toBeTruthy();
86+
});
7687
it('bookingPartnerId', () => {
7788
const bookingPartnerId = template.bookingPartnerId.regExp;
7889
expect(bookingPartnerId.test('something')).toBeFalsy();
@@ -173,9 +184,7 @@ describe('search tests', () => {
173184
const fullName = faker.name.findName().split(' ');
174185
const retVal = await app.createBooking({
175186
axios,
176-
token: {
177-
bookingPartnerId: 181
178-
},
187+
token,
179188
typeDefsAndQueries,
180189
payload: {
181190
availabilityKey: jwt.sign(({

0 commit comments

Comments
 (0)