Skip to content

Commit

Permalink
Merge pull request #243 from gradientedge/search_customers
Browse files Browse the repository at this point in the history
feat(api): Add customer and order search functions
  • Loading branch information
jimmythomson authored Dec 9, 2024
2 parents b3634a4 + f929360 commit 469370b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib/api/CommercetoolsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import type {
CustomerGroupPagedQueryResponse,
CustomerGroupUpdate,
CustomerPagedQueryResponse,
CustomerPagedSearchResponse,
CustomerResetPassword,
CustomerSearchRequest,
CustomerSignin,
CustomerSignInResult,
CustomerToken,
Expand All @@ -52,6 +54,8 @@ import type {
OrderFromCartDraft,
OrderImportDraft,
OrderPagedQueryResponse,
OrderPagedSearchResponse,
OrderSearchRequest,
OrderUpdate,
Payment,
PaymentDraft,
Expand Down Expand Up @@ -1527,6 +1531,19 @@ export class CommercetoolsApi {
})
}

/**
* Search orders:
* https://docs.commercetools.com/api/projects/order-search#search-orders
*/
searchOrders(options: CommonRequestOptions & { data: OrderSearchRequest }): Promise<OrderPagedSearchResponse> {
return this.request({
...this.extractCommonRequestOptions(options),
path: '/orders/search',
method: 'POST',
data: options.data,
})
}

/**
* Create a product:
* https://docs.commercetools.com/api/projects/products#create-a-product
Expand Down Expand Up @@ -1968,6 +1985,21 @@ export class CommercetoolsApi {
})
}

/**
* Search customers:
* https://docs.commercetools.com/api/projects/customer-search#search-customers
*/
searchCustomers(
options: CommonRequestOptions & { data: CustomerSearchRequest },
): Promise<CustomerPagedSearchResponse> {
return this.request({
...this.extractCommonRequestOptions(options),
path: '/customers/search',
method: 'POST',
data: options.data,
})
}

/**
* Update a customer by id:
* https://docs.commercetools.com/api/projects/customers#update-customer-by-id
Expand Down
3 changes: 3 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export type {
CustomerGroupPagedQueryResponse,
CustomerGroupUpdate,
CustomerPagedQueryResponse,
CustomerPagedSearchResponse,
CustomerResetPassword,
CustomerSignin,
CustomerSignInResult,
Expand Down Expand Up @@ -176,6 +177,7 @@ export type {
OrderFromCartDraft,
OrderImportDraft,
OrderPagedQueryResponse,
OrderPagedSearchResponse,
OrderUpdate,
OrderUpdateAction,
PagedQueryResponse,
Expand All @@ -201,6 +203,7 @@ export type {
ProductCatalogData,
ProductTypePagedQueryResponse,
ProductUpdate,
ProductUpdateAction,
ReplicaCartDraft,
ResourceCreatedDeliveryPayload,
ResourceDeletedDeliveryPayload,
Expand Down
68 changes: 68 additions & 0 deletions src/test/api/CommercetoolsApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,40 @@ describe('CommercetoolsApi', () => {
expect(order).toEqual({ success: true })
})
})

describe('searchOrders', () => {
it('should make a POST request to the correct endpoint', async () => {
nock('https://api.europe-west1.gcp.commercetools.com', {
reqheaders: {
authorization: 'Bearer test-access-token',
},
})
.post('/test-project-key/orders/search', {
query: 'orderNumber="1234"',
limit: 5,
offset: 10,
sort: [
{
field: 'orderNumber',
order: 'asc',
},
],
})
.reply(200, { success: true })
const api = new CommercetoolsApi(defaultConfig)

const order = await api.searchOrders({
data: {
query: 'orderNumber="1234"',
limit: 5,
offset: 10,
sort: [{ field: 'orderNumber', order: 'asc' }],
},
})

expect(order).toEqual({ success: true })
})
})
})

describe('Payment', () => {
Expand Down Expand Up @@ -2702,6 +2736,40 @@ describe('CommercetoolsApi', () => {
})
})

describe('searchCustomers', () => {
it('should make a POST request to the correct endpoint', async () => {
nock('https://api.europe-west1.gcp.commercetools.com', {
reqheaders: {
authorization: 'Bearer test-access-token',
},
})
.post('/test-project-key/customers/search', {
query: 'firstName="Jimmy"',
limit: 5,
offset: 10,
sort: [
{
field: 'customerNumber',
order: 'desc',
},
],
})
.reply(200, { success: true })
const api = new CommercetoolsApi(defaultConfig)

const order = await api.searchCustomers({
data: {
query: 'firstName="Jimmy"',
limit: 5,
offset: 10,
sort: [{ field: 'customerNumber', order: 'desc' }],
},
})

expect(order).toEqual({ success: true })
})
})

describe('createAccount', () => {
it('should make a POST request to the correct endpoint with the expected data', async () => {
nock('https://api.europe-west1.gcp.commercetools.com', {
Expand Down

0 comments on commit 469370b

Please sign in to comment.