src/AccountDeleter/AccountDeleteMessageHandler.cs
/// <summary>
/// Handles incoming messages of AccountDeleteMessage type
/// </summary>
/// <param name="command"></param>
/// <returns>True if processed successfully, false if we failed for some reason.</returns>
/// <remarks>If accountManager.DeleteAccount throws, the entire method will throw and the message will go back into the queue.</remarks>
Handles incoming messages of AccountDeleteMessage type
Handles incoming messages of AccountDeleteMessage type
public async Task<bool> HandleAsync(AccountDeleteMessage command)
        {
            var messageProcessed = true;
            var source = command.Source;
            try
            {
                _logger.LogInformation("Processing Request from Source {Source}", source);
                _accountDeleteConfigurationAccessor.Value.GetSourceConfiguration(source);
                var username = command.Username;
                var user = _userService.FindByUsername(username);
                if (user == null)
                {
                    throw new UserNotFoundException();
                }
                var recipientEmail = user.EmailAddress;
                var emailAllowed = user.EmailAllowed;
                var deleteSuccess = await _accountManager.DeleteAccount(user, source);
                _telemetryService.TrackDeleteResult(source, deleteSuccess);
                if (recipientEmail == null)
                {
                    _logger.LogWarning("User has no confirmed email address. The user has been deleted but no email was sent.");
                    _telemetryService.TrackUnconfirmedUser(source);
                    messageProcessed = true;
                }
                else if (_accountDeleteConfigurationAccessor.Value.RespectEmailContactSetting && !emailAllowed)
                {
                    _logger.LogWarning("User did not allow Email Contact. The user has been deleted but no email was sent.");
                    _telemetryService.TrackEmailBlocked(source);
                    messageProcessed = true;
                }
                else
                {
                    var baseEmailBuilder = _emailBuilderFactory.GetEmailBuilder(source, deleteSuccess);
                    if (baseEmailBuilder != null)
                    {
                        var toEmail = new List<MailAddress>();
                        var configuration = _accountDeleteConfigurationAccessor.Value;
                        var senderAddress = configuration.EmailConfiguration.GalleryOwner;
                        var ccEmail = new List<MailAddress>();
                        toEmail.Add(new MailAddress(recipientEmail));
                        ccEmail.Add(new MailAddress(senderAddress));
                        var recipients = new EmailRecipients(toEmail, ccEmail);
                        var emailBuilder = new DisposableEmailBuilder(baseEmailBuilder, recipients, username);
                        await _messenger.SendMessageAsync(emailBuilder);
                        _telemetryService.TrackEmailSent(source, emailAllowed);
                        messageProcessed = true;
                    }
                }
            }
            catch (UnknownSourceException)
            {
                _logger.LogError("Unknown message source detected: {Source}.", command.Source);
                _telemetryService.TrackUnknownSource(source);
                messageProcessed = false;
            }
            catch (UserNotFoundException)
            {
                _logger.LogWarning("User was not found. They may have already been deleted.");
                _telemetryService.TrackUserNotFound(source);
                messageProcessed = true;
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An unknown exception occurred.");
                throw;
            }
            return messageProcessed;
        }
public async Task < bool > HandleAsync ( AccountDeleteMessage command ) { var messageProcessed = true ; var source = command . Source ; try { _logger . LogInformation ( " " , source ) ; _accountDeleteConfigurationAccessor . Value . GetSourceConfiguration ( source ) ; var username = command . Username ; var user = _userService . FindByUsername ( username ) ; if ( user == null ) { throw new UserNotFoundException ( ) ; } var recipientEmail = user . EmailAddress ; var emailAllowed = user . EmailAllowed ; var deleteSuccess = await _accountManager . DeleteAccount ( user , source ) ; _telemetryService . TrackDeleteResult ( source , deleteSuccess ) ; if ( recipientEmail == null ) { _logger . LogWarning ( " " ) ; _telemetryService . TrackUnconfirmedUser ( source ) ; messageProcessed = true ; } else if ( _accountDeleteConfigurationAccessor . Value . RespectEmailContactSetting && ! emailAllowed ) { _logger . LogWarning ( " " ) ; _telemetryService . TrackEmailBlocked ( source ) ; messageProcessed = true ; } else { var baseEmailBuilder = _emailBuilderFactory . GetEmailBuilder ( source , deleteSuccess ) ; if ( baseEmailBuilder != null ) { var toEmail = new List < MailAddress > ( ) ; var configuration = _accountDeleteConfigurationAccessor . Value ; var senderAddress = configuration . EmailConfiguration . GalleryOwner ; var ccEmail = new List < MailAddress > ( ) ; toEmail . Add ( new MailAddress ( recipientEmail ) ) ; ccEmail . Add ( new MailAddress ( senderAddress ) ) ; var recipients = new EmailRecipients ( toEmail , ccEmail ) ; var emailBuilder = new DisposableEmailBuilder ( baseEmailBuilder , recipients , username ) ; await _messenger . SendMessageAsync ( emailBuilder ) ; _telemetryService . TrackEmailSent ( source , emailAllowed ) ; messageProcessed = true ; } } } catch ( UnknownSourceException ) { _logger . LogError ( " " , command . Source ) ; _telemetryService . TrackUnknownSource ( source ) ; messageProcessed = false ; } catch ( UserNotFoundException ) { _logger . LogWarning ( " " ) ; _telemetryService . TrackUserNotFound ( source ) ; messageProcessed = true ; } catch ( Exception e ) { _logger . LogError ( e , " " ) ; throw ; } return messageProcessed ; }
Handles incoming messages of AccountDeleteMessage type
Handles incoming messages of AccountDeleteMessage type
// Should definitely log if source isn't expected. Should we even send mail? or log and alert? // Log unknown source and fail.
True if processed successfully, false if we failed for some reason.
True if processed successfully false if we failed for some reason .
command
AccountDeleteMessage
None
remarks
If accountManager.DeleteAccount throws, the entire method will throw and the message will go back into the queue.
If accountManager . DeleteAccount throws the entire method will throw and the message will go back into the queue .