platform/server-plugins/setting-resources/src/index.ts
Denis Bykhov 1b4a574a1c
Fix automation (#4531)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
2024-02-05 21:36:50 +07:00

105 lines
3.7 KiB
TypeScript

//
// 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 { Account, Doc, Ref } from '@hcengineering/core'
import { 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<string> {
return await integrationTextPresenter(doc, control)
}
/**
* @public
*/
export async function integrationTextPresenter (doc: Doc, control: TriggerControl): Promise<string> {
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<string, Doc>): Promise<string | undefined> {
const value = context[setting.class.Integration] as Integration
if (value === undefined) return
return value.value
}
async function getEmployee (control: TriggerControl, _id: Ref<Account>): Promise<Person | undefined> {
const employeeAccount = (
await control.modelDb.findAll(contact.class.PersonAccount, {
_id: _id as Ref<PersonAccount>
})
)[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<string, Doc>
): Promise<string | undefined> {
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<string, Doc>
): Promise<string | undefined> {
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<string, Doc>
): Promise<string | undefined> {
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
}
}
// 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
}
})