Skip to content

Commit 91c9b5b

Browse files
authored
Merge pull request #919 from duffelhq/jo-alternative-search-params
Allow guests as payload and receive it in some responses
2 parents f139f09 + b668325 commit 91c9b5b

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

src/Stays/Stays.spec.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import nock from 'nock'
22
import { Duffel } from '../index'
33
import { MOCK_SEARCH_RESULT } from './mocks'
4+
import { StaysSearchParams } from './StaysTypes'
45

56
const duffel = new Duffel({ token: 'mockToken' })
67
describe('Stays', () => {
@@ -10,7 +11,7 @@ describe('Stays', () => {
1011

1112
it('should post location params to /stays/search when `search` is called', async () => {
1213
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
13-
const mockSearchParams = {
14+
const mockSearchParams: StaysSearchParams = {
1415
location: {
1516
radius: 5,
1617
geographic_coordinates: {
@@ -36,7 +37,7 @@ describe('Stays', () => {
3637

3738
it('should post accommodation params to /stays/search when `search` is called', async () => {
3839
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
39-
const mockSearchParams = {
40+
const mockSearchParams: StaysSearchParams = {
4041
accommodation: {
4142
ids: ['acc_12345'],
4243
fetch_rates: true,
@@ -56,4 +57,63 @@ describe('Stays', () => {
5657
const response = await duffel.stays.search(mockSearchParams)
5758
expect(response.data).toEqual(mockResponse.data)
5859
})
60+
61+
it('should alternatively accept `guests` with `accommodation` as a search criteria and post it to /stays/search', async () => {
62+
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
63+
const mockSearchParams: StaysSearchParams = {
64+
accommodation: {
65+
ids: ['acc_12345'],
66+
fetch_rates: true,
67+
},
68+
check_in_date: '2023-10-20',
69+
check_out_date: '2023-10-24',
70+
guests: [
71+
{ type: 'adult' },
72+
{ type: 'adult' },
73+
{ type: 'child', age: 5 },
74+
{ type: 'child', age: 11 },
75+
],
76+
rooms: 2,
77+
}
78+
79+
nock(/(.*)/)
80+
.post('/stays/search', (body) => {
81+
expect(body.data).toEqual(mockSearchParams)
82+
return true
83+
})
84+
.reply(200, mockResponse)
85+
const response = await duffel.stays.search(mockSearchParams)
86+
expect(response.data).toEqual(mockResponse.data)
87+
})
88+
89+
it('should alternatively accept `guests` with `location` as a search criteria and post it to /stays/search', async () => {
90+
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
91+
const mockSearchParams: StaysSearchParams = {
92+
location: {
93+
radius: 5,
94+
geographic_coordinates: {
95+
latitude: 40.73061,
96+
longitude: -73.935242,
97+
},
98+
},
99+
check_in_date: '2023-10-20',
100+
check_out_date: '2023-10-24',
101+
guests: [
102+
{ type: 'adult' },
103+
{ type: 'adult' },
104+
{ type: 'child', age: 5 },
105+
{ type: 'child', age: 11 },
106+
],
107+
rooms: 2,
108+
}
109+
110+
nock(/(.*)/)
111+
.post('/stays/search', (body) => {
112+
expect(body.data).toEqual(mockSearchParams)
113+
return true
114+
})
115+
.reply(200, mockResponse)
116+
const response = await duffel.stays.search(mockSearchParams)
117+
expect(response.data).toEqual(mockResponse.data)
118+
})
59119
})

src/Stays/StaysTypes.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ export interface StaysQuote {
530530
* The number of rooms this quote is for
531531
*/
532532
rooms: number
533+
534+
/*
535+
* A list of guests representing the requested occupancy for this quote
536+
*/
537+
538+
guests: Array<Guest>
533539
}
534540

535541
export type StaysBookingStatus = 'confirmed' | 'cancelled'
@@ -606,12 +612,20 @@ export interface StaysBooking {
606612
key_collection: StaysBookingKeyCollection | null
607613
}
608614

609-
interface CommonStaysSearchParams {
615+
// Age is not required for adult, but required for child
616+
type Adult = { type: 'adult'; age?: number }
617+
type Child = { type: 'child'; age: number }
618+
619+
export type Guest = Adult | Child
620+
621+
type OccupancyCriteria = {
622+
rooms: number
623+
} & ({ adults: number } | { guests: Array<Guest> })
624+
625+
type CommonStaysSearchParams = {
610626
check_in_date: string
611627
check_out_date: string
612-
adults: number
613-
rooms: number
614-
}
628+
} & OccupancyCriteria
615629

616630
type LocationSearchParams = {
617631
location: {
@@ -639,4 +653,5 @@ export interface StaysSearchResult {
639653
accommodation: StaysAccommodation
640654
adults: number
641655
rooms: number
656+
guests: Array<Guest>
642657
}

src/Stays/mocks.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ export const MOCK_SEARCH_RESULT: StaysSearchResult = {
155155
id: 'sta_something',
156156
check_in_date: '2023-03-24',
157157
check_out_date: '2023-03-28',
158-
adults: 1,
158+
adults: 2,
159159
rooms: 1,
160+
guests: [{ type: 'adult' }, { type: 'adult' }],
160161
}
161162

162163
export const MOCK_BOOKING: StaysBooking = {
@@ -213,6 +214,7 @@ export const MOCK_QUOTE: StaysQuote = {
213214
due_at_accommodation_currency: 'USD',
214215
supported_loyalty_programme: 'duffel_hotel_group_rewards',
215216
card_component_key: null,
216-
adults: 1,
217+
adults: 2,
217218
rooms: 1,
219+
guests: [{ type: 'adult' }, { type: 'adult' }],
218220
}

0 commit comments

Comments
 (0)