Example #1
0
        /// <inheritdoc />
        public DBResult <CommunicationEmail> Add(CommunicationEmail communicationEmail, bool commit = true)
        {
            this.logger.LogTrace($"Adding Communication Email to DB...");
            DBResult <CommunicationEmail> result = new DBResult <CommunicationEmail>()
            {
                Payload = communicationEmail,
                Status  = DBStatusCode.Deferred,
            };

            this.dbContext.CommunicationEmail.Add(communicationEmail);
            if (commit)
            {
                try
                {
                    this.dbContext.SaveChanges();
                    result.Status = DBStatusCode.Created;
                }
                catch (DbUpdateException e)
                {
                    this.logger.LogError($"Unable to save Communication Email to DB {e}");
                    result.Status  = DBStatusCode.Error;
                    result.Message = e.Message;
                }
            }

            this.logger.LogDebug($"Finished adding Communication in DB");
            return(result);
        }
        public void CreateCommunicationEmailsForNewCommunications()
        {
            this.logger.LogDebug($"Creating emails & communication emails...");
            IList <Communication> communications = this.communicationDelegate.GetEmailCommunicationsToSend();

            if (communications.Count > 0)
            {
                this.logger.LogInformation($"Found {communications.Count} communications which need to be processed / re-processed.");

                foreach (Communication communication in communications)
                {
#pragma warning disable CA1031 //We want to catch exception.
                    try
                    {
                        IList <UserProfile> usersToSendCommEmails = new List <UserProfile>();
                        bool moreUsersToCreateCommunicationEmails = false;

                        if (communication.CommunicationStatusCode != CommunicationStatus.Processing)
                        {
                            communication.CommunicationStatusCode = CommunicationStatus.Processing;
                            this.communicationDelegate.Update(communication, false);
                        }

                        do
                        {
                            usersToSendCommEmails = this.commEmailDelegate.GetProfilesForCommunication(communication.Id, this.maxFetchSize);
                            foreach (UserProfile profile in usersToSendCommEmails)
                            {
                                // Insert a new Email record into db.
                                Email email = new Email()
                                {
                                    From       = this.fromEmailAddressHGDonotreply,
                                    To         = profile.Email,
                                    Subject    = communication.Subject,
                                    Body       = communication.Text,
                                    FormatCode = EmailFormat.HTML,
                                    Priority   = communication.Priority,
                                };
                                this.emailQueueService.QueueNewEmail(email, false);

                                // Inser a new CommunicationEmail record into
                                CommunicationEmail commEmail = new CommunicationEmail()
                                {
                                    Email         = email,
                                    UserProfile   = profile,
                                    Communication = communication,
                                };
                                this.commEmailDelegate.Add(commEmail, false);
                            }

                            this.dbContext.SaveChanges();

                            moreUsersToCreateCommunicationEmails = usersToSendCommEmails.Count == this.maxFetchSize;
                        }while (moreUsersToCreateCommunicationEmails);

                        // Update Communication Status to Processed
                        communication.CommunicationStatusCode = CommunicationStatus.Processed;
                        this.communicationDelegate.Update(communication, true);
                    }
                    catch (Exception e)
                    {
                        // log the exception as a warning but we can continue
                        this.logger.LogWarning($"Error while creating new emails and communication email records for Email Communication - skipping for now\n{e.ToString()}");

                        if (communication.CommunicationStatusCode != CommunicationStatus.Processed)
                        {
                            try
                            {
                                // Update Communication Status to Error
                                communication.CommunicationStatusCode = CommunicationStatus.Error;
                                this.communicationDelegate.Update(communication, true);
                            }
                            catch (Exception ex)
                            {
                                // log the exception as a warning but we can continue
                                this.logger.LogWarning($"Error while updating communication with Error status - skipping for now\n{ex.ToString()}");
                            }
                        }
                    }
#pragma warning restore CA1031 // Restore warnings.
                }
            }
        }