public async Task <IActionResult> GetPendingEmails()
        {
            var result = await _emailsRepository.GetPendingEmails();

            if (result?.Response == null || !result.Response.Any())
            {
                return(StatusCode(404, result?.Message));
            }

            return(Ok(result));
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(SendAllPendingEmails request,
                                        CancellationToken cancellationToken)
        {
            List <EmailModel> pendingEmails = await emailsRepository.GetPendingEmails(cancellationToken);

            foreach (EmailModel pendingEmail in pendingEmails)
            {
                try
                {
                    await emailsService.SendEmail(pendingEmail, cancellationToken);
                }
                catch (EmailSendException emailSendException)
                {
                    await mediator.Publish(new FailedToSendEmail
                    {
                        Id     = pendingEmail.Id,
                        Reason = emailSendException.ToString()
                    });

                    throw emailSendException;
                }

                // TODO: what if email was sent but we fail to mark it as sent?
                //    it might be sent again when this is called next time
                await emailsRepository.MarkAsSent(pendingEmail.Id, cancellationToken);
            }

            return(Unit.Value);
        }