diff --git a/addons/mail/static/tests/activity_tests.js b/addons/mail/static/tests/activity_tests.js index 92af5da6c6b..ed32a3dd096 100644 --- a/addons/mail/static/tests/activity_tests.js +++ b/addons/mail/static/tests/activity_tests.js @@ -4,10 +4,9 @@ import ActivityView from '@mail/js/views/activity/activity_view'; import testUtils from 'web.test_utils'; import { createWebClient } from "@web/../tests/webclient/actions/helpers"; -import { legacyExtraNextTick } from "@web/../tests/helpers/utils"; +import { legacyExtraNextTick, patchWithCleanup } from "@web/../tests/helpers/utils"; import { doAction, getActionManagerTestConfig } from "@web/../tests/webclient/actions/helpers"; import { registry } from "@web/core/registry"; -import { makeFakeUserService } from "@web/../tests/helpers/mock_services"; let testConfig; @@ -392,15 +391,7 @@ QUnit.test("activity view: no group_by_menu and no comparison_menu", async funct } }; - registry.category("services").add( - "user", - makeFakeUserService({ - session_info: { - user_context: { lang: "zz_ZZ" }, - }, - }), - { force: true } - ); + patchWithCleanup(odoo.session_info.user_context, { lang: "zz_ZZ" }); const webClient = await createWebClient({ testConfig, mockRPC , legacyParams: {withLegacyMockServer: true}}); diff --git a/addons/mail/static/tests/tools/debug_manager_tests.js b/addons/mail/static/tests/tools/debug_manager_tests.js index 24c35286a7d..dc4243f208b 100644 --- a/addons/mail/static/tests/tools/debug_manager_tests.js +++ b/addons/mail/static/tests/tools/debug_manager_tests.js @@ -1,7 +1,7 @@ /** @odoo-module **/ import { manageMessages } from "@mail/js/tools/debug_manager"; -import { click, legacyExtraNextTick } from "@web/../tests/helpers/utils"; +import { click, legacyExtraNextTick, patchWithCleanup } from "@web/../tests/helpers/utils"; import { createWebClient, doAction, @@ -15,9 +15,8 @@ QUnit.module("DebugMenu"); QUnit.test("Manage Messages", async function (assert) { assert.expect(6); - const testConfig = Object.assign(getActionManagerTestConfig(), { - debug: "1", - }); + patchWithCleanup(odoo, { debug: "1" }); + const testConfig = getActionManagerTestConfig(); // Add fake "mail.message" model and arch testConfig.serverData.models["mail.message"] = { diff --git a/addons/partner_autocomplete/static/src/js/web_company_autocomplete.js b/addons/partner_autocomplete/static/src/js/web_company_autocomplete.js index 803919fdf1d..245abe4740b 100644 --- a/addons/partner_autocomplete/static/src/js/web_company_autocomplete.js +++ b/addons/partner_autocomplete/static/src/js/web_company_autocomplete.js @@ -3,11 +3,11 @@ import { registry } from "@web/core/registry"; export const companyAutocompleteService = { - dependencies: ["orm", "user"], + dependencies: ["orm", "company"], - start(env, { orm, user }) { + start(env, { orm, company }) { if (odoo.session_info.iap_company_enrich) { - const currentCompanyId = user.current_company.id; + const currentCompanyId = company.currentCompany.id; orm.silent.call("res.company", "iap_enrich_auto", [currentCompanyId], {}); } }, diff --git a/addons/web/static/src/core/user_service.js b/addons/web/static/src/core/user_service.js index 9e4652480c5..dea43afbe4f 100644 --- a/addons/web/static/src/core/user_service.js +++ b/addons/web/static/src/core/user_service.js @@ -1,135 +1,50 @@ /** @odoo-module **/ -import { browser } from "./browser/browser"; import { registry } from "./registry"; -function parseCompanyIds(cidsFromHash) { - const cids = []; - if (typeof cidsFromHash === "string") { - cids.push(...cidsFromHash.split(",").map(Number)); - } else if (typeof cidsFromHash === "number") { - cids.push(cidsFromHash); - } - return cids; -} - -export function computeAllowedCompanyIds(cids) { - const { user_companies } = odoo.session_info; - - let allowedCompanies = cids || []; - const allowedCompaniesFromSession = user_companies.allowed_companies; - const notReallyAllowedCompanies = allowedCompanies.filter( - (id) => !(id in allowedCompaniesFromSession) - ); - - if (!allowedCompanies.length || notReallyAllowedCompanies.length) { - allowedCompanies = [user_companies.current_company]; - } - return allowedCompanies; -} - -/** - * @param {function} getAllowedCompanyIds function that returns the allowed - * company ids. - * @returns {function} function that takes a mode ('toggle' or 'loginto') and - * a company id, and that returns the selected company ids following that - * operation. - */ -export function makeSetCompanies(getAllowedCompanyIds) { - return function setCompanies(mode, companyId) { - let nextCompanyIds = getAllowedCompanyIds().slice(); - if (mode === "toggle") { - if (nextCompanyIds.includes(companyId)) { - nextCompanyIds = nextCompanyIds.filter((id) => id !== companyId); - } else { - nextCompanyIds.push(companyId); - } - } else if (mode === "loginto") { - if (nextCompanyIds.length === 1) { - // 1 enabled company: stay in single company mode - nextCompanyIds = [companyId]; - } else { - // multi company mode - if (nextCompanyIds.includes(companyId)) { - nextCompanyIds = nextCompanyIds.filter((id) => id !== companyId); - } - nextCompanyIds.unshift(companyId); - } - } - return nextCompanyIds.length ? nextCompanyIds : [companyId]; - }; -} - export const userService = { - dependencies: ["router", "cookie"], - start(env, { router, cookie }) { - const info = odoo.session_info; - const { - user_context, - username, - name, - is_system, - is_admin, - partner_id, - user_companies, - home_action_id, - show_effect: showEffect, - } = info; + start() { + const sessionInfo = odoo.session_info; - let cids; - if ("cids" in router.current.hash) { - cids = parseCompanyIds(router.current.hash.cids); - } else if ("cids" in cookie.current) { - cids = parseCompanyIds(cookie.current.cids); - } - const allowedCompanies = computeAllowedCompanyIds(cids); - let context = { - lang: user_context.lang, - tz: user_context.tz, - uid: info.uid, - allowed_company_ids: allowedCompanies, + const context = { + ...sessionInfo.user_context, + uid: sessionInfo.uid, }; - - const stringCIds = allowedCompanies.join(","); - router.replaceState({ cids: stringCIds }, { lock: true }); - cookie.setCookie("cids", stringCIds); - - const setCompanies = makeSetCompanies(() => allowedCompanies); return { - context, + get context() { + return Object.assign({}, context); + }, + removeFromContext(key) { + delete context[key]; + }, + updateContext(update) { + Object.assign(context, update); + }, + name: sessionInfo.name, + userName: sessionInfo.username, + isAdmin: sessionInfo.is_admin, + isSystem: sessionInfo.is_system, + partnerId: sessionInfo.partner_id, + home_action_id: sessionInfo.home_action_id, + showEffect: sessionInfo.show_effect, get userId() { return context.uid; }, - name, - userName: username, - isAdmin: is_admin, - isSystem: is_system, - partnerId: partner_id, - allowed_companies: user_companies.allowed_companies, - current_company: user_companies.allowed_companies[allowedCompanies[0]], get lang() { return context.lang; }, get tz() { return context.tz; }, - home_action_id, get db() { const res = { - name: info.db, + name: sessionInfo.db, }; - if ("dbuuid" in info) { - res.uuid = info.dbuuid; + if ("dbuuid" in sessionInfo) { + res.uuid = sessionInfo.dbuuid; } return res; }, - showEffect, - setCompanies: (mode, companyId) => { - const nextCompanyIds = setCompanies(mode, companyId).join(","); - router.pushState({ cids: nextCompanyIds }, { lock: true }); - cookie.setCookie("cids", nextCompanyIds); - browser.setTimeout(() => window.location.reload()); // history.pushState is a little async - }, }; }, }; diff --git a/addons/web/static/src/env.js b/addons/web/static/src/env.js index 8ebd82ab021..f5fbdc4f508 100644 --- a/addons/web/static/src/env.js +++ b/addons/web/static/src/env.js @@ -23,15 +23,14 @@ import { registry } from "./core/registry"; /** * Return a value Odoo Env object * - * @param {string} debug * @returns {OdooEnv} */ -export function makeEnv(debug) { +export function makeEnv() { return { qweb: new owl.QWeb(), bus: new owl.core.EventBus(), services: {}, - debug, + debug: odoo.debug, _t: () => { throw new Error("Translations are not ready yet. Maybe use _lt instead?"); }, diff --git a/addons/web/static/src/start.js b/addons/web/static/src/start.js index 73d9de92c3e..95b0a26fe02 100644 --- a/addons/web/static/src/start.js +++ b/addons/web/static/src/start.js @@ -27,7 +27,7 @@ export async function startWebClient(Webclient) { odoo.isReady = false; // setup environment - const env = makeEnv(odoo.debug); + const env = makeEnv(); const [, templates] = await Promise.all([ startServices(env), odoo.loadTemplatesPromise.then(processTemplates), diff --git a/addons/web/static/src/webclient/company_service.js b/addons/web/static/src/webclient/company_service.js new file mode 100644 index 00000000000..ff940bc4326 --- /dev/null +++ b/addons/web/static/src/webclient/company_service.js @@ -0,0 +1,88 @@ +/** @odoo-module **/ + +import { browser } from "@web/core/browser/browser"; +import { registry } from "@web/core/registry"; + +function parseCompanyIds(cidsFromHash) { + const cids = []; + if (typeof cidsFromHash === "string") { + cids.push(...cidsFromHash.split(",").map(Number)); + } else if (typeof cidsFromHash === "number") { + cids.push(cidsFromHash); + } + return cids; +} + +function computeAllowedCompanyIds(cids) { + const { user_companies } = odoo.session_info; + let allowedCompanyIds = cids || []; + const availableCompaniesFromSession = user_companies.allowed_companies; + const notReallyAllowedCompanies = allowedCompanyIds.filter( + (id) => !(id in availableCompaniesFromSession) + ); + + if (!allowedCompanyIds.length || notReallyAllowedCompanies.length) { + allowedCompanyIds = [user_companies.current_company]; + } + return allowedCompanyIds; +} + +export const companyService = { + dependencies: ["user", "router", "cookie"], + start(env, { user, router, cookie }) { + let cids; + if ("cids" in router.current.hash) { + cids = parseCompanyIds(router.current.hash.cids); + } else if ("cids" in cookie.current) { + cids = parseCompanyIds(cookie.current.cids); + } + let allowedCompanyIds = computeAllowedCompanyIds(cids); + + const stringCIds = allowedCompanyIds.join(","); + router.replaceState({ cids: stringCIds }, { lock: true }); + cookie.setCookie("cids", stringCIds); + + user.updateContext({ allowed_company_ids: allowedCompanyIds }); + const availableCompanies = odoo.session_info.user_companies.allowed_companies; + + return { + availableCompanies, + get allowedCompanyIds() { + return allowedCompanyIds.slice(); + }, + get currentCompany() { + return availableCompanies[allowedCompanyIds[0]]; + }, + setCompanies(mode, companyId) { + // compute next company ids + let nextCompanyIds = allowedCompanyIds.slice(); + if (mode === "toggle") { + if (nextCompanyIds.includes(companyId)) { + nextCompanyIds = nextCompanyIds.filter((id) => id !== companyId); + } else { + nextCompanyIds.push(companyId); + } + } else if (mode === "loginto") { + if (nextCompanyIds.length === 1) { + // 1 enabled company: stay in single company mode + nextCompanyIds = [companyId]; + } else { + // multi company mode + if (nextCompanyIds.includes(companyId)) { + nextCompanyIds = nextCompanyIds.filter((id) => id !== companyId); + } + nextCompanyIds.unshift(companyId); + } + } + allowedCompanyIds = nextCompanyIds.length ? nextCompanyIds : [companyId]; + + // apply them + router.pushState({ cids: allowedCompanyIds }, { lock: true }); + cookie.setCookie("cids", allowedCompanyIds); + browser.setTimeout(() => browser.location.reload()); // history.pushState is a little async + }, + }; + }, +}; + +registry.category("services").add("company", companyService); diff --git a/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.js b/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.js index 87d67f0e8fb..62a4f097f2d 100644 --- a/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.js +++ b/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.js @@ -5,20 +5,21 @@ import { registry } from "../../core/registry"; export class SwitchCompanyMenu extends owl.Component { static isDisplayed(env) { - const allowedCompanies = env.services.user.allowed_companies; - return Object.keys(allowedCompanies).length > 1 && !env.isSmall; + const { availableCompanies } = env.services.company; + return Object.keys(availableCompanies).length > 1 && !env.isSmall; } setup() { - this.user = useService("user"); + this.companyService = useService("company"); + this.currentCompany = this.companyService.currentCompany; } toggleCompany(companyId) { - this.user.setCompanies("toggle", companyId); + this.companyService.setCompanies("toggle", companyId); } logIntoCompany(companyId) { - this.user.setCompanies("loginto", companyId); + this.companyService.setCompanies("loginto", companyId); } } SwitchCompanyMenu.template = "web.SwitchCompanyMenu"; diff --git a/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.xml b/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.xml index 32aec873e04..2a5b08afd1e 100644 --- a/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.xml +++ b/addons/web/static/src/webclient/switch_company_menu/switch_company_menu.xml @@ -4,12 +4,12 @@ - + - + - + @@ -19,8 +19,8 @@
- - + +