mirror of
https://github.com/hcengineering/platform.git
synced 2025-05-31 04:38:02 +00:00
uberf-9534: ensure person (#8117)
Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
parent
3483789d93
commit
34cfc564b8
@ -26,7 +26,9 @@ import {
|
||||
WorkspaceMode,
|
||||
concatLink,
|
||||
type WorkspaceUserOperation,
|
||||
WorkspaceUuid
|
||||
type WorkspaceUuid,
|
||||
type PersonId,
|
||||
type SocialIdType
|
||||
} from '@hcengineering/core'
|
||||
import platform, { PlatformError, Severity, Status } from '@hcengineering/platform'
|
||||
import type { LoginInfo, OtpInfo, WorkspaceLoginInfo, RegionInfo, WorkspaceOperation } from './types'
|
||||
@ -109,6 +111,12 @@ export interface AccountClient {
|
||||
assignWorkspace: (email: string, workspaceUuid: string, role: AccountRole) => Promise<void>
|
||||
updateBackupInfo: (info: BackupStatus) => Promise<void>
|
||||
updateWorkspaceRoleBySocialId: (socialKey: string, targetRole: AccountRole) => Promise<void>
|
||||
ensurePerson: (
|
||||
socialType: SocialIdType,
|
||||
socialValue: string,
|
||||
firstName: string,
|
||||
lastName: string
|
||||
) => Promise<{ uuid: PersonUuid, socialId: PersonId }>
|
||||
|
||||
setCookie: () => Promise<void>
|
||||
deleteCookie: () => Promise<void>
|
||||
@ -592,6 +600,20 @@ class AccountClientImpl implements AccountClient {
|
||||
await this.rpc(request)
|
||||
}
|
||||
|
||||
async ensurePerson (
|
||||
socialType: SocialIdType,
|
||||
socialValue: string,
|
||||
firstName: string,
|
||||
lastName: string
|
||||
): Promise<{ uuid: PersonUuid, socialId: PersonId }> {
|
||||
const request = {
|
||||
method: 'ensurePerson' as const,
|
||||
params: { socialType, socialValue, firstName, lastName }
|
||||
}
|
||||
|
||||
return await this.rpc(request)
|
||||
}
|
||||
|
||||
async setCookie (): Promise<void> {
|
||||
const url = concatLink(this.url, '/cookie')
|
||||
const response = await fetch(url, { ...this.request, method: 'PUT' })
|
||||
|
@ -1657,6 +1657,43 @@ export async function assignWorkspace (
|
||||
}
|
||||
}
|
||||
|
||||
export async function ensurePerson (
|
||||
ctx: MeasureContext,
|
||||
db: AccountDB,
|
||||
branding: Branding | null,
|
||||
token: string,
|
||||
params: {
|
||||
socialType: SocialIdType
|
||||
socialValue: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
}
|
||||
): Promise<{ uuid: PersonUuid, socialId: PersonId }> {
|
||||
const { extra } = decodeTokenVerbose(ctx, token)
|
||||
verifyAllowedServices(['schedule'], extra)
|
||||
|
||||
const { socialType, socialValue, firstName, lastName } = params
|
||||
|
||||
if (
|
||||
!Object.values(SocialIdType).includes(socialType) ||
|
||||
firstName.length === 0 ||
|
||||
lastName.length === 0 ||
|
||||
socialValue.length === 0
|
||||
) {
|
||||
throw new PlatformError(new Status(Severity.ERROR, platform.status.BadRequest, {}))
|
||||
}
|
||||
|
||||
const socialId = await db.socialId.findOne({ type: socialType, value: socialValue })
|
||||
if (socialId != null) {
|
||||
return { uuid: socialId.personUuid, socialId: socialId.key }
|
||||
}
|
||||
|
||||
const personUuid = await db.person.insertOne({ firstName, lastName })
|
||||
const newSocialId = await db.socialId.insertOne({ type: socialType, value: socialValue, personUuid })
|
||||
|
||||
return { uuid: personUuid, socialId: newSocialId }
|
||||
}
|
||||
|
||||
export type AccountMethods =
|
||||
| 'login'
|
||||
| 'loginOtp'
|
||||
@ -1698,6 +1735,7 @@ export type AccountMethods =
|
||||
| 'findPerson'
|
||||
| 'performWorkspaceOperation'
|
||||
| 'updateWorkspaceRoleBySocialId'
|
||||
| 'ensurePerson'
|
||||
|
||||
/**
|
||||
* @public
|
||||
@ -1748,7 +1786,8 @@ export function getMethods (hasSignUp: boolean = true): Partial<Record<AccountMe
|
||||
assignWorkspace: wrap(assignWorkspace),
|
||||
listWorkspaces: wrap(listWorkspaces),
|
||||
performWorkspaceOperation: wrap(performWorkspaceOperation),
|
||||
updateWorkspaceRoleBySocialId: wrap(updateWorkspaceRoleBySocialId)
|
||||
updateWorkspaceRoleBySocialId: wrap(updateWorkspaceRoleBySocialId),
|
||||
ensurePerson: wrap(ensurePerson)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user