From c95b493a33387b171ae7725358df91bff1a2f729 Mon Sep 17 00:00:00 2001
From: Andrey Sobolev <haiodo@gmail.com>
Date: Wed, 26 Jan 2022 17:16:50 +0700
Subject: [PATCH] Fix model versioning

1. Using of package.json was wrong ides, so revert to bundled version value.
2. Do not duplicate record in accounts db and find EmployeeAccount

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
---
 models/all/src/index.ts     | 11 +++-------
 server/account/src/index.ts | 40 ++++++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/models/all/src/index.ts b/models/all/src/index.ts
index 9d0441e157..d0b41cc8b9 100644
--- a/models/all/src/index.ts
+++ b/models/all/src/index.ts
@@ -36,8 +36,6 @@ import { createModel as templatesModel } from '@anticrm/model-templates'
 import { createModel as textEditorModel } from '@anticrm/model-text-editor'
 import { createModel as viewModel } from '@anticrm/model-view'
 import { createModel as workbenchModel } from '@anticrm/model-workbench'
-import { readFileSync } from 'fs'
-import { join } from 'path'
 
 const builder = new Builder()
 
@@ -70,14 +68,11 @@ const builders = [
 for (const b of builders) {
   b(builder)
 }
-const packageFile = readFileSync(join(__dirname, '..', 'package.json')).toString()
-const json = JSON.parse(packageFile)
-const packageVersion = json.version.split('.')
 
 export const version: Data<Version> = {
-  major: parseInt(packageVersion[0]),
-  minor: parseInt(packageVersion[1]),
-  patch: parseInt(packageVersion[2])
+  major: 0,
+  minor: 6,
+  patch: 0
 }
 
 builder.createDoc(core.class.Version, core.space.Model, version, core.version.Model)
diff --git a/server/account/src/index.ts b/server/account/src/index.ts
index ae8191fdb0..178e44a5af 100644
--- a/server/account/src/index.ts
+++ b/server/account/src/index.ts
@@ -391,10 +391,10 @@ async function getWorkspaceAndAccount (
 export async function assignWorkspace (db: Db, email: string, workspace: string): Promise<void> {
   const { workspaceId, accountId } = await getWorkspaceAndAccount(db, email, workspace)
   // Add account into workspace.
-  await db.collection(WORKSPACE_COLLECTION).updateOne({ _id: workspaceId }, { $push: { accounts: accountId } })
+  await db.collection(WORKSPACE_COLLECTION).updateOne({ _id: workspaceId }, { $addToSet: { accounts: accountId } })
 
   // Add workspace to account
-  await db.collection(ACCOUNT_COLLECTION).updateOne({ _id: accountId }, { $push: { workspaces: workspaceId } })
+  await db.collection(ACCOUNT_COLLECTION).updateOne({ _id: accountId }, { $addToSet: { workspaces: workspaceId } })
 
   const account = await db.collection<Account>(ACCOUNT_COLLECTION).findOne({ _id: accountId })
 
@@ -408,17 +408,33 @@ async function createEmployeeAccount (account: Account, workspace: string): Prom
 
     const name = combineName(account.first, account.last)
 
-    const employee = await ops.createDoc(contact.class.Employee, contact.space.Employee, {
-      name,
-      city: '',
-      channels: []
-    })
+    // Check if EmployeeAccoun is not exists
+    const existingAccount = await ops.findOne(contact.class.EmployeeAccount, { email: account.email })
 
-    await ops.createDoc(contact.class.EmployeeAccount, core.space.Model, {
-      email: account.email,
-      employee,
-      name
-    })
+    if (existingAccount === undefined) {
+      const employee = await ops.createDoc(contact.class.Employee, contact.space.Employee, {
+        name,
+        city: '',
+        channels: []
+      })
+
+      await ops.createDoc(contact.class.EmployeeAccount, core.space.Model, {
+        email: account.email,
+        employee,
+        name
+      })
+    } else {
+      const employee = await ops.findOne(contact.class.Employee, { _id: existingAccount.employee })
+      if (employee === undefined) {
+        // Employee was deleted, let's restore it.
+        const employeeId = await ops.createDoc(contact.class.Employee, contact.space.Employee, {
+          name,
+          city: '',
+          channels: []
+        })
+        await ops.updateDoc(contact.class.EmployeeAccount, existingAccount.space, existingAccount._id, { employee: employeeId })
+      }
+    }
   } finally {
     await connection.close()
   }