[FIX] im_livechat: fix chatbot restart

Before this PR, a race condition could occur when restarting the
chatbot: if the rpc returned before the restart message reception
on the bus, the chat would not restart. This was due to the message
being inserted twice. Since the id is used as an unique `t-key` in
the thread template, any duplicate would lead to an error.

This PR fixes the issue by checking that the message is not already
present before adding it to the thread messages.

Steps to reproduce the issue:
- Go to the contact us page
- Start a chat with the bot and proceed until the end
- Put your network in slow 3g mode
- Click on the restart button
- The chatbot is not restarting as expected

closes odoo/odoo#150975

X-original-commit: 6c535bb71edc40aaf3fa75bab702fb9be4a24779
Signed-off-by: Matthieu Stockbauer (tsm) <tsm@odoo.com>
This commit is contained in:
tsm-odoo
2024-01-25 11:50:09 +00:00
parent e1316a425c
commit bb495ef55e
@@ -122,11 +122,19 @@ export class ChatBotService {
localStorage.removeItem(
`im_livechat.chatbot.state.uuid_${this.livechatService.thread.uuid}`
);
const message = await this.rpc("/chatbot/restart", {
channel_uuid: this.livechatService.thread.uuid,
chatbot_script_id: this.chatbot.scriptId,
});
this.livechatService.thread?.messages.push({ ...message, body: markup(message.body) });
const message = this.store.Message.insert(
await this.rpc("/chatbot/restart", {
channel_uuid: this.livechatService.thread.uuid,
chatbot_script_id: this.chatbot.scriptId,
}),
{ html: true }
);
if (!this.livechatService.thread) {
return;
}
if (message.notIn(this.livechatService.thread.messages)) {
this.livechatService.thread.messages.push(message);
}
this.currentStep = null;
this.start();
}