Qfix assigning aibot to workspace ()

Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
Alexey Zinoviev 2024-10-29 15:26:21 +04:00 committed by GitHub
parent 7c69f6f35a
commit fe2399e29e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 10 deletions
dev/tool/src
server/account/src
services/ai-bot/pod-ai-bot/src

View File

@ -15,7 +15,7 @@
// //
import accountPlugin, { import accountPlugin, {
assignWorkspace, assignAccountToWs,
confirmEmail, confirmEmail,
createAcc, createAcc,
createWorkspace as createWorkspaceRecord, createWorkspace as createWorkspaceRecord,
@ -320,7 +320,7 @@ export function devTool (
console.log('assigning to workspace', workspaceInfo, endpoint) console.log('assigning to workspace', workspaceInfo, endpoint)
const client = await createClient(endpoint, token) const client = await createClient(endpoint, token)
console.log('assigning to workspace connected', workspaceInfo, endpoint) console.log('assigning to workspace connected', workspaceInfo, endpoint)
await assignWorkspace( await assignAccountToWs(
toolCtx, toolCtx,
db, db,
null, null,
@ -1744,7 +1744,7 @@ export function devTool (
version version
}) })
await createAcc(toolCtx, db, null, email, '1234', '', '', true) await createAcc(toolCtx, db, null, email, '1234', '', '', true)
await assignWorkspace(toolCtx, db, null, email, ws, AccountRole.User) await assignAccountToWs(toolCtx, db, null, email, ws, AccountRole.User)
console.log('Workspace created in', new Date().getTime() - start.getTime(), 'ms') console.log('Workspace created in', new Date().getTime() - start.getTime(), 'ms')
const token = generateToken(systemAccountEmail, wsid) const token = generateToken(systemAccountEmail, wsid)
const endpoint = await getTransactorEndpoint(token, 'external') const endpoint = await getTransactorEndpoint(token, 'external')

View File

@ -544,7 +544,7 @@ export async function join (
const invite = await getInvite(db, inviteId) const invite = await getInvite(db, inviteId)
const workspace = await checkInvite(ctx, invite, email) const workspace = await checkInvite(ctx, invite, email)
ctx.info(`join attempt:${email}, ${workspace.name}`) ctx.info(`join attempt:${email}, ${workspace.name}`)
const ws = await assignWorkspace( const ws = await assignAccountToWs(
ctx, ctx,
db, db,
branding, branding,
@ -679,7 +679,7 @@ export async function signUpJoin (
last, last,
invite?.emailMask === email || invite?.personId !== undefined || sesURL === undefined || sesURL === '' invite?.emailMask === email || invite?.personId !== undefined || sesURL === undefined || sesURL === ''
) )
const ws = await assignWorkspace( const ws = await assignAccountToWs(
ctx, ctx,
db, db,
branding, branding,
@ -1213,7 +1213,7 @@ async function postCreateUserWorkspace (
} }
) )
try { try {
await assignWorkspace( await assignAccountToWs(
ctx, ctx,
db, db,
branding, branding,
@ -1607,6 +1607,38 @@ export async function createMissingEmployee (
* @public * @public
*/ */
export async function assignWorkspace ( export async function assignWorkspace (
ctx: MeasureContext,
db: AccountDB,
branding: Branding | null,
token: string,
_email: string,
workspaceId: string,
role: AccountRole,
personId?: Ref<Person>,
shouldReplaceAccount: boolean = false,
client?: Client,
personAccountId?: Ref<PersonAccount>
): Promise<Workspace> {
const decodedToken = decodeToken(ctx, token)
if (decodedToken.extra?.service !== 'aibot') {
throw new PlatformError(new Status(Severity.ERROR, platform.status.Forbidden, {}))
}
return await assignAccountToWs(
ctx,
db,
branding,
_email,
workspaceId,
role,
personId,
shouldReplaceAccount,
client,
personAccountId
)
}
export async function assignAccountToWs (
ctx: MeasureContext, ctx: MeasureContext,
db: AccountDB, db: AccountDB,
branding: Branding | null, branding: Branding | null,
@ -2294,7 +2326,7 @@ export async function joinWithProvider (
return result return result
} }
const wsRes = await assignWorkspace( const wsRes = await assignAccountToWs(
ctx, ctx,
db, db,
branding, branding,
@ -2318,7 +2350,7 @@ export async function joinWithProvider (
} }
const newAccount = await createAcc(ctx, db, branding, email, null, first, last, true, true, extra) const newAccount = await createAcc(ctx, db, branding, email, null, first, last, true, true, extra)
const token = generateToken(email, getWorkspaceId(''), getExtra(newAccount)) const token = generateToken(email, getWorkspaceId(''), getExtra(newAccount))
const ws = await assignWorkspace( const ws = await assignAccountToWs(
ctx, ctx,
db, db,
branding, branding,

View File

@ -15,11 +15,13 @@
import { LoginInfo, Workspace, WorkspaceLoginInfo } from '@hcengineering/account' import { LoginInfo, Workspace, WorkspaceLoginInfo } from '@hcengineering/account'
import aiBot, { aiBotAccountEmail } from '@hcengineering/ai-bot' import aiBot, { aiBotAccountEmail } from '@hcengineering/ai-bot'
import { AccountRole } from '@hcengineering/core' import { AccountRole, systemAccountEmail } from '@hcengineering/core'
import { generateToken } from '@hcengineering/server-token'
import config from './config' import config from './config'
export async function assignBotToWorkspace (workspace: string): Promise<Workspace> { export async function assignBotToWorkspace (workspace: string): Promise<Workspace> {
const token = generateToken(systemAccountEmail, { name: '-' }, { service: 'aibot' })
const accountsUrl = config.AccountsURL const accountsUrl = config.AccountsURL
const res = await ( const res = await (
await fetch(accountsUrl, { await fetch(accountsUrl, {
@ -29,7 +31,16 @@ export async function assignBotToWorkspace (workspace: string): Promise<Workspac
}, },
body: JSON.stringify({ body: JSON.stringify({
method: 'assignWorkspace', method: 'assignWorkspace',
params: [aiBotAccountEmail, workspace, AccountRole.User, undefined, false, undefined, aiBot.account.AIBot] params: [
token,
aiBotAccountEmail,
workspace,
AccountRole.User,
undefined,
false,
undefined,
aiBot.account.AIBot
]
}) })
}) })
).json() ).json()