Exemple #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>();

            // Initialize instance properties
            _action      = action;
            _rockContext = rockContext;

            Person person  = GetPersonFromAttributeValue(_action, AttributeKey.Person, true, _rockContext);
            int?   groupId = GetAttributeValue(_action, AttributeKey.GroupId, true).AsIntegerOrNull();

            var    globalAttributeCache = GlobalAttributesCache.Get();
            string accessToken          = globalAttributeCache.GetValue(SystemKey.GlobalAttribute.GROUPME_API_TOKEN);

            if (accessToken.IsNullOrWhiteSpace())
            {
                errorMessages.Add("GroupMe Access Token Global Attribute is missing or blank");
            }
            else if (person.IsNull())
            {
                errorMessages.Add("The Person is required");
            }
            else if (groupId.IsNullOrZero())
            {
                errorMessages.Add("Invalid GroupMe Group Id.");
            }

            if (errorMessages.Any())
            {
                return(false);
            }

            var mobilePhoneType = Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE.AsGuid();
            var mobileNumber    = new PhoneNumberService(rockContext)
                                  .Queryable()
                                  .AsNoTracking()
                                  .Where(p => p.PersonId == person.Id)
                                  .Where(a => a.NumberTypeValue.Guid == mobilePhoneType)
                                  .FirstOrDefault();

            if (mobileNumber.IsNull())
            {
                errorMessages.Add("The Person does not have a mobile number.");
                return(false);
            }

            var groupMeAPI = new GroupMeAPI(accessToken);
            var group      = groupMeAPI.GetGroup(groupId.Value);

            if (group.IsNull() || group.Name.IsNullOrWhiteSpace())
            {
                errorMessages.Add("GroupMe API Error.");
                return(false);
            }

            var groupmember = new GroupMeGroupMemberAdd
            {
                Nickname    = person.FullName,
                PhoneNumber = mobileNumber.Number
            };

            var added = groupMeAPI.AddGroupMember(group, groupmember, out int memberId);

            if (!added || memberId == 0)
            {
                errorMessages.Add("GroupMe API Error.");
                return(false);
            }

            action.AddLogEntry($"Successfully added { person.FullName } to \"{ group.Name }\" on Groupme.");
            SetWorkflowAttributeValue(_action, AttributeKey.MemberId, memberId);

            return(true);
        }
        /// <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>();

            // Initialize instance properties
            _action      = action;
            _rockContext = rockContext;

            int?memberId = GetAttributeValue(_action, AttributeKey.MemberId, true).AsIntegerOrNull();
            int?groupId  = GetAttributeValue(_action, AttributeKey.GroupId, true).AsIntegerOrNull();

            var    globalAttributeCache = GlobalAttributesCache.Get();
            string accessToken          = globalAttributeCache.GetValue(SystemKey.GlobalAttribute.GROUPME_API_TOKEN);

            if (accessToken.IsNullOrWhiteSpace())
            {
                errorMessages.Add("GroupMe Access Token Global Attribute is missing or blank");
            }
            else if (memberId.IsNullOrZero())
            {
                errorMessages.Add("Invalid GroupMe Member Id.");
            }
            else if (groupId.IsNullOrZero())
            {
                errorMessages.Add("Invalid GroupMe Group Id.");
            }

            if (errorMessages.Any())
            {
                return(false);
            }

            var groupMeAPI = new GroupMeAPI(accessToken);
            var group      = groupMeAPI.GetGroup(groupId.Value);

            if (group.IsNull() || group.Name.IsNullOrWhiteSpace())
            {
                errorMessages.Add("GroupMe API Error.");
                return(false);
            }

            var member = group.Members.AsQueryable().Where(m => m.Id.AsIntegerOrNull() == memberId).FirstOrDefault();

            if (member.IsNull())
            {
                errorMessages.Add("The provided memberId was not found in the group.");
                return(false);
            }

            var removed = groupMeAPI.RemoveGroupMember(group, member);

            if (!removed)
            {
                errorMessages.Add("GroupMe API Error.");
                return(false);
            }

            action.AddLogEntry($"Successfully removed \"{ member.Nickname }\" from \"{ group.Name }\" on Groupme.");

            return(true);
        }