Skip to content

Commit f47a461

Browse files
author
Hovhannes Babayan
committed
implemented remove() method
1 parent a99773b commit f47a461

9 files changed

Lines changed: 122 additions & 17 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Logs in, then creates a new template with name "API Template", then removes it
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 creates a new template with name "API Template", then removes it\n');
14+
util.log('Logging in ...');
15+
instance.login('new@user.attask', 'user').then(
16+
function(data) {
17+
util.log('Creating new template ...');
18+
instance.post('tmpl', {name: 'API Template', description: 'This template has been created using API'}).then(
19+
function(data) {
20+
util.log('Create success. Received data:');
21+
console.log(util.inspect(data, {colors:true}));
22+
util.log('Removing template with objID=' + data.ID + ' ...');
23+
instance.remove('tmpl', data.ID, true).then(
24+
function() {
25+
util.log('Remove success');
26+
},
27+
function(error) {
28+
util.log('Remove failure. Received data:');
29+
console.log(util.inspect(error, {colors:true}));
30+
}
31+
);
32+
},
33+
function(error) {
34+
util.log('Create failure. Received data:');
35+
console.log(util.inspect(error, {colors:true}));
36+
}
37+
);
38+
},
39+
function(error) {
40+
util.log('Login failure. Received data:');
41+
console.log(util.inspect(error, {colors:true}));
42+
}
43+
);

examples/node/logout.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ instance.login('new@user.attask', 'user').then(
1616
function(data) {
1717
util.log('Logging out ...');
1818
instance.logout().then(
19-
function(data) {
20-
util.log('Logout success. Received data:');
21-
console.log(util.inspect(data, {colors:true}));
19+
function() {
20+
util.log('Logout success.');
2221
},
2322
function(error) {
2423
util.log('Logout failure. Received data:');

src/Api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ require('./plugins/search')(Api);
4343
require('./plugins/get')(Api);
4444
require('./plugins/post')(Api);
4545
require('./plugins/put')(Api);
46-
require('./plugins/delete')(Api);
46+
require('./plugins/remove')(Api);
4747
require('./plugins/report')(Api);
4848
require('./plugins/count')(Api);
4949
require('./plugins/copy')(Api);

src/plugins/delete.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/plugins/logout.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
module.exports = function(Api) {
22
/**
33
* Logs out from AtTask
4-
* @return {Promise} A promise which will resolved with <code>true</code> if everything went ok and rejected otherwise
4+
* @return {Promise} A promise which will resolved if everything went ok and rejected otherwise
55
*/
66
Api.prototype.logout = function () {
77
var that = this;
88
return new Promise(function (resolve, reject) {
99
that.request('logout', null, null, 'GET').then(function (result) {
10-
if (result) {
10+
if (result && result.success) {
1111
delete that.httpOptions.headers.sessionID;
12-
resolve(result.success);
12+
resolve();
1313
} else {
14-
reject(false);
14+
reject();
1515
}
1616
});
1717
});

src/plugins/remove.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = function(Api) {
2+
3+
/**
4+
* Deletes an 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 {Boolean} [bForce] Pass true to cause the server to remove the specified data and its dependants
8+
* @returns {Promise} A promise which will resolved if everything went ok and rejected otherwise
9+
*/
10+
Api.prototype.remove = function (objCode, objID, bForce) {
11+
var that = this;
12+
return new Promise(function (resolve, reject) {
13+
var params = bForce ? {force: true} : null;
14+
return that.request(objCode + '/' + objID, params, null, 'DELETE').then(function (result) {
15+
if (result && result.success) {
16+
resolve();
17+
} else {
18+
reject();
19+
}
20+
});
21+
});
22+
23+
};
24+
};

test/Api.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Create new instance for API', function() {
1111
expect(api).to.respondTo('post');
1212
expect(api).to.respondTo('put');
1313
expect(api).to.respondTo('search');
14-
expect(api).to.respondTo('delete');
14+
expect(api).to.respondTo('remove');
1515
expect(api).to.respondTo('report');
1616
expect(api).to.respondTo('count');
1717
expect(api).to.respondTo('copy');

test/plugins/remove.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.remove() 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 (force=false)', function() {
21+
api.remove('foo', '123');
22+
expect(api.request).to.have.callCount(1);
23+
expect(api.request).to.have.been.calledWith('foo/123', null, null, "DELETE");
24+
});
25+
26+
it('should call request() with proper params (force=true)', function() {
27+
api.remove('foo', '123', true);
28+
expect(api.request).to.have.callCount(1);
29+
expect(api.request).to.have.been.calledWith('foo/123', {force: true}, null, "DELETE");
30+
});
31+
32+
it('should resolve returned promise if removal was ok', function(done) {
33+
api.request.resolves({success: true});
34+
var promise = api.logout();
35+
expect(promise).to.be.fulfilled.and.notify(done);
36+
});
37+
38+
it('should reject returned promise if removal was not ok', function(done) {
39+
api.httpOptions.headers.sessionID = 123;
40+
api.request.resolves(false);
41+
var promise = api.remove('foo', '123');
42+
expect(promise).to.be.rejected.and.notify(done);
43+
});
44+
});

0 commit comments

Comments
 (0)