6
6
SubscriberRepository ,
7
7
SubscriberEntity ,
8
8
FeedRepository ,
9
+ EnforceEnvId ,
9
10
} from '@novu/dal' ;
10
11
import { ChannelTypeEnum , WebSocketEventEnum } from '@novu/shared' ;
11
12
import {
@@ -43,7 +44,7 @@ export class RemoveAllMessages {
43
44
}
44
45
}
45
46
46
- const deleteMessageQuery : Partial < MessageEntity > = {
47
+ const deleteMessageQuery : Partial < MessageEntity > & EnforceEnvId = {
47
48
_environmentId : command . environmentId ,
48
49
_organizationId : command . organizationId ,
49
50
_subscriberId : subscriber . _id ,
@@ -53,32 +54,33 @@ export class RemoveAllMessages {
53
54
if ( feed ) {
54
55
deleteMessageQuery . _feedId = feed . _id ;
55
56
}
57
+ const deletedMessages = await this . messageRepository . delete ( deleteMessageQuery ) ;
56
58
57
- await this . messageRepository . deleteMany ( deleteMessageQuery ) ;
58
-
59
- await this . updateServices ( command , subscriber , MarkEnum . SEEN ) ;
60
- await this . updateServices ( command , subscriber , MarkEnum . READ ) ;
59
+ if ( deletedMessages . deletedCount > 0 ) {
60
+ await Promise . all ( [
61
+ this . updateServices ( command , subscriber , MarkEnum . SEEN ) ,
62
+ this . updateServices ( command , subscriber , MarkEnum . READ ) ,
63
+ this . invalidateCache . invalidateQuery ( {
64
+ key : buildFeedKey ( ) . invalidate ( {
65
+ subscriberId : command . subscriberId ,
66
+ _environmentId : command . environmentId ,
67
+ } ) ,
68
+ } ) ,
69
+ this . invalidateCache . invalidateQuery ( {
70
+ key : buildMessageCountKey ( ) . invalidate ( {
71
+ subscriberId : command . subscriberId ,
72
+ _environmentId : command . environmentId ,
73
+ } ) ,
74
+ } ) ,
75
+ ] ) ;
76
+ }
61
77
62
78
this . analyticsService . track ( `Removed All Feed Messages - [Notification Center]` , command . organizationId , {
63
79
_subscriber : subscriber . _id ,
64
80
_organization : command . organizationId ,
65
81
_environment : command . environmentId ,
66
82
_feedId : command . feedId ,
67
83
} ) ;
68
-
69
- await this . invalidateCache . invalidateQuery ( {
70
- key : buildFeedKey ( ) . invalidate ( {
71
- subscriberId : command . subscriberId ,
72
- _environmentId : command . environmentId ,
73
- } ) ,
74
- } ) ;
75
-
76
- await this . invalidateCache . invalidateQuery ( {
77
- key : buildMessageCountKey ( ) . invalidate ( {
78
- subscriberId : command . subscriberId ,
79
- _environmentId : command . environmentId ,
80
- } ) ,
81
- } ) ;
82
84
} catch ( e ) {
83
85
if ( e instanceof DalException ) {
84
86
throw new BadRequestException ( e . message ) ;
@@ -87,14 +89,14 @@ export class RemoveAllMessages {
87
89
}
88
90
}
89
91
90
- private async updateServices ( command : RemoveAllMessagesCommand , subscriber , marked : string ) {
91
- this . updateSocketCount ( subscriber , marked ) ;
92
+ private async updateServices ( command : RemoveAllMessagesCommand , subscriber , marked : string ) : Promise < void > {
93
+ await this . updateSocketCount ( subscriber , marked ) ;
92
94
}
93
95
94
- private updateSocketCount ( subscriber : SubscriberEntity , mark : string ) {
96
+ private async updateSocketCount ( subscriber : SubscriberEntity , mark : string ) : Promise < void > {
95
97
const eventMessage = mark === MarkEnum . READ ? WebSocketEventEnum . UNREAD : WebSocketEventEnum . UNSEEN ;
96
98
97
- this . webSocketsQueueService . add ( {
99
+ await this . webSocketsQueueService . add ( {
98
100
name : 'sendMessage' ,
99
101
data : {
100
102
event : eventMessage ,
0 commit comments