From 844e3934604d7791a0775746594f9a80f1286b8d Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Mon, 6 May 2024 17:47:34 +0400 Subject: [PATCH] ezqms-834: fix roles ids and names (#5520) Signed-off-by: Alexey Zinoviev --- models/core/src/spaceType.ts | 6 ++-- packages/core/src/utils.ts | 26 ++------------ .../typeEditors/RoleAssignmentEditor.svelte | 24 +++++++------ plugins/setting/src/utils.ts | 36 +++++++------------ server-plugins/setting-resources/src/index.ts | 10 ++++-- 5 files changed, 37 insertions(+), 65 deletions(-) diff --git a/models/core/src/spaceType.ts b/models/core/src/spaceType.ts index b55938776f..bfc5f7d5db 100644 --- a/models/core/src/spaceType.ts +++ b/models/core/src/spaceType.ts @@ -15,7 +15,7 @@ import { ArrOf, Prop, TypeRef, type Builder } from '@hcengineering/model' import { type Asset } from '@hcengineering/platform' -import { getRoleAttributeBaseProps } from '@hcengineering/core' +import { getRoleAttributeLabel } from '@hcengineering/core' import { TSpacesTypeData } from './security' import core from './component' @@ -30,10 +30,10 @@ const roles = [ export function defineSpaceType (builder: Builder): void { for (const role of roles) { - const { label, id } = getRoleAttributeBaseProps(role, role._id) + const label = getRoleAttributeLabel(role.name) const roleAssgtType = ArrOf(TypeRef(core.class.Account)) - Prop(roleAssgtType, label)(TSpacesTypeData.prototype, id) + Prop(roleAssgtType, label)(TSpacesTypeData.prototype, role._id) } builder.createModel(TSpacesTypeData) diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index b2ff944152..554f55c6de 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -18,9 +18,7 @@ import { Account, AccountRole, AnyAttribute, - AttachedData, AttachedDoc, - Attribute, Class, ClassifierKind, Collection, @@ -36,7 +34,6 @@ import { IndexKind, Obj, Permission, - PropertyType, Ref, Role, Space, @@ -609,27 +606,8 @@ export async function checkPermission ( /** * @public */ -export interface RoleAttributeBaseProps { - label: IntlString - id: Ref> -} - -/** - * @public - */ -export function getRoleAttributeBaseProps (data: AttachedData, roleId: Ref): RoleAttributeBaseProps { - const name = data.name.trim() - const label = getEmbeddedLabel(`Role: ${name}`) - const id = getRoleAttributeId(roleId) - - return { label, id } -} - -/** - * @public - */ -export function getRoleAttributeId (roleId: Ref): Ref> { - return `role-${roleId}` as Ref> +export function getRoleAttributeLabel (roleName: string): IntlString { + return getEmbeddedLabel(`Role: ${roleName.trim()}`) } /** diff --git a/plugins/setting-resources/src/components/typeEditors/RoleAssignmentEditor.svelte b/plugins/setting-resources/src/components/typeEditors/RoleAssignmentEditor.svelte index 16b242313c..3c9057f9d9 100644 --- a/plugins/setting-resources/src/components/typeEditors/RoleAssignmentEditor.svelte +++ b/plugins/setting-resources/src/components/typeEditors/RoleAssignmentEditor.svelte @@ -18,7 +18,7 @@ import { IntlString } from '@hcengineering/platform' import { ButtonKind, ButtonSize } from '@hcengineering/ui' - export let object: TypedSpace + export let object: TypedSpace | undefined export let label: IntlString export let value: Ref[] export let onChange: ((refs: Ref[]) => void) | undefined @@ -30,13 +30,15 @@ $: members = object?.members ?? [] - +{#if object !== undefined} + +{/if} diff --git a/plugins/setting/src/utils.ts b/plugins/setting/src/utils.ts index e6250eb185..1f7eec8775 100644 --- a/plugins/setting/src/utils.ts +++ b/plugins/setting/src/utils.ts @@ -15,18 +15,16 @@ import core, { AttachedData, - Attribute, Class, ClassifierKind, Data, - PropertyType, Ref, Role, Space, SpaceType, TxOperations, TypeAny as TypeAnyType, - getRoleAttributeBaseProps + getRoleAttributeLabel } from '@hcengineering/core' import { TypeAny } from '@hcengineering/model' import { getEmbeddedLabel, IntlString } from '@hcengineering/platform' @@ -71,18 +69,13 @@ export async function createSpaceType ( export interface RoleAttributeProps { label: IntlString roleType: TypeAnyType - id: Ref> } -export function getRoleAttributeProps (data: AttachedData, roleId: Ref): RoleAttributeProps { - const baseProps = getRoleAttributeBaseProps(data, roleId) - const roleType = TypeAny( - setting.component.RoleAssignmentEditor, - baseProps.label, - setting.component.RoleAssignmentEditor - ) +export function getRoleAttributeProps (name: string): RoleAttributeProps { + const label = getRoleAttributeLabel(name) + const roleType = TypeAny(setting.component.RoleAssignmentEditor, label, setting.component.RoleAssignmentEditor) - return { ...baseProps, roleType } + return { label, roleType } } export async function createSpaceTypeRole ( @@ -101,19 +94,14 @@ export async function createSpaceTypeRole ( _id ) - const { label, roleType, id } = getRoleAttributeProps(data, roleId) + const { label, roleType } = getRoleAttributeProps(data.name) - await client.createDoc( - core.class.Attribute, - core.space.Model, - { - name: roleId, - attributeOf: type.targetClass, - type: roleType, - label - }, - id - ) + await client.createDoc(core.class.Attribute, core.space.Model, { + name: roleId, + attributeOf: type.targetClass, + type: roleType, + label + }) return roleId } diff --git a/server-plugins/setting-resources/src/index.ts b/server-plugins/setting-resources/src/index.ts index 6246e95373..fc79d12e99 100644 --- a/server-plugins/setting-resources/src/index.ts +++ b/server-plugins/setting-resources/src/index.ts @@ -14,7 +14,7 @@ // import contact, { Person, PersonAccount, getFirstName, getLastName } from '@hcengineering/contact' -import core, { Account, Doc, Ref, Role, Tx, TxProcessor, TxUpdateDoc, getRoleAttributeId } from '@hcengineering/core' +import core, { Account, Doc, Ref, Role, Tx, TxProcessor, TxUpdateDoc } from '@hcengineering/core' import { getEmbeddedLabel, translate } from '@hcengineering/platform' import type { TriggerControl } from '@hcengineering/server-core' import setting, { Integration } from '@hcengineering/setting' @@ -101,8 +101,12 @@ export async function OnRoleNameUpdate (tx: Tx, control: TriggerControl): Promis if (updateTx.operations?.name === undefined) return [] // Update the related mixin attribute - const roleAttrId = getRoleAttributeId(updateTx.objectId) - const updAttrTx = control.txFactory.createTxUpdateDoc(core.class.Attribute, core.space.Model, roleAttrId, { + const roleAttribute = await control.modelDb.findOne(core.class.Attribute, { + name: updateTx.objectId + }) + if (roleAttribute === undefined) return [] + + const updAttrTx = control.txFactory.createTxUpdateDoc(core.class.Attribute, core.space.Model, roleAttribute._id, { label: getEmbeddedLabel(updateTx.operations.name) })