Skip to content

Commit 8f8099b

Browse files
author
Hovhannes Babayan
committed
implemented execute() and namedQuery() methods
1 parent f47a461 commit 8f8099b

10 files changed

Lines changed: 188 additions & 13 deletions

File tree

dist/attask.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Logs in, loads list of active users, then calls assignUserToken action for the first user
3+
*/
4+
5+
var ApiFactory = require('./../../').ApiFactory;
6+
var ApiConstants = require('./../../').ApiConstants;
7+
var util = require('util');
8+
9+
var instance = ApiFactory.getInstance({
10+
url: 'http://localhost:8080',
11+
version: '4.0'
12+
});
13+
14+
util.print('Logs in, loads list of active users, then calls assignUserToken action for the first user\n');
15+
util.log('Logging in ...');
16+
instance.login('new@user.attask', 'user').then(
17+
function(data) {
18+
util.log('Loading list of active users ...');
19+
var query = {};
20+
query['isActive'] = true;
21+
query[ApiConstants.LIMIT] = 1;
22+
instance.search('user', query).then(
23+
function(data) {
24+
util.log('Load success. Received data:');
25+
console.log(util.inspect(data, {colors:true}));
26+
util.log('Calling action for user with objID=' + data[0].ID + ' ...');
27+
instance.execute('user', data[0].ID, 'assignUserToken').then(
28+
function(data) {
29+
util.log('Action success. Received data:');
30+
console.log(util.inspect(data, {colors:true}));
31+
},
32+
function(error) {
33+
util.log('Action failure. Received data:');
34+
console.log(util.inspect(error, {colors:true}));
35+
}
36+
);
37+
},
38+
function(error) {
39+
util.log('Load failure. Received data:');
40+
console.log(util.inspect(error, {colors:true}));
41+
}
42+
);
43+
},
44+
function(error) {
45+
util.log('Login failure. Received data:');
46+
console.log(util.inspect(error, {colors:true}));
47+
}
48+
);

examples/node/named-query.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Logs in, then calls myWork named query for Work object (Items in My Work)
3+
*/
4+
5+
var ApiFactory = require('./../../').ApiFactory;
6+
var util = require('util');
7+
8+
var instance = ApiFactory.getInstance({
9+
url: 'http://localhost:8080',
10+
version: '4.0'
11+
});
12+
13+
util.print('Logs in, then calls myWork named query for Work object (Items in My Work)\n');
14+
util.log('Logging in ...');
15+
instance.login('new@user.attask', 'user').then(
16+
function(data) {
17+
util.log('Calling named query ...');
18+
instance.namedQuery('work', 'myWork').then(
19+
function(data) {
20+
util.log('Query success. Received data:');
21+
console.log(util.inspect(data, {colors:true}));
22+
},
23+
function(error) {
24+
util.log('Query failure. Received data:');
25+
console.log(util.inspect(error, {colors:true}));
26+
}
27+
);
28+
},
29+
function(error) {
30+
util.log('Login failure. Received data:');
31+
console.log(util.inspect(error, {colors:true}));
32+
}
33+
);

