@@ -37,6 +37,10 @@ import { i18n } from '../../../../server/lib/i18n';
37
37
import { hasRoleAsync } from '../../../authorization/server/functions/hasRole' ;
38
38
import { sendNotification } from '../../../lib/server' ;
39
39
import { sendMessage } from '../../../lib/server/functions/sendMessage' ;
40
+ import {
41
+ notifyOnLivechatDepartmentAgentChanged ,
42
+ notifyOnLivechatDepartmentAgentChangedByAgentsAndDepartmentId ,
43
+ } from '../../../lib/server/lib/notifyListener' ;
40
44
import { settings } from '../../../settings/server' ;
41
45
import { Livechat as LivechatTyped } from './LivechatTyped' ;
42
46
import { queueInquiry , saveQueueInquiry } from './QueueManager' ;
@@ -697,14 +701,31 @@ export const updateDepartmentAgents = async (
697
701
} ) ;
698
702
699
703
const { upsert = [ ] , remove = [ ] } = agents ;
700
- const agentsRemoved = [ ] ;
704
+
705
+ const agentsUpdated = [ ] ;
706
+ const agentsRemoved = remove . map ( ( { agentId } : { agentId : string } ) => agentId ) ;
701
707
const agentsAdded = [ ] ;
702
- for await ( const { agentId } of remove ) {
703
- await LivechatDepartmentAgents . removeByDepartmentIdAndAgentId ( departmentId , agentId ) ;
704
- agentsRemoved . push ( agentId ) ;
705
- }
706
708
707
709
if ( agentsRemoved . length > 0 ) {
710
+ const removedIds = await LivechatDepartmentAgents . findByAgentsAndDepartmentId ( agentsRemoved , departmentId , {
711
+ projection : { agentId : 1 } ,
712
+ } ) . toArray ( ) ;
713
+
714
+ const { deletedCount } = await LivechatDepartmentAgents . removeByIds ( removedIds . map ( ( { _id } ) => _id ) ) ;
715
+
716
+ if ( deletedCount > 0 ) {
717
+ removedIds . forEach ( ( { _id, agentId } ) => {
718
+ void notifyOnLivechatDepartmentAgentChanged (
719
+ {
720
+ _id,
721
+ agentId,
722
+ departmentId,
723
+ } ,
724
+ 'removed' ,
725
+ ) ;
726
+ } ) ;
727
+ }
728
+
708
729
callbacks . runAsync ( 'livechat.removeAgentDepartment' , { departmentId, agentsId : agentsRemoved } ) ;
709
730
}
710
731
@@ -714,14 +735,28 @@ export const updateDepartmentAgents = async (
714
735
continue ;
715
736
}
716
737
717
- await LivechatDepartmentAgents . saveAgent ( {
738
+ const livechatDepartmentAgent = await LivechatDepartmentAgents . saveAgent ( {
718
739
agentId : agent . agentId ,
719
740
departmentId,
720
741
username : agentFromDb . username || '' ,
721
742
count : agent . count ? parseFromIntOrStr ( agent . count ) : 0 ,
722
743
order : agent . order ? parseFromIntOrStr ( agent . order ) : 0 ,
723
744
departmentEnabled,
724
745
} ) ;
746
+
747
+ if ( livechatDepartmentAgent . upsertedId ) {
748
+ void notifyOnLivechatDepartmentAgentChanged (
749
+ {
750
+ _id : livechatDepartmentAgent . upsertedId as any ,
751
+ agentId : agent . agentId ,
752
+ departmentId,
753
+ } ,
754
+ 'inserted' ,
755
+ ) ;
756
+ } else {
757
+ agentsUpdated . push ( agent . agentId ) ;
758
+ }
759
+
725
760
agentsAdded . push ( agent . agentId ) ;
726
761
}
727
762
@@ -732,6 +767,10 @@ export const updateDepartmentAgents = async (
732
767
} ) ;
733
768
}
734
769
770
+ if ( agentsUpdated . length > 0 ) {
771
+ void notifyOnLivechatDepartmentAgentChangedByAgentsAndDepartmentId ( agentsUpdated , departmentId ) ;
772
+ }
773
+
735
774
if ( agentsRemoved . length > 0 || agentsAdded . length > 0 ) {
736
775
const numAgents = await LivechatDepartmentAgents . countByDepartmentId ( departmentId ) ;
737
776
await LivechatDepartment . updateNumAgentsById ( departmentId , numAgents ) ;
0 commit comments