예제 #1
0
        public void Handle(RedeemEmailConfirmation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the confirmation
            var confirmation = _entities.Get <EmailConfirmation>()
                               .EagerLoad(_entities, new Expression <Func <EmailConfirmation, object> >[]
            {
                c => c.EmailAddress
            })
                               .ByToken(command.Token);

            // redeem
            if (!confirmation.RedeemedOnUtc.HasValue)
            {
                confirmation.EmailAddress.IsConfirmed = true;
                confirmation.RedeemedOnUtc            = DateTime.UtcNow;
                confirmation.Ticket = HandleGenerateRandomSecretQuery.Handle(
                    new GenerateRandomSecret(256));
                _entities.Update(confirmation);
            }

            command.Ticket = confirmation.Ticket;
        }
        public void Handle(SendConfirmEmailMessage command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the person
            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                x => x.Emails,
            })
                         .ByEmail(command.EmailAddress);

            // get the email
            var email = person.Emails.ByValue(command.EmailAddress);

            // create the confirmation
            var secretCode = HandleGenerateRandomSecretQuery.Handle(
                new GenerateRandomSecret(12));
            var confirmation = new EmailConfirmation(command.Intent)
            {
                EmailAddress = email,
                SecretCode   = secretCode,
            };

            command.ConfirmationToken = confirmation.Token;
            _entities.Create(confirmation);

            // get the email template
            var template = _queryProcessor.Execute(
                new EmailTemplateByName(command.TemplateName.AsSentenceFragment()));

            // create the message
            var message = _queryProcessor.Execute(
                new ComposeEmailMessage(template, email)
            {
                Formatters = _queryProcessor.Execute(
                    new ConfirmEmailFormatters(confirmation, command.SendFromUrl)
                    )
            }
                );

            _entities.Create(message);

            // send the message
            _sendHandler.Handle(
                new SendEmailMessageCommand
            {
                PersonId      = message.ToPerson.RevisionId,
                MessageNumber = message.Number,
            }
                );
        }