src/plugins/execute.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module.exports = function(Api) {
2-
Api.prototype.execute = function () {
3-
throw new Error('Not implemented')
2+
3+
/**
4+
* Executes an action for the given object
5+
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
6+
* @param {String} objID ID of object
7+
* @param {String} action An action to execute. A list of allowed named queries are available within the {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} under "actions" for each object.
8+
* @param {Object} [actionArgs] Optional. Arguments for the action. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of valid arguments
9+
* @returns {Promise} A promise which will resolved if everything went ok and rejected otherwise
10+
*/
11+
Api.prototype.execute = function (objCode, objID, action, actionArgs) {
12+
return this.request(objCode + '/' + objID + '/' + action, actionArgs, null, 'PUT');
413
};
514
};

src/plugins/namedQuery.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module.exports = function(Api) {
2-
Api.prototype.namedQuery = function () {
3-
throw new Error('Not implemented')
2+
3+
/**
4+
* Executes a named query for the given obj code
5+
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
6+
* @param {String} query A query to execute. A list of allowed named queries are available within the {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} under "actions" for each object.
7+
* @param {Object} [queryArgs] Optional. Arguments for the action. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of valid arguments
8+
* @param {Object} fields Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
9+
* @returns {Promise} A promise which will resolved with received data if everything went ok and rejected with error info otherwise
10+
*/
11+
Api.prototype.namedQuery = function (objCode, query, queryArgs, fields) {
12+
return this.request(objCode + '/' + query, queryArgs, fields, 'GET');
413
};
514
};

src/plugins/remove.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ module.exports = function(Api) {
1111
var that = this;
1212
return new Promise(function (resolve, reject) {
1313
var params = bForce ? {force: true} : null;
14-
return that.request(objCode + '/' + objID, params, null, 'DELETE').then(function (result) {
14+
that.request(objCode + '/' + objID, params, null, 'DELETE').then(function (result) {
1515
if (result && result.success) {
1616
resolve();
1717
} else {
1818
reject();
1919
}
20-
});
20+
}, reject);
2121
});
22-
2322
};
2423
};

src/plugins/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = function(Api) {
33
* Used for object retrieval by multiple search criteria.
44
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
55
* @param {Object} query An object with search criteria
6-
* @param {Array} fields Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
6+
* @param {Array} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer} for the list of available fields for the given objCode.
77
* @return {Promise} A promise which will resolved with search results if everything went ok and rejected otherwise
88
*/
99
Api.prototype.search = function (objCode, query, fields) {

test/plugins/execute.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.execute() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params (without args)', function() {
21+
api.execute('foo', '123', 'doSomething');
22+
expect(api.request).to.have.callCount(1);
23+
expect(api.request).to.have.been.calledWith('foo/123/doSomething', undefined, null, "PUT");
24+
});
25+
26+
it('should call request() with proper params (with args)', function() {
27+
api.execute('foo', '123', 'doSomething', {objID: 111});
28+
expect(api.request).to.have.callCount(1);
29+
expect(api.request).to.have.been.calledWith('foo/123/doSomething', {objID: 111}, null, "PUT");
30+
});
31+
});

test/plugins/namedQuery.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.namedQuery() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params and return value received from request()', function() {
21+
var queryArgs = {
22+
'foo': 'bar'
23+
};
24+
25+
var fields = [
26+
'*', "zzz:*"
27+
];
28+
29+
var objCode = 'baz';
30+
31+
var query = 'myQuery';
32+
33+
var expectedReturnValue = 123;
34+
api.request.returns(expectedReturnValue);
35+
36+
var actualReturnedValue = api.namedQuery(objCode, query, queryArgs, fields);
37+
expect(api.request).to.have.callCount(1);
38+
expect(api.request).to.have.been.calledWith(objCode + "/" + query, queryArgs, fields, "GET");
39+
expect(actualReturnedValue).to.equal(expectedReturnValue);
40+
});
41+
});

test/plugins/remove.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ describe('Api.remove() method', function() {
3131

3232
it('should resolve returned promise if removal was ok', function(done) {
3333
api.request.resolves({success: true});
34-
var promise = api.logout();
34+
var promise = api.remove('foo', '123', true);
3535
expect(promise).to.be.fulfilled.and.notify(done);
3636
});
3737

3838
it('should reject returned promise if removal was not ok', function(done) {
39-
api.httpOptions.headers.sessionID = 123;
4039
api.request.resolves(false);
4140
var promise = api.remove('foo', '123');
4241
expect(promise).to.be.rejected.and.notify(done);
4342
});
43+
44+
it('should reject returned promise if removal was not ok (request() rejected)', function(done) {
45+
api.request.rejects();
46+
var promise = api.remove('foo', '123');
47+
expect(promise).to.be.rejected.and.notify(done);
48+
});
4449
});

0 commit comments

Comments
 (0)