From 133e750e9d2444bed507434a2dbb6b27939b6f08 Mon Sep 17 00:00:00 2001
From: Maksim Karmatskikh <mkarmatskih@gmail.com>
Date: Fri, 24 Nov 2023 10:53:46 +0100
Subject: [PATCH] UBERF-4384: Update space from attributes (#4049)

Signed-off-by: Maxim Karmatskikh <mkarmatskih@gmail.com>
---
 server/core/src/indexer/fulltextPush.ts |  5 +++++
 server/core/src/indexer/indexer.ts      | 11 +++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/server/core/src/indexer/fulltextPush.ts b/server/core/src/indexer/fulltextPush.ts
index f5ffc3d5dd..b018f49f63 100644
--- a/server/core/src/indexer/fulltextPush.ts
+++ b/server/core/src/indexer/fulltextPush.ts
@@ -303,4 +303,9 @@ function updateDoc2Elastic (
       }
     }
   }
+
+  const spaceKey = docKey('space', { _class: core.class.Doc })
+  if (doc[spaceKey] !== undefined) {
+    doc.space = doc[spaceKey]
+  }
 }
diff --git a/server/core/src/indexer/indexer.ts b/server/core/src/indexer/indexer.ts
index b931d30f91..b8c0cf4aaf 100644
--- a/server/core/src/indexer/indexer.ts
+++ b/server/core/src/indexer/indexer.ts
@@ -31,7 +31,8 @@ import core, {
   _getOperator,
   setObjectValue,
   toFindResult,
-  versionToString
+  versionToString,
+  docKey
 } from '@hcengineering/core'
 import { DbAdapter } from '../adapter'
 import { RateLimitter } from '../limitter'
@@ -145,7 +146,7 @@ export class FullTextIndexPipeline implements FullTextPipeline {
     }
   }
 
-  updateDoc<T extends Doc>(doc: T, tx: DocumentUpdate<T>, updateDate: boolean): T {
+  updateDoc (doc: DocIndexState, tx: DocumentUpdate<DocIndexState>, updateDate: boolean): DocIndexState {
     for (const key in tx) {
       if (key.startsWith('$')) {
         const operator = _getOperator(key)
@@ -154,6 +155,12 @@ export class FullTextIndexPipeline implements FullTextPipeline {
         setObjectValue(key, doc, (tx as any)[key])
       }
     }
+
+    const spaceKey = docKey('space', { _class: core.class.Doc })
+    if (doc.attributes !== undefined && doc.attributes[spaceKey] !== undefined) {
+      doc.space = doc.attributes[spaceKey]
+    }
+
     if (updateDate) {
       doc.modifiedBy = core.account.System
       doc.modifiedOn = Date.now()