Skip to content

Commit

Permalink
Refs #41 Refactor 'creme.cti.phoneCall' and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joehybird committed Jul 23, 2020
1 parent de962be commit 47397d9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
# Apps :
* Reports :
- A property 'models.AbstractReportGraph.abscissa_info' has been added.
* CTI :
- Javascript :
- Refactor 'creme.cti.phoneCall' and add 'creme.cti.PhoneCallAction'

Breaking changes :
------------------
Expand Down
51 changes: 50 additions & 1 deletion creme/cti/static/cti/js/cti.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
Creme is a free/open-source Customer Relationship Management software
Copyright (C) 2009-2017 Hybird
Copyright (C) 2009-2020 Hybird
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
Expand All @@ -25,6 +25,54 @@

creme.cti = {};

creme.cti.PhoneCallAction = creme.component.Action.sub({
_init_: function(options) {
this._super_(creme.component.Action, '_init_', this._run, options);
},

_run: function(options) {
options = $.extend({}, this.options(), options || {});

var self = this;
var call = creme.ajax.query(options.ctiServerUrl);

call.onFail(function() {
creme.dialogs.warning(gettext("Unable to start the phone call. Please check your CTI configuration."))
.onClose(function() { self.fail(); })
.open();
}).onDone(function() {
var query = creme.ajax.query(options.saveCallUrl);

query.onFail(function() {
// TODO: better error message (wait for jsonify improvement)
creme.dialogs.warning(gettext("Failed to save the phone call."))
.onClose(function() { self.fail(); })
.open();
}).onDone(function(event, message) {
creme.dialogs.html($('<p>').append(message), {'title': gettext("Phone call")})
.onClose(function() { self.done(); })
.open();
}).post({
entity_id: options.callerId
});
}).get({
n_tel: options.number
});
}
});

creme.cti.phoneCall = function(external_url, creme_url, number, entity_id) {
var action = new creme.cti.PhoneCallAction({
ctiServerUrl: external_url,
saveCallUrl: creme_url,
number: number,
callerId: entity_id
});

return action.start();
};

/*
creme.cti.phoneCall = function(external_url, creme_url, number, entity_id) {
creme.ajax.get({
url: external_url,
Expand All @@ -43,5 +91,6 @@ creme.cti.phoneCall = function(external_url, creme_url, number, entity_id) {
}
});
};
*/

}(jQuery));
112 changes: 112 additions & 0 deletions creme/cti/static/cti/js/tests/cti-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
(function($) {

QUnit.module("creme.cti.actions", new QUnitMixin(QUnitEventMixin,
QUnitAjaxMixin,
QUnitDialogMixin, {
beforeEach: function() {
var backend = this.backend;

this.setMockBackendGET({
'mock/cti/call/fail': backend.response(400, ''),
'mock/cti/call': backend.response(200, '')
});

this.setMockBackendPOST({
'mock/call/save/fail': backend.response(400, ''),
'mock/call/save': backend.response(200, 'Call saved')
});

this.ctiActionListener = {
'fail': this.mockListener('action-fail'),
'cancel': this.mockListener('action-cancel'),
'done': this.mockListener('action-done')
};
}
}));

QUnit.test('creme.cti.PhoneCallAction (call failed)', function(assert) {
var action = new creme.cti.PhoneCallAction({
ctiServerUrl: 'mock/cti/call/fail',
saveCallUrl: 'mock/call/save',
number: '007',
callerId: 12
}).on(this.ctiActionListener);

this.assertClosedDialog();

action.start();

this.assertOpenedAlertDialog(gettext("Unable to start the phone call. Please check your CTI configuration."));
this.closeDialog();

deepEqual([['fail']], this.mockListenerCalls('action-fail'));
deepEqual([
['GET', {n_tel: '007'}]
], this.mockBackendUrlCalls('mock/cti/call/fail'));
});

QUnit.test('creme.cti.PhoneCallAction (save failed)', function(assert) {
var action = new creme.cti.PhoneCallAction({
ctiServerUrl: 'mock/cti/call',
saveCallUrl: 'mock/call/save/fail',
number: '007',
callerId: 12
}).on(this.ctiActionListener);

this.assertClosedDialog();

action.start();

this.assertOpenedAlertDialog(gettext("Failed to save the phone call."));
this.closeDialog();

deepEqual([['fail']], this.mockListenerCalls('action-fail'));
deepEqual([
['GET', {n_tel: '007'}]
], this.mockBackendUrlCalls('mock/cti/call'));
deepEqual([
['POST', {entity_id: 12}]
], this.mockBackendUrlCalls('mock/call/save/fail'));
});

QUnit.test('creme.cti.PhoneCallAction (ok)', function(assert) {
var action = new creme.cti.PhoneCallAction({
ctiServerUrl: 'mock/cti/call',
saveCallUrl: 'mock/call/save',
number: '007',
callerId: 12
}).on(this.ctiActionListener);

this.assertClosedDialog();

action.start();

this.assertOpenedDialog(gettext("Call saved"));
this.closeDialog();

deepEqual([['done']], this.mockListenerCalls('action-done'));
deepEqual([
['GET', {n_tel: '007'}]
], this.mockBackendUrlCalls('mock/cti/call'));
deepEqual([
['POST', {entity_id: 12}]
], this.mockBackendUrlCalls('mock/call/save'));
});

QUnit.test('creme.cti.phoneCall', function(assert) {
var action = creme.cti.phoneCall('mock/cti/call', 'mock/call/save', '007', 12);

this.assertOpenedDialog(gettext("Call saved"));
this.closeDialog();

ok(action.isStatusDone());

deepEqual([
['GET', {n_tel: '007'}]
], this.mockBackendUrlCalls('mock/cti/call'));
deepEqual([
['POST', {entity_id: 12}]
], this.mockBackendUrlCalls('mock/call/save'));
});

}(jQuery));
1 change: 1 addition & 0 deletions creme/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@
('creme.billing', 'billing/js/tests/billing-listview.js'),
('creme.commercial', 'commercial/js/tests/commercial-score.js'),
('creme.crudity', 'crudity/js/tests/crudity-actions.js'),
('creme.cti', 'cti/js/tests/cti-actions.js'),
('creme.emails', 'emails/js/tests/emails-actions.js'),
('creme.emails', 'emails/js/tests/emails-listview.js'),
('creme.events', 'events/js/tests/events-listview.js'),
Expand Down

0 comments on commit 47397d9

Please sign in to comment.