From 06f1a05fa1d81ed76e55856a25cdf71fc494c1eb Mon Sep 17 00:00:00 2001 From: kobros-tech Date: Mon, 13 Jan 2025 16:27:38 +0300 Subject: [PATCH 1/2] [18.0][IMP] sign_oca: adding events for bus synchronization --- sign_oca/__manifest__.py | 3 +- sign_oca/models/sign_oca_request.py | 13 +++++ .../src/js/sign_requests_service.esm.js | 58 +++++++++++++++++++ sign_oca/static/src/js/systray_service.esm.js | 18 +----- sign_oca/static/src/xml/systray.xml | 9 ++- 5 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 sign_oca/static/src/js/sign_requests_service.esm.js diff --git a/sign_oca/__manifest__.py b/sign_oca/__manifest__.py index 21eb9a11..f8e94ba1 100644 --- a/sign_oca/__manifest__.py +++ b/sign_oca/__manifest__.py @@ -9,7 +9,7 @@ "license": "AGPL-3", "author": "Dixmit,Odoo Community Association (OCA)", "website": "https://github.com/OCA/sign", - "depends": ["web_editor", "portal", "base_sparse_field"], + "depends": ["web_editor", "portal", "base_sparse_field", "bus"], "data": [ "security/security.xml", "views/menu.xml", @@ -48,6 +48,7 @@ "sign_oca/static/src/elements/signature.esm.js", "sign_oca/static/src/elements/check.esm.js", "sign_oca/static/src/components/sign_oca_pdf/sign_oca_pdf.esm.js", + "sign_oca/static/src/js/sign_requests_service.esm.js", "sign_oca/static/src/js/sign_oca.esm.js", "sign_oca/static/src/js/systray_service.esm.js", "sign_oca/static/src/xml/*.xml", diff --git a/sign_oca/models/sign_oca_request.py b/sign_oca/models/sign_oca_request.py index 079628d8..b0abe168 100644 --- a/sign_oca/models/sign_oca_request.py +++ b/sign_oca/models/sign_oca_request.py @@ -328,11 +328,24 @@ def _set_action_log(self, action, **kwargs): .create(self._set_action_log_vals(action, **kwargs)) ) + @api.model + def notify_changes(self, partner_recs): + # send notification to the list of subscribers + partner_list_ids = partner_recs.mapped("id") + for partner_id in partner_list_ids: + self.env["bus.bus"]._sendone( + "broadcast", + f"sign_oca_request_updates_{partner_id}", + {"message": "Sign OCA Requests Model updated"}, + ) + @api.model_create_multi def create(self, vals_list): records = super().create(vals_list) for record in records: record._set_action_log("create") + partner_recs = record.signer_ids.mapped("partner_id") + self.notify_changes(partner_recs) return records diff --git a/sign_oca/static/src/js/sign_requests_service.esm.js b/sign_oca/static/src/js/sign_requests_service.esm.js new file mode 100644 index 00000000..aa5cd461 --- /dev/null +++ b/sign_oca/static/src/js/sign_requests_service.esm.js @@ -0,0 +1,58 @@ +/** @odoo-module **/ +/* global */ +/* Copyright 2025 Kencove - Mohamed Alkobrosli + * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ + +import {Reactive} from "@web/core/utils/reactive"; +import {reactive} from "@odoo/owl"; +import {registry} from "@web/core/registry"; +import {user} from "@web/core/user"; + +export class WatchSignRequestsService extends Reactive { + static modelToLoad = []; + static serviceDependencies = ["bus_service", "orm", "notification"]; + + constructor() { + super(); + this.ready = this.setup(...arguments).then(() => this); + } + + async setup(env, {bus_service, orm, notification}) { + this.env = env; + this.bus_service = bus_service; + this.orm = orm; + this.notification = notification; + this.sign_requests = reactive({signerCounter: 0, signerGroups: []}); + + this.bus_service.subscribe( + `sign_oca_request_updates_${user.partnerId}`, + async ({message}) => { + if (message) { + await this.fetchSystraySigner(); + } + } + ); + } + async fetchSystraySigner() { + const groups = await this.orm.call("res.users", "sign_oca_request_user_count"); + let total = 0; + for (const group of groups) { + total += group.total_records || 0; + } + this.sign_requests.signerGroups = groups; + this.sign_requests.signerCounter = total; + return {groups, total}; + } + getTotalSignRequests() { + return this.sign_requests.signerCounter; + } +} + +export const watchSignRequestsService = { + dependencies: WatchSignRequestsService.serviceDependencies, + async start(env, services) { + return new WatchSignRequestsService(env, services).ready; + }, +}; + +registry.category("services").add("watchSignRequests", watchSignRequestsService); diff --git a/sign_oca/static/src/js/systray_service.esm.js b/sign_oca/static/src/js/systray_service.esm.js index d7e6fa33..9b5ab721 100644 --- a/sign_oca/static/src/js/systray_service.esm.js +++ b/sign_oca/static/src/js/systray_service.esm.js @@ -15,26 +15,14 @@ export class SignerMenuView extends Component { this.discussSystray = useDiscussSystray(); this.orm = useService("orm"); this.action = useService("action"); + this.watchSignRequests = useService("watchSignRequests"); this.state = useState({ - signerGroups: [], - signerCounter: 0, + sign_requests: this.watchSignRequests.sign_requests, }); onMounted(async () => { - await this.fetchSystraySigner(); + await this.watchSignRequests.fetchSystraySigner(); }); } - async fetchSystraySigner() { - const groups = await this.orm.call("res.users", "sign_oca_request_user_count"); - let total = 0; - for (const group of groups) { - total += group.total_records || 0; - } - this.state.signerCounter = total; - this.state.signerGroups = groups; - } - async onBeforeOpen() { - await this.fetchSystraySigner(); - } availableViews() { return [ [false, "kanban"], diff --git a/sign_oca/static/src/xml/systray.xml b/sign_oca/static/src/xml/systray.xml index 797db60d..0f5c2c97 100644 --- a/sign_oca/static/src/xml/systray.xml +++ b/sign_oca/static/src/xml/systray.xml @@ -3,7 +3,6 @@ @@ -11,16 +10,16 @@
No requests to sign. @@ -30,7 +29,7 @@ name="activityGroups" > Date: Mon, 16 Jun 2025 11:23:53 +0300 Subject: [PATCH 2/2] [IMP] sign_oca: use single channel --- sign_oca/__manifest__.py | 2 +- sign_oca/models/sign_oca_request.py | 11 ++++------- sign_oca/static/src/js/sign_requests_service.esm.js | 12 ++++-------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/sign_oca/__manifest__.py b/sign_oca/__manifest__.py index f8e94ba1..ae8d6470 100644 --- a/sign_oca/__manifest__.py +++ b/sign_oca/__manifest__.py @@ -9,7 +9,7 @@ "license": "AGPL-3", "author": "Dixmit,Odoo Community Association (OCA)", "website": "https://github.com/OCA/sign", - "depends": ["web_editor", "portal", "base_sparse_field", "bus"], + "depends": ["web_editor", "portal", "base_sparse_field"], "data": [ "security/security.xml", "views/menu.xml", diff --git a/sign_oca/models/sign_oca_request.py b/sign_oca/models/sign_oca_request.py index b0abe168..14cfd31d 100644 --- a/sign_oca/models/sign_oca_request.py +++ b/sign_oca/models/sign_oca_request.py @@ -331,13 +331,10 @@ def _set_action_log(self, action, **kwargs): @api.model def notify_changes(self, partner_recs): # send notification to the list of subscribers - partner_list_ids = partner_recs.mapped("id") - for partner_id in partner_list_ids: - self.env["bus.bus"]._sendone( - "broadcast", - f"sign_oca_request_updates_{partner_id}", - {"message": "Sign OCA Requests Model updated"}, - ) + channel = "sign_oca_request_updates" + message = {"message": "Sign OCA Requests Model updated"} + for partner_id in partner_recs: + partner_id._bus_send(channel, message) @api.model_create_multi def create(self, vals_list): diff --git a/sign_oca/static/src/js/sign_requests_service.esm.js b/sign_oca/static/src/js/sign_requests_service.esm.js index aa5cd461..1cdeba5a 100644 --- a/sign_oca/static/src/js/sign_requests_service.esm.js +++ b/sign_oca/static/src/js/sign_requests_service.esm.js @@ -6,7 +6,6 @@ import {Reactive} from "@web/core/utils/reactive"; import {reactive} from "@odoo/owl"; import {registry} from "@web/core/registry"; -import {user} from "@web/core/user"; export class WatchSignRequestsService extends Reactive { static modelToLoad = []; @@ -24,14 +23,11 @@ export class WatchSignRequestsService extends Reactive { this.notification = notification; this.sign_requests = reactive({signerCounter: 0, signerGroups: []}); - this.bus_service.subscribe( - `sign_oca_request_updates_${user.partnerId}`, - async ({message}) => { - if (message) { - await this.fetchSystraySigner(); - } + this.bus_service.subscribe("sign_oca_request_updates", async ({message}) => { + if (message) { + await this.fetchSystraySigner(); } - ); + }); } async fetchSystraySigner() { const groups = await this.orm.call("res.users", "sign_oca_request_user_count");