diff --git a/server-plugins/card-resources/package.json b/server-plugins/card-resources/package.json index 0585e0f984..7a050599dd 100644 --- a/server-plugins/card-resources/package.json +++ b/server-plugins/card-resources/package.json @@ -44,6 +44,7 @@ "@hcengineering/platform": "^0.6.11", "@hcengineering/server-core": "^0.6.1", "@hcengineering/communication-sdk-types": "0.1.155", - "@hcengineering/server-contact": "^0.6.1" + "@hcengineering/server-contact": "^0.6.1", + "@hcengineering/contact": "^0.6.24" } } diff --git a/server-plugins/card-resources/src/index.ts b/server-plugins/card-resources/src/index.ts index bd0b22867f..4770abe238 100644 --- a/server-plugins/card-resources/src/index.ts +++ b/server-plugins/card-resources/src/index.ts @@ -15,6 +15,7 @@ import card, { Card, MasterTag, Tag } from '@hcengineering/card' import core, { + AccountUuid, AnyAttribute, Data, Doc, @@ -23,6 +24,7 @@ import core, { Mixin, Ref, splitMixinUpdate, + systemAccount, Tx, TxCreateDoc, TxMixin, @@ -34,7 +36,8 @@ import { TriggerControl } from '@hcengineering/server-core' import setting from '@hcengineering/setting' import view from '@hcengineering/view' import { RequestEventType } from '@hcengineering/communication-sdk-types' -import { getEmployee } from '@hcengineering/server-contact' +import { getEmployee, getPersonSpaces } from '@hcengineering/server-contact' +import contact from '@hcengineering/contact' async function OnAttribute (ctx: TxCreateDoc[], control: TriggerControl): Promise { const attr = TxProcessor.createDoc2Doc(ctx[0]) @@ -379,23 +382,44 @@ async function OnCardCreate (ctx: TxCreateDoc[], control: TriggerControl): } } - const { communicationApi } = control - if (communicationApi == null) return [] - - for (const tx of ctx) { - const employee = await getEmployee(control, tx.modifiedBy) - if (employee?.personUuid == null || !employee.active) continue - // TODO: add account - void communicationApi.event({} as any, { - type: RequestEventType.AddCollaborators, - card: tx.objectId, - collaborators: [employee.personUuid] - }) - } + await updateCollaborators(control, ctx) return res } +async function updateCollaborators (control: TriggerControl, ctx: TxCreateDoc[]): Promise { + const { communicationApi } = control + if (communicationApi == null) return + + for (const tx of ctx) { + const modifier = await getEmployee(control, tx.modifiedBy) + const collaborators: AccountUuid[] = [] + if (modifier?.personUuid != null && modifier.active) { + collaborators.push(modifier.personUuid) + } + + const personSpaces = await getPersonSpaces(control) + const personSpace = personSpaces.find((it) => it._id === tx.objectSpace) + + if (personSpace != null && personSpace.person !== modifier?._id) { + const spacePerson = (await control.findAll(control.ctx, contact.class.Person, { _id: personSpace.person }))[0] + if (spacePerson?.personUuid != null) { + collaborators.push(spacePerson.personUuid as AccountUuid) + } + } + + if (collaborators.length === 0) continue + void communicationApi.event( + { account: systemAccount }, + { + type: RequestEventType.AddCollaborators, + card: tx.objectId, + collaborators + } + ) + } +} + export async function OnCardTag (ctx: TxMixin[], control: TriggerControl): Promise { const res: Tx[] = [] for (const tx of ctx) { diff --git a/server-plugins/contact/src/utils.ts b/server-plugins/contact/src/utils.ts index 101d1a4c89..872c9a54da 100644 --- a/server-plugins/contact/src/utils.ts +++ b/server-plugins/contact/src/utils.ts @@ -14,7 +14,13 @@ // import { TriggerControl } from '@hcengineering/server-core' -import contact, { Employee, type Person, pickPrimarySocialId, SocialIdentityRef } from '@hcengineering/contact' +import contact, { + Employee, + type Person, + PersonSpace, + pickPrimarySocialId, + SocialIdentityRef +} from '@hcengineering/contact' import { AccountUuid, parseSocialIdString, PersonId, type Ref, toIdMap } from '@hcengineering/core' export async function getCurrentPerson (control: TriggerControl): Promise { @@ -259,3 +265,7 @@ export async function getAccountBySocialKey (control: TriggerControl, socialKey: return employee[0]?.personUuid ?? null } + +export async function getPersonSpaces (control: TriggerControl): Promise[]> { + return await control.queryFind(control.ctx, contact.class.PersonSpace, {}, { projection: { _id: 1, person: 1 } }) +} diff --git a/server-plugins/notification-resources/src/utils.ts b/server-plugins/notification-resources/src/utils.ts index 7390514214..aac014aaa0 100644 --- a/server-plugins/notification-resources/src/utils.ts +++ b/server-plugins/notification-resources/src/utils.ts @@ -20,7 +20,6 @@ import contact, { formatName, includesAny, Person, - PersonSpace, SocialIdentity, SocialIdentityRef } from '@hcengineering/contact' @@ -70,6 +69,7 @@ import { encodeObjectURI } from '@hcengineering/view' import { workbenchId } from '@hcengineering/workbench' import { NotifyResult } from './types' +import { getPersonSpaces } from '@hcengineering/server-contact' /** * @public @@ -472,12 +472,7 @@ export async function getReceiversInfo ( ) if (employees.length === 0) return [] - const spaces: Pick[] = await control.queryFind( - ctx, - contact.class.PersonSpace, - {}, - { projection: { _id: 1, person: 1 } } - ) + const spaces = await getPersonSpaces(control) if (spaces.length === 0) return [] const socialIds: Pick[] = await control.findAll(