Skip to content

Commit 96b1f71

Browse files
authored
added sort by metadata field (#474)
1 parent c1e1f42 commit 96b1f71

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

lib-es5/api.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,22 @@ exports.restore_metadata_field_datasource = function restore_metadata_field_data
689689
options.content_type = "json";
690690
var params = { external_ids: entries_external_id };
691691
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
692+
};
693+
694+
/**
695+
* Sorts metadata field datasource. Currently supports only value
696+
* @param {String} field_external_id The ID of the metadata field
697+
* @param {String} sort_by Criteria for the sort. Currently supports only value
698+
* @param {String} direction Optional (gets either asc or desc)
699+
* @param {Function} callback Callback function
700+
* @param {Object} options Configuration options
701+
*
702+
* @return {Object}
703+
*/
704+
exports.sort_metadata_field_datasource = function sort_metadata_field_datasource(field_external_id, sort_by, direction, callback) {
705+
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
706+
707+
options.content_type = "json";
708+
var params = { sort_by: sort_by, direction: direction };
709+
return call_api("post", ["metadata_fields", field_external_id, "datasource", "sort"], params, callback, options);
692710
};

lib-es5/v2/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ v1_adapters(exports, api, {
6060
update_metadata_field: 2,
6161
update_metadata_field_datasource: 2,
6262
delete_datasource_entries: 2,
63-
restore_metadata_field_datasource: 2
63+
restore_metadata_field_datasource: 2,
64+
sort_metadata_field_datasource: 3
6465
});

lib/api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,19 @@ exports.restore_metadata_field_datasource = function restore_metadata_field_data
551551
const params = { external_ids: entries_external_id };
552552
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
553553
};
554+
555+
/**
556+
* Sorts metadata field datasource. Currently supports only value
557+
* @param {String} field_external_id The ID of the metadata field
558+
* @param {String} sort_by Criteria for the sort. Currently supports only value
559+
* @param {String} direction Optional (gets either asc or desc)
560+
* @param {Function} callback Callback function
561+
* @param {Object} options Configuration options
562+
*
563+
* @return {Object}
564+
*/
565+
exports.sort_metadata_field_datasource = function sort_metadata_field_datasource(field_external_id, sort_by, direction, callback, options = {}) {
566+
options.content_type = "json";
567+
const params = { sort_by: sort_by, direction: direction};
568+
return call_api("post", ["metadata_fields", field_external_id, "datasource", "sort"], params, callback, options);
569+
};

lib/v2/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ v1_adapters(exports, api, {
5858
update_metadata_field: 2,
5959
update_metadata_field_datasource: 2,
6060
delete_datasource_entries: 2,
61-
restore_metadata_field_datasource: 2
61+
restore_metadata_field_datasource: 2,
62+
sort_metadata_field_datasource: 3
6263
});

test/integration/api/admin/structured_metadata_spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,28 @@ describe("structured metadata api", function () {
401401
});
402402
});
403403

404+
describe("sort_metadata_field_datasource", function () {
405+
it("should sort by asc in a metadata field datasource", function () {
406+
// datasource is set with values in the order v2, v3, v4
407+
return api.sort_metadata_field_datasource(EXTERNAL_ID_SET_3, 'value', 'asc')
408+
.then((result) => {
409+
expect(result).to.beADatasource();
410+
// ascending order means v2 is the first value
411+
expect(result.values[0].value).to.eql('v2');
412+
})
413+
});
414+
415+
it("should sort by desc in a metadata field datasource", function () {
416+
// datasource is set with values in the order v2, v3, v4
417+
return api.sort_metadata_field_datasource(EXTERNAL_ID_SET_3, 'value', 'desc')
418+
.then((result) => {
419+
expect(result).to.beADatasource();
420+
// descending order means v4 is the first value
421+
expect(result.values[0].value).to.eql('v4');
422+
})
423+
});
424+
});
425+
404426
describe("restore_metadata_field_datasource", function () {
405427
it("should restore a deleted entry in a metadata field datasource", function () {
406428
return api.delete_datasource_entries(EXTERNAL_ID_SET_3, [DATASOURCE_ENTRY_EXTERNAL_ID])

0 commit comments

Comments
 (0)