From 9dbdf4dba85a24d4d3fc75616cd0665201ddf76f Mon Sep 17 00:00:00 2001
From: Denis Bykhov <bykhov.denis@gmail.com>
Date: Fri, 28 Oct 2022 21:16:09 +0600
Subject: [PATCH] Fix 365 (#2337)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
---
 dev/tool/src/csv/talant-importer.ts |  4 +++-
 models/all/src/index.ts             |  8 ++++----
 models/recruit/src/migration.ts     | 13 +++++++++++++
 packages/core/src/operations.ts     |  7 ++++++-
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/dev/tool/src/csv/talant-importer.ts b/dev/tool/src/csv/talant-importer.ts
index e123c92b08..5bd1189f75 100644
--- a/dev/tool/src/csv/talant-importer.ts
+++ b/dev/tool/src/csv/talant-importer.ts
@@ -660,7 +660,9 @@ async function createTalants (
               const document = await recognize(rekoniUrl, data.base64Data, token)
               if (document !== undefined) {
                 if (document.title !== undefined) {
-                  await client.update(doc, { title: document.title })
+                  await client.updateMixin(doc._id, contact.class.Person, doc.space, recruit.mixin.Candidate, {
+                    title: document.title
+                  })
                 }
 
                 await updateAvatar(doc, document, connection, client)
diff --git a/models/all/src/index.ts b/models/all/src/index.ts
index 16d1b3ddb8..720c3b30a5 100644
--- a/models/all/src/index.ts
+++ b/models/all/src/index.ts
@@ -87,11 +87,13 @@ const builders: [(b: Builder) => void, string][] = [
   [preferenceModel, 'preference'],
   [hrModel, 'hr'],
   [documentModel, 'document'],
+  [trackerModel, 'tracker'],
+  [boardModel, 'board'],
+  [calendarModel, 'calendar'],
 
   [serverCoreModel, 'server-core'],
   [serverAttachmentModel, 'server-attachment'],
   [serverContactModel, 'server-contact'],
-  [serverNotificationModel, 'server-notification'],
   [serveSettingModel, 'server-setting'],
   [serverChunterModel, 'server-chunter'],
   [serverInventoryModel, 'server-inventory'],
@@ -104,9 +106,7 @@ const builders: [(b: Builder) => void, string][] = [
   [serverGmailModel, 'server-gmail'],
   [serverTelegramModel, 'server-telegram'],
   [serverHrModel, 'server-hr'],
-  [trackerModel, 'tracker'],
-  [boardModel, 'board'],
-  [calendarModel, 'calendar'],
+  [serverNotificationModel, 'server-notification'],
   [automationModel, 'automation']
 ]
 
diff --git a/models/recruit/src/migration.ts b/models/recruit/src/migration.ts
index bac5844397..15275e8717 100644
--- a/models/recruit/src/migration.ts
+++ b/models/recruit/src/migration.ts
@@ -26,8 +26,21 @@ import { getCategories } from '@anticrm/skillset'
 import { KanbanTemplate } from '@hcengineering/task'
 import recruit from './plugin'
 
+async function fixImportedTitle (client: MigrationClient): Promise<void> {
+  await client.update(
+    DOMAIN_CONTACT,
+    {
+      title: { $exists: true }
+    },
+    {
+      $rename: { title: 'recruit:mixin:Candidate.title' }
+    }
+  )
+}
+
 export const recruitOperation: MigrateOperation = {
   async migrate (client: MigrationClient): Promise<void> {
+    await fixImportedTitle(client)
     await client.update(
       DOMAIN_CALENDAR,
       {
diff --git a/packages/core/src/operations.ts b/packages/core/src/operations.ts
index 6e94af4b3b..295e3e4ddb 100644
--- a/packages/core/src/operations.ts
+++ b/packages/core/src/operations.ts
@@ -161,7 +161,12 @@ export class TxOperations implements Omit<Client, 'notify'> {
   }
 
   update<T extends Doc>(doc: T, update: DocumentUpdate<T>, retrieve?: boolean): Promise<TxResult> {
-    if (this.client.getHierarchy().isDerived(doc._class, core.class.AttachedDoc)) {
+    const hierarchy = this.client.getHierarchy()
+    if (hierarchy.isMixin(doc._class)) {
+      const baseClass = hierarchy.getBaseClass(doc._class)
+      return this.updateMixin(doc._id, baseClass, doc.space, doc._class, update)
+    }
+    if (hierarchy.isDerived(doc._class, core.class.AttachedDoc)) {
       const adoc = doc as unknown as AttachedDoc
       return this.updateCollection(
         doc._class,