mock_server.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /** @odoo-module **/
  2. import '@im_livechat/../tests/helpers/mock_server'; // ensure mail overrides are applied first
  3. import { patch } from "@web/core/utils/patch";
  4. import { MockServer } from "@web/../tests/helpers/mock_server";
  5. patch(MockServer.prototype, 'website_livechat', {
  6. /**
  7. * @override
  8. */
  9. async _performRPC(route, args) {
  10. if (route === '/web/dataset/call_button') {
  11. return this._mockCallButton(args);
  12. }
  13. return this._super(route, args);
  14. },
  15. /**
  16. * Simulate a 'call_button' operation from a view.
  17. *
  18. * @override
  19. */
  20. _mockCallButton({ args, kwargs, method, model }) {
  21. if (model === 'website.visitor' && method === 'action_send_chat_request') {
  22. return this._mockWebsiteVisitorActionSendChatRequest(args[0]);
  23. }
  24. return this._super(...arguments);
  25. },
  26. /**
  27. * Overrides to add visitor information to livechat channels.
  28. *
  29. * @override
  30. */
  31. _mockMailChannelChannelInfo(ids) {
  32. const channelInfos = this._super(...arguments);
  33. for (const channelInfo of channelInfos) {
  34. const channel = this.getRecords('mail.channel', [['id', '=', channelInfo.id]])[0];
  35. if (channel.channel_type === 'livechat' && channelInfo.livechat_visitor_id) {
  36. const visitor = this.getRecords('website.visitor', [['id', '=', channelInfo.livechat_visitor_id]])[0];
  37. const country = this.getRecords('res.country', [['id', '=', visitor.country_id]])[0];
  38. channelInfo.visitor = {
  39. country_code: country && country.code,
  40. country_id: country && country.id,
  41. display_name: visitor.display_name,
  42. history: visitor.history, // TODO should be computed
  43. id: visitor.id,
  44. is_connected: visitor.is_connected,
  45. lang_name: visitor.lang_name,
  46. partner_id: visitor.partner_id,
  47. website_name: visitor.website_name,
  48. };
  49. }
  50. }
  51. return channelInfos;
  52. },
  53. /**
  54. * @private
  55. * @param {integer[]} ids
  56. */
  57. _mockWebsiteVisitorActionSendChatRequest(ids) {
  58. const visitors = this.getRecords('website.visitor', [['id', 'in', ids]]);
  59. for (const visitor of visitors) {
  60. const country = visitor.country_id
  61. ? this.getRecords('res.country', [['id', '=', visitor.country_id]])
  62. : undefined;
  63. const visitor_name = `${visitor.display_name}${country ? `(${country.name})` : ''}`;
  64. const membersToAdd = [[0, 0, { partner_id: this.currentPartnerId }]];
  65. if (visitor.partner_id) {
  66. membersToAdd.push([0, 0, { partner_id: visitor.partner_id }]);
  67. } else {
  68. membersToAdd.push([0, 0, { partner_id: this.publicPartnerId }]);
  69. }
  70. const livechatId = this.pyEnv['mail.channel'].create({
  71. anonymous_name: visitor_name,
  72. channel_member_ids: membersToAdd,
  73. channel_type: 'livechat',
  74. livechat_operator_id: this.currentPartnerId,
  75. });
  76. // notify operator
  77. this.pyEnv['bus.bus']._sendone(this.currentPartner, 'website_livechat.send_chat_request',
  78. this._mockMailChannelChannelInfo([livechatId])[0]
  79. );
  80. }
  81. },
  82. });