[FIX] website_livechat: ensure session data integrity during visitor lifecycle

This commit fixes two issues encountered during the visitor lifecycle.

1. When a visitor arrives on the website on a non tracked page, a
website.visitor is not created yet, but the visitor can still start a livechat
session. When navigating to a tracked page, a website.visitor is created but the
lifechat session already started is not recovered and the discussion is lost at
visitor's side. This commit links the already started livechat session to the
newly created visitor to ensure that the conversation can still continue
normally.

2. When a visitor logs in, his attributed visitor is linked to the partner. But
there can be only one visitor per partner. If there was already a visitor linked
to the partner, the later visitor's livechat session are updated with the
partner and the visitor is then deleted. But the visitor's livechat sessions are
not linked to the main partner visitor. So when looking at all the session the
partner had, we can only see the main visitor's livechat session.
This commit copy the livechat session of that later visitor to the main partner
visitor to ensure keeping the complete livechat history for each partner.

Task ID: 2460892

closes odoo/odoo#69402

X-original-commit: 4e1034ff2f08fa20bcdc9ac39eb2489d34a9715d
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
David Beguin
2021-04-16 14:11:34 +00:00
parent 5c4ac1d016
commit 6d6a442ecd
@@ -6,6 +6,7 @@ import json
from odoo import models, api, fields, _
from odoo.exceptions import UserError
from odoo.http import request
class WebsiteVisitor(models.Model):
@@ -92,6 +93,12 @@ class WebsiteVisitor(models.Model):
notifications.append([(self._cr.dbname, 'res.partner', operator.partner_id.id), mail_channel_info])
self.env['bus.bus'].sendmany(notifications)
def _link_to_visitor(self, target, keep_unique=True):
""" Copy sessions of the secondary visitors to the main partner visitor. """
if target.partner_id:
target.mail_channel_ids |= self.mail_channel_ids
super(WebsiteVisitor, self)._link_to_visitor(target, keep_unique=keep_unique)
def _link_to_partner(self, partner, update_values=None):
""" Adapt partner in members of related livechats """
if partner:
@@ -100,3 +107,14 @@ class WebsiteVisitor(models.Model):
(4, partner.id),
]
super(WebsiteVisitor, self)._link_to_partner(partner, update_values=update_values)
def _create_visitor(self):
visitor = super(WebsiteVisitor, self)._create_visitor()
mail_channel_uuid = json.loads(request.httprequest.cookies.get('im_livechat_session', '{}')).get('uuid')
if mail_channel_uuid:
mail_channel = request.env["mail.channel"].sudo().search([("uuid", "=", mail_channel_uuid)])
mail_channel.write({
'livechat_visitor_id': visitor.id,
'anonymous_name': visitor.display_name
})
return visitor