mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-23 00:37:47 +00:00
Fix ai bot workspace assign (#7116)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
parent
43c681236b
commit
aa4eaeae84
@ -13,7 +13,15 @@
|
|||||||
// limitations under the License.
|
// 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 { getMetadata, PlatformError, unknownError } from '@hcengineering/platform'
|
||||||
|
|
||||||
import plugin from './plugin'
|
import plugin from './plugin'
|
||||||
@ -296,3 +304,52 @@ function getAccoutsUrlOrFail (): string {
|
|||||||
}
|
}
|
||||||
return accountsUrl
|
return accountsUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function assignWorkspace (
|
||||||
|
token: string,
|
||||||
|
email: string,
|
||||||
|
workspace: string,
|
||||||
|
role: AccountRole = AccountRole.User,
|
||||||
|
personId?: Ref<Doc>,
|
||||||
|
shouldReplaceAccount = false,
|
||||||
|
personAccountId?: Ref<Doc>
|
||||||
|
): Promise<WorkspaceLoginInfo> {
|
||||||
|
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<WorkspaceLoginInfo> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -15,15 +15,15 @@
|
|||||||
|
|
||||||
import { setMetadata } from '@hcengineering/platform'
|
import { setMetadata } from '@hcengineering/platform'
|
||||||
import serverAiBot from '@hcengineering/server-ai-bot'
|
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 serverToken from '@hcengineering/server-token'
|
||||||
import { initStatisticsContext } from '@hcengineering/server-core'
|
import { initStatisticsContext } from '@hcengineering/server-core'
|
||||||
|
import { aiBotAccountEmail } from '@hcengineering/ai-bot'
|
||||||
|
|
||||||
import config from './config'
|
import config from './config'
|
||||||
import { AIControl } from './controller'
|
import { AIControl } from './controller'
|
||||||
import { registerLoaders } from './loaders'
|
import { registerLoaders } from './loaders'
|
||||||
import { closeDB, DbStorage, getDB } from './storage'
|
import { closeDB, DbStorage, getDB } from './storage'
|
||||||
import { createBotAccount } from './utils/account'
|
|
||||||
import { createServer, listen } from './server/server'
|
import { createServer, listen } from './server/server'
|
||||||
|
|
||||||
export const start = async (): Promise<void> => {
|
export const start = async (): Promise<void> => {
|
||||||
@ -42,7 +42,7 @@ export const start = async (): Promise<void> => {
|
|||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
ctx.info('Creating bot account', { attempt: i })
|
ctx.info('Creating bot account', { attempt: i })
|
||||||
try {
|
try {
|
||||||
await createBotAccount()
|
await createAccount(aiBotAccountEmail, config.Password, config.FirstName, config.LastName)
|
||||||
break
|
break
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.error('Error during account creation', { error: e })
|
ctx.error('Error during account creation', { error: e })
|
||||||
|
@ -13,10 +13,11 @@
|
|||||||
// limitations under the License.
|
// 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 aiBot, { aiBotAccountEmail } from '@hcengineering/ai-bot'
|
||||||
import { AccountRole, isWorkspaceCreating, MeasureContext, systemAccountEmail } from '@hcengineering/core'
|
import { AccountRole, isWorkspaceCreating, MeasureContext, systemAccountEmail } from '@hcengineering/core'
|
||||||
import { generateToken } from '@hcengineering/server-token'
|
import { generateToken } from '@hcengineering/server-token'
|
||||||
|
import { assignWorkspace } from '@hcengineering/server-client'
|
||||||
|
|
||||||
import config from '../config'
|
import config from '../config'
|
||||||
import { wait } from './common'
|
import { wait } from './common'
|
||||||
@ -24,52 +25,6 @@ import { wait } from './common'
|
|||||||
const ASSIGN_WORKSPACE_DELAY_MS = 5 * 1000 // 5 secs
|
const ASSIGN_WORKSPACE_DELAY_MS = 5 * 1000 // 5 secs
|
||||||
const MAX_ASSIGN_ATTEMPTS = 5
|
const MAX_ASSIGN_ATTEMPTS = 5
|
||||||
|
|
||||||
export async function assignBotToWorkspace (workspace: string): Promise<Workspace> {
|
|
||||||
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<Workspace> {
|
|
||||||
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<LoginInfo | undefined> {
|
export async function loginBot (): Promise<LoginInfo | undefined> {
|
||||||
const accountsUrl = config.AccountsURL
|
const accountsUrl = config.AccountsURL
|
||||||
const workspace = await (
|
const workspace = await (
|
||||||
@ -155,7 +110,8 @@ export async function tryAssignToWorkspace (
|
|||||||
return false
|
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 })
|
ctx.info('Assigned to workspace: ', { workspace })
|
||||||
return true
|
return true
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user