Skip to content

Commit

Permalink
added sort by metadata field (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
strausr authored Feb 21, 2021
1 parent c1e1f42 commit 96b1f71
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib-es5/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,22 @@ exports.restore_metadata_field_datasource = function restore_metadata_field_data
options.content_type = "json";
var params = { external_ids: entries_external_id };
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
};

/**
* Sorts metadata field datasource. Currently supports only value
* @param {String} field_external_id The ID of the metadata field
* @param {String} sort_by Criteria for the sort. Currently supports only value
* @param {String} direction Optional (gets either asc or desc)
* @param {Function} callback Callback function
* @param {Object} options Configuration options
*
* @return {Object}
*/
exports.sort_metadata_field_datasource = function sort_metadata_field_datasource(field_external_id, sort_by, direction, callback) {
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};

options.content_type = "json";
var params = { sort_by: sort_by, direction: direction };
return call_api("post", ["metadata_fields", field_external_id, "datasource", "sort"], params, callback, options);
};
3 changes: 2 additions & 1 deletion lib-es5/v2/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ v1_adapters(exports, api, {
update_metadata_field: 2,
update_metadata_field_datasource: 2,
delete_datasource_entries: 2,
restore_metadata_field_datasource: 2
restore_metadata_field_datasource: 2,
sort_metadata_field_datasource: 3
});
16 changes: 16 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,19 @@ exports.restore_metadata_field_datasource = function restore_metadata_field_data
const params = { external_ids: entries_external_id };
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
};

/**
* Sorts metadata field datasource. Currently supports only value
* @param {String} field_external_id The ID of the metadata field
* @param {String} sort_by Criteria for the sort. Currently supports only value
* @param {String} direction Optional (gets either asc or desc)
* @param {Function} callback Callback function
* @param {Object} options Configuration options
*
* @return {Object}
*/
exports.sort_metadata_field_datasource = function sort_metadata_field_datasource(field_external_id, sort_by, direction, callback, options = {}) {
options.content_type = "json";
const params = { sort_by: sort_by, direction: direction};
return call_api("post", ["metadata_fields", field_external_id, "datasource", "sort"], params, callback, options);
};
3 changes: 2 additions & 1 deletion lib/v2/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ v1_adapters(exports, api, {
update_metadata_field: 2,
update_metadata_field_datasource: 2,
delete_datasource_entries: 2,
restore_metadata_field_datasource: 2
restore_metadata_field_datasource: 2,
sort_metadata_field_datasource: 3
});
22 changes: 22 additions & 0 deletions test/integration/api/admin/structured_metadata_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,28 @@ describe("structured metadata api", function () {
});
});

describe("sort_metadata_field_datasource", function () {
it("should sort by asc in a metadata field datasource", function () {
// datasource is set with values in the order v2, v3, v4
return api.sort_metadata_field_datasource(EXTERNAL_ID_SET_3, 'value', 'asc')
.then((result) => {
expect(result).to.beADatasource();
// ascending order means v2 is the first value
expect(result.values[0].value).to.eql('v2');
})
});

it("should sort by desc in a metadata field datasource", function () {
// datasource is set with values in the order v2, v3, v4
return api.sort_metadata_field_datasource(EXTERNAL_ID_SET_3, 'value', 'desc')
.then((result) => {
expect(result).to.beADatasource();
// descending order means v4 is the first value
expect(result.values[0].value).to.eql('v4');
})
});
});

describe("restore_metadata_field_datasource", function () {
it("should restore a deleted entry in a metadata field datasource", function () {
return api.delete_datasource_entries(EXTERNAL_ID_SET_3, [DATASOURCE_ENTRY_EXTERNAL_ID])
Expand Down

0 comments on commit 96b1f71

Please sign in to comment.