Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZQMS-798: Fix role name update #5514

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion models/server-setting/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//

import { type Builder } from '@hcengineering/model'

import serverCore from '@hcengineering/server-core'
import core from '@hcengineering/core'
import serverNotification from '@hcengineering/server-notification'
import serverSetting from '@hcengineering/server-setting'
Expand Down Expand Up @@ -64,4 +64,15 @@ export function createModel (builder: Builder): void {
serverFunc: serverSetting.function.GetOwnerPosition
}
)

builder.createDoc(serverCore.class.Trigger, core.space.Model, {
trigger: serverSetting.trigger.OnRoleNameUpdate,
txMatch: {
_class: core.class.TxCollectionCUD,
objectSpace: core.space.Model,
collection: 'roles',
'tx._class': core.class.TxUpdateDoc,
'tx.objectClass': core.class.Role
}
})
}
9 changes: 8 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,18 @@ export interface RoleAttributeBaseProps {
export function getRoleAttributeBaseProps (data: AttachedData<Role>, roleId: Ref<Role>): RoleAttributeBaseProps {
const name = data.name.trim()
const label = getEmbeddedLabel(`Role: ${name}`)
const id = `role-${roleId}` as Ref<Attribute<PropertyType>>
const id = getRoleAttributeId(roleId)

return { label, id }
}

/**
* @public
*/
export function getRoleAttributeId (roleId: Ref<Role>): Ref<Attribute<PropertyType>> {
return `role-${roleId}` as Ref<Attribute<PropertyType>>
}

/**
* @public
*/
Expand Down
25 changes: 23 additions & 2 deletions server-plugins/setting-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//

import contact, { Person, PersonAccount, getFirstName, getLastName } from '@hcengineering/contact'
import { Account, Doc, Ref } from '@hcengineering/core'
import { translate } from '@hcengineering/platform'
import core, { Account, Doc, Ref, Role, Tx, TxProcessor, TxUpdateDoc, getRoleAttributeId } from '@hcengineering/core'
import { getEmbeddedLabel, translate } from '@hcengineering/platform'
import type { TriggerControl } from '@hcengineering/server-core'
import setting, { Integration } from '@hcengineering/setting'

Expand Down Expand Up @@ -91,6 +91,24 @@ export async function getOwnerPosition (
}
}

/**
* @public
*/
export async function OnRoleNameUpdate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const actualTx = TxProcessor.extractTx(tx)
const updateTx = actualTx as TxUpdateDoc<Role>

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, {
label: getEmbeddedLabel(updateTx.operations.name)
})

return [updAttrTx]
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => ({
function: {
Expand All @@ -100,5 +118,8 @@ export default async () => ({
GetFirstName: getOwnerFirstName,
GetLastName: getOwnerLastName,
GetOwnerPosition: getOwnerPosition
},
trigger: {
OnRoleNameUpdate
}
})
4 changes: 4 additions & 0 deletions server-plugins/setting/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import type { Plugin, Resource } from '@hcengineering/platform'
import { plugin } from '@hcengineering/platform'
import { Presenter } from '@hcengineering/server-notification'
import { TriggerFunc } from '@hcengineering/server-core'
import { TemplateFieldServerFunc } from '@hcengineering/server-templates'

/**
Expand All @@ -34,5 +35,8 @@ export default plugin(serverSettingId, {
GetFirstName: '' as Resource<TemplateFieldServerFunc>,
GetLastName: '' as Resource<TemplateFieldServerFunc>,
GetOwnerPosition: '' as Resource<TemplateFieldServerFunc>
},
trigger: {
OnRoleNameUpdate: '' as Resource<TriggerFunc>
}
})