Skip to content

Commit

Permalink
feat(UA-9179): add properties endpoints to client
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron-coveo committed Jul 16, 2024
1 parent 92d3e2b commit b45fb12
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/resources/AnalyticsAdmin/Properties/Properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Resource from '../../Resource.js';
import API from '../../../APICore.js';
import {PageModel, Paginated} from '../../BaseInterfaces.js';
import {PropertiesResponseMessage, PropertyModel} from './PropertiesInterfaces.js';

export default class Properties extends Resource {
static baseUrl = `/rest/organizations/${API.orgPlaceholder}/analyticsadmin/v1`;
/**
* Build the request path, handling the optional `org` query parameter.
*
* @param route The path part of the request.
* @param queryParams Optional query parameters object.
* If this object contains an `org` property, it will override the value from the configuration.
* @returns The request path including formatted query parameters.
*/
protected buildPathWithOrg(route: string, queryParams?: any): string {
return super.buildPath(route, {org: this.api.organizationId, ...queryParams});
}

/**
* List all properties
*
* @returns Promise<PageModel<PropertyModel>>
*/
listProperties(pagination?: Paginated): Promise<PageModel<PropertyModel>> {
return this.api.get<PageModel<PropertyModel>>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/list`, pagination),
);
}

/**
* Get a property
*
* @returns Promise<PropertyModel>
*/
getProperty(trackingId: string): Promise<PropertyModel> {
return this.api.get<PropertyModel>(this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`));
}

/**
* Create a property
*
* @returns Promise<PropertiesResponseMessage>
*/
createProperty(trackingId: string, displayName: string): Promise<PropertiesResponseMessage> {
return this.api.post<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`, {displayName}),
);
}

/**
* Edit a property
*
* @returns Promise<PropertiesResponseMessage>
*/
updateProperty(trackingId: string, displayName: string): Promise<PropertiesResponseMessage> {
return this.api.put<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`, {displayName}),
);
}

/**
* Delete a property
*
* @returns Promise<PropertiesResponseMessage>
*/
deleteProperty(trackingId: string): Promise<PropertiesResponseMessage> {
return this.api.delete<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface PropertyModel {
trackingId: string;
displayName: string;
}

export interface PropertiesResponseMessage {
message: string;
}
2 changes: 2 additions & 0 deletions src/resources/AnalyticsAdmin/Properties/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Properties.js';
export * from './PropertiesInterfaces.js';
1 change: 1 addition & 0 deletions src/resources/AnalyticsAdmin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Properties/index.js';
71 changes: 71 additions & 0 deletions src/resources/AnalyticsAdmin/tests/Properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import API from '../../../APICore.js';
import Properties from '../Properties/Properties.js';

jest.mock('../../../APICore.js');

const APIMock: jest.Mock<API> = API as any;

describe('Properties', () => {
let properties: Properties;
const api = new APIMock() as jest.Mocked<API>;
const serverlessApi = new APIMock() as jest.Mocked<API>;

beforeEach(() => {
jest.clearAllMocks();
properties = new Properties(api, serverlessApi);
});

describe('listProperties', () => {
it('should make a GET call to the Properties list path', () => {
properties.listProperties();

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/list`);
});

it('should make a GET call to the Properties list path with pagination', () => {
properties.listProperties({page: 1, perPage: 2});

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/list?page=1&perPage=2`);
});
});

describe('getProperty', () => {
it('should make a GET call to the properties path', () => {
properties.getProperty('trackingId');

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId`);
});
});

describe('createProperty', () => {
it('should make a POST call to the properties path', () => {
properties.createProperty('trackingId', 'displayName');

expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(
`${Properties.baseUrl}/properties/trackingId?displayName=displayName`,
);
});
});

describe('updateProperty', () => {
it('should make a PUT call to the properties path', () => {
properties.updateProperty('trackingId', 'displayName');

expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId?displayName=displayName`);
});
});

describe('deleteProperty', () => {
it('should make a DELETE call to the properties path', () => {
properties.deleteProperty('trackingId');

expect(api.delete).toHaveBeenCalledTimes(1);
expect(api.delete).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId`);
});
});
});
3 changes: 3 additions & 0 deletions src/resources/PlatformResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import SearchAnalysis from './SearchAnalysis/SearchAnalysis.js';
import Project from './Projects/Project.js';
import Resources from './Resources/Resources.js';
import CatalogContent from './Catalogs/CatalogContent.js';
import Properties from './AnalyticsAdmin/Properties/Properties.js';

const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'activity', resource: Activity},
Expand Down Expand Up @@ -91,6 +92,7 @@ const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'productListing', resource: ProductListing},
{key: 'productListingConfiguration', resource: ProductListingConfiguration},
{key: 'products', resource: Products},
{key: 'properties', resource: Properties},
{key: 'pushApi', resource: PushApi},
{key: 'resourceSnapshot', resource: ResourceSnapshots},
{key: 'saml', resource: Saml},
Expand Down Expand Up @@ -151,6 +153,7 @@ class PlatformResources {
productListing: ProductListing;
productListingConfiguration: ProductListingConfiguration;
products: Products;
properties: Properties;
pushApi: PushApi;
resourceSnapshot: ResourceSnapshots;
saml: Saml;
Expand Down
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ export * from './Sources/index.js';
export * from './UsageAnalytics/index.js';
export * from './Users/index.js';
export * from './Vaults/index.js';
export * from './AnalyticsAdmin/index.js';

0 comments on commit b45fb12

Please sign in to comment.