Skip to content

Commit

Permalink
feat(metadata-taxonomy): Add id to APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
JChan106 committed Oct 24, 2024
1 parent f9e120e commit 8d6f332
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 65 deletions.
24 changes: 20 additions & 4 deletions src/api/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,14 +1165,19 @@ class Metadata extends File {
/**
* Gets taxonomy levels associated with a taxonomy key.
*
* @param id
* @param scope
* @param taxonomyKey
* @param nodeID
* @returns {Promise<MetadataTaxonomyLevels>}
*/
async getMetadataTaxonomyLevels(scope: string, taxonomyKey: string) {
async getMetadataTaxonomyLevels(id: string, scope: string, taxonomyKey: string) {
this.errorCode = ERROR_CODE_FETCH_METADATA_TAXONOMY_LEVELS;

if (!id) {
throw getBadItemError();
}

if (!scope) {
throw new Error('Missing scope');
}
Expand All @@ -1183,7 +1188,7 @@ class Metadata extends File {

const url = this.getMetadataTaxonomyLevelsUrl(scope, taxonomyKey);

const metadataTaxonomyLevels = await this.xhr.get({ url });
const metadataTaxonomyLevels = await this.xhr.get({ url, id: getTypedFileId(id) });

return getProp(metadataTaxonomyLevels, 'data', {});
}
Expand All @@ -1206,15 +1211,26 @@ class Metadata extends File {
/**
* Gets info associated with a taxonomy node.
*
* @param id
* @param scope
* @param taxonomyKey
* @param nodeID
* @param includeAncestors
* @returns {Promise<MetadataTaxonomyNode>}
*/
async getMetadataTaxonomyNode(scope: string, taxonomyKey: string, nodeID: string, includeAncestors?: boolean) {
async getMetadataTaxonomyNode(
id: string,
scope: string,
taxonomyKey: string,
nodeID: string,
includeAncestors?: boolean,
) {
this.errorCode = ERROR_CODE_FETCH_METADATA_TAXONOMY_NODE;

if (!id) {
throw getBadItemError();
}

if (!nodeID) {
throw new Error('Missing nodeID');
}
Expand All @@ -1229,7 +1245,7 @@ class Metadata extends File {

const url = this.getMetadataTaxonomyNodeUrl(scope, taxonomyKey, nodeID, includeAncestors);

const metadataTaxonomyNode = await this.xhr.get({ url });
const metadataTaxonomyNode = await this.xhr.get({ url, id: getTypedFileId(id) });

return getProp(metadataTaxonomyNode, 'data', {});
}
Expand Down
136 changes: 76 additions & 60 deletions src/api/__tests__/Metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3010,57 +3010,62 @@ describe('api/Metadata', () => {
describe('getMetadataTaxonomyLevels', () => {
const scope = 'enterprise';
const taxonomyKey = '12345';
const fileID = 'id';

it('should build getMetadataTaxonomyLevelsUrl correctly', () => {
test('should build getMetadataTaxonomyLevelsUrl correctly', () => {
const url = metadata.getMetadataTaxonomyLevelsUrl(scope, taxonomyKey);

expect(url).toBe(`https://api.box.com/2.0/metadata_taxonomies/${scope}/${taxonomyKey}`);
});

it('should fetch metadata taxonomy levels successfully', async () => {
test('should fetch metadata taxonomy levels successfully', async () => {
const mockResponse = {
data: {
displayName: 'Geography',
namespace: 'my_enterprise',
id: 'this-is-a-taxonomy-id',
key: 'geography',
levels: [
{
displayName: 'Independent Nations States',
description: 'Country',
level: 1,
},
{
displayName: 'States of a Specific Country',
description: 'State',
level: 2,
},
],
},
displayName: 'Geography',
namespace: 'my_enterprise',
id: 'this-is-a-taxonomy-id',
key: 'geography',
levels: [
{
displayName: 'Independent Nations States',
description: 'Country',
level: 1,
},
{
displayName: 'States of a Specific Country',
description: 'State',
level: 2,
},
],
};
metadata.xhr.get = jest.fn().mockReturnValueOnce({ data: mockResponse });
metadata.getMetadataTaxonomyLevelsUrl = jest.fn().mockReturnValueOnce('levels_url');

const result = await metadata.getMetadataTaxonomyLevels(scope, taxonomyKey);
const result = await metadata.getMetadataTaxonomyLevels(fileID, scope, taxonomyKey);

expect(result).toEqual(mockResponse);

expect(metadata.errorCode).toBe(ERROR_CODE_FETCH_METADATA_TAXONOMY_LEVELS);
expect(metadata.getMetadataTaxonomyLevelsUrl).toHaveBeenCalled();
expect(metadata.xhr.get).toHaveBeenCalledWith({
id: 'file_id',
url: 'levels_url',
});
});

it('should throw an error if scope is missing', async () => {
await expect(metadata.getMetadataTaxonomyLevels('', taxonomyKey)).rejects.toThrow('Missing scope');
test('should throw an error if id is missing', async () => {
await expect(() => metadata.getMetadataTaxonomyLevels('', scope, taxonomyKey)).rejects.toThrow(
ErrorUtil.getBadItemError(),
);
});

test('should throw an error if scope is missing', async () => {
await expect(metadata.getMetadataTaxonomyLevels(fileID, '', taxonomyKey)).rejects.toThrow('Missing scope');
});

it('should throw an error if taxonomyKey is missing', async () => {
await expect(metadata.getMetadataTaxonomyLevels(scope, '')).rejects.toThrow('Missing taxonomyKey');
test('should throw an error if taxonomyKey is missing', async () => {
await expect(metadata.getMetadataTaxonomyLevels(fileID, scope, '')).rejects.toThrow('Missing taxonomyKey');
});

it('should set the correct error code', async () => {
test('should set the correct error code', async () => {
try {
await metadata.getMetadataTaxonomyLevels(scope, taxonomyKey);
} catch (error) {
Expand All @@ -3071,86 +3076,97 @@ describe('api/Metadata', () => {
});
});
describe('getMetadataTaxonomyNode', () => {
const fileID = 'id';
const scope = 'enterprise';
const taxonomyKey = '12345';
const nodeID = '67890';

it('should build getMetadataTaxonomyNodeUrl correctly', () => {
test('should build getMetadataTaxonomyNodeUrl correctly', () => {
const url = metadata.getMetadataTaxonomyNodeUrl(scope, taxonomyKey, nodeID, true);

expect(url).toBe(
`https://api.box.com/2.0/metadata_taxonomies/${scope}/${taxonomyKey}/nodes/${nodeID}?include-ancestors=true`,
);
});

it('should fetch metadata taxonomy node successfully with ancestors', async () => {
test('should fetch metadata taxonomy node successfully with ancestors', async () => {
const mockResponse = {
data: {
id: 'this-is-a-node-id',
displayName: 'Florida',
level: 2,
createdAt: '2024-10-09 13:04:28',
updatedAt: '2024-10-09 13:04:28',
ancestors: [
{
id: 'this-is-a-parent-node-id',
displayName: 'United States',
level: 1,
},
],
},
id: 'this-is-a-node-id',
displayName: 'Florida',
level: 2,
createdAt: '2024-10-09 13:04:28',
updatedAt: '2024-10-09 13:04:28',
ancestors: [
{
id: 'this-is-a-parent-node-id',
displayName: 'United States',
level: 1,
},
],
};

metadata.xhr.get = jest.fn().mockReturnValueOnce({ data: mockResponse });
metadata.getMetadataTaxonomyNodeUrl = jest.fn().mockReturnValueOnce('node_url');

const result = await metadata.getMetadataTaxonomyNode(scope, taxonomyKey, nodeID, true);
const result = await metadata.getMetadataTaxonomyNode(fileID, scope, taxonomyKey, nodeID, true);

expect(result).toEqual(mockResponse);
expect(metadata.errorCode).toBe(ERROR_CODE_FETCH_METADATA_TAXONOMY_NODE);
expect(metadata.getMetadataTaxonomyNodeUrl).toHaveBeenCalled();
expect(metadata.xhr.get).toHaveBeenCalledWith({
id: 'file_id',
url: 'node_url',
});
});

it('should fetch metadata taxonomy node successfully without ancestors', async () => {
test('should fetch metadata taxonomy node successfully without ancestors', async () => {
const noAncestorsMock = {
data: {
id: 'this-is-a-node-id',
displayName: 'Florida',
level: 2,
createdAt: '2024-10-09 13:04:28',
updatedAt: '2024-10-09 13:04:28',
},
id: 'this-is-a-node-id',
displayName: 'Florida',
level: 2,
createdAt: '2024-10-09 13:04:28',
updatedAt: '2024-10-09 13:04:28',
};

metadata.xhr.get = jest.fn().mockReturnValueOnce({ data: noAncestorsMock });
metadata.getMetadataTaxonomyNodeUrl = jest.fn().mockReturnValueOnce('node_url');

const result = await metadata.getMetadataTaxonomyNode(scope, taxonomyKey, nodeID);
const result = await metadata.getMetadataTaxonomyNode(fileID, scope, taxonomyKey, nodeID);

expect(result).toEqual(noAncestorsMock);
expect(metadata.errorCode).toBe(ERROR_CODE_FETCH_METADATA_TAXONOMY_NODE);
expect(metadata.getMetadataTaxonomyNodeUrl).toHaveBeenCalled();
expect(metadata.xhr.get).toHaveBeenCalledWith({
id: 'file_id',
url: 'node_url',
});
});

it('should throw an error if scope is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode('', taxonomyKey, nodeID)).rejects.toThrow('Missing scope');
test('should throw an error if id is missing', async () => {
await expect(() => metadata.getMetadataTaxonomyNode('', scope, taxonomyKey, nodeID)).rejects.toThrow(
ErrorUtil.getBadItemError(),
);
});

test('should throw an error if scope is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode(fileID, '', taxonomyKey, nodeID)).rejects.toThrow(
'Missing scope',
);
});

it('should throw an error if taxonomyKey is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode(scope, '', nodeID)).rejects.toThrow('Missing taxonomyKey');
test('should throw an error if taxonomyKey is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode(fileID, scope, '', nodeID)).rejects.toThrow(
'Missing taxonomyKey',
);
});

it('should throw an error if nodeID is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode(scope, taxonomyKey, '')).rejects.toThrow('Missing nodeID');
test('should throw an error if nodeID is missing', async () => {
await expect(metadata.getMetadataTaxonomyNode(fileID, scope, taxonomyKey, '')).rejects.toThrow(
'Missing nodeID',
);
});

it('should set the correct error code', async () => {
test('should set the correct error code', async () => {
try {
await metadata.getMetadataTaxonomyNode(scope, taxonomyKey, nodeID);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const metadataTaxonomyNodeAncestorsFetcher = async (

// Create a hashmap of levels to easily hydrate with data from metadataTaxonomyNode
const levelsMap = new Map();
for (const item of metadataTaxonomyLevels) {
for (const item of metadataTaxonomyLevels.levels) {
const levelData = {
level: item.level,
levelName: item.displayName,
Expand Down

0 comments on commit 8d6f332

Please sign in to comment.