Skip to content

Commit baafe1d

Browse files
author
Tim Rogers
authored
Merge pull request #396 from duffelhq/timrogers/order-cancellations-list
feat(order-cancellations): add support for listing Order Cancellations
2 parents 2ab9298 + d7d584e commit baafe1d

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

src/booking/OrderCancellations/OrderCancellations.spec.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nock from 'nock'
22
import { Client } from '../../Client'
3-
import { mockOrderCancellations } from './mockOrderCancellations'
3+
import { mockOrderCancellation } from './mockOrderCancellations'
44
import { OrderCancellations } from './OrderCancellations'
55

66
describe('OrderCancellations', () => {
@@ -11,7 +11,7 @@ describe('OrderCancellations', () => {
1111
test('should create a order cancellation', async () => {
1212
nock(/(.*)/)
1313
.post(`/air/order_cancellations`)
14-
.reply(200, { data: mockOrderCancellations })
14+
.reply(200, { data: mockOrderCancellation })
1515

1616
const response = await new OrderCancellations(
1717
new Client({ token: 'mockToken' })
@@ -24,13 +24,59 @@ describe('OrderCancellations', () => {
2424
test('should confirm the order cancellation', async () => {
2525
nock(/(.*)/)
2626
.post(
27-
`/air/order_cancellations/${mockOrderCancellations.id}/actions/confirm`
27+
`/air/order_cancellations/${mockOrderCancellation.id}/actions/confirm`
2828
)
29-
.reply(200, { data: mockOrderCancellations })
29+
.reply(200, { data: mockOrderCancellation })
3030

3131
const response = await new OrderCancellations(
3232
new Client({ token: 'mockToken' })
3333
).confirm('ore_00009qzZWzjDipIkqpaUAj')
3434
expect(response.data?.order_id).toBe('ord_00009hthhsUZ8W4LxQgkjo')
3535
})
36+
37+
test('should get a page of order cancellations', async () => {
38+
nock(/(.*)/)
39+
.get(`/air/order_cancellations`)
40+
.query((queryObject) => {
41+
expect(queryObject?.order_id).toBe('ord_123')
42+
expect(queryObject?.limit).toEqual('1')
43+
return true
44+
})
45+
.reply(200, {
46+
data: [mockOrderCancellation],
47+
meta: { limit: 1, before: null, after: null },
48+
})
49+
50+
const response = await new OrderCancellations(
51+
new Client({ token: 'mockToken' })
52+
).list({
53+
order_id: 'ord_123',
54+
limit: 1,
55+
})
56+
expect(response.data).toHaveLength(1)
57+
expect(response.data[0].id).toBe(mockOrderCancellation.id)
58+
})
59+
60+
test('should get all order cancellations paginated', async () => {
61+
nock(/(.*)/)
62+
.get(`/air/order_cancellations`)
63+
.query((queryObject) => {
64+
expect(queryObject?.order_id).toBe('ord_123')
65+
return true
66+
})
67+
.reply(200, {
68+
data: [mockOrderCancellation],
69+
meta: { limit: 50, before: null, after: null },
70+
})
71+
72+
const response = await new OrderCancellations(
73+
new Client({ token: 'mockToken' })
74+
).listWithGenerator({
75+
order_id: 'ord_123',
76+
})
77+
78+
for await (const page of response) {
79+
expect(page.data.id).toBe(mockOrderCancellation.id)
80+
}
81+
})
3682
})

src/booking/OrderCancellations/OrderCancellations.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CreateOrderCancellation,
44
DuffelResponse,
55
OrderCancellation,
6+
ListOrderCancellationsParams,
67
} from '../../types'
78

89
export class OrderCancellations extends Resource {
@@ -24,6 +25,26 @@ export class OrderCancellations extends Resource {
2425
public get = async (id: string): Promise<DuffelResponse<OrderCancellation>> =>
2526
this.request({ method: 'GET', path: `${this.path}/${id}` })
2627

28+
/**
29+
* Retrieves a page of order cancellations. The results may be returned in any order.
30+
* @param {Object.<ListOrderCancellationsParams>} params - Endpoint options (optional: limit, after, before, order_id)
31+
* @link https://duffel.com/docs/api/order-cancellations/get-order-cancellations
32+
*/
33+
public list = (
34+
params?: ListOrderCancellationsParams
35+
): Promise<DuffelResponse<OrderCancellation[]>> =>
36+
this.request({ method: 'GET', path: this.path, params })
37+
38+
/**
39+
* Retrieves a generator of all order cancellations. The results may be returned in any order.
40+
* @param {Object.<ListOrderCancellationsParams>} params - Endpoint options (optional: limit, after, before, order_id)
41+
* @link https://duffel.com/docs/api/order-cancellations/get-order-cancellations
42+
*/
43+
public listWithGenerator = (
44+
params?: ListOrderCancellationsParams
45+
): AsyncGenerator<DuffelResponse<OrderCancellation>, void, unknown> =>
46+
this.paginatedRequest({ path: this.path, params })
47+
2748
/**
2849
* Create order cancellation
2950
* @description To begin the process of cancelling an order you need to create an order cancellation.

src/booking/OrderCancellations/OrderCancellationsTypes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import { PaginationMeta } from '../../types'
2+
13
export interface CreateOrderCancellation {
24
/**
35
* Duffel's unique identifier for the order
46
*/
57
order_id: string
68
}
79

10+
export interface ListOrderCancellationsParams extends PaginationMeta {
11+
/**
12+
* Duffel's unique identifier for the order, returned when it was created
13+
*/
14+
order_id?: string
15+
}
16+
817
export interface OrderCancellation {
918
/**
1019
* The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) datetime that indicates when the order cancellation was confirmed

src/booking/OrderCancellations/mockOrderCancellations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OrderCancellation } from '../../types'
22

3-
export const mockOrderCancellations: OrderCancellation = {
3+
export const mockOrderCancellation: OrderCancellation = {
44
refund_to: 'arc_bsp_cash',
55
refund_currency: 'GBP',
66
refund_amount: '90.80',

0 commit comments

Comments
 (0)