[PERF] spreadsheet: avoid company currency rpc

Pivot/list monetary fields needs the company currency to display
the value in the said currency format.
Until now, a RPC was made to fetch the currency.
However, since odoo/o-spreadsheet@8710839 and odoo/enterprise@8c0a785
the currency format is already in the model config.
There's no need for the RPC.

This saves one network request and one full spreadsheet evaluation (which
would have occured after the request is done)

Note: see next commit for dashboards.
Part-of: odoo/odoo#151725
This commit is contained in:
Lucas Lefèvre (lul)
2024-02-12 20:24:01 +00:00
parent 2b07d5f54b
commit fc032efc2d
4 changed files with 67 additions and 1 deletions
@@ -14,6 +14,7 @@ const DATA_SOURCE_ID = "CURRENCIES";
class CurrencyPlugin extends UIPlugin {
constructor(config) {
super(config);
this.currentCompanyCurrencyFormat = config.defaultCurrencyFormat;
this.dataSources = config.custom.dataSources;
if (this.dataSources) {
this.dataSources.add(DATA_SOURCE_ID, CurrencyDataSource);
@@ -74,6 +75,9 @@ class CurrencyPlugin extends UIPlugin {
* @returns {string | undefined}
*/
getCompanyCurrencyFormat(companyId) {
if (!companyId && this.currentCompanyCurrencyFormat) {
return this.currentCompanyCurrencyFormat;
}
const currency =
this.dataSources &&
this.dataSources.get(DATA_SOURCE_ID).getCompanyCurrencyFormat(companyId);
@@ -0,0 +1,58 @@
/** @odoo-module */
import { nextTick } from "@web/../tests/helpers/utils";
import { createModelWithDataSource } from "../utils/model";
QUnit.module("spreadsheet currency plugin");
QUnit.test("get default currency format when it's in the config", async (assert) => {
const model = await createModelWithDataSource({
modelConfig: {
defaultCurrencyFormat: "#,##0.00[$θ]",
},
mockRPC: async function (route, args) {
throw new Error("Should not make any RPC");
},
});
assert.strictEqual(model.getters.getCompanyCurrencyFormat(), "#,##0.00[$θ]");
});
QUnit.test("get default currency format when it's not in the config", async (assert) => {
const model = await createModelWithDataSource({
mockRPC: async function (route, args) {
if (args.method === "get_company_currency_for_spreadsheet") {
return {
code: "Odoo",
symbol: "θ",
position: "after",
decimalPlaces: 2,
};
}
},
});
assert.throws(() => model.getters.getCompanyCurrencyFormat(), "Data is loading");
await nextTick();
assert.strictEqual(model.getters.getCompanyCurrencyFormat(), "#,##0.00[$θ]");
assert.verifySteps([]);
});
QUnit.test("get specific currency format", async (assert) => {
const model = await createModelWithDataSource({
modelConfig: {
defaultCurrencyFormat: "#,##0.00[$θ]",
},
mockRPC: async function (route, args) {
if (args.method === "get_company_currency_for_spreadsheet" && args.args[0] === 42) {
return {
code: "Odoo",
symbol: "O",
position: "after",
decimalPlaces: 2,
};
}
},
});
assert.throws(() => model.getters.getCompanyCurrencyFormat(42), "Data is loading");
await nextTick();
assert.strictEqual(model.getters.getCompanyCurrencyFormat(42), "#,##0.00[$O]");
});
@@ -7,7 +7,7 @@ import {
waitForDataSourcesLoaded,
} from "@spreadsheet/../tests/utils/model";
QUnit.module("spreadsheet > Currency");
QUnit.module("spreadsheet > ODOO.CURRENCY.RATE function");
QUnit.test("Basic exchange formula", async (assert) => {
const model = await createModelWithDataSource({
@@ -28,6 +28,7 @@ export function setupDataSourceEvaluation(model) {
*
* @param {object} params
* @param {object} [params.spreadsheetData] Spreadsheet data to import
* @param {object} [params.modelConfig]
* @param {ServerData} [params.serverData] Data to be injected in the mock server
* @param {function} [params.mockRPC] Mock rpc function
*/
@@ -41,10 +42,13 @@ export async function createModelWithDataSource(params = {}) {
serverData: params.serverData || getBasicServerData(),
mockRPC: params.mockRPC,
});
const config = params.modelConfig;
const model = new Model(params.spreadsheetData, {
...config,
custom: {
env,
dataSources: new DataSources(env),
...config?.custom,
},
});
setupDataSourceEvaluation(model);