UBERF-10346: Combined ensure person (#8701)
Some checks are pending
CI / build (push) Waiting to run
CI / svelte-check (push) Blocked by required conditions
CI / formatting (push) Blocked by required conditions
CI / test (push) Blocked by required conditions
CI / uitest (push) Waiting to run
CI / uitest-pg (push) Waiting to run
CI / uitest-qms (push) Waiting to run
CI / uitest-workspaces (push) Waiting to run
CI / docker-build (push) Blocked by required conditions
CI / dist-build (push) Blocked by required conditions

This commit is contained in:
Alexey Zinoviev 2025-04-25 04:53:58 +04:00 committed by GitHub
parent 8317713867
commit 26f23559c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 172 additions and 16 deletions

View File

@ -136,9 +136,10 @@ function hookOpenWindow (window: BrowserWindow): void {
} }
function setupCookieHandler (config: Config): void { function setupCookieHandler (config: Config): void {
const normalizedAccountsUrl = config.ACCOUNTS_URL.endsWith('/') ? config.ACCOUNTS_URL : config.ACCOUNTS_URL + '/'
const urls = [ const urls = [
config.ACCOUNTS_URL, normalizedAccountsUrl,
config.ACCOUNTS_URL + '*' normalizedAccountsUrl + '*'
] ]
session.defaultSession.webRequest.onHeadersReceived({ urls }, handleSetCookie) session.defaultSession.webRequest.onHeadersReceived({ urls }, handleSetCookie)

View File

@ -25,10 +25,13 @@ import {
Hierarchy, Hierarchy,
MeasureMetricsContext, MeasureMetricsContext,
ModelDb, ModelDb,
PersonId,
PersonUuid,
type Ref, type Ref,
type SearchOptions, type SearchOptions,
type SearchQuery, type SearchQuery,
type SearchResult, type SearchResult,
SocialIdType,
type Tx, type Tx,
type TxResult, type TxResult,
type WithLookup type WithLookup
@ -237,4 +240,28 @@ export class RestClientImpl implements RestClient {
} }
return await extractJson<TxResult>(response) return await extractJson<TxResult>(response)
} }
async ensurePerson (
socialType: SocialIdType,
socialValue: string,
firstName: string,
lastName: string
): Promise<{ uuid: PersonUuid, socialId: PersonId, localPerson: string }> {
const requestUrl = concatLink(this.endpoint, `/api/v1/ensure-person/${this.workspace}`)
const response = await fetch(requestUrl, {
method: 'POST',
headers: this.jsonHeaders(),
keepalive: true,
body: JSON.stringify({
socialType,
socialValue,
firstName,
lastName
})
})
if (!response.ok) {
throw new PlatformError(unknownError(response.statusText))
}
return await extractJson<{ uuid: PersonUuid, socialId: PersonId, localPerson: string }>(response)
}
} }

View File

@ -14,6 +14,9 @@
// //
import { import {
PersonId,
PersonUuid,
SocialIdType,
type Account, type Account,
type Class, type Class,
type Doc, type Doc,
@ -36,4 +39,10 @@ export interface RestClient extends Storage {
) => Promise<WithLookup<T> | undefined> ) => Promise<WithLookup<T> | undefined>
getModel: () => Promise<{ hierarchy: Hierarchy, model: ModelDb }> getModel: () => Promise<{ hierarchy: Hierarchy, model: ModelDb }>
ensurePerson: (
socialType: SocialIdType,
socialValue: string,
firstName: string,
lastName: string
) => Promise<{ uuid: PersonUuid, socialId: PersonId, localPerson: string }>
} }

View File

@ -1,6 +1,11 @@
import core, { import core, {
buildSocialIdString,
generateId, generateId,
pickPrimarySocialId,
TxFactory,
TxProcessor, TxProcessor,
type AttachedData,
type Data,
type Class, type Class,
type Doc, type Doc,
type MeasureContext, type MeasureContext,
@ -11,8 +16,16 @@ import core, {
} from '@hcengineering/core' } from '@hcengineering/core'
import type { ClientSessionCtx, ConnectionSocket, Session, SessionManager } from '@hcengineering/server-core' import type { ClientSessionCtx, ConnectionSocket, Session, SessionManager } from '@hcengineering/server-core'
import { decodeToken } from '@hcengineering/server-token' import { decodeToken } from '@hcengineering/server-token'
import { rpcJSONReplacer, type RateLimitInfo } from '@hcengineering/rpc' import { rpcJSONReplacer, type RateLimitInfo } from '@hcengineering/rpc'
import contact, {
AvatarType,
combineName,
type SocialIdentity,
type Person,
type SocialIdentityRef
} from '@hcengineering/contact'
import { type AccountClient, getClient as getAccountClientRaw } from '@hcengineering/account-client'
import { createHash } from 'crypto' import { createHash } from 'crypto'
import { type Express, type Response as ExpressResponse, type Request } from 'express' import { type Express, type Response as ExpressResponse, type Request } from 'express'
import type { OutgoingHttpHeaders } from 'http2' import type { OutgoingHttpHeaders } from 'http2'
@ -110,13 +123,22 @@ async function sendJson (
res.end(body) res.end(body)
} }
export function registerRPC (app: Express, sessions: SessionManager, ctx: MeasureContext): void { export function registerRPC (app: Express, sessions: SessionManager, ctx: MeasureContext, accountsUrl: string): void {
const rpcSessions = new Map<string, RPCClientInfo>() const rpcSessions = new Map<string, RPCClientInfo>()
function getAccountClient (token?: string): AccountClient {
return getAccountClientRaw(accountsUrl, token)
}
async function withSession ( async function withSession (
req: Request, req: Request,
res: ExpressResponse, res: ExpressResponse,
operation: (ctx: ClientSessionCtx, session: Session, rateLimit?: RateLimitInfo) => Promise<void> operation: (
ctx: ClientSessionCtx,
session: Session,
rateLimit: RateLimitInfo | undefined,
token: string
) => Promise<void>
): Promise<void> { ): Promise<void> {
try { try {
if (req.params.workspaceId === undefined || req.params.workspaceId === '') { if (req.params.workspaceId === undefined || req.params.workspaceId === '') {
@ -156,7 +178,7 @@ export function registerRPC (app: Express, sessions: SessionManager, ctx: Measur
const rpc = transactorRpc const rpc = transactorRpc
const rateLimit = await sessions.handleRPC(ctx, rpc.session, rpc.client, async (ctx, rateLimit) => { const rateLimit = await sessions.handleRPC(ctx, rpc.session, rpc.client, async (ctx, rateLimit) => {
await operation(ctx, rpc.session, rateLimit) await operation(ctx, rpc.session, rateLimit, token)
}) })
if (rateLimit !== undefined) { if (rateLimit !== undefined) {
const { remaining, limit, reset, retryAfter } = rateLimit const { remaining, limit, reset, retryAfter } = rateLimit
@ -302,6 +324,69 @@ export function registerRPC (app: Express, sessions: SessionManager, ctx: Measur
}) })
}) })
app.post('/api/v1/ensure-person/:workspaceId', (req, res) => {
void withSession(req, res, async (ctx, session, rateLimit, token) => {
const { socialType, socialValue, firstName, lastName } = (await retrieveJson(req)) ?? {}
const accountClient = getAccountClient(token)
const { uuid, socialId } = await accountClient.ensurePerson(socialType, socialValue, firstName, lastName)
const primaryPersonId = pickPrimarySocialId(session.getSocialIds())
const txFactory: TxFactory = new TxFactory(primaryPersonId._id)
const [person] = await session.findAllRaw(ctx, contact.class.Person, { personUuid: uuid }, { limit: 1 })
let personRef: Ref<Person> = person?._id
if (personRef === undefined) {
const createPersonTx = txFactory.createTxCreateDoc(contact.class.Person, contact.space.Contacts, {
avatarType: AvatarType.COLOR,
name: combineName(firstName, lastName),
personUuid: uuid
})
await session.txRaw(ctx, createPersonTx)
personRef = createPersonTx.objectId
}
const [socialIdentity] = await session.findAllRaw(
ctx,
contact.class.SocialIdentity,
{
attachedTo: personRef,
type: socialType,
value: socialValue
},
{ limit: 1 }
)
if (socialIdentity === undefined) {
const data: AttachedData<SocialIdentity> = {
key: buildSocialIdString({ type: socialType, value: socialValue }),
type: socialType,
value: socialValue
}
const addSocialIdentityTx = txFactory.createTxCollectionCUD(
contact.class.Person,
personRef,
contact.space.Contacts,
'socialIds',
txFactory.createTxCreateDoc(
contact.class.SocialIdentity,
contact.space.Contacts,
data as Data<SocialIdentity>,
socialId as SocialIdentityRef
)
)
await session.txRaw(ctx, addSocialIdentityTx)
}
const result = { uuid, socialId, localPerson: personRef }
await sendJson(req, res, result, rateLimitToHeaders(rateLimit))
})
})
// To use in non-js (rust) clients that can't link to @hcengineering/core // To use in non-js (rust) clients that can't link to @hcengineering/core
app.get('/api/v1/generate-id/:workspaceId', (req, res) => { app.get('/api/v1/generate-id/:workspaceId', (req, res) => {
void withSession(req, res, async (ctx, session, rateLimit) => { void withSession(req, res, async (ctx, session, rateLimit) => {

View File

@ -396,7 +396,7 @@ export function startHttpServer (
}) })
) )
registerRPC(app, sessions, ctx) registerRPC(app, sessions, ctx, accountsUrl)
app.put('/api/v1/broadcast', (req, res) => { app.put('/api/v1/broadcast', (req, res) => {
try { try {

View File

@ -22,25 +22,27 @@ import {
type WorkspaceToken type WorkspaceToken
} from '@hcengineering/api-client' } from '@hcengineering/api-client'
import core, { import core, {
buildSocialIdString,
generateId, generateId,
MeasureMetricsContext, MeasureMetricsContext,
pickPrimarySocialId, pickPrimarySocialId,
SocialIdType,
type Ref,
type SocialId, type SocialId,
type Space, type Space,
type TxCreateDoc, type TxCreateDoc,
type TxOperations type TxOperations
} from '@hcengineering/core' } from '@hcengineering/core'
import { type AccountClient, getClient as getAccountClient } from '@hcengineering/account-client'
import { getClient as getAccountClient } from '@hcengineering/account-client'
import chunter from '@hcengineering/chunter' import chunter from '@hcengineering/chunter'
import contact, { ensureEmployee } from '@hcengineering/contact' import contact, { ensureEmployee, type SocialIdentityRef, type Person } from '@hcengineering/contact'
describe('rest-api-server', () => { describe('rest-api-server', () => {
const testCtx = new MeasureMetricsContext('test', {}) const testCtx = new MeasureMetricsContext('test', {})
const wsName = 'api-tests' const wsName = 'api-tests'
let apiWorkspace1: WorkspaceToken let apiWorkspace1: WorkspaceToken
let apiWorkspace2: WorkspaceToken let apiWorkspace2: WorkspaceToken
let accountClient: AccountClient
beforeAll(async () => { beforeAll(async () => {
const config = await loadServerConfig('http://huly.local:8083') const config = await loadServerConfig('http://huly.local:8083')
@ -65,10 +67,10 @@ describe('rest-api-server', () => {
config config
) )
const account = getAccountClient(config.ACCOUNTS_URL, apiWorkspace1.token) accountClient = getAccountClient(config.ACCOUNTS_URL, apiWorkspace1.token)
const person = await account.getPerson() const person = await accountClient.getPerson()
const socialIds: SocialId[] = await account.getSocialIds() const socialIds: SocialId[] = await accountClient.getSocialIds()
// Ensure employee is created // Ensure employee is created
@ -214,7 +216,35 @@ describe('rest-api-server', () => {
expect(employee.length).toBeGreaterThanOrEqual(1) expect(employee.length).toBeGreaterThanOrEqual(1)
expect(employee[0].active).toBe(true) expect(employee[0].active).toBe(true)
}) })
it('ensure-person', async () => {
const socialType = SocialIdType.TELEGRAM
const socialValue = '123456789'
const first = 'John'
const last = 'Doe'
const conn = connect()
const { uuid, socialId, localPerson } = await conn.ensurePerson(socialType, socialValue, first, last)
const globalPerson = await accountClient.findPersonBySocialKey(
buildSocialIdString({ type: socialType, value: socialValue })
)
expect(globalPerson).toBe(uuid)
const person = await conn.findOne(contact.class.Person, { _id: localPerson as Ref<Person>, personUuid: uuid })
expect(person).not.toBeNull()
const socialIdObj = await conn.findOne(contact.class.SocialIdentity, {
type: socialType,
value: socialValue,
attachedTo: person?._id,
_id: socialId as SocialIdentityRef
})
expect(socialIdObj).not.toBeNull()
})
}) })
async function checkFindPerformance (conn: RestClient): Promise<void> { async function checkFindPerformance (conn: RestClient): Promise<void> {
let ops = 0 let ops = 0
let total = 0 let total = 0

View File

@ -384,7 +384,6 @@ services:
- STATS_URL=http://huly.local:4901 - STATS_URL=http://huly.local:4901
- REKONI_URL=http://huly.local:4007 - REKONI_URL=http://huly.local:4007
- ACCOUNTS_URL=http://huly.local:3003 - ACCOUNTS_URL=http://huly.local:3003
- SERVER_SECRET=secret
restart: unless-stopped restart: unless-stopped
aiBot: aiBot:
image: hardcoreeng/ai-bot image: hardcoreeng/ai-bot

View File

@ -28,13 +28,18 @@ fi
./wait-elastic.sh 9201 ./wait-elastic.sh 9201
# Create user record in accounts echo "Creating user accounts..."
./tool.sh create-account admin -f Super -l Admin -p 1234 ./tool.sh create-account admin -f Super -l Admin -p 1234
./tool.sh create-account user1 -f John -l Appleseed -p 1234 ./tool.sh create-account user1 -f John -l Appleseed -p 1234
./tool.sh create-account user2 -f Kainin -l Dirak -p 1234 ./tool.sh create-account user2 -f Kainin -l Dirak -p 1234
echo "Creating workspace api-tests..."
./tool.sh create-workspace api-tests email:user1 ./tool.sh create-workspace api-tests email:user1
echo "Creating workspace api-tests-cr..."
./tool-europe.sh create-workspace api-tests-cr email:user1 --region 'europe' ./tool-europe.sh create-workspace api-tests-cr email:user1 --region 'europe'
echo "Assigning user1 to workspaces..."
./tool.sh assign-workspace user1 api-tests ./tool.sh assign-workspace user1 api-tests
./tool.sh assign-workspace user1 api-tests-cr ./tool.sh assign-workspace user1 api-tests-cr