Skip to content

Commit

Permalink
feat(adds-assign-user-ids): adds assignUserIDs method and tests (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Sep 26, 2019
1 parent 5f7fb6c commit 9ae3ced
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/AlgoliaSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,31 @@ AlgoliaSearch.prototype.assignUserID = function(data, callback) {
});
};

/**
* Assign a array of userIDs to a cluster.
*
* @param {Array} data.userIDs The array of userIDs to assign to a new cluster
* @param {string} data.cluster The cluster to assign the user to
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* client.assignUserIDs({ cluster: 'c1-test', userIDs: ['some-user-1', 'some-user-2'] });
*/
AlgoliaSearch.prototype.assignUserIDs = function(data, callback) {
if (!data.userIDs || !data.cluster) {
throw new errors.AlgoliaSearchError('You have to provide both an array of userIDs and cluster', data);
}
return this._jsonRequest({
method: 'POST',
url: '/1/clusters/mapping/batch',
hostType: 'write',
body: {
cluster: data.cluster,
users: data.userIDs
},
callback: callback
});
};

/**
* Get the top userIDs
*
Expand Down
1 change: 1 addition & 0 deletions test/spec/common/client/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('AlgoliaSearch client API spec', function(t) {
'addUserKey',
'addUserKeyWithValidity',
'assignUserID',
'assignUserIDs',
'batch',
'clearCache',
'copyIndex',
Expand Down
35 changes: 35 additions & 0 deletions test/spec/common/client/test-cases/assignUserIDs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var testCases = [];

if (process.browser) {
if (require('faux-jax').support.xhr.cors) {
testCases.push({
testName: 'client.assignUserIDs({userID, cluster}, cb)',
object: 'client',
methodName: 'assignUserIDs',
callArguments: [{userIDs: ['one', 'two'], cluster: 'the big one'}],
action: 'write',
expectedRequest: {
method: 'POST',
body: {cluster: 'the big one', users: ['one', 'two']},
URL: {pathname: '/1/clusters/mapping/batch'}
}
});
}
} else {
testCases.push({
testName: 'client.assignUserIDs({userID, cluster}, cb)',
object: 'client',
methodName: 'assignUserIDs',
callArguments: [{userIDs: ['one', 'two'], cluster: 'the big one'}],
action: 'write',
expectedRequest: {
method: 'POST',
body: {cluster: 'the big one', users: ['one', 'two']},
URL: {pathname: '/1/clusters/mapping/batch'}
}
});
}

module.exports = testCases;

0 comments on commit 9ae3ced

Please sign in to comment.