diff --git a/TransactionProcessor.BusinessLogic/EventHandling/TransactionDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/TransactionDomainEventHandler.cs index d1db7d20..b5680242 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/TransactionDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/TransactionDomainEventHandler.cs @@ -203,13 +203,13 @@ private async Task HandleSpecificDomainEvent(CustomerEmailReceiptRequestedEvent TransactionAggregate transactionAggregate = await this.TransactionAggregateManager.GetAggregate(domainEvent.EstateId, domainEvent.TransactionId, cancellationToken); - var merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken, domainEvent.EstateId, domainEvent.MerchantId, cancellationToken); + MerchantResponse merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken, domainEvent.EstateId, domainEvent.MerchantId, cancellationToken); // Determine the body of the email String receiptMessage = await this.TransactionReceiptBuilder.GetEmailReceiptMessage(transactionAggregate.GetTransaction(), merchant, cancellationToken); // Send the message - await this.SendEmailMessage(this.TokenResponse.AccessToken, domainEvent.EstateId, "Transaction Successful", receiptMessage, domainEvent.CustomerEmailAddress, cancellationToken); + await this.SendEmailMessage(this.TokenResponse.AccessToken, domainEvent.EventId, domainEvent.EstateId, "Transaction Successful", receiptMessage, domainEvent.CustomerEmailAddress, cancellationToken); } @@ -217,12 +217,14 @@ private async Task HandleSpecificDomainEvent(CustomerEmailReceiptRequestedEvent /// Sends the email message. /// /// The access token. + /// The message identifier. /// The estate identifier. /// The subject. /// The body. /// The email address. /// The cancellation token. private async Task SendEmailMessage(String accessToken, + Guid messageId, Guid estateId, String subject, String body, @@ -231,6 +233,7 @@ private async Task SendEmailMessage(String accessToken, { SendEmailRequest sendEmailRequest = new SendEmailRequest { + MessageId = messageId, Body = body, ConnectionIdentifier = estateId, FromAddress = "golfhandicapping@btinternet.com", // TODO: lookup from config @@ -244,7 +247,19 @@ private async Task SendEmailMessage(String accessToken, // TODO: may decide to record the message Id againsts the Transaction Aggregate in future, but for now // we wont do this... - await this.MessagingServiceClient.SendEmail(accessToken, sendEmailRequest, cancellationToken); + try + { + await this.MessagingServiceClient.SendEmail(accessToken, sendEmailRequest, cancellationToken); + } + catch(Exception ex) when (ex.InnerException != null && ex.InnerException.GetType() == typeof(InvalidOperationException)) + { + // Only bubble up if not a duplicate message + if (ex.InnerException.Message.Contains("Cannot send a message to provider that has already been sent", StringComparison.InvariantCultureIgnoreCase) == false) + { + throw; + } + } + } #endregion diff --git a/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj b/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj index 58b03ea7..2b5d2e9f 100644 --- a/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj +++ b/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj @@ -6,7 +6,7 @@ - +