Skip to content

Commit

Permalink
fix: handle general category, delete article/category & move to categ…
Browse files Browse the repository at this point in the history
…ory validations
  • Loading branch information
RitvikSardana committed Jan 17, 2025
1 parent 897927b commit 731161a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 25 deletions.
15 changes: 4 additions & 11 deletions desk/src/components/ListRows.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
>
<div class="flex items-center gap-2 w-full">
<component v-if="group.icon" :is="group.icon" />
<div v-if="group.group.label == ''">
{{ "General" }}
<span class="text-xs text-ink-gray-6"
>{{
group.rows.length +
" Article" +
(group.rows.length > 1 ? "s" : "")
}}
</span>
</div>
<div v-else class="flex items-center gap-1 w-full">
<div
v-if="group.group.label != ''"
class="flex items-center gap-1 w-full"
>
<span>{{ group.group.label }}</span>
<span class="text-xs text-ink-gray-5"
>{{
Expand Down
28 changes: 23 additions & 5 deletions desk/src/pages/knowledge-base/KnowledgeBaseAgent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
Button,
confirmDialog,
Dropdown,
createResource,
} from "frappe-ui";
import { useRouter } from "vue-router";
import {
Expand All @@ -52,6 +53,7 @@ import ListViewBuilder from "@/components/ListViewBuilder.vue";
import CategoryModal from "@/components/knowledge-base/CategoryModal.vue";
import MoveToCategoryModal from "@/components/knowledge-base/MoveToCategoryModal.vue";
import { createToast } from "@/utils";
import { Error } from "@/types";
const router = useRouter();
Expand All @@ -67,6 +69,12 @@ const editTitle = ref(false);
const showCategoryModal = ref(false);
const moveToModal = ref(false);
const generalCategory = createResource({
url: "helpdesk.api.knowledge_base.get_general_category",
auto: true,
cache: ["GeneralCategory"],
});
const headerOptions = [
{
label: "Category",
Expand All @@ -81,7 +89,15 @@ const headerOptions = [
label: "Article",
icon: "file-text",
onClick: () => {
router.push({ name: "NewArticle" });
router.push({
name: "NewArticle",
params: {
id: generalCategory.data,
},
query: {
title: "General",
},
});
},
},
];
Expand All @@ -93,8 +109,10 @@ const groupByActions = [
onClick: (groupedRow) => {
router.push({
name: "NewArticle",
params: {
id: groupedRow.group.value,
},
query: {
category: groupedRow.group.value,
title: groupedRow.group.label,
},
});
Expand Down Expand Up @@ -164,9 +182,9 @@ function handleMoveToCategory(category: string) {
iconClasses: "text-green-600",
});
},
onError: (error: string) => {
onError: (error: Error) => {
createToast({
title: error,
title: error?.messages?.[0] || error.message,
icon: "x",
iconClasses: "text-red-600",
});
Expand Down Expand Up @@ -264,7 +282,7 @@ function handleCategoryDelete(groupedRow) {
{
onSuccess: () => {
createToast({
title: "Article deleted successfully",
title: "Category deleted successfully",
icon: "check",
iconClasses: "text-green-600",
});
Expand Down
9 changes: 8 additions & 1 deletion desk/src/pages/knowledge-base/NewArticle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ const route = useRoute();
const title = ref("");
const content = ref("");
const categoryId = ref(route.query.category || null);
const props = defineProps({
id: {
type: String,
required: true,
},
});
const categoryId = ref(props.id || null);
const categoryName = computed(() => (route.query.title as string) || "");
function handleCreateArticle() {
Expand Down
3 changes: 2 additions & 1 deletion desk/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ const routes = [
props: true,
},
{
path: "articles/new",
path: "articles/new/:id",
name: "NewArticle",
component: () => import("@/pages/knowledge-base/NewArticle.vue"),
props: true,
},
{
path: "customers",
Expand Down
6 changes: 2 additions & 4 deletions desk/src/stores/knowledgeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export const moveToCategory = createResource({
};
},
validate({ category, articles }) {
if (!category) throw "Category is required";
if (!articles) throw "Articles are required";
if (!category) throw {message:"Category is required"};
if (!articles) throw {message:"Articles are required"};
},
});

Expand All @@ -78,8 +78,6 @@ export const categories = createResource({
cache: ["categories"],
});



export const categoryName = createResource({
url: "helpdesk.api.knowledge_base.get_category_title",
cache: ["categoryName"],
Expand Down
21 changes: 20 additions & 1 deletion helpdesk/api/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ def create_category(title: str):
@frappe.whitelist()
def move_to_category(category, articles):
for article in articles:
frappe.db.set_value("HD Article", article, "category", category)
try:
article_category = frappe.db.get_value("HD Article", article, "category")
category_existing_articles = frappe.db.count(
"HD Article", {"category": article_category}
)
if category_existing_articles == 1:
frappe.throw("Category must have atleast one article")
return
else:
frappe.db.set_value("HD Article", article, "category", category)
except Exception as e:
frappe.db.rollback()
frappe.throw("Error moving article to category")


@frappe.whitelist()
Expand Down Expand Up @@ -94,6 +106,13 @@ def get_category_articles(category):
return articles


@frappe.whitelist()
def get_general_category():
return frappe.db.get_value(
"HD Article Category", {"category_name": "General"}, "name"
)


@frappe.whitelist()
def get_category_title(category):
return frappe.db.get_value("HD Article Category", category, "category_name")
16 changes: 16 additions & 0 deletions helpdesk/helpdesk/doctype/hd_article/hd_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


class HDArticle(Document):
def validate(self):
if self.has_value_changed("category"):
old_category = self.get_doc_before_save().get("category")
self.check_category_length(old_category)

def before_insert(self):
self.author = frappe.session.user

Expand All @@ -31,6 +36,17 @@ def before_save(self):
)
)

def on_trash(self):
self.check_category_length()

def check_category_length(self, category=None):
category = category or self.get("category")
if not category:
return
category_articles = frappe.db.count("HD Article", {"category": category})
if category_articles == 1:
frappe.throw("Category must have atleast one article")

@staticmethod
def default_list_data():
columns = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def validate(self):
self.validate_default_category()

def validate_default_category(self):
if self.has_value_changed("category_name"):
old_doc = self.get_doc_before_save()
if not old_doc:
return
old_value = old_doc.get("category_name")

if self.has_value_changed("category_name") and old_value == "General":
frappe.throw(_("General category name can't be changed"))

def on_trash(self):
Expand All @@ -22,8 +27,15 @@ def on_trash(self):
articles = frappe.get_all(
"HD Article", filters={"category": self.name}, pluck="name"
)

general_category = frappe.db.get_value(
"HD Article Category", {"category_name": "General"}, "name"
)
if not general_category:
return

try:
for article in articles:
frappe.db.set_value("HD Article", article, "category", None)
frappe.db.set_value("HD Article", article, "category", general_category)
except Exception as e:
frappe.db.rollback()

0 comments on commit 731161a

Please sign in to comment.