Example #1
0
        /// <summary>
        /// Creates the communication to the recipient's mobile device with attachments.
        /// </summary>
        /// <param name="fromPerson">From person.</param>
        /// <param name="toPersonAliasId">To person alias identifier.</param>
        /// <param name="message">The message.</param>
        /// <param name="fromPhone">From phone.</param>
        /// <param name="responseCode">The response code.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="attachments">The attachments.</param>
        public static void CreateCommunicationMobile(Person fromPerson, int?toPersonAliasId, string message, DefinedValueCache fromPhone, string responseCode, Rock.Data.RockContext rockContext, List <BinaryFile> attachments)
        {
            // NOTE: fromPerson should never be null since a Nameless Person record should have been created if a regular person record wasn't found
            string communicationName = fromPerson != null?string.Format("From: {0}", fromPerson.FullName) : "From: unknown person";

            var communicationService = new CommunicationService(rockContext);

            var createSMSCommunicationArgs = new CommunicationService.CreateSMSCommunicationArgs
            {
                FromPerson            = fromPerson,
                ToPersonAliasId       = toPersonAliasId,
                Message               = message,
                FromPhone             = fromPhone,
                CommunicationName     = communicationName,
                ResponseCode          = responseCode,
                SystemCommunicationId = null,
            };

            Rock.Model.Communication communication = communicationService.CreateSMSCommunication(createSMSCommunicationArgs);

            rockContext.SaveChanges();

            // Now that we have a communication ID we can add the attachments
            if (attachments != null && attachments.Any())
            {
                foreach (var attachment in attachments)
                {
                    var communicationAttachment = new CommunicationAttachment
                    {
                        BinaryFileId      = attachment.Id,
                        CommunicationId   = communication.Id,
                        CommunicationType = CommunicationType.SMS
                    };

                    communication.AddAttachment(communicationAttachment, CommunicationType.SMS);
                }

                rockContext.SaveChanges();
            }

            // queue the sending
            var transaction = new ProcessSendCommunication.Message()
            {
                CommunicationId = communication.Id,
            };

            transaction.Send();
        }
Example #2
0
        /// <summary>
        /// Handles the Click event of the btnSendConfirmed control which sends the actual communication.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSendConfirmed_Click(object sender, EventArgs e)
        {
            if (Page.IsValid && CurrentPerson != null)
            {
                SetActionButtons(resetToSend: true);

                var people = GetMatchingPeople();

                // Get new communication record
                var rockContext   = new RockContext();
                var communication = GetCommunication(rockContext, people.Select(p => p.Id).ToList());

                if (communication != null)
                {
                    string message = string.Empty;

                    if (CheckApprovalRequired(communication.Recipients.Count) && !IsUserAuthorized("Approve"))
                    {
                        communication.Status = CommunicationStatus.PendingApproval;
                        message = "Communication has been submitted for approval.";
                    }
                    else
                    {
                        communication.Status                = CommunicationStatus.Approved;
                        communication.ReviewedDateTime      = RockDateTime.Now;
                        communication.ReviewerPersonAliasId = CurrentPersonAliasId;
                        message = "Communication has been queued for sending.";
                    }

                    rockContext.SaveChanges();

                    if (communication.Status == CommunicationStatus.Approved)
                    {
                        var processSendCommunicationMsg = new ProcessSendCommunication.Message
                        {
                            CommunicationId = communication.Id
                        };
                        processSendCommunicationMsg.Send();
                    }

                    pnlSuccess.Visible = true;
                    pnlForm.Visible    = false;
                    nbSuccess.Text     = message;
                }
            }
        }
        /// <summary>
        /// Populates all the recipients and then sends the communication.
        /// </summary>
        /// <param name="communication">The communication.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <remarks>This method should be called from a background task.</remarks>
        private void SendCommunication(Rock.Model.Communication communication, RockContext rockContext)
        {
            var pushData             = communication.PushData.FromJsonOrNull <PushData>();
            var pageEntityTypeId     = EntityTypeCache.Get <Rock.Model.Page>().Id;
            var websiteMediumValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.INTERACTIONCHANNELTYPE_WEBSITE).Id;
            var filterDate           = RockDateTime.Now.AddDays(-GetAttributeValue(AttributeKey.PersonalDeviceActiveDuration).AsInteger());

            //
            // Build a query for all the personal devices that have a recent interaction.
            // We go in through the Interaction table in the hopes that this query will
            // be faster than trying to query the PersonalDevice table first and then cross
            // join it to the Interaction table somehow. Hopefully SQL Server optimizes this well.
            //
            var personalDeviceIds = new InteractionService(rockContext)
                                    .Queryable()
                                    .AsNoTracking()
                                    .Where(a => a.InteractionComponent.InteractionChannel.ChannelTypeMediumValueId == websiteMediumValueId)
                                    .Where(a => a.InteractionComponent.InteractionChannel.ChannelEntityId == pushData.MobileApplicationId)
                                    .Where(a => a.InteractionDateTime > filterDate)
                                    .Where(a => a.PersonalDevice.IsActive && a.PersonalDevice.NotificationsEnabled && !string.IsNullOrEmpty(a.PersonalDevice.DeviceRegistrationId))
                                    .Select(a => a.PersonalDeviceId.Value)
                                    .Distinct()
                                    .ToList();

            PopulateCommunicationRecipients(communication, personalDeviceIds);

            //
            // Update the communication status to mark it as ready to send.
            //
            communication.Status = CommunicationStatus.Approved;
            rockContext.SaveChanges();

            if (GetAttributeValue(AttributeKey.SendImmediately).AsBoolean())
            {
                var processSendCommunicationMsg = new ProcessSendCommunication.Message
                {
                    CommunicationId = communication.Id,
                };
                processSendCommunicationMsg.Send();
            }
        }