From 4f8518b881dbd61a9a3687a20e8b69bd2c86c2c9 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 12 Nov 2021 09:17:48 -0600 Subject: [PATCH] Replace all occurrences of a placeholder on string instead of just first one --- .../server/hooks/onMessageSentParsePlaceholder.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ee/app/canned-responses/server/hooks/onMessageSentParsePlaceholder.ts b/ee/app/canned-responses/server/hooks/onMessageSentParsePlaceholder.ts index 7afe45463a93..c8a0006c7c07 100644 --- a/ee/app/canned-responses/server/hooks/onMessageSentParsePlaceholder.ts +++ b/ee/app/canned-responses/server/hooks/onMessageSentParsePlaceholder.ts @@ -29,6 +29,8 @@ const placeholderFields = { }, }; +const replaceAll = (text: string, old: string, replace: string): string => text.replace(new RegExp(old, 'g'), replace); + const handleBeforeSaveMessage = (message: IMessage, room: IOmnichannelRoom): any => { if (!message.msg || message.msg === '') { return message; @@ -50,7 +52,7 @@ const handleBeforeSaveMessage = (message: IMessage, room: IOmnichannelRoom): any const placeholderConfig = placeholderFields[field as keyof typeof placeholderFields]; const from = placeholderConfig.from === 'agent' ? agent : visitor; const data = get(from, placeholderConfig.dataKey, ''); - messageText = messageText.replace(templateKey, data); + messageText = replaceAll(messageText, templateKey, data); return messageText; });