TSK-1349 Send notification integration disconnect (#3096)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov
2023-04-27 23:42:41 +07:00
committed by GitHub
parent e319a22a72
commit 97396cc5f2
9 changed files with 106 additions and 68 deletions
+18 -57
View File
@@ -13,72 +13,33 @@
// limitations under the License.
//
import core, {
Account,
Data,
Doc,
generateId,
Ref,
Tx,
TxCollectionCUD,
TxCreateDoc,
TxUpdateDoc
} from '@hcengineering/core'
import { Doc } from '@hcengineering/core'
import { translate } from '@hcengineering/platform'
import type { TriggerControl } from '@hcengineering/server-core'
import setting, { Integration } from '@hcengineering/setting'
import contact, { EmployeeAccount } from '@hcengineering/contact'
import notification, { Notification, NotificationStatus } from '@hcengineering/notification'
/**
* @public
*/
export async function OnIntegrationDisable (tx: Tx, control: TriggerControl): Promise<Tx[]> {
if (!control.hierarchy.isDerived(tx._class, core.class.TxUpdateDoc)) return []
const ctx = tx as TxUpdateDoc<Integration>
if (!control.hierarchy.isDerived(ctx.objectClass, setting.class.Integration)) return []
if (ctx.operations.disabled === true) {
const account = (
await control.modelDb.findAll(core.class.Account, { _id: ctx.objectSpace as string as Ref<Account> })
)[0]
if (account === undefined) return []
const employeeRef = (account as EmployeeAccount).employee
if (employeeRef === undefined) return []
export async function integrationHTMLPresenter (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 ''
return await translate(type.label, {})
}
const createTx: TxCreateDoc<Notification> = {
objectClass: notification.class.Notification,
objectSpace: notification.space.Notifications,
objectId: generateId(),
modifiedOn: ctx.modifiedOn,
modifiedBy: ctx.modifiedBy,
space: ctx.space,
_id: generateId(),
_class: core.class.TxCreateDoc,
attributes: {
tx: ctx._id,
status: NotificationStatus.New
} as unknown as Data<Notification>
}
const createNotificationTx: TxCollectionCUD<Doc, Notification> = {
objectId: employeeRef,
objectClass: contact.class.Employee,
objectSpace: contact.space.Employee,
modifiedOn: ctx.modifiedOn,
space: core.space.Tx,
_class: core.class.TxCollectionCUD,
modifiedBy: ctx.modifiedBy,
_id: generateId(),
collection: 'notifications',
tx: createTx
}
return [createNotificationTx]
}
return []
/**
* @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 ''
return await translate(type.label, {})
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => ({
trigger: {
OnIntegrationDisable
}
IntegrationHTMLPresenter: integrationHTMLPresenter,
IntegrationTextPresenter: integrationTextPresenter
})