From aa4eaeae846e2a68c69a6e2e2f28a175343fe7dc Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 7 Nov 2024 10:18:25 +0400 Subject: [PATCH] Fix ai bot workspace assign (#7116) Signed-off-by: Kristina Fefelova --- server/client/src/account.ts | 59 ++++++++++++++++++- services/ai-bot/pod-ai-bot/src/start.ts | 6 +- .../ai-bot/pod-ai-bot/src/utils/account.ts | 52 ++-------------- 3 files changed, 65 insertions(+), 52 deletions(-) diff --git a/server/client/src/account.ts b/server/client/src/account.ts index 96136395c3..8f2abc1aea 100644 --- a/server/client/src/account.ts +++ b/server/client/src/account.ts @@ -13,7 +13,15 @@ // limitations under the License. // -import { type BaseWorkspaceInfo, type Data, type Version, BackupStatus } from '@hcengineering/core' +import { + type BaseWorkspaceInfo, + type Data, + type Version, + BackupStatus, + AccountRole, + Ref, + Doc +} from '@hcengineering/core' import { getMetadata, PlatformError, unknownError } from '@hcengineering/platform' import plugin from './plugin' @@ -296,3 +304,52 @@ function getAccoutsUrlOrFail (): string { } return accountsUrl } + +export async function assignWorkspace ( + token: string, + email: string, + workspace: string, + role: AccountRole = AccountRole.User, + personId?: Ref, + shouldReplaceAccount = false, + personAccountId?: Ref +): Promise { + const accountsUrl = getAccoutsUrlOrFail() + const res = await ( + await fetch(accountsUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + method: 'assignWorkspace', + params: [token, email, workspace, role, personId, shouldReplaceAccount, undefined, personAccountId] + }) + }) + ).json() + + return res.result as WorkspaceLoginInfo +} + +export async function createAccount ( + email: string, + password: string, + firstName: string, + lastName: string +): Promise { + const accountsUrl = getAccoutsUrlOrFail() + const workspace = await ( + await fetch(accountsUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + method: 'createAccount', + params: [email, password, firstName, lastName] + }) + }) + ).json() + + return workspace.result as WorkspaceLoginInfo +} diff --git a/services/ai-bot/pod-ai-bot/src/start.ts b/services/ai-bot/pod-ai-bot/src/start.ts index 040e9f319f..0731c9c745 100644 --- a/services/ai-bot/pod-ai-bot/src/start.ts +++ b/services/ai-bot/pod-ai-bot/src/start.ts @@ -15,15 +15,15 @@ import { setMetadata } from '@hcengineering/platform' import serverAiBot from '@hcengineering/server-ai-bot' -import serverClient from '@hcengineering/server-client' +import serverClient, { createAccount } from '@hcengineering/server-client' import serverToken from '@hcengineering/server-token' import { initStatisticsContext } from '@hcengineering/server-core' +import { aiBotAccountEmail } from '@hcengineering/ai-bot' import config from './config' import { AIControl } from './controller' import { registerLoaders } from './loaders' import { closeDB, DbStorage, getDB } from './storage' -import { createBotAccount } from './utils/account' import { createServer, listen } from './server/server' export const start = async (): Promise => { @@ -42,7 +42,7 @@ export const start = async (): Promise => { for (let i = 0; i < 5; i++) { ctx.info('Creating bot account', { attempt: i }) try { - await createBotAccount() + await createAccount(aiBotAccountEmail, config.Password, config.FirstName, config.LastName) break } catch (e) { ctx.error('Error during account creation', { error: e }) diff --git a/services/ai-bot/pod-ai-bot/src/utils/account.ts b/services/ai-bot/pod-ai-bot/src/utils/account.ts index cc5561b824..73d745415d 100644 --- a/services/ai-bot/pod-ai-bot/src/utils/account.ts +++ b/services/ai-bot/pod-ai-bot/src/utils/account.ts @@ -13,10 +13,11 @@ // limitations under the License. // -import { LoginInfo, Workspace, WorkspaceLoginInfo } from '@hcengineering/account' +import { LoginInfo, WorkspaceLoginInfo } from '@hcengineering/account' import aiBot, { aiBotAccountEmail } from '@hcengineering/ai-bot' import { AccountRole, isWorkspaceCreating, MeasureContext, systemAccountEmail } from '@hcengineering/core' import { generateToken } from '@hcengineering/server-token' +import { assignWorkspace } from '@hcengineering/server-client' import config from '../config' import { wait } from './common' @@ -24,52 +25,6 @@ import { wait } from './common' const ASSIGN_WORKSPACE_DELAY_MS = 5 * 1000 // 5 secs const MAX_ASSIGN_ATTEMPTS = 5 -export async function assignBotToWorkspace (workspace: string): Promise { - const token = generateToken(systemAccountEmail, { name: '-' }, { service: 'aibot' }) - const accountsUrl = config.AccountsURL - const res = await ( - await fetch(accountsUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - method: 'assignWorkspace', - params: [ - token, - aiBotAccountEmail, - workspace, - AccountRole.User, - undefined, - false, - undefined, - aiBot.account.AIBot - ] - }) - }) - ).json() - - return res.result as Workspace -} - -export async function createBotAccount (): Promise { - const accountsUrl = config.AccountsURL - const workspace = await ( - await fetch(accountsUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - method: 'createAccount', - params: [aiBotAccountEmail, config.Password, config.FirstName, config.LastName] - }) - }) - ).json() - - return workspace.result as Workspace -} - export async function loginBot (): Promise { const accountsUrl = config.AccountsURL const workspace = await ( @@ -155,7 +110,8 @@ export async function tryAssignToWorkspace ( return false } - await assignBotToWorkspace(workspace) + const token = generateToken(systemAccountEmail, { name: '-' }, { service: 'aibot' }) + await assignWorkspace(token, aiBotAccountEmail, workspace, AccountRole.User, undefined, false, aiBot.account.AIBot) ctx.info('Assigned to workspace: ', { workspace }) return true } catch (e) {