From 4f36208880d5e0b45c24dc0a4e559fd76904cd98 Mon Sep 17 00:00:00 2001
From: Andrey Sobolev <haiodo@users.noreply.github.com>
Date: Fri, 14 Apr 2023 21:34:03 +0700
Subject: [PATCH] TSK-1189: Fix showing all available categories (#2987)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
---
 .../src/components/kanban/KanbanView.svelte          |  9 ++++++++-
 .../src/components/issues/KanbanView.svelte          |  8 +++++++-
 plugins/view-resources/src/utils.ts                  | 12 ++++++++++++
 plugins/view-resources/src/viewOptions.ts            |  8 --------
 4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/plugins/task-resources/src/components/kanban/KanbanView.svelte b/plugins/task-resources/src/components/kanban/KanbanView.svelte
index 46adea43a4..82fbd67663 100644
--- a/plugins/task-resources/src/components/kanban/KanbanView.svelte
+++ b/plugins/task-resources/src/components/kanban/KanbanView.svelte
@@ -42,6 +42,7 @@
     ActionContext,
     focusStore,
     getCategories,
+    getCategorySpaces,
     getGroupByValues,
     getPresenter,
     groupBy,
@@ -167,9 +168,15 @@
       const categoryFunc = viewOption as CategoryOption
       if (viewOptions[viewOption.key] ?? viewOption.defaultValue) {
         const categoryAction = await getResource(categoryFunc.action)
+
+        const spaces = getCategorySpaces(categories)
+        if (space !== undefined) {
+          spaces.push(space)
+        }
+
         const res = await categoryAction(
           _class,
-          space ? { space } : {},
+          spaces.length > 0 ? { space: { $in: Array.from(spaces.values()) } } : {},
           groupByKey,
           update,
           queryId,
diff --git a/plugins/tracker-resources/src/components/issues/KanbanView.svelte b/plugins/tracker-resources/src/components/issues/KanbanView.svelte
index 2384b8f4fb..860e6af7ad 100644
--- a/plugins/tracker-resources/src/components/issues/KanbanView.svelte
+++ b/plugins/tracker-resources/src/components/issues/KanbanView.svelte
@@ -55,6 +55,7 @@
     ActionContext,
     focusStore,
     getCategories,
+    getCategorySpaces,
     getGroupByValues,
     getPresenter,
     groupBy,
@@ -193,9 +194,14 @@
       const categoryFunc = viewOption as CategoryOption
       if (viewOptions[viewOption.key] ?? viewOption.defaultValue) {
         const categoryAction = await getResource(categoryFunc.action)
+
+        const spaces = getCategorySpaces(categories)
+        if (space !== undefined) {
+          spaces.push(space)
+        }
         const res = await categoryAction(
           _class,
-          space !== undefined ? { space } : {},
+          spaces.length > 0 ? { space: { $in: Array.from(spaces.values()) } } : {},
           groupByKey,
           update,
           queryId,
diff --git a/plugins/view-resources/src/utils.ts b/plugins/view-resources/src/utils.ts
index d071a12d69..34d4b80dfa 100644
--- a/plugins/view-resources/src/utils.ts
+++ b/plugins/view-resources/src/utils.ts
@@ -647,6 +647,18 @@ export async function getCategories (
   )
 }
 
+/**
+ * @public
+ */
+export function getCategorySpaces (categories: CategoryType[]): Array<Ref<Space>> {
+  return Array.from(
+    (categories.filter((it) => typeof it === 'object') as StatusValue[]).reduce<Set<Ref<Space>>>((arr, val) => {
+      val.values.forEach((it) => arr.add(it.space))
+      return arr
+    }, new Set())
+  )
+}
+
 export function concatCategories (arr1: CategoryType[], arr2: CategoryType[]): CategoryType[] {
   const uniqueValues: Set<string | number | undefined> = new Set()
   const uniqueObjects: Map<string | number, StatusValue> = new Map()
diff --git a/plugins/view-resources/src/viewOptions.ts b/plugins/view-resources/src/viewOptions.ts
index 2148a329eb..8043327b9b 100644
--- a/plugins/view-resources/src/viewOptions.ts
+++ b/plugins/view-resources/src/viewOptions.ts
@@ -139,14 +139,6 @@ export async function showEmptyGroups (
       const resQuery: DocumentQuery<Status> = {}
       if (space !== undefined) {
         resQuery.space = space
-      } else {
-        const spaceRefs = Array.from(new Set(statusList.map((p) => p.space)))
-        const spaces = await client.findAll(
-          core.class.Space,
-          { _id: { $in: spaceRefs }, archived: false },
-          { projection: { _id: 1 } }
-        )
-        resQuery.space = { $in: spaces.map((p) => p._id) }
       }
       if (st !== undefined) {
         resQuery._id = st