Skip to content

Commit 4bf236a

Browse files
committed
add customer list function
1 parent e01682d commit 4bf236a

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,58 @@
33
This is a library for interacting with the Replicated Vendor Portal API using TypeScript.
44

55
Source code: [Github](https://github.com/replicatedhq/replicated-lib)
6+
7+
## Testing Locally
8+
9+
### Prerequisites
10+
11+
- Node.js >= 20.0.0
12+
- npm
13+
14+
### Running Tests
15+
16+
1. **Install dependencies:**
17+
```bash
18+
npm install
19+
```
20+
Or using Make:
21+
```bash
22+
make deps
23+
```
24+
25+
2. **Run all tests:**
26+
```bash
27+
npm test
28+
```
29+
Or using Make:
30+
```bash
31+
make test
32+
```
33+
34+
This will:
35+
- Build the TypeScript source code
36+
- Run all test suites with coverage reporting
37+
- Display verbose output for each test
38+
39+
3. **Run tests without building first:**
40+
```bash
41+
npx jest --coverage --verbose --setupFiles ./pacts/configuration.ts
42+
```
43+
44+
### Test Structure
45+
46+
- Test files are located alongside source files with `.spec.ts` extension (e.g., `src/customers.spec.ts`)
47+
- Tests use `mockttp` for HTTP request mocking
48+
- Some tests use Pact for contract testing (configured in `pacts/configuration.ts`)
49+
50+
### Running Specific Tests
51+
52+
To run a specific test file:
53+
```bash
54+
npx jest src/customers.spec.ts
55+
```
56+
57+
To run tests matching a pattern:
58+
```bash
59+
npx jest --testNamePattern="List Customers"
60+
```

src/customers.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export class Customer {
1212
license: string;
1313
}
1414

15+
export class CustomerSummary {
16+
name: string;
17+
customerId: string;
18+
}
19+
1520
interface entitlementValue {
1621
name: string;
1722
value: string;
@@ -113,6 +118,44 @@ export async function archiveCustomer(vendorPortalApi: VendorPortalApi, customer
113118
}
114119
}
115120

121+
export async function listCustomersByName(vendorPortalApi: VendorPortalApi, appSlug: string, customerName: string): Promise<CustomerSummary[]> {
122+
const http = await vendorPortalApi.client();
123+
124+
// 1. get the app
125+
const app = await getApplicationDetails(vendorPortalApi, appSlug);
126+
127+
// 2. list customers filtered by name
128+
const listCustomersUri = `${vendorPortalApi.endpoint}/app/${app.id}/customers?name=${encodeURIComponent(customerName)}`;
129+
const listCustomersRes = await http.get(listCustomersUri);
130+
if (listCustomersRes.message.statusCode != 200) {
131+
let body = "";
132+
try {
133+
body = await listCustomersRes.readBody();
134+
} catch (err) {
135+
// ignore
136+
}
137+
throw new Error(`Failed to list customers: Server responded with ${listCustomersRes.message.statusCode}: ${body}`);
138+
}
139+
const listCustomersBody: any = JSON.parse(await listCustomersRes.readBody());
140+
141+
// 3. Convert response body into CustomerSummary array
142+
let customers: CustomerSummary[] = [];
143+
144+
// check if listCustomersBody.customers is undefined
145+
if (!listCustomersBody.customers) {
146+
return customers;
147+
}
148+
149+
for (const customer of listCustomersBody.customers) {
150+
customers.push({
151+
name: customer.name,
152+
customerId: customer.id
153+
});
154+
}
155+
156+
return customers;
157+
}
158+
116159
export async function getUsedKubernetesDistributions(vendorPortalApi: VendorPortalApi, appSlug: string): Promise<KubernetesDistribution[]> {
117160
const http = await vendorPortalApi.client();
118161

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export { VendorPortalApi } from "./configuration";
22
export { getApplicationDetails } from "./applications";
33
export { Channel, createChannel, getChannelDetails, archiveChannel, pollForAirgapReleaseStatus, getDownloadUrlAirgapBuildRelease } from "./channels";
44
export { ClusterVersion, createCluster, createClusterWithLicense, pollForStatus, getKubeconfig, removeCluster, upgradeCluster, getClusterVersions, createAddonObjectStore, pollForAddonStatus, exposeClusterPort } from "./clusters";
5-
export { KubernetesDistribution, archiveCustomer, createCustomer, getUsedKubernetesDistributions } from "./customers";
5+
export { KubernetesDistribution, CustomerSummary, archiveCustomer, createCustomer, getUsedKubernetesDistributions, listCustomersByName } from "./customers";
66
export { Release, CompatibilityResult, createRelease, createReleaseFromChart, promoteRelease, reportCompatibilityResult } from "./releases";

0 commit comments

Comments
 (0)