From d9aa362914945e6cb095b04fa94cfb3469abd907 Mon Sep 17 00:00:00 2001 From: Xavier Dubuc Date: Tue, 10 Nov 2020 08:59:25 +0000 Subject: [FIX] hr_holidays: bring back the out of office panel in threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit task-2277511 closes odoo/odoo#61586 Signed-off-by: Sébastien Theys (seb) --- addons/hr_holidays/i18n/hr_holidays.pot | 7 ++ addons/hr_holidays/static/src/bugfix/bugfix.js | 78 ++++++++++++++++++ .../hr_holidays/static/src/bugfix/bugfix_tests.js | 92 ++++++++++++++++++++++ .../src/components/thread_view/thread_view.js | 29 ------- .../src/components/thread_view/thread_view.xml | 4 +- .../static/tests/helpers/mock_models.js | 28 +++++++ .../static/tests/helpers/mock_server.js | 29 +++++++ addons/hr_holidays/views/hr_leave_template.xml | 2 + addons/mail/static/src/js/core/translation.js | 5 +- addons/mail/static/src/models/locale/locale.js | 14 ++++ addons/mail/static/src/models/partner/partner.js | 14 ++++ 11 files changed, 269 insertions(+), 33 deletions(-) create mode 100644 addons/hr_holidays/static/tests/helpers/mock_models.js create mode 100644 addons/hr_holidays/static/tests/helpers/mock_server.js (limited to 'addons') diff --git a/addons/hr_holidays/i18n/hr_holidays.pot b/addons/hr_holidays/i18n/hr_holidays.pot index 62cc0887314..c952b719400 100644 --- a/addons/hr_holidays/i18n/hr_holidays.pot +++ b/addons/hr_holidays/i18n/hr_holidays.pot @@ -3308,3 +3308,10 @@ msgstr "" #: model_terms:ir.ui.view,arch_db:hr_holidays.hr_leave_view_kanban msgid "validated" msgstr "" + +#. module: hr_holidays +#. openerp-web +#: code:addons/hr_holidays/static/src/bugfix/bugfix.js:0 +#, python-format +msgid "Out of office until %s" +msgstr "" diff --git a/addons/hr_holidays/static/src/bugfix/bugfix.js b/addons/hr_holidays/static/src/bugfix/bugfix.js index 1f0782785eb..41d3e558cc0 100644 --- a/addons/hr_holidays/static/src/bugfix/bugfix.js +++ b/addons/hr_holidays/static/src/bugfix/bugfix.js @@ -8,3 +8,81 @@ odoo.define('hr_holidays/static/src/bugfix/bugfix.js', function (require) { 'use strict'; }); + +// FIXME move me in hr_holidays/static/src/models/partner/partner.js +odoo.define('hr_holidays/static/src/models/partner/partner.js', function (require) { +'use strict'; + +const { + registerClassPatchModel, + registerFieldPatchModel, + registerInstancePatchModel, +} = require('mail/static/src/model/model_core.js'); +const { attr, one2one } = require('mail/static/src/model/model_field.js'); +const { clear } = require('mail/static/src/model/model_field_command.js'); + +const { str_to_datetime } = require('web.time'); + +registerClassPatchModel('mail.partner', 'hr_holidays/static/src/models/partner/partner.js', { + /** + * @override + */ + convertData(data) { + const data2 = this._super(data); + if ('out_of_office_date_end' in data) { + data2.outOfOfficeDateEnd = data.out_of_office_date_end ? data.out_of_office_date_end : clear(); + } + return data2; + }, +}); + +registerInstancePatchModel('mail.partner', 'hr_holidays/static/src/models/partner/partner.js', { + /** + * @private + */ + _computeOutOfOfficeText() { + if (!this.outOfOfficeDateEnd) { + return clear(); + } + if (!this.env.messaging.locale.language) { + return clear(); + } + const currentDate = new Date(); + const date = str_to_datetime(this.outOfOfficeDateEnd); + const options = { day: 'numeric', month: 'short' }; + if (currentDate.getFullYear() !== date.getFullYear()) { + options.year = 'numeric'; + } + const localeCode = this.env.messaging.locale.language.replace(/_/g,'-'); + const formattedDate = date.toLocaleDateString(localeCode, options); + return _.str.sprintf(this.env._t("Out of office until %s"), formattedDate); + }, + +}); + +registerFieldPatchModel('mail.partner', 'hr/static/src/models/partner/partner.js', { + /** + * Serves as compute dependency. + */ + locale: one2one('mail.locale', { + related: 'messaging.locale', + }), + /** + * Date of end of the out of office period of the partner as string. + * String is expected to use Odoo's datetime string format + * (examples: '2011-12-01 15:12:35.832' or '2011-12-01 15:12:35'). + */ + outOfOfficeDateEnd: attr(), + /** + * Text shown when partner is out of office. + */ + outOfOfficeText: attr({ + compute: '_computeOutOfOfficeText', + dependencies: [ + 'locale', + 'outOfOfficeDateEnd', + ], + }), +}); + +}); diff --git a/addons/hr_holidays/static/src/bugfix/bugfix_tests.js b/addons/hr_holidays/static/src/bugfix/bugfix_tests.js index a18e79a4020..6f655603071 100644 --- a/addons/hr_holidays/static/src/bugfix/bugfix_tests.js +++ b/addons/hr_holidays/static/src/bugfix/bugfix_tests.js @@ -16,3 +16,95 @@ QUnit.module('bugfix_tests.js', { }); }); + +// FIXME move me in hr_holidays/static/src/components/thread_view/thread_view_tests.js +odoo.define('hr_holidays/static/src/components/thread_view/thread_view_tests.js', function (require) { +'use strict'; + +const components = { + ThreadView: require('mail/static/src/components/thread_view/thread_view.js'), +}; +const { + afterEach, + beforeEach, + createRootComponent, + start, +} = require('mail/static/src/utils/test_utils.js'); + +QUnit.module('hr_holidays', {}, function () { +QUnit.module('components', {}, function () { +QUnit.module('thread_view', {}, function () { +QUnit.module('thread_view_tests.js', { + beforeEach() { + beforeEach(this); + + /** + * @param {mail.thread_view} threadView + * @param {Object} [otherProps={}] + */ + this.createThreadViewComponent = async (threadView, otherProps = {}) => { + const target = this.widget.el; + const props = Object.assign({ threadViewLocalId: threadView.localId }, otherProps); + await createRootComponent(this, components.ThreadView, { props, target }); + }; + + this.start = async params => { + const { afterEvent, env, widget } = await start(Object.assign({}, params, { + data: this.data, + })); + this.afterEvent = afterEvent; + this.env = env; + this.widget = widget; + }; + }, + afterEach() { + afterEach(this); + }, +}); + +QUnit.test('out of office message on direct chat with out of office partner', async function (assert) { + assert.expect(2); + + // Returning date of the out of office partner, simulates he'll be back in a month + const returningDate = moment.utc().add(1, 'month'); + // Needed partner & user to allow simulation of message reception + this.data['res.partner'].records.push({ + id: 11, + name: "Foreigner partner", + out_of_office_date_end: returningDate.format("YYYY-MM-DD HH:mm:ss"), + }); + this.data['mail.channel'].records = [{ + channel_type: 'chat', + id: 20, + members: [this.data.currentPartnerId, 11], + }]; + await this.start(); + const thread = this.env.models['mail.thread'].findFromIdentifyingData({ + id: 20, + model: 'mail.channel' + }); + const threadViewer = this.env.models['mail.thread_viewer'].create({ + hasThreadView: true, + thread: [['link', thread]], + }); + await this.createThreadViewComponent(threadViewer.threadView, { hasComposer: true }); + assert.containsOnce( + document.body, + '.o_ThreadView_outOfOffice', + "should have an out of office alert on thread view" + ); + const formattedDate = returningDate.toDate().toLocaleDateString( + this.env.messaging.locale.language.replace(/_/g,'-'), + { day: 'numeric', month: 'short' } + ); + assert.ok( + document.querySelector('.o_ThreadView_outOfOffice').textContent.includes(formattedDate), + "out of office message should mention the returning date" + ); +}); + +}); +}); +}); + +}); diff --git a/addons/hr_holidays/static/src/components/thread_view/thread_view.js b/addons/hr_holidays/static/src/components/thread_view/thread_view.js index 89923ff0384..c07ab2189a9 100644 --- a/addons/hr_holidays/static/src/components/thread_view/thread_view.js +++ b/addons/hr_holidays/static/src/components/thread_view/thread_view.js @@ -5,38 +5,9 @@ const components = { ThreadView: require('mail/static/src/components/thread_view/thread_view.js'), }; -const { str_to_datetime } = require('web.time'); const { patch } = require('web.utils'); patch(components.ThreadView, 'hr_holidays/static/src/components/thread_view/thread_view.js', { - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - /** - * Returns the "out of office" text for the correspondent of the thread if - * applicable. - * - * @returns {string} - */ - getOutOfOfficeText() { - if (!this.threadView.thread.correspondent) { - return ""; - } - if (!this.threadView.thread.correspondent.out_of_office_date_end) { - return ""; - } - const currentDate = new Date(); - const date = str_to_datetime(this.threadView.thread.correspondent.out_of_office_date_end); - const options = { day: 'numeric', month: 'short' }; - if (currentDate.getFullYear() !== date.getFullYear()) { - options.year = 'numeric'; - } - const formattedDate = date.toLocaleDateString(window.navigator.language, options); - return _.str.sprintf(this.env._t("Out of office until %s."), formattedDate); - }, - //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- diff --git a/addons/hr_holidays/static/src/components/thread_view/thread_view.xml b/addons/hr_holidays/static/src/components/thread_view/thread_view.xml index f5e0dce55d8..796dcfba651 100644 --- a/addons/hr_holidays/static/src/components/thread_view/thread_view.xml +++ b/addons/hr_holidays/static/src/components/thread_view/thread_view.xml @@ -2,8 +2,8 @@ - -