@@ -16,6 +16,7 @@ import {
1616 type SendAlertRequest ,
1717} from '../../dialect-cloud-api/v2/application-api' ;
1818import { withErrorParsing } from '../../dialect-cloud-api/data-service-errors' ;
19+ import { chunk } from '../../utils/collection-utils' ;
1920
2021export const AddressTypeToChannel = {
2122 [ AddressType . Email ] : Channel . Email ,
@@ -43,6 +44,59 @@ export class AlertsV2DappMessages implements DappMessages {
4344 return this . broadcast ( command ) ;
4445 }
4546
47+ async sendBatch ( commands : UnicastDappMessageCommand [ ] ) : Promise < void > {
48+ if ( commands . length === 0 ) {
49+ return ;
50+ }
51+
52+ // Convert commands to SendAlertRequest using map and filter, applying same business logic as send method
53+ const alerts = commands
54+ . filter ( ( command ) => command . addressTypes ?. length !== 0 )
55+ . map ( ( command ) => this . unicastCommandToAlert ( command ) ) ;
56+
57+ // If no valid alerts after filtering, return early
58+ if ( alerts . length === 0 ) {
59+ return ;
60+ }
61+
62+ // Split into chunks of max 500 items
63+ const chunks = chunk ( alerts , 500 ) ;
64+
65+ // Send chunks sequentially
66+ for ( const alertChunk of chunks ) {
67+ try {
68+ await withErrorParsing (
69+ this . api . sendBatch ( this . dappId , {
70+ alerts : alertChunk ,
71+ } ) ,
72+ ) ;
73+ } catch ( e ) {
74+ console . error (
75+ `Error during sending batch dapp messages: ${ JSON . stringify ( e ) } ` ,
76+ ) ;
77+ }
78+ }
79+ }
80+
81+ private unicastCommandToAlert (
82+ command : UnicastDappMessageCommand ,
83+ ) : SendAlertRequest {
84+ return {
85+ recipient : {
86+ type : 'subscriber' ,
87+ walletAddress : command . recipient . toString ( ) ,
88+ } ,
89+ channels : AlertsV2DappMessages . mapChannels ( command . addressTypes ) ,
90+ topicId : command . notificationTypeId ,
91+ message : {
92+ title : command . title ,
93+ body : command . message ,
94+ image : command . imageUrl ,
95+ actions : AlertsV2DappMessages . mapActions ( command . actionsV2 ) ,
96+ } ,
97+ } ;
98+ }
99+
46100 static mapChannels ( addressTypes ?: AddressType [ ] ) {
47101 if ( ! addressTypes ) {
48102 return [ Channel . InApp , Channel . Email , Channel . Telegram ] ;
@@ -73,22 +127,8 @@ export class AlertsV2DappMessages implements DappMessages {
73127 }
74128
75129 private async unicast ( command : UnicastDappMessageCommand ) {
76- await withErrorParsing (
77- this . api . sendAlert ( this . dappId , {
78- recipient : {
79- type : 'subscriber' ,
80- walletAddress : command . recipient . toString ( ) ,
81- } ,
82- channels : AlertsV2DappMessages . mapChannels ( command . addressTypes ) ,
83- topicId : command . notificationTypeId ,
84- message : {
85- title : command . title ,
86- body : command . message ,
87- image : command . imageUrl ,
88- actions : AlertsV2DappMessages . mapActions ( command . actionsV2 ) ,
89- } ,
90- } ) ,
91- ) ;
130+ const alertRequest = this . unicastCommandToAlert ( command ) ;
131+ await withErrorParsing ( this . api . sendAlert ( this . dappId , alertRequest ) ) ;
92132 }
93133
94134 private async multicast ( command : MulticastDappMessageCommand ) {
0 commit comments