@@ -17,15 +17,21 @@ import card, { Card, MasterTag, Tag } from '@hcengineering/card'
1717import core , {
1818 AccountUuid ,
1919 AnyAttribute ,
20+ ArrOf ,
21+ Class ,
2022 Data ,
2123 Doc ,
24+ DocumentUpdate ,
2225 fillDefaults ,
2326 generateId ,
2427 getDiffUpdate ,
2528 Mixin ,
29+ MixinUpdate ,
2630 notEmpty ,
2731 OperationDomain ,
32+ PersonId ,
2833 Ref ,
34+ RefTo ,
2935 Space ,
3036 splitMixinUpdate ,
3137 Tx ,
@@ -310,6 +316,18 @@ async function OnCardRemove (ctx: TxRemoveDoc<Card>[], control: TriggerControl):
310316 return res
311317}
312318
319+ function unwrapPush ( push : Record < string , any > ) : Record < string , any > {
320+ const res : Record < string , any > = { }
321+ for ( const [ key , value ] of Object . entries ( push ) ) {
322+ if ( value . $each !== undefined ) {
323+ res [ key ] = value . $each
324+ } else {
325+ res [ key ] = value
326+ }
327+ }
328+ return res
329+ }
330+
313331async function OnCardUpdate ( ctx : TxUpdateDoc < Card > [ ] , control : TriggerControl ) : Promise < Tx [ ] > {
314332 const updateTx = ctx [ 0 ]
315333 const doc = ( await control . findAll ( control . ctx , card . class . Card , { _id : updateTx . objectId } ) ) [ 0 ]
@@ -381,9 +399,89 @@ async function OnCardUpdate (ctx: TxUpdateDoc<Card>[], control: TriggerControl):
381399 }
382400
383401 res . push ( ...( await updatePeers ( control , doc , updateTx ) ) )
402+
403+ await updateCollaborators ( control , updateTx . operations , doc . _class , doc , updateTx . modifiedBy )
384404 return res
385405}
386406
407+ function getUpdateEmployees (
408+ control : TriggerControl ,
409+ field : string ,
410+ value : any ,
411+ _class : Ref < Class < Doc > >
412+ ) : Ref < Employee > [ ] | undefined {
413+ const res : Ref < Employee > [ ] = [ ]
414+ const attr = control . hierarchy . findAttribute ( _class , field )
415+ if ( attr === undefined ) return
416+ const parentType = attr . type . _class === core . class . ArrOf ? ( attr . type as ArrOf < Doc > ) . of : attr . type
417+ if ( parentType . _class !== core . class . RefTo ) return
418+ const to = ( parentType as RefTo < Doc > ) . to
419+ if ( control . hierarchy . isDerived ( to , contact . mixin . Employee ) ) {
420+ if ( value != null ) {
421+ if ( Array . isArray ( value ) ) {
422+ for ( const val of value ) {
423+ res . push ( val )
424+ }
425+ } else {
426+ res . push ( value )
427+ }
428+ }
429+ }
430+ return res
431+ }
432+
433+ async function addCollaborators (
434+ collaboratorRefs : Set < Ref < Employee > > ,
435+ control : TriggerControl ,
436+ doc : Card ,
437+ modifiedBy : PersonId
438+ ) : Promise < void > {
439+ if ( collaboratorRefs . size > 0 ) {
440+ const employees = await control . findAll ( control . ctx , contact . mixin . Employee , {
441+ _id : { $in : [ ...collaboratorRefs ] }
442+ } )
443+ const collaborators = employees . map ( ( p ) => p . personUuid ) . filter ( ( p ) => p != null )
444+ const event : AddCollaboratorsEvent = {
445+ type : NotificationEventType . AddCollaborators ,
446+ cardId : doc . _id ,
447+ cardType : doc . _class ,
448+ collaborators,
449+ socialId : modifiedBy
450+ }
451+ await control . domainRequest ( control . ctx , 'communication' as OperationDomain , {
452+ event
453+ } )
454+ }
455+ }
456+
457+ async function updateCollaborators (
458+ control : TriggerControl ,
459+ ops : MixinUpdate < Card , Card > | DocumentUpdate < Card > ,
460+ _class : Ref < Class < Doc > > ,
461+ doc : Card ,
462+ modifiedBy : PersonId
463+ ) : Promise < void > {
464+ const collaboratorRefs = new Set < Ref < Employee > > ( )
465+
466+ for ( const [ field , value ] of Object . entries ( ops ) ) {
467+ if ( field === '$push' ) {
468+ const unwrap = unwrapPush ( value )
469+ for ( const [ field , value ] of Object . entries ( unwrap ) ) {
470+ const col = getUpdateEmployees ( control , field , value , _class )
471+ if ( col !== undefined ) {
472+ col . map ( ( p ) => collaboratorRefs . add ( p ) )
473+ }
474+ }
475+ }
476+ if ( field . startsWith ( '$' ) ) continue
477+ const col = getUpdateEmployees ( control , field , value , _class )
478+ if ( col !== undefined ) {
479+ col . map ( ( p ) => collaboratorRefs . add ( p ) )
480+ }
481+ }
482+ await addCollaborators ( collaboratorRefs , control , doc , modifiedBy )
483+ }
484+
387485async function updatePeers ( control : TriggerControl , doc : Card , updateTx : TxUpdateDoc < Card > ) : Promise < Tx [ ] > {
388486 if ( updateTx . space === core . space . DerivedTx ) return [ ]
389487 const isDirect = control . hierarchy . isDerived ( doc . _class , communication . type . Direct )
@@ -668,12 +766,12 @@ async function OnCardCreate (ctx: TxCreateDoc<Card>[], control: TriggerControl):
668766 }
669767 }
670768
671- await updateCollaborators ( control , ctx )
769+ await createCollaborators ( control , ctx )
672770
673771 return res
674772}
675773
676- async function updateCollaborators ( control : TriggerControl , ctx : TxCreateDoc < Card > [ ] ) : Promise < void > {
774+ async function createCollaborators ( control : TriggerControl , ctx : TxCreateDoc < Card > [ ] ) : Promise < void > {
677775 for ( const tx of ctx ) {
678776 const modifier = await getEmployee ( control , tx . modifiedBy )
679777 const collaborators : AccountUuid [ ] = [ ]
@@ -736,6 +834,7 @@ export async function OnCardTag (ctx: TxMixin<Card, Card>[], control: TriggerCon
736834 res . push ( control . txFactory . createTxUpdateDoc ( it [ 0 ] , doc . space , doc . _id , it [ 1 ] ) )
737835 }
738836 }
837+ await updateCollaborators ( control , tx . attributes , tx . mixin , doc , tx . modifiedBy )
739838 }
740839 return res
741840}
0 commit comments