From d205cd9370b8b92f840341140711f4fdd3e6ed14 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Wed, 24 Jan 2024 15:31:42 +0600 Subject: [PATCH] Fix get account (#4425) Signed-off-by: Denis Bykhov --- server/ws/src/client.ts | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/server/ws/src/client.ts b/server/ws/src/client.ts index 2aff541d04..ce146f1e98 100644 --- a/server/ws/src/client.ts +++ b/server/ws/src/client.ts @@ -36,7 +36,8 @@ import core, { generateId, type SearchQuery, type SearchOptions, - type SearchResult + type SearchResult, + TxFactory } from '@hcengineering/core' import { type Pipeline, type SessionContext } from '@hcengineering/server-core' import { type Token } from '@hcengineering/server-token' @@ -82,20 +83,31 @@ export class ClientSession implements Session { async getAccount (ctx: MeasureContext): Promise { const account = await this._pipeline.modelDb.findAll(core.class.Account, { email: this.token.email }) if (account.length === 0 && this.token.extra?.admin === 'true') { - // Generate fake account for admin user - const account = { - _id: core.account.System, - _class: 'contact:class:PersonAccount' as Ref>, - name: 'System,Ghost', - email: this.token.email, - space: core.space.Model, - modifiedBy: core.account.System, - modifiedOn: Date.now(), - role: AccountRole.Owner + const systemAccount = await this._pipeline.modelDb.findAll(core.class.Account, { + _id: this.token.email as Ref + }) + if (systemAccount.length === 0) { + // Generate account for admin user + const factory = new TxFactory(core.account.System) + const email = `system:${this.token.email}` + const createTx = factory.createTxCreateDoc( + core.class.Account, + core.space.Model, + { + role: AccountRole.Owner, + email + }, + this.token.email as Ref + ) + const context = ctx as SessionContext + context.userEmail = this.token.email + context.admin = this.token.extra?.admin === 'true' + await this._pipeline.tx(context, createTx) + const acc = TxProcessor.createDoc2Doc(createTx) + return acc + } else { + return systemAccount[0] } - // Add for other services to work properly - this._pipeline.modelDb.addDoc(account) - return account } return account[0] }