// // Copyright © 2022 Hardcore Engineering Inc. // // Licensed under the Eclipse Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. You may // obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // // See the License for the specific language governing permissions and // limitations under the License. // import contact, { Person, PersonAccount, getFirstName, getLastName } from '@hcengineering/contact' 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' /** * @public */ export async function integrationHTMLPresenter (doc: Doc, control: TriggerControl): Promise { return await integrationTextPresenter(doc, control) } /** * @public */ export async function integrationTextPresenter (doc: Doc, control: TriggerControl): Promise { const integration = doc as Integration const type = (await control.modelDb.findAll(setting.class.IntegrationType, { _id: integration.type }))[0] if (type === undefined) return '' const label = await translate(type.label, {}) return `${label} (${integration.value})` } export async function getValue (control: TriggerControl, context: Record): Promise { const value = context[setting.class.Integration] as Integration if (value === undefined) return return value.value } async function getEmployee (control: TriggerControl, _id: Ref): Promise { const employeeAccount = ( await control.modelDb.findAll(contact.class.PersonAccount, { _id: _id as Ref }) )[0] if (employeeAccount !== undefined) { const employee = ( await control.findAll(contact.class.Person, { _id: employeeAccount.person }) )[0] return employee } } export async function getOwnerFirstName ( control: TriggerControl, context: Record ): Promise { const value = context[setting.class.Integration] as Integration if (value === undefined) return const employee = await getEmployee(control, value.modifiedBy) return employee != null ? getFirstName(employee.name) : undefined } export async function getOwnerLastName ( control: TriggerControl, context: Record ): Promise { const value = context[setting.class.Integration] as Integration if (value === undefined) return const employee = await getEmployee(control, value.modifiedBy) return employee != null ? getLastName(employee.name) : undefined } export async function getOwnerPosition ( control: TriggerControl, context: Record ): Promise { const value = context[setting.class.Integration] as Integration if (value === undefined) return const employee = await getEmployee(control, value.modifiedBy) if (employee !== undefined) { return control.hierarchy.as(employee, contact.mixin.Employee)?.position ?? undefined } } /** * @public */ export async function OnRoleNameUpdate (tx: Tx, control: TriggerControl): Promise { const actualTx = TxProcessor.extractTx(tx) const updateTx = actualTx as TxUpdateDoc if (updateTx.operations?.name === undefined) return [] // Update the related mixin attribute 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) }) return [updAttrTx] } // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export default async () => ({ function: { IntegrationHTMLPresenter: integrationHTMLPresenter, IntegrationTextPresenter: integrationTextPresenter, GetValue: getValue, GetFirstName: getOwnerFirstName, GetLastName: getOwnerLastName, GetOwnerPosition: getOwnerPosition }, trigger: { OnRoleNameUpdate } })