Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
fix: prevent duplicate Help Center article creation (#15761)
Sony Mathew7fe41fe03731
diff --git a/app/javascript/dashboard/components-next/HelpCenter/Pages/ArticleEditorPage/ArticleEditor.vue b/app/javascript/dashboard/components-next/HelpCenter/Pages/ArticleEditorPage/ArticleEditor.vueindex b36b9d0f30e..b34a5701afc 100644--- a/app/javascript/dashboard/components-next/HelpCenter/Pages/ArticleEditorPage/ArticleEditor.vue+++ b/app/javascript/dashboard/components-next/HelpCenter/Pages/ArticleEditorPage/ArticleEditor.vue@@ -118,8 +118,16 @@ watch(});+// A new article has no id to save to yet, so hand every edit to the page right+// away; a debounced save would die with this editor when the create redirects.const scheduleSave = () => {- if (isNewArticle.value) return;+ if (isNewArticle.value) {+ emit('saveArticle', {+ title: localTitle.value,+ content: localContent.value,+ });+ return;+ }debouncedSave();};diff --git a/app/javascript/dashboard/routes/dashboard/helpcenter/pages/PortalsArticlesNewPage.vue b/app/javascript/dashboard/routes/dashboard/helpcenter/pages/PortalsArticlesNewPage.vueindex 6351461e2aa..bf84221ec04 100644--- a/app/javascript/dashboard/routes/dashboard/helpcenter/pages/PortalsArticlesNewPage.vue+++ b/app/javascript/dashboard/routes/dashboard/helpcenter/pages/PortalsArticlesNewPage.vue@@ -46,6 +46,27 @@ const setCategoryId = newCategoryId => {selectedCategoryId.value = newCategoryId;};+// Nothing to save to until the create returns an id; keep edits local and let+// createNewArticle save them.+const saveArticle = values => Object.assign(article.value, values);++const currentArticle = () => ({+ title: article.value.title,+ content: article.value.content,+ author_id: selectedAuthorId.value || currentUserId.value,+ category_id: selectedCategoryId.value || categoryId.value,+});++// Save what changed while the create request ran, since the edit page opens+// with the stored article.+const saveEditsMadeWhileCreating = async (articleId, saved) => {+ const latest = currentArticle();+ if (Object.keys(latest).every(key => latest[key] === saved[key])) return;++ await store.dispatch('articles/update', { portalSlug, articleId, ...latest });+ await saveEditsMadeWhileCreating(articleId, latest);+};+const createNewArticle = async ({ title, content }) => {if (title) article.value.title = title;if (content) article.value.content = content;@@ -55,18 +76,25 @@ const createNewArticle = async ({ title, content }) => {isUpdating.value = true;try {const { locale } = route.params;- const resolvedCategoryId = selectedCategoryId.value || categoryId.value;+ const created = currentArticle();const articleId = await store.dispatch('articles/create', {portalSlug,- content: article.value.content,- title: article.value.title,+ title: created.title,+ content: created.content,locale: locale,- authorId: selectedAuthorId.value || currentUserId.value,- categoryId: resolvedCategoryId,+ authorId: created.author_id,+ categoryId: created.category_id,});useTrack(PORTALS_EVENTS.CREATE_ARTICLE, { locale });+ // The article exists now, so open it even if saving the later edits fails;+ // staying here would create it again on the next title blur.+ await saveEditsMadeWhileCreating(articleId, created).catch(error =>+ useAlert(error?.message || t('HELP_CENTER.EDIT_ARTICLE_PAGE.API.ERROR'))+ );++ const resolvedCategoryId = currentArticle().category_id;const resolvedSlug = categories.value?.find(c => c.id === resolvedCategoryId)?.slug;@@ -116,6 +144,7 @@ const goBackToArticles = () => {:is-updating="isUpdating":is-saved="isSaved"@create-article="createNewArticle"+ @save-article="saveArticle"@go-back="goBackToArticles"@set-author="setAuthorId"@set-category="setCategoryId"diff --git a/app/models/article.rb b/app/models/article.rbindex b2842c846d8..4f8b9255789 100644--- a/app/models/article.rb+++ b/app/models/article.rb@@ -60,6 +60,7 @@ class Article < ApplicationRecord# Slugs that collide with help center routes (e.g. /hc/:slug/:locale/search)RESERVED_SLUGS = %w[search articles categories].freeze+ SLUG_RANDOM_BYTES = 4validates :account_id, presence: truevalidates :author_id, presence: true@@ -236,7 +237,13 @@ class Article < ApplicationRecordenddef ensure_article_slug- self.slug ||= "#{Time.now.utc.to_i}-#{title.underscore.parameterize(separator: '-')}" if title.present?+ return if slug.present? || title.blank?++ timestamp = Time.current.to_i.to_s+ random_suffix = SecureRandom.hex(SLUG_RANDOM_BYTES)+ max_title_length = MAX_STRING_COLUMN_LENGTH - timestamp.length - random_suffix.length - 2+ parameterized_title = title.underscore.parameterize(separator: '-').first(max_title_length)+ self.slug = "#{timestamp}-#{parameterized_title}-#{random_suffix}"endendArticle.include_mod_with('Concerns::Article')