@@ -36,10 +36,10 @@ export const matchAvailableProxyAddresses = async (activeProxyAddresses: ActiveP
3636 } )
3737
3838 if ( availableNumbers . length < 1 ) {
39- throw new Error ( ' Not enough numbers available in pool.' )
39+ throw new Error ( ` Not enough numbers available in pool for ${ key } ` )
4040 }
4141
42- proxyBindings [ key ] = availableNumbers [ 0 ] ;
42+ proxyBindings [ key ] = availableNumbers ;
4343 }
4444
4545 return proxyBindings ;
@@ -49,18 +49,42 @@ export const matchAvailableProxyAddresses = async (activeProxyAddresses: ActiveP
4949 }
5050}
5151
52+ const retryParticipantAdd = async ( conversationSid : string , participantAddress : string , proxyAddresses : Array < string > ) => {
53+ try {
54+ while ( proxyAddresses . length > 0 ) {
55+ try {
56+ const participant = {
57+ 'messagingBinding.address' : participantAddress ,
58+ 'messagingBinding.proxyAddress' : proxyAddresses [ 0 ]
59+ } as any
60+
61+ return addParticipant ( conversationSid , participant )
62+
63+ } catch ( err ) {
64+ if ( err . code === 50416 ) {
65+ const remainingProxyAddresses = proxyAddresses . shift ( )
66+ retryParticipantAdd ( conversationSid , participantAddress , remainingProxyAddresses as any )
67+ }
68+ console . log ( err )
69+ throw new Error ( err )
70+ }
71+ }
72+
73+ throw new Error ( `No proxy addresses available for ${ participantAddress } ` )
74+ } catch ( err ) {
75+ console . log ( err )
76+ throw new Error ( err )
77+ }
78+ }
79+
5280export const addParticipantsToConversation = async ( conversationSid : string , proxyBindings : ProxyBindings ) => {
5381 const promises = [ ]
5482
55- for ( const [ key , value ] of Object . entries ( proxyBindings ) ) {
56- const participant = {
57- 'messagingBinding.address' : key ,
58- 'messagingBinding.proxyAddress' : value
59- } as any
83+ for ( const [ participantAddress , proxyAddresses ] of Object . entries ( proxyBindings ) ) {
6084
6185 try {
62- const participantRequest = addParticipant ( conversationSid , participant )
63- promises . push ( participantRequest )
86+ const participantAttempt = retryParticipantAdd ( conversationSid , participantAddress , proxyAddresses )
87+ promises . push ( participantAttempt )
6488 } catch ( err ) {
6589 console . log ( err )
6690 throw new Error ( err )
0 commit comments