platform/plugins/telegram-resources/src/utils.ts
Kristina b997b0c753
UBERF-7604: Preparation for telegram notifications ()
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
2024-08-06 15:27:42 +07:00

56 lines
2.2 KiB
TypeScript

//
// Copyright © 2023 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, { type Employee, type PersonAccount } from '@hcengineering/contact'
import { employeeByIdStore, getContactChannel } from '@hcengineering/contact-resources'
import { type Ref, getCurrentAccount } from '@hcengineering/core'
import { getClient } from '@hcengineering/presentation'
import setting from '@hcengineering/setting'
import { type TemplateDataProvider } from '@hcengineering/templates'
import { get } from 'svelte/store'
import { getMetadata } from '@hcengineering/platform'
import telegram from './plugin'
export async function getCurrentEmployeeTG (): Promise<string | undefined> {
const me = getCurrentAccount() as PersonAccount
const client = getClient()
const employee = await client.findOne(contact.mixin.Employee, { _id: me.person as unknown as Ref<Employee> })
if (employee !== undefined) {
return await getContactChannel(employee, contact.channelProvider.Telegram)
}
}
export async function getIntegrationOwnerTG (provider: TemplateDataProvider): Promise<string | undefined> {
const value = provider.get(setting.class.Integration)
if (value === undefined) return
const client = getClient()
const employeeAccount = await client.findOne(contact.class.PersonAccount, {
_id: value.modifiedBy as Ref<PersonAccount>
})
if (employeeAccount !== undefined) {
const employee = get(employeeByIdStore).get(employeeAccount.person as Ref<Employee>)
if (employee !== undefined) {
return await getContactChannel(employee, contact.channelProvider.Telegram)
}
}
}
export function isTelegramNotificationsAvailable (): boolean {
const botEndpoint = getMetadata(telegram.metadata.BotUrl) ?? ''
return botEndpoint !== ''
}