Skip to content

Commit 2a3fe05

Browse files
authored
Merge pull request #1515 from commercetools/1292-add-byordernumber-support-on-order-service
feat(request-builder): add orderNumber support on order service
2 parents 48bf8b6 + 9f874c4 commit 2a3fe05

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

packages/api-request-builder/src/create-service.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const requiredDefinitionProps = ['type', 'endpoint', 'features']
3535

3636
function getIdOrKey(params: Object): string {
3737
if (params.id) return `/${params.id}`
38+
if (params.orderNumber) return `/order-number=${params.orderNumber}`
3839
if (params.key && !params.container) return `/key=${params.key}`
3940
if (params.key && params.container)
4041
return `/${params.container}/${params.key}`

packages/api-request-builder/src/default-params.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export function setParams(params: ServiceBuilderParams) {
133133
'withTotal',
134134
'applyOrderEditTo',
135135
'container',
136+
'orderNumber',
136137
]
137138
Object.keys(params).forEach((key: string) => {
138139
if (!knownKeys.includes(key)) throw new Error(`Unknown key "${key}"`)
@@ -173,6 +174,7 @@ export function setParams(params: ServiceBuilderParams) {
173174
if (hasKey(params, 'priceCustomerGroup'))
174175
this.priceCustomerGroup(params.priceCustomerGroup)
175176
if (hasKey(params, 'priceChannel')) this.priceChannel(params.priceChannel)
177+
if (hasKey(params, 'orderNumber')) this.byOrderNumber(params.orderNumber)
176178

177179
// query-search
178180
if (params.text) this.text(params.text.value, params.text.language)

packages/api-request-builder/src/query-id.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ export function byCustomerId(custId: string): Object {
6969
return this
7070
}
7171

72+
/**
73+
* Set the given `orderNumber` to the `orderNumber` internal state of the service instance.
74+
* For querying orders
75+
*
76+
* @param {number} orderNumber - An order number
77+
* @throws If `orderNumber` is missing or invalid
78+
* @return {Object} The instance of the service, can be chained.
79+
*/
80+
export function byOrderNumber(orderNumber: number): Object {
81+
if (typeof orderNumber !== 'number')
82+
throw new Error(
83+
'Required argument for `byOrderNumber` is missing or invalid'
84+
)
85+
86+
this.params.orderNumber = orderNumber
87+
return this
88+
}
89+
7290
/**
7391
* Set the given `id` to the `cartId` internal state of the service instance.
7492
* For querying shipping methods by cart id

packages/api-request-builder/test/create-service.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const expectedServiceProperties = [
3737
'byCurrency',
3838
'byCountry',
3939
'byState',
40+
'byOrderNumber',
4041
]
4142
const projectKey = 'my-project1'
4243

@@ -423,6 +424,12 @@ describe('createService', () => {
423424
)
424425
})
425426

427+
test('should mix orderNumber and queryParams', () => {
428+
expect(service.byOrderNumber(123).expand('baz').build()).toBe(
429+
'/my-project1/test/order-number=123?expand=baz'
430+
)
431+
})
432+
426433
test('should mix cartId and queryParams', () => {
427434
expect(service.byCartId('foo').expand('baz').build()).toBe(
428435
'/my-project1/test?cartId=foo&expand=baz'

packages/api-request-builder/test/query-id.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ describe('queryId', () => {
4040
)
4141
})
4242

43+
test('should set the orderNumber param', () => {
44+
service.byOrderNumber(123)
45+
expect(service.params.orderNumber).toBe(123)
46+
})
47+
48+
test('should throw if orderNumber is missing', () => {
49+
expect(() => service.byOrderNumber()).toThrow(
50+
/Required argument for `byOrderNumber` is missing/
51+
)
52+
})
53+
54+
test('should throw if orderNumber is invalid', () => {
55+
expect(() => service.byOrderNumber('hi')).toThrow(
56+
/Required argument for `byOrderNumber` is missing/
57+
)
58+
})
59+
4360
test('should set the cartId param', () => {
4461
service.byCartId('myCart')
4562
expect(service.params.cartId).toBe('myCart')

types/sdk.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ export type ServiceBuilderParams = {
377377

378378
// container
379379
container?: ?string,
380+
381+
// params
382+
orderNumber?: number,
380383
}
381384
export type ServiceBuilder = {
382385
type: string,

0 commit comments

Comments
 (0)