index.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { customerServicePayloadType, IMessageModel } from '../interface';
  2. import { CUSTOM_MESSAGE_SRC, TYPES } from '../constant';
  3. // Determine if it is a JSON string
  4. export function isJSON(str: string): boolean {
  5. // eslint-disable-next-line no-useless-escape
  6. if (typeof str === 'string') {
  7. try {
  8. const data = JSON.parse(str);
  9. if (data) {
  10. return true;
  11. }
  12. return false;
  13. } catch (error: any) {
  14. return false;
  15. }
  16. }
  17. return false;
  18. }
  19. // Determine if it is a JSON string
  20. export function JSONToObject(str: string) {
  21. if (!isJSON(str)) {
  22. return str;
  23. }
  24. return JSON.parse(str);
  25. }
  26. export function isCustomerServiceMessage(message: IMessageModel): boolean {
  27. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  28. if (customerServicePayload?.customerServicePlugin == 0) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. export const isMessageInvisible = (message: IMessageModel): boolean => {
  34. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  35. const robotCommandArray = ['feedback', 'updateBotStatus'];
  36. const whiteList = [
  37. CUSTOM_MESSAGE_SRC.MENU,
  38. CUSTOM_MESSAGE_SRC.BRANCH,
  39. CUSTOM_MESSAGE_SRC.FROM_INPUT,
  40. CUSTOM_MESSAGE_SRC.PRODUCT_CARD,
  41. ];
  42. const isCustomerMessage = message?.type === TYPES.MSG_CUSTOM;
  43. const isExistWhiteList = whiteList.includes(customerServicePayload?.src);
  44. const isRobot = customerServicePayload?.src === CUSTOM_MESSAGE_SRC.ROBOT && robotCommandArray.indexOf(customerServicePayload?.content?.command) !== -1;
  45. return isCustomerMessage && (!isExistWhiteList || isRobot);
  46. };
  47. export const isMessageRating = (message: IMessageModel): boolean => {
  48. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  49. if (
  50. (message?.type === TYPES.MSG_CUSTOM && customerServicePayload?.customerServicePlugin == 0
  51. && customerServicePayload.src === CUSTOM_MESSAGE_SRC.MENU)
  52. ) {
  53. return true;
  54. }
  55. return false;
  56. };
  57. export const isRenderMessage = (message: IMessageModel): boolean => {
  58. if (!message.isRevoked) {
  59. if (message?.type !== TYPES.MSG_CUSTOM) {
  60. return true;
  61. }
  62. if (message?.type === TYPES.MSG_CUSTOM && isCustomerServiceMessage(message) && !isMessageInvisible(message)) {
  63. return true;
  64. }
  65. if (message?.type === TYPES.MSG_CUSTOM && isCustomerServiceMessage(message) && isMessageInvisible(message)) {
  66. return false;
  67. }
  68. if (message?.type === TYPES.MSG_CUSTOM && !isCustomerServiceMessage(message)) {
  69. return true;
  70. }
  71. } else {
  72. return false;
  73. }
  74. return false;
  75. };