Skip to content

Commit 6ed8e37

Browse files
authored
make appSlug optional for list customers (#69)
* make appSlug optional for list customers * prettier
1 parent bd9ebd3 commit 6ed8e37

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

src/customers.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,45 @@ describe("List Customers By Name", () => {
225225
expect(customers[100].customerId).toEqual("customer-101");
226226
expect(customers[149].customerId).toEqual("customer-150");
227227
});
228+
229+
it("should search across all apps when appSlug is undefined", async () => {
230+
const customersResponse = {
231+
data: [
232+
{ id: "customer-1", name: "Test Customer", app_name: "App 1" },
233+
{ id: "customer-2", name: "Test Customer Two", app_name: "App 2" },
234+
{ id: "customer-3", name: "Test Customer Three", app_name: "App 3" }
235+
],
236+
total_count: 3
237+
};
238+
239+
// When appSlug is undefined, we should NOT call the /apps endpoint
240+
// Just set up the search endpoint response
241+
await mockServer.forPost("/customers/search").thenReply(200, JSON.stringify(customersResponse));
242+
243+
const customers: CustomerSummary[] = await listCustomersByName(apiClient, undefined, "Test Customer");
244+
expect(customers).toHaveLength(3);
245+
expect(customers[0].name).toEqual("Test Customer");
246+
expect(customers[1].name).toEqual("Test Customer Two");
247+
expect(customers[2].name).toEqual("Test Customer Three");
248+
});
249+
250+
it("should include app_id when appSlug is provided", async () => {
251+
const appId = "test-app-123";
252+
const appSlug = "test-app";
253+
const expectedApplications = {
254+
apps: [{ id: appId, name: "Test App", slug: appSlug }]
255+
};
256+
const customersResponse = {
257+
data: [{ id: "customer-1", name: "Test Customer" }],
258+
total_count: 1
259+
};
260+
261+
// When appSlug is provided, we should call /apps first, then search
262+
await mockServer.forGet("/apps").thenReply(200, JSON.stringify(expectedApplications));
263+
await mockServer.forPost("/customers/search").thenReply(200, JSON.stringify(customersResponse));
264+
265+
const customers: CustomerSummary[] = await listCustomersByName(apiClient, appSlug, "Test Customer");
266+
expect(customers).toHaveLength(1);
267+
expect(customers[0].name).toEqual("Test Customer");
268+
});
228269
});

src/customers.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ export async function getUsedKubernetesDistributions(vendorPortalApi: VendorPort
157157
return kubernetesDistributions;
158158
}
159159

160-
export async function listCustomersByName(vendorPortalApi: VendorPortalApi, appSlug: string, customerName: string): Promise<CustomerSummary[]> {
160+
export async function listCustomersByName(vendorPortalApi: VendorPortalApi, appSlug: string | undefined, customerName: string): Promise<CustomerSummary[]> {
161161
const http = await vendorPortalApi.client();
162162

163-
// Get the app ID from the app slug to filter results
164-
const app = await getApplicationDetails(vendorPortalApi, appSlug);
163+
// Get the app ID from the app slug to filter results (if appSlug is provided)
164+
let app: any;
165+
if (appSlug) {
166+
app = await getApplicationDetails(vendorPortalApi, appSlug);
167+
}
165168

166169
// Use the searchTeamCustomers endpoint to search for customers by name and app
167170
const searchCustomersUri = `${vendorPortalApi.endpoint}/customers/search`;
@@ -172,7 +175,7 @@ export async function listCustomersByName(vendorPortalApi: VendorPortalApi, appS
172175
let hasMorePages = true;
173176

174177
while (hasMorePages) {
175-
const requestBody = {
178+
const requestBody: any = {
176179
include_paid: true,
177180
include_inactive: true,
178181
include_dev: true,
@@ -182,11 +185,15 @@ export async function listCustomersByName(vendorPortalApi: VendorPortalApi, appS
182185
include_test: true,
183186
include_trial: true,
184187
query: `name:${customerName}`,
185-
app_id: app.id,
186188
offset: offset,
187189
page_size: pageSize
188190
};
189191

192+
// Only add app_id if appSlug was provided
193+
if (app) {
194+
requestBody.app_id = app.id;
195+
}
196+
190197
const searchCustomersRes = await http.post(searchCustomersUri, JSON.stringify(requestBody));
191198
if (searchCustomersRes.message.statusCode != 200) {
192199
let body = "";

0 commit comments

Comments
 (0)