Skip to content

Commit

Permalink
Merge pull request #919 from duffelhq/jo-alternative-search-params
Browse files Browse the repository at this point in the history
Allow guests as payload and receive it in some responses
  • Loading branch information
jekku authored May 31, 2024
2 parents f139f09 + b668325 commit 91c9b5b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
64 changes: 62 additions & 2 deletions src/Stays/Stays.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import nock from 'nock'
import { Duffel } from '../index'
import { MOCK_SEARCH_RESULT } from './mocks'
import { StaysSearchParams } from './StaysTypes'

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

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

it('should post accommodation params to /stays/search when `search` is called', async () => {
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
const mockSearchParams = {
const mockSearchParams: StaysSearchParams = {
accommodation: {
ids: ['acc_12345'],
fetch_rates: true,
Expand All @@ -56,4 +57,63 @@ describe('Stays', () => {
const response = await duffel.stays.search(mockSearchParams)
expect(response.data).toEqual(mockResponse.data)
})

it('should alternatively accept `guests` with `accommodation` as a search criteria and post it to /stays/search', async () => {
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
const mockSearchParams: StaysSearchParams = {
accommodation: {
ids: ['acc_12345'],
fetch_rates: true,
},
check_in_date: '2023-10-20',
check_out_date: '2023-10-24',
guests: [
{ type: 'adult' },
{ type: 'adult' },
{ type: 'child', age: 5 },
{ type: 'child', age: 11 },
],
rooms: 2,
}

nock(/(.*)/)
.post('/stays/search', (body) => {
expect(body.data).toEqual(mockSearchParams)
return true
})
.reply(200, mockResponse)
const response = await duffel.stays.search(mockSearchParams)
expect(response.data).toEqual(mockResponse.data)
})

it('should alternatively accept `guests` with `location` as a search criteria and post it to /stays/search', async () => {
const mockResponse = { data: { results: [MOCK_SEARCH_RESULT] } }
const mockSearchParams: StaysSearchParams = {
location: {
radius: 5,
geographic_coordinates: {
latitude: 40.73061,
longitude: -73.935242,
},
},
check_in_date: '2023-10-20',
check_out_date: '2023-10-24',
guests: [
{ type: 'adult' },
{ type: 'adult' },
{ type: 'child', age: 5 },
{ type: 'child', age: 11 },
],
rooms: 2,
}

nock(/(.*)/)
.post('/stays/search', (body) => {
expect(body.data).toEqual(mockSearchParams)
return true
})
.reply(200, mockResponse)
const response = await duffel.stays.search(mockSearchParams)
expect(response.data).toEqual(mockResponse.data)
})
})
23 changes: 19 additions & 4 deletions src/Stays/StaysTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ export interface StaysQuote {
* The number of rooms this quote is for
*/
rooms: number

/*
* A list of guests representing the requested occupancy for this quote
*/

guests: Array<Guest>
}

export type StaysBookingStatus = 'confirmed' | 'cancelled'
Expand Down Expand Up @@ -606,12 +612,20 @@ export interface StaysBooking {
key_collection: StaysBookingKeyCollection | null
}

interface CommonStaysSearchParams {
// Age is not required for adult, but required for child
type Adult = { type: 'adult'; age?: number }
type Child = { type: 'child'; age: number }

export type Guest = Adult | Child

type OccupancyCriteria = {
rooms: number
} & ({ adults: number } | { guests: Array<Guest> })

type CommonStaysSearchParams = {
check_in_date: string
check_out_date: string
adults: number
rooms: number
}
} & OccupancyCriteria

type LocationSearchParams = {
location: {
Expand Down Expand Up @@ -639,4 +653,5 @@ export interface StaysSearchResult {
accommodation: StaysAccommodation
adults: number
rooms: number
guests: Array<Guest>
}
6 changes: 4 additions & 2 deletions src/Stays/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ export const MOCK_SEARCH_RESULT: StaysSearchResult = {
id: 'sta_something',
check_in_date: '2023-03-24',
check_out_date: '2023-03-28',
adults: 1,
adults: 2,
rooms: 1,
guests: [{ type: 'adult' }, { type: 'adult' }],
}

export const MOCK_BOOKING: StaysBooking = {
Expand Down Expand Up @@ -213,6 +214,7 @@ export const MOCK_QUOTE: StaysQuote = {
due_at_accommodation_currency: 'USD',
supported_loyalty_programme: 'duffel_hotel_group_rewards',
card_component_key: null,
adults: 1,
adults: 2,
rooms: 1,
guests: [{ type: 'adult' }, { type: 'adult' }],
}

0 comments on commit 91c9b5b

Please sign in to comment.