@@ -203,26 +203,28 @@ private async Task HandleSpecificDomainEvent(CustomerEmailReceiptRequestedEvent
203203
204204 TransactionAggregate transactionAggregate = await this . TransactionAggregateManager . GetAggregate ( domainEvent . EstateId , domainEvent . TransactionId , cancellationToken ) ;
205205
206- var merchant = await this . EstateClient . GetMerchant ( this . TokenResponse . AccessToken , domainEvent . EstateId , domainEvent . MerchantId , cancellationToken ) ;
206+ MerchantResponse merchant = await this . EstateClient . GetMerchant ( this . TokenResponse . AccessToken , domainEvent . EstateId , domainEvent . MerchantId , cancellationToken ) ;
207207
208208 // Determine the body of the email
209209 String receiptMessage = await this . TransactionReceiptBuilder . GetEmailReceiptMessage ( transactionAggregate . GetTransaction ( ) , merchant , cancellationToken ) ;
210210
211211 // Send the message
212- await this . SendEmailMessage ( this . TokenResponse . AccessToken , domainEvent . EstateId , "Transaction Successful" , receiptMessage , domainEvent . CustomerEmailAddress , cancellationToken ) ;
212+ await this . SendEmailMessage ( this . TokenResponse . AccessToken , domainEvent . EventId , domainEvent . EstateId , "Transaction Successful" , receiptMessage , domainEvent . CustomerEmailAddress , cancellationToken ) ;
213213
214214 }
215215
216216 /// <summary>
217217 /// Sends the email message.
218218 /// </summary>
219219 /// <param name="accessToken">The access token.</param>
220+ /// <param name="messageId">The message identifier.</param>
220221 /// <param name="estateId">The estate identifier.</param>
221222 /// <param name="subject">The subject.</param>
222223 /// <param name="body">The body.</param>
223224 /// <param name="emailAddress">The email address.</param>
224225 /// <param name="cancellationToken">The cancellation token.</param>
225226 private async Task SendEmailMessage ( String accessToken ,
227+ Guid messageId ,
226228 Guid estateId ,
227229 String subject ,
228230 String body ,
@@ -231,6 +233,7 @@ private async Task SendEmailMessage(String accessToken,
231233 {
232234 SendEmailRequest sendEmailRequest = new SendEmailRequest
233235 {
236+ MessageId = messageId ,
234237 Body = body ,
235238 ConnectionIdentifier = estateId ,
236239 FromAddress = "[email protected] " , // TODO: lookup from config @@ -244,7 +247,19 @@ private async Task SendEmailMessage(String accessToken,
244247
245248 // TODO: may decide to record the message Id againsts the Transaction Aggregate in future, but for now
246249 // we wont do this...
247- await this . MessagingServiceClient . SendEmail ( accessToken , sendEmailRequest , cancellationToken ) ;
250+ try
251+ {
252+ await this . MessagingServiceClient . SendEmail ( accessToken , sendEmailRequest , cancellationToken ) ;
253+ }
254+ catch ( Exception ex ) when ( ex . InnerException != null && ex . InnerException . GetType ( ) == typeof ( InvalidOperationException ) )
255+ {
256+ // Only bubble up if not a duplicate message
257+ if ( ex . InnerException . Message . Contains ( "Cannot send a message to provider that has already been sent" , StringComparison . InvariantCultureIgnoreCase ) == false )
258+ {
259+ throw ;
260+ }
261+ }
262+
248263 }
249264
250265 #endregion
0 commit comments