2022-01-20 09:27:41 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
2023-03-03 17:02:11 +00:00
|
|
|
// Copyright © 2021, 2022, 2023 Hardcore Engineering Inc.
|
2022-01-20 09:27:41 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2023-04-07 15:39:53 +00:00
|
|
|
import contact, {
|
|
|
|
Contact,
|
|
|
|
contactId,
|
|
|
|
Employee,
|
|
|
|
EmployeeAccount,
|
|
|
|
getName,
|
|
|
|
Organization,
|
|
|
|
Person
|
|
|
|
} from '@hcengineering/contact'
|
2023-03-03 17:02:11 +00:00
|
|
|
import core, {
|
2023-04-07 15:39:53 +00:00
|
|
|
Account,
|
2023-03-03 17:02:11 +00:00
|
|
|
AttachedDoc,
|
|
|
|
Collection,
|
|
|
|
concatLink,
|
|
|
|
Doc,
|
|
|
|
Ref,
|
|
|
|
RefTo,
|
2023-04-07 15:39:53 +00:00
|
|
|
Timestamp,
|
2023-03-03 17:02:11 +00:00
|
|
|
Tx,
|
2023-04-07 15:39:53 +00:00
|
|
|
TxBuilder,
|
2023-03-03 17:02:11 +00:00
|
|
|
TxRemoveDoc,
|
2023-04-07 15:39:53 +00:00
|
|
|
TxUpdateDoc,
|
|
|
|
updateAttribute
|
2023-03-03 17:02:11 +00:00
|
|
|
} from '@hcengineering/core'
|
2022-11-16 15:03:03 +00:00
|
|
|
import { getMetadata } from '@hcengineering/platform'
|
2023-04-07 15:39:53 +00:00
|
|
|
import serverCore, { AsyncTriggerControl, TriggerControl } from '@hcengineering/server-core'
|
2022-11-16 15:03:03 +00:00
|
|
|
import { workbenchId } from '@hcengineering/workbench'
|
2022-01-20 09:27:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-02-07 04:46:28 +00:00
|
|
|
export async function OnContactDelete (
|
|
|
|
tx: Tx,
|
2023-02-21 13:45:05 +00:00
|
|
|
{ findAll, hierarchy, storageFx, removedMap, txFactory }: TriggerControl
|
2023-02-07 04:46:28 +00:00
|
|
|
): Promise<Tx[]> {
|
2022-01-20 09:27:41 +00:00
|
|
|
if (tx._class !== core.class.TxRemoveDoc) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const rmTx = tx as TxRemoveDoc<Contact>
|
|
|
|
|
|
|
|
if (!hierarchy.isDerived(rmTx.objectClass, contact.class.Contact)) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2023-02-07 04:46:28 +00:00
|
|
|
const removeContact = removedMap.get(rmTx.objectId) as Contact
|
|
|
|
if (removeContact === undefined) {
|
2022-01-20 09:27:41 +00:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2023-02-07 04:46:28 +00:00
|
|
|
const avatar: string | undefined = [removeContact.avatar].filter((x): x is string => x !== undefined).slice(-1)[0]
|
2022-01-20 09:27:41 +00:00
|
|
|
|
|
|
|
if (avatar === undefined) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2023-01-31 05:48:40 +00:00
|
|
|
if (avatar?.includes('://') && !avatar?.startsWith('image://')) {
|
2023-01-25 13:42:55 +00:00
|
|
|
return []
|
|
|
|
}
|
2023-04-07 15:39:53 +00:00
|
|
|
if (avatar === '') {
|
|
|
|
return []
|
|
|
|
}
|
2023-01-25 13:42:55 +00:00
|
|
|
|
2022-01-20 09:27:41 +00:00
|
|
|
storageFx(async (adapter, bucket) => {
|
2022-11-16 15:03:03 +00:00
|
|
|
await adapter.remove(bucket, [avatar])
|
|
|
|
|
|
|
|
const extra = await adapter.list(bucket, avatar)
|
|
|
|
if (extra.length > 0) {
|
|
|
|
await adapter.remove(
|
|
|
|
bucket,
|
|
|
|
Array.from(extra.entries()).map((it) => it[1].name)
|
|
|
|
)
|
2022-07-02 09:26:23 +00:00
|
|
|
}
|
2022-01-20 09:27:41 +00:00
|
|
|
})
|
|
|
|
|
2023-02-21 13:45:05 +00:00
|
|
|
const result: Tx[] = []
|
|
|
|
|
|
|
|
const members = await findAll(contact.class.Member, { contact: removeContact._id })
|
|
|
|
for (const member of members) {
|
|
|
|
const removeTx = txFactory.createTxRemoveDoc(member._class, member.space, member._id)
|
|
|
|
const tx = txFactory.createTxCollectionCUD(
|
|
|
|
member.attachedToClass,
|
|
|
|
member.attachedTo,
|
|
|
|
member.space,
|
|
|
|
member.collection,
|
|
|
|
removeTx
|
|
|
|
)
|
|
|
|
result.push(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2022-01-20 09:27:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 15:39:53 +00:00
|
|
|
async function updateAllRefs (
|
|
|
|
control: AsyncTriggerControl,
|
|
|
|
sourceAccount: EmployeeAccount,
|
|
|
|
targetAccount: EmployeeAccount,
|
|
|
|
modifiedOn: Timestamp,
|
|
|
|
modifiedBy: Ref<Account>
|
2023-03-03 17:02:11 +00:00
|
|
|
): Promise<Tx[]> {
|
|
|
|
const res: Tx[] = []
|
2023-04-07 15:39:53 +00:00
|
|
|
// Move all possible references to Account and Employee and replace to target one.
|
|
|
|
const reftos = (await control.modelDb.findAll(core.class.Attribute, { 'type._class': core.class.RefTo })).filter(
|
|
|
|
(it) => {
|
|
|
|
const to = it.type as RefTo<Doc>
|
|
|
|
return to.to === contact.class.Employee || to.to === core.class.Account || to.to === contact.class.EmployeeAccount
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const attr of reftos) {
|
|
|
|
if (attr.name === '_id') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const to = attr.type as RefTo<Doc>
|
|
|
|
if (to.to === contact.class.Employee) {
|
|
|
|
const descendants = control.hierarchy.getDescendants(attr.attributeOf)
|
|
|
|
for (const d of descendants) {
|
|
|
|
if (control.hierarchy.findDomain(d) !== undefined) {
|
|
|
|
const values = await control.findAll(d, { [attr.name]: sourceAccount.employee })
|
|
|
|
|
|
|
|
const builder = new TxBuilder(control.hierarchy, control.modelDb, modifiedBy)
|
|
|
|
for (const v of values) {
|
|
|
|
await updateAttribute(builder, v, d, { key: attr.name, attr }, targetAccount.employee, targetAccount._id)
|
|
|
|
}
|
|
|
|
await control.apply(builder.txes, true, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(to.to === contact.class.EmployeeAccount || to.to === core.class.Account) &&
|
|
|
|
sourceAccount !== undefined &&
|
|
|
|
targetAccount !== undefined
|
|
|
|
) {
|
|
|
|
const descendants = control.hierarchy.getDescendants(attr.attributeOf)
|
|
|
|
for (const d of descendants) {
|
|
|
|
if (control.hierarchy.findDomain(d) !== undefined) {
|
|
|
|
const values = await control.findAll(d, { [attr.name]: sourceAccount._id })
|
|
|
|
const builder = new TxBuilder(control.hierarchy, control.modelDb, modifiedBy)
|
|
|
|
for (const v of values) {
|
|
|
|
await updateAttribute(builder, v, d, { key: attr.name, attr }, targetAccount._id, targetAccount._id)
|
|
|
|
}
|
|
|
|
await control.apply(builder.txes, true, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const arrs = await control.findAll(core.class.Attribute, { 'type._class': core.class.ArrOf })
|
|
|
|
for (const attr of arrs) {
|
|
|
|
if (attr.name === '_id') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const to = attr.type as RefTo<Doc>
|
|
|
|
if (to.to === contact.class.Employee) {
|
|
|
|
const descendants = control.hierarchy.getDescendants(attr.attributeOf)
|
|
|
|
for (const d of descendants) {
|
|
|
|
if (control.hierarchy.findDomain(d) !== undefined) {
|
|
|
|
const values = await control.findAll(attr.attributeOf, { [attr.name]: sourceAccount.employee })
|
|
|
|
const builder = new TxBuilder(control.hierarchy, control.modelDb, modifiedBy)
|
|
|
|
for (const v of values) {
|
|
|
|
await updateAttribute(builder, v, d, { key: attr.name, attr }, targetAccount.employee, targetAccount._id)
|
|
|
|
}
|
|
|
|
await control.apply(builder.txes, true, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(to.to === contact.class.EmployeeAccount || to.to === core.class.Account) &&
|
|
|
|
sourceAccount !== undefined &&
|
|
|
|
targetAccount !== undefined
|
|
|
|
) {
|
|
|
|
const descendants = control.hierarchy.getDescendants(attr.attributeOf)
|
|
|
|
for (const d of descendants) {
|
|
|
|
if (control.hierarchy.findDomain(d) !== undefined) {
|
|
|
|
const values = await control.findAll(d, { [attr.name]: sourceAccount._id })
|
|
|
|
const builder = new TxBuilder(control.hierarchy, control.modelDb, modifiedBy)
|
|
|
|
for (const v of values) {
|
|
|
|
await updateAttribute(builder, v, d, { key: attr.name, attr }, targetAccount._id, targetAccount._id)
|
|
|
|
}
|
|
|
|
await control.apply(builder.txes, true, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const employee = (await control.findAll(contact.class.Employee, { _id: sourceAccount.employee })).shift()
|
|
|
|
const builder = new TxBuilder(control.hierarchy, control.modelDb, modifiedBy)
|
|
|
|
await builder.remove(sourceAccount)
|
|
|
|
if (employee !== undefined) {
|
|
|
|
await builder.remove(employee)
|
|
|
|
}
|
|
|
|
await control.apply(builder.txes, true, true)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
async function mergeEmployee (control: AsyncTriggerControl, uTx: TxUpdateDoc<Employee>): Promise<Tx[]> {
|
|
|
|
if (uTx.operations.mergedTo === undefined) return []
|
|
|
|
const target = uTx.operations.mergedTo
|
|
|
|
const res: Tx[] = []
|
|
|
|
|
|
|
|
const attributes = control.hierarchy.getAllAttributes(contact.class.Employee)
|
|
|
|
|
2023-03-03 17:02:11 +00:00
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (control.hierarchy.isDerived(attribute[1].type._class, core.class.Collection)) {
|
|
|
|
if (attribute[1]._id === contact.class.Contact + '_channels') continue
|
|
|
|
const collection = attribute[1].type as Collection<AttachedDoc>
|
2023-04-07 15:39:53 +00:00
|
|
|
const allAttached = await control.findAll(collection.of, { attachedTo: uTx.objectId })
|
2023-03-03 17:02:11 +00:00
|
|
|
for (const attached of allAttached) {
|
|
|
|
const tx = control.txFactory.createTxUpdateDoc(attached._class, attached.space, attached._id, {
|
2023-04-07 15:39:53 +00:00
|
|
|
attachedTo: target
|
2023-03-03 17:02:11 +00:00
|
|
|
})
|
|
|
|
const parent = control.txFactory.createTxCollectionCUD(
|
|
|
|
attached.attachedToClass,
|
2023-04-07 15:39:53 +00:00
|
|
|
target,
|
2023-03-03 17:02:11 +00:00
|
|
|
attached.space,
|
|
|
|
attached.collection,
|
|
|
|
tx
|
|
|
|
)
|
|
|
|
res.push(parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:39:53 +00:00
|
|
|
const oldEmployeeAccount = (
|
|
|
|
await control.modelDb.findAll(contact.class.EmployeeAccount, { employee: uTx.objectId })
|
|
|
|
)[0]
|
|
|
|
const newEmployeeAccount = (await control.modelDb.findAll(contact.class.EmployeeAccount, { employee: target }))[0]
|
2023-03-03 17:02:11 +00:00
|
|
|
|
|
|
|
if (oldEmployeeAccount === undefined || newEmployeeAccount === undefined) return res
|
|
|
|
const accountTxes = await updateAllRefs(
|
|
|
|
control,
|
2023-04-07 15:39:53 +00:00
|
|
|
oldEmployeeAccount,
|
|
|
|
newEmployeeAccount,
|
|
|
|
uTx.modifiedOn,
|
|
|
|
uTx.modifiedBy
|
2023-03-03 17:02:11 +00:00
|
|
|
)
|
|
|
|
res.push(...accountTxes)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-04-07 15:39:53 +00:00
|
|
|
export async function OnEmployeeUpdate (tx: Tx, control: AsyncTriggerControl): Promise<Tx[]> {
|
2023-03-03 17:02:11 +00:00
|
|
|
if (tx._class !== core.class.TxUpdateDoc) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const uTx = tx as TxUpdateDoc<Employee>
|
|
|
|
|
|
|
|
if (!control.hierarchy.isDerived(uTx.objectClass, contact.class.Employee)) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: Tx[] = []
|
|
|
|
|
|
|
|
const txes = await mergeEmployee(control, uTx)
|
|
|
|
result.push(...txes)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:09:13 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-03-15 14:06:03 +00:00
|
|
|
export async function personHTMLPresenter (doc: Doc, control: TriggerControl): Promise<string> {
|
2022-02-22 09:09:13 +00:00
|
|
|
const person = doc as Person
|
2023-03-22 02:48:57 +00:00
|
|
|
const front = getMetadata(serverCore.metadata.FrontUrl) ?? ''
|
2023-03-20 08:45:52 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}/${doc._id}`
|
2023-02-01 11:27:15 +00:00
|
|
|
const link = concatLink(front, path)
|
2023-03-09 09:05:42 +00:00
|
|
|
return `<a href="${link}">${getName(person)}</a>`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function personTextPresenter (doc: Doc): string {
|
|
|
|
const person = doc as Person
|
2023-03-09 09:05:42 +00:00
|
|
|
return `${getName(person)}`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-03-15 14:06:03 +00:00
|
|
|
export async function organizationHTMLPresenter (doc: Doc, control: TriggerControl): Promise<string> {
|
2022-02-22 09:09:13 +00:00
|
|
|
const organization = doc as Organization
|
2023-03-22 02:48:57 +00:00
|
|
|
const front = getMetadata(serverCore.metadata.FrontUrl) ?? ''
|
2023-03-20 08:45:52 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}/${doc._id}`
|
2023-02-01 11:27:15 +00:00
|
|
|
const link = concatLink(front, path)
|
|
|
|
return `<a href="${link}">${organization.name}</a>`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function organizationTextPresenter (doc: Doc): string {
|
|
|
|
const organization = doc as Organization
|
|
|
|
return `${organization.name}`
|
|
|
|
}
|
|
|
|
|
2022-01-20 09:27:41 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
export default async () => ({
|
|
|
|
trigger: {
|
2023-03-03 17:02:11 +00:00
|
|
|
OnContactDelete,
|
|
|
|
OnEmployeeUpdate
|
2022-02-22 09:09:13 +00:00
|
|
|
},
|
|
|
|
function: {
|
|
|
|
PersonHTMLPresenter: personHTMLPresenter,
|
|
|
|
PersonTextPresenter: personTextPresenter,
|
|
|
|
OrganizationHTMLPresenter: organizationHTMLPresenter,
|
|
|
|
OrganizationTextPresenter: organizationTextPresenter
|
2022-01-20 09:27:41 +00:00
|
|
|
}
|
|
|
|
})
|