Exemplo n.º 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 continueOnError = GetAttributeValue(action, "ContinueOnError").AsBoolean();

            // Get the gateway
            var gateway = GetEntityFromAttributeValue <FinancialGateway>(action, "FinancialGateway", true, rockContext);

            if (gateway == null)
            {
                errorMessages.Add("The gateway is not valid");
            }

            // Get the person
            var person = GetPersonFromAttributeValue(action, "Person", true, rockContext);

            if (person == null || !person.PrimaryAliasId.HasValue)
            {
                errorMessages.Add("A valid person was not provided.");
            }

            // If there are any errors accumulated, exit
            if (errorMessages.Any())
            {
                return(HandleExit(action, errorMessages, continueOnError));
            }

            // Find the saved account
            var savedAccountQry = new FinancialPersonSavedAccountService(rockContext)
                                  .GetByPersonId(person.Id)
                                  .AsNoTracking()
                                  .Where(sa => sa.FinancialGatewayId == gateway.Id);
            var savedAccount = savedAccountQry.FirstOrDefault(sa => sa.IsDefault) ?? savedAccountQry.FirstOrDefault();

            // Log the result and set the result attribute
            if (savedAccount == null)
            {
                action.AddLogEntry(string.Format(
                                       "{0} does not have a saved account for {1}",
                                       person.FullName,
                                       gateway.Name));

                var attribute = SetWorkflowAttributeValue(action, "ResultAttribute", ( int? )null);
                if (attribute != null)
                {
                    action.AddLogEntry(string.Format("Set '{0}' attribute to null.", attribute.Name));
                }
            }
            else
            {
                action.AddLogEntry(string.Format(
                                       "{0} has a saved account with ID {1} for {2}",
                                       person.FullName,
                                       savedAccount.Id,
                                       gateway.Name));

                var attribute = SetWorkflowAttributeValue(action, "ResultAttribute", savedAccount.Id);
                if (attribute != null)
                {
                    action.AddLogEntry(string.Format("Set '{0}' attribute to '{1}'.", attribute.Name, savedAccount.Id));
                }
            }

            return(HandleExit(action, errorMessages, continueOnError));
        }