Skip to content

Commit

Permalink
Merge pull request #95 from sygic-travel/hotel-places-filter
Browse files Browse the repository at this point in the history
getHotels: Places added to filter
  • Loading branch information
NeosinneR authored Sep 14, 2017
2 parents 778aa4d + 4912c9b commit f1982e9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export namespace Hotels {
maxPrice?: number | null;
minPrice?: number | null;
minReviewScore?: number | null;
places?: string[] | null;
bounds?: Geo.Bounds | null;
mapTileBounds?: string[] | null;
stars?: number[] | null;
Expand Down
6 changes: 4 additions & 2 deletions src/Hotels/DataAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ describe('HotelsDataAccess', () => {
const hotelsFilter: HotelsFilter = new HotelsFilter({
adults: 2,
checkIn: '2017-11-11',
checkOut: '2017-11-12'
checkOut: '2017-11-12',
places: ['poi:1', 'poi:2']
});

chai.expect(await Dao.getHotels(hotelsFilter))
.to.deep.equal(HotelsResults.availableHotels);
chai.expect(apiStub.callCount).to.equal(1);
chai.expect(apiStub.getCall(0).args[0]).to.equal('hotels/list/?adults=2&check_in=2017-11-11&check_out=2017-11-12');
chai.expect(apiStub.getCall(0).args[0])
.to.equal('hotels/list/?adults=2&check_in=2017-11-11&check_out=2017-11-12&places=poi%3A1%7Cpoi%3A2');
});

it('should use map tiles if bounds and zoom are passed', async () => {
Expand Down
40 changes: 38 additions & 2 deletions src/Hotels/Filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ describe('HotelsFilter', () => {
};
chai.expect(createFilter).to.throw('Adults count is mandatory.');
});
it('should raise error when bounds passed witn mapTileBounds', () => {
it('should raise error on missing bounds', () => {
const createFilter = () => {
return new HotelsFilter({
adults: 1,
checkIn: '2017-11-11',
checkOut: '2017-11-12',
});
};
chai.expect(createFilter).to.throw(
'Bounds, mapTileBounds and places have to be used exclusively and one of them has to be present.'
);
});
it('should raise error when bounds passed with mapTileBounds', () => {
const createFilter = () => {
return new HotelsFilter({
adults: 1,
Expand All @@ -77,14 +89,36 @@ describe('HotelsFilter', () => {
mapTileBounds: ['1', '2']
});
};
chai.expect(createFilter).to.throw('Bounds and mapTileBounds have to be used exclusively.');
chai.expect(createFilter).to.throw(
'Bounds, mapTileBounds and places have to be used exclusively and one of them has to be present.'
);
});
it('should raise error when bounds passed with places', () => {
const createFilter = () => {
return new HotelsFilter({
adults: 1,
checkIn: '2017-11-11',
checkOut: '2017-11-12',
bounds: {
south: 1,
west: 2,
north: 3,
east: 4,
},
places: ['poi:1', 'poi:2']
});
};
chai.expect(createFilter).to.throw(
'Bounds, mapTileBounds and places have to be used exclusively and one of them has to be present.'
);
});
it('should raise error when same dates are passed', () => {
const createFilter = () => {
return new HotelsFilter({
adults: 1,
checkIn: '2017-11-11',
checkOut: '2017-11-11',
mapTileBounds: ['123', '321'],
});
};
chai.expect(createFilter).to.throw('Invalid checkIn/checkOut combination.');
Expand All @@ -95,6 +129,7 @@ describe('HotelsFilter', () => {
adults: 1,
checkIn: 'xxx',
checkOut: 'fdfdfd',
mapTileBounds: ['123', '321'],
});
};
chai.expect(createFilter).to.throw('Invalid checkIn date.');
Expand All @@ -105,6 +140,7 @@ describe('HotelsFilter', () => {
adults: 1,
checkIn: '2017-11-11',
checkOut: 'fdfdfd',
places: ['poi:1'],
});
};
chai.expect(createFilter).to.throw('Invalid checkOut date.');
Expand Down
21 changes: 14 additions & 7 deletions src/Hotels/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface HotelsFilterJSON {
maxPrice?: number | null;
minPrice?: number | null;
minReviewScore?: number | null;
places?: string[] | null;
bounds?: Bounds | null;
mapTileBounds?: string[] | null;
stars?: number[] | null;
Expand All @@ -29,6 +30,7 @@ export interface HotelsFilterQuery {
max_price?: number;
min_price?: number;
min_review_score?: number;
places?: string;
bounds?: string;
map_tile_bounds?: string;
stars?: string;
Expand All @@ -48,15 +50,16 @@ export class HotelsFilter {
protected _maxPrice?: number | null;
protected _minPrice?: number | null;
protected _minReviewScore?: number | null;
private _bounds?: Bounds | null;
protected _places?: string[] | null;
protected _bounds?: Bounds | null;
protected _mapTileBounds?: string[] | null;
protected _stars?: number[] | null;
protected _currency?: string | null;
protected _propertyTypes?: string[] | null;
protected _hotelFacilities?: string[] | null;
protected _roomFacilities?: string[] | null;
protected _limit?: number;
private _zoom?: number;
protected _zoom?: number;

constructor(filter: HotelsFilterJSON) {
this._checkIn = filter.checkIn;
Expand All @@ -67,6 +70,7 @@ export class HotelsFilter {
this._minPrice = filter.minPrice;
this._minReviewScore = filter.minReviewScore;
this._bounds = filter.bounds;
this._places = filter.places;
this._mapTileBounds = filter.mapTileBounds;
this._stars = filter.stars;
this._currency = filter.currency;
Expand Down Expand Up @@ -104,6 +108,9 @@ export class HotelsFilter {
if (this._minReviewScore) {
query.min_review_score = this._minReviewScore;
}
if (this._places) {
query.places = this._places.join('|');
}
if (this._bounds) {
query.bounds = this._bounds.south + ',' + this._bounds.west + ',' + this._bounds.north + ',' + this._bounds.east;
}
Expand Down Expand Up @@ -146,8 +153,11 @@ export class HotelsFilter {
}

private validate(): void {
if (this._bounds && this._mapTileBounds) {
throw new Error('Bounds and mapTileBounds have to be used exclusively.');
if (!this._adults) {
throw new Error('Adults count is mandatory.');
}
if ([this._bounds, this._mapTileBounds, this._places].filter((it) => it).length !== 1) {
throw new Error('Bounds, mapTileBounds and places have to be used exclusively and one of them has to be present.');
}
const chInDate = new Date(this._checkIn);
const chOutDate = new Date(this._checkOut);
Expand All @@ -160,8 +170,5 @@ export class HotelsFilter {
if (chOutDate <= chInDate) {
throw new Error('Invalid checkIn/checkOut combination.');
}
if (!this._adults) {
throw new Error('Adults count is mandatory.');
}
}
}

0 comments on commit f1982e9

Please sign in to comment.