예제 #1
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute( RockContext rockContext, WorkflowAction action, Object entity, out List<string> errorMessages )
        {
            errorMessages = new List<string>();

            var mergeFields = GetMergeFields( action );

            var homePhoneValueId = DefinedValueCache.Read( SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME ).Id;
            var mobilePhoneValueId = DefinedValueCache.Read( SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE ).Id;
            var workPhoneValueId = DefinedValueCache.Read( SystemGuid.DefinedValue.PERSON_PHONE_TYPE_WORK ).Id;

            // get requester
            var requestPerson = new PersonAliasService( rockContext ).Get( GetAttributeValue( action, "Person", true ).AsGuid() ).Person;
            if (requestPerson == null )
            {
                var errorMessage = "Could not determine the person for the request.";
                errorMessages.Add( errorMessage );
                action.AddLogEntry( errorMessage, true );
                return false;
            }

            // get case worker
            var caseWorker = new PersonAliasService( rockContext ).Get( GetAttributeValue( action, "CaseWorker", true ).AsGuid() )?.Person;

            // get request status
            var statusValue = DefinedValueCache.Read( GetAttributeValue( action, "RequestStatus" ) );
            if ( statusValue == null )
            {
                var errorMessage = "Invalid request status provided.";
                errorMessages.Add( errorMessage );
                action.AddLogEntry( errorMessage, true );
                return false;
            }

            // get request description
            var requestDescription = GetAttributeValue( action, "RequestDescription", true ).ResolveMergeFields( mergeFields );
            if ( string.IsNullOrWhiteSpace( requestDescription ) )
            {
                var errorMessage = "Request description is requried.";
                errorMessages.Add( errorMessage );
                action.AddLogEntry( errorMessage, true );
                return false;
            }

            // get government id
            var governmentId = GetAttributeValue( action, "GovernmentId", true ).ResolveMergeFields( mergeFields );

            // get campus
            int? campusId = CampusCache.Read( GetAttributeValue( action, "Campus" ).AsGuid() )?.Id;

            // create benevolence request
            BenevolenceRequestService benevolenceRequestService = new BenevolenceRequestService( rockContext );

            BenevolenceRequest request = new BenevolenceRequest();
            benevolenceRequestService.Add( request );

            request.RequestDateTime = RockDateTime.Now;
            request.RequestText = requestDescription;
            request.RequestedByPersonAliasId = requestPerson.PrimaryAliasId;
            request.FirstName = requestPerson.NickName;
            request.LastName = requestPerson.LastName;
            request.Email = requestPerson.Email;
            request.LocationId = requestPerson.GetHomeLocation()?.Id;
            request.GovernmentId = governmentId;

            if ( campusId.HasValue )
            {
                request.CampusId = campusId.Value;
            }
            else
            {
                request.CampusId = requestPerson.GetCampus()?.Id;
            }

            var requestorPhoneNumbers = requestPerson.PhoneNumbers;

            if ( requestorPhoneNumbers != null )
            {
                request.HomePhoneNumber = requestorPhoneNumbers.Where( p => p.NumberTypeValueId == homePhoneValueId ).FirstOrDefault()?.NumberFormatted;
                request.CellPhoneNumber = requestorPhoneNumbers.Where( p => p.NumberTypeValueId == mobilePhoneValueId ).FirstOrDefault()?.NumberFormatted;
                request.WorkPhoneNumber = requestorPhoneNumbers.Where( p => p.NumberTypeValueId == workPhoneValueId ).FirstOrDefault()?.NumberFormatted;
            }

            if( caseWorker != null )
            {
                request.CaseWorkerPersonAliasId = caseWorker.PrimaryAliasId;
            }

            request.ConnectionStatusValueId = requestPerson.ConnectionStatusValueId;
            request.RequestStatusValueId = statusValue.Id;

            rockContext.SaveChanges();

            action.Activity.Workflow.SetAttributeValue( "BenevolenceRequest", request.Guid );
            action.AddLogEntry( $"Set 'Benevolence Request' attribute to '{request.Guid}'." );
            return true;
        }