From ef873e0c2927ca21a29667e2a98d60f9ee19417b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Mon, 24 Aug 2020 10:37:25 +0000 Subject: [PATCH] [FIX] mail: introduce findFromIdentifyingData This is necessary to check the existence of a record based on identifying data without having to use `insert` (or the inefficient `find`). PR: #56243 Part of task-2323670 X-original-commit: c4bbfc5e2a8d236e5fc79a63c13575de146afbd7 --- addons/mail/static/src/model/model_manager.js | 17 +++++++++++++++-- addons/mail/static/src/models/model/model.js | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/addons/mail/static/src/model/model_manager.js b/addons/mail/static/src/model/model_manager.js index 183e0d27e27..003e4fd8067 100644 --- a/addons/mail/static/src/model/model_manager.js +++ b/addons/mail/static/src/model/model_manager.js @@ -171,6 +171,20 @@ class ModelManager { return this.all(Model).find(findFunc); } + /** + * Gets the unique record of provided model that matches the given + * identifying data, if it exists. + * @see `_createRecordLocalId` for criteria of identification. + * + * @param {mail.model} Model class + * @param {Object} data + * @returns {mail.model|undefined} + */ + findFromIdentifyingData(Model, data) { + const localId = Model._createRecordLocalId(data); + return Model.get(localId); + } + /** * This method returns the record of provided model that matches provided * local id. Useful to convert a local id to a record. @@ -785,8 +799,7 @@ class ModelManager { const dataList = isMulti ? data : [data]; const records = []; for (const data of dataList) { - const localId = Model._createRecordLocalId(data); - let record = Model.get(localId); + let record = Model.findFromIdentifyingData(data); if (!record) { record = this._create(Model, data); } else { diff --git a/addons/mail/static/src/models/model/model.js b/addons/mail/static/src/models/model/model.js index f700b330a93..3696332ae85 100644 --- a/addons/mail/static/src/models/model/model.js +++ b/addons/mail/static/src/models/model/model.js @@ -110,6 +110,19 @@ function factory() { return this.env.modelManager.find(this, findFunc); } + /** + * Gets the unique record that matches the given identifying data, if it + * exists. + * @see `_createRecordLocalId` for criteria of identification. + * + * @static + * @param {Object} data + * @returns {mail.model|undefined} + */ + static findFromIdentifyingData(data) { + return this.env.modelManager.findFromIdentifyingData(this, data); + } + /** * This method returns the record of this model that matches provided * local id. Useful to convert a local id to a record. Note that even