Exemplo n.º 1
0
        /// <summary>
        /// Search members in database by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        protected virtual async Task <MemberSearchResult> RegularSearchMembersAsync(MembersSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(SearchMembersAsync), criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CustomerSearchCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();
                    var result = AbstractTypeFactory <MemberSearchResult> .TryCreateInstance();

                    var sortInfos = BuildSortExpression(criteria);
                    var query = BuildQuery(repository, criteria);

                    result.TotalCount = await query.CountAsync();
                    if (criteria.Take > 0)
                    {
                        var ids = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                  .Select(x => x.Id)
                                  .Skip(criteria.Skip).Take(criteria.Take)
                                  .ToArrayAsync();

                        result.Results = (await _memberService.GetByIdsAsync(ids, criteria.ResponseGroup)).OrderBy(x => Array.IndexOf(ids, x.Id)).ToList();
                    }
                    return result;
                }
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Search in multiple data sources.
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public MembersSearchResult SearchMembers(MembersSearchCriteria criteria)
        {
            var retVal      = new MembersSearchResult();
            var skip        = criteria.Skip;
            var take        = criteria.Take;
            var memberTypes = criteria.MemberTypes;
            /// !!!Ahtung!!!: Because members can be searched in multiple data sources we have to always use sorting by memberType field (asc or desc)
            /// instead pagination will not works properly
            var sortByMemberType = criteria.SortInfos.FirstOrDefault(x => string.Equals(x.SortColumn, "memberType", StringComparison.OrdinalIgnoreCase)) ?? new SortInfo {
                SortColumn = "memberType"
            };
            var sortInfos = criteria.SortInfos.Where(x => x != sortByMemberType);

            criteria.Sort = SortInfo.ToString(new[] { sortByMemberType }.Concat(sortInfos));

            foreach (var memberMapping in _memberMappings)
            {
                criteria.MemberTypes = memberTypes.IsNullOrEmpty() ? memberMapping.AllSupportedMemberTypeNames.ToArray() : memberMapping.AllSupportedMemberTypeNames.Intersect(memberTypes, StringComparer.OrdinalIgnoreCase).ToArray();
                if (!criteria.MemberTypes.IsNullOrEmpty() && criteria.Take >= 0)
                {
                    var result = memberMapping.MemberSearchService.SearchMembers(criteria);
                    retVal.Members.AddRange(result.Members);
                    retVal.TotalCount += result.TotalCount;
                    criteria.Skip      = Math.Max(0, skip - retVal.TotalCount);
                    criteria.Take      = Math.Max(0, take - result.Members.Count());
                }
            }
            //restore back criteria property values
            criteria.Skip        = skip;
            criteria.Take        = take;
            criteria.MemberTypes = memberTypes;
            return(retVal);
        }
Exemplo n.º 3
0
        private async Task LoadChildren(MembersSearchCriteria criteria, IEnumerable <Organization> organizations, bool withoutOrganizations, string[] orgMemberTypes, MemberSearchResult result)
        {
            foreach (var organization in organizations)
            {
                var searchChildrenCriteria = new MembersSearchCriteria()
                {
                    MemberId      = organization.Id,
                    DeepSearch    = true,
                    Skip          = 0,
                    Take          = int.MaxValue,
                    Sort          = criteria.Sort,
                    MemberTypes   = criteria.MemberTypes,
                    ResponseGroup = criteria.ResponseGroup
                };

                var searchChildrenResult = await RegularSearchMembersAsync(searchChildrenCriteria);

                var childOrganizations = searchChildrenResult.Results.OfType <Organization>().ToArray();

                var childResults = withoutOrganizations ? searchChildrenResult.Results.Where(x => orgMemberTypes.Contains(x.MemberType)).ToList() : searchChildrenResult.Results.ToList();

                var resultIds = result.Results.Select(x => x.Id).ToArray();
                childResults = childResults.Where(c => !resultIds.Contains(c.Id)).ToList();
                result.Results.AddRange(childResults);
                result.TotalCount += childResults.Count;

                if (!childOrganizations.IsNullOrEmpty())
                {
                    await LoadChildren(criteria, childOrganizations, withoutOrganizations, orgMemberTypes, result);
                }
            }
        }
        public GenericSearchResult <Member> SearchMembers(MembersSearchCriteria criteria)
        {
            var cacheKey = GetCacheKey("MemberSearchService.SearchMembers", criteria.GetCacheKey());
            var retVal   = _cacheManager.Get(cacheKey, RegionName, () => _memberSearchService.SearchMembers(criteria));

            return(retVal);
        }
        public async Task <bool> IsAuthorizedAsync(string identifier)
        {
            var criteria = new MembersSearchCriteria
            {
                SearchPhrase = $"botusername:{identifier}"
            };

            var result = await _customerModuleApi.SearchContactsAsync(criteria);

            var vcCustomer = result.Results.FirstOrDefault();

            if (vcCustomer != null)
            {
                _customer.Id      = vcCustomer.Id;
                _customer.StoreId = vcCustomer.SecurityAccounts.FirstOrDefault()?.StoreId;
                _customer.Name    = vcCustomer.Name;

                if (!string.IsNullOrEmpty(_customer.StoreId))
                {
                    var store = await _storeModule.GetStoreByIdAsync(_customer.StoreId);

                    _customer.Currency = store.DefaultCurrency;
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <ActionResult <ContactSearchResult> > SearchContacts([FromBody] MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = AbstractTypeFactory <MembersSearchCriteria> .TryCreateInstance();
            }

            criteria.MemberType  = typeof(Contact).Name;
            criteria.MemberTypes = new[] { criteria.MemberType };

            if (!(await AuthorizeAsync(criteria, ModuleConstants.Security.Permissions.Read)).Succeeded)
            {
                return(Unauthorized());
            }

            var searchResult = await _memberSearchService.SearchMembersAsync(criteria);

            var result = new ContactSearchResult
            {
                TotalCount = searchResult.TotalCount,
                Results    = searchResult.Results.OfType <Contact>().ToList()
            };

            return(Ok(result));
        }
        public async Task <ActionResult> BulkDeleteMembersBySearchCriteria([FromBody] MembersSearchCriteria criteria)
        {
            bool hasSearchCriteriaMembers;
            var  listIds = new List <string>();

            do
            {
                var searchResult = await _memberSearchService.SearchMembersAsync(criteria);

                hasSearchCriteriaMembers = searchResult.Results.Any();
                if (hasSearchCriteriaMembers)
                {
                    foreach (var member in searchResult.Results)
                    {
                        listIds.Add(member.Id);
                    }

                    criteria.Skip += criteria.Take;
                }
            }while (hasSearchCriteriaMembers);

            listIds.ProcessWithPaging(criteria.Take, async(ids, currentItem, totalCount) =>
            {
                await _memberService.DeleteAsync(ids.ToArray());
            });


            return(NoContent());
        }
        public override GenericSearchResult <Member> SearchMembers(MembersSearchCriteria criteria)
        {
            var retVal = base.SearchMembers(criteria);

            retVal.Results.ProcessSecurityAccounts(_securityService);
            return(retVal);
        }
Exemplo n.º 9
0
        public virtual Task <MemberSearchResult> SearchMembersAsync(MembersSearchCriteria criteria)
        {
            var result = !string.IsNullOrEmpty(criteria?.Keyword)
                ? IndexedSearchMembersAsync(criteria)
                : RegularSearchMembersAsync(criteria);

            return(result);
        }
        public virtual GenericSearchResult <Member> SearchMembers(MembersSearchCriteria criteria)
        {
            var result = !string.IsNullOrEmpty(criteria?.SearchPhrase)
                ? SearchIndex(criteria)
                : _memberSearchService.SearchMembers(criteria);

            return(result);
        }
Exemplo n.º 11
0
        public MembersSearchCriteriaBuilder()
        {
            Criteria = AbstractTypeFactory <MembersSearchCriteria> .TryCreateInstance();

            Criteria.DeepSearch    = true;
            Criteria.MemberType    = typeof(Contact).Name;
            Criteria.ResponseGroup = MemberResponseGroup.Default.ToString();
        }
Exemplo n.º 12
0
        public virtual Task <GenericSearchResult <Member> > SearchMembersAsync(MembersSearchCriteria criteria)
        {
            var result = !string.IsNullOrEmpty(criteria?.Keyword)
                ? SearchIndexAsync(criteria)
                : MemberSearchService.SearchMembersAsync(criteria);

            return(result);
        }
        public async Task <ActionResult <MemberSearchResult> > SearchMember([FromBody] MembersSearchCriteria criteria)
        {
            if (!(await AuthorizeAsync(criteria, ModuleConstants.Security.Permissions.Access)).Succeeded)
            {
                return(Unauthorized());
            }
            var result = await _memberSearchService.SearchMembersAsync(criteria);

            return(Ok(result));
        }
Exemplo n.º 14
0
        public IEnumerable <Contact> GetNewContacts()
        {
            var searchCrit = new MembersSearchCriteria
            {
                MemberType = "Contact",
                Take       = 100,
                Sort       = "CreatedDate:desc"
            };

            return(_memberSearchService.SearchMembers(searchCrit).Members.OfType <Contact>());
        }
        public virtual async Task <GenericSearchResult <Member> > SearchAsync(MembersSearchCriteria criteria)
        {
            var requestBuilder = GetRequestBuilder(criteria);
            var request        = await requestBuilder?.BuildRequestAsync(criteria);

            var response = await _searchProvider.SearchAsync(criteria.ObjectType, request);

            var result = await ConvertResponseAsync(response, criteria);

            return(result);
        }
        public IHttpActionResult ListOrganizations()
        {
            var searchCriteria = new MembersSearchCriteria
            {
                MemberType = typeof(Organization).Name,
                DeepSearch = true,
                Take       = int.MaxValue
            };
            var result = _memberSearchService.SearchMembers(searchCriteria);

            return(Ok(result.Results.OfType <Organization>()));
        }
        protected virtual ISearchRequestBuilder GetRequestBuilder(MembersSearchCriteria criteria)
        {
            var requestBuilder = _searchRequestBuilders?.FirstOrDefault(b => b.DocumentType.Equals(criteria.ObjectType)) ??
                                 _searchRequestBuilders?.FirstOrDefault(b => string.IsNullOrEmpty(b.DocumentType));

            if (requestBuilder == null)
            {
                throw new InvalidOperationException($"No query builders found for document type '{criteria.ObjectType}'");
            }

            return(requestBuilder);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Used to define extra where clause for members search
 /// </summary>
 /// <param name="criteria"></param>
 /// <returns></returns>
 protected virtual Expression <Func <MemberEntity, bool> > GetQueryPredicate(MembersSearchCriteria criteria)
 {
     if (!string.IsNullOrEmpty(criteria.Keyword))
     {
         var predicate = PredicateBuilder.False <MemberEntity>();
         predicate = predicate.Or(m => m.Name.Contains(criteria.Keyword) || m.Emails.Any(e => e.Address.Contains(criteria.Keyword)));
         //Should use Expand() to all predicates to prevent EF error
         //http://stackoverflow.com/questions/2947820/c-sharp-predicatebuilder-entities-the-parameter-f-was-not-bound-in-the-specif?rq=1
         return(LinqKit.Extensions.Expand(predicate));
     }
     return(PredicateBuilder.True <MemberEntity>());
 }
Exemplo n.º 19
0
        public async Task <ActionResult <Organization[]> > ListOrganizations()
        {
            var searchCriteria = new MembersSearchCriteria
            {
                MemberType = typeof(Organization).Name,
                DeepSearch = true,
                Take       = int.MaxValue
            };
            var result = await _memberSearchService.SearchMembersAsync(searchCriteria);

            return(Ok(result.Results.OfType <Organization>()));
        }
Exemplo n.º 20
0
        protected virtual IList <SortingField> GetSorting(MembersSearchCriteria criteria)
        {
            var result = new List <SortingField>();

            foreach (var sortInfo in criteria.SortInfos)
            {
                var fieldName    = sortInfo.SortColumn.ToLowerInvariant();
                var isDescending = sortInfo.SortDirection == SortDirection.Descending;
                result.Add(new SortingField(fieldName, isDescending));
            }

            return(result);
        }
        public async Task <MemberSearchResult> Handle(SearchOrganizationMembersQuery request, CancellationToken cancellationToken)
        {
            var criteria = new MembersSearchCriteria
            {
                MemberId     = request.OrganizationId,
                SearchPhrase = request.SearchPhrase,
                Skip         = request.Skip,
                Take         = request.Take,
                Sort         = request.Sort,
                MemberType   = nameof(Contact)
            };

            return(await _memberSearchService.SearchMembersAsync(criteria));
        }
Exemplo n.º 22
0
        public override async Task <MemberSearchResult> SearchMembersAsync(MembersSearchCriteria criteria)
        {
            var result = new MemberSearchResult();

            if (criteria == null || (!criteria.DeepSearch || (criteria.MemberId == null && criteria.ObjectIds.IsNullOrEmpty() && criteria.Keyword.IsNullOrEmpty())))
            {
                result = await base.SearchMembersAsync(criteria);
            }
            else
            {
                var orgSkip        = criteria.Skip;
                var orgTake        = criteria.Take;
                var orgMemberTypes = criteria.MemberTypes?.Select(x => x).ToArray();
                //do not use index searching if concrete members are selected
                if (!criteria.ObjectIds.IsNullOrEmpty())
                {
                    criteria.Keyword = null;
                }

                var withoutOrganizations = criteria.MemberTypes != null && !criteria.MemberTypes.Contains(OrganizationMemberType);

                criteria.Skip = 0;
                criteria.Take = criteria.Keyword.IsNullOrEmpty() ? int.MaxValue : ElasticMaxTake;

                if (withoutOrganizations)
                {
                    criteria.MemberTypes = criteria.MemberTypes.Union(new[] {
                        OrganizationMemberType
                    }).ToArray();
                }

                var firstResult = await base.SearchMembersAsync(criteria);

                var organizations = firstResult.Results.OfType <Organization>().ToArray();

                result.Results    = withoutOrganizations ? firstResult.Results.Where(x => orgMemberTypes.Contains(x.MemberType)).ToList() : firstResult.Results.ToList();
                result.TotalCount = result.Results.Count;

                if (!organizations.IsNullOrEmpty())
                {
                    await LoadChildren(criteria, organizations, withoutOrganizations, orgMemberTypes, result);
                }

                //skip take as firstly
                result.Results = result.Results.Skip(orgSkip).Take(orgTake).ToList();
            }

            return(result);
        }
Exemplo n.º 23
0
        protected virtual IList <SortInfo> BuildSortExpression(MembersSearchCriteria criteria)
        {
            var sortInfos = criteria.SortInfos;

            if (sortInfos.IsNullOrEmpty())
            {
                sortInfos = new[] {
                    new SortInfo {
                        SortColumn = nameof(Member.MemberType), SortDirection = SortDirection.Descending
                    },
                    new SortInfo {
                        SortColumn = nameof(Member.Name), SortDirection = SortDirection.Ascending
                    },
                };
            }
            return(sortInfos);
        }
        public async Task <ActionResult <Organization[]> > ListOrganizations()
        {
            var searchCriteria = new MembersSearchCriteria
            {
                MemberType = typeof(Organization).Name,
                DeepSearch = true,
                Take       = int.MaxValue
            };

            if (!(await AuthorizeAsync(searchCriteria, ModuleConstants.Security.Permissions.Access)).Succeeded)
            {
                return(Unauthorized());
            }
            var result = await _memberSearchService.SearchMembersAsync(searchCriteria);

            return(Ok(result.Results.OfType <Organization>()));
        }
Exemplo n.º 25
0
        /// <summary>
        /// Search members in database by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public virtual MembersSearchResult SearchMembers(MembersSearchCriteria criteria)
        {
            var retVal = new MembersSearchResult();

            using (var repository = RepositoryFactory())
            {
                var query = LinqKit.Extensions.AsExpandable(repository.Members);

                if (!criteria.MemberTypes.IsNullOrEmpty())
                {
                    query = query.Where(x => criteria.MemberTypes.Contains(x.MemberType) || x.MemberType == null);
                }

                if (criteria.MemberId != null)
                {
                    //TODO: DeepSearch in specified member
                    query = query.Where(x => x.MemberRelations.Any(y => y.AncestorId == criteria.MemberId));
                }
                else
                {
                    if (!criteria.DeepSearch)
                    {
                        query = query.Where(x => !x.MemberRelations.Any());
                    }
                }
                //Get extra predicates (where clause)
                var predicate = GetQueryPredicate(criteria);

                query = query.Where(LinqKit.Extensions.Expand(predicate));

                var sortInfos = criteria.SortInfos;
                if (sortInfos.IsNullOrEmpty())
                {
                    sortInfos = new[] { new SortInfo {
                                            SortColumn = ReflectionUtility.GetPropertyName <Member>(x => x.MemberType), SortDirection = SortDirection.Descending
                                        } };
                }
                query = query.OrderBySortInfos(sortInfos);

                retVal.TotalCount = query.Count();

                retVal.Members = query.Skip(criteria.Skip).Take(criteria.Take).ToArray()
                                 .Select(x => x.ToMember(MemberFactory.TryCreateMember(x.MemberType))).ToList();
                return(retVal);
            }
        }
        public async Task <ActionResult <VendorSearchResult> > SearchVendors([FromBody] MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new MembersSearchCriteria();
            }

            criteria.MemberType  = typeof(Vendor).Name;
            criteria.MemberTypes = new[] { criteria.MemberType };
            var searchResult = await _memberSearchService.SearchMembersAsync(criteria);

            var result = AbstractTypeFactory <VendorSearchResult> .TryCreateInstance();

            result.TotalCount = searchResult.TotalCount;
            result.Results    = searchResult.Results.OfType <Vendor>().ToList();

            return(Ok(result));
        }
        public IHttpActionResult SearchVendors(MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new MembersSearchCriteria();
            }

            criteria.MemberType = typeof(Vendor).Name;
            var searchResult = _memberSearchService.SearchMembers(criteria);

            var result = new VendorSearchResult
            {
                TotalCount = searchResult.TotalCount,
                Vendors    = searchResult.Results.OfType <Vendor>().ToList()
            };

            return(Ok(result));
        }
Exemplo n.º 28
0
        public async Task <ActionResult <GenericSearchResult <Organization> > > SearchOrganizations(MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new MembersSearchCriteria();
            }

            criteria.MemberType  = typeof(Organization).Name;
            criteria.MemberTypes = new[] { criteria.MemberType };
            var searchResult = await _memberSearchService.SearchMembersAsync(criteria);

            var result = new GenericSearchResult <Organization>
            {
                TotalCount = searchResult.TotalCount,
                Results    = searchResult.Results.OfType <Organization>().ToList()
            };

            return(Ok(result));
        }
Exemplo n.º 29
0
        public async Task <ActionResult <GenericSearchResult <Vendor> > > SearchVendors([FromBody] MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new MembersSearchCriteria();
            }

            criteria.MemberType  = typeof(Vendor).Name;
            criteria.MemberTypes = new[] { criteria.MemberType };
            var searchResult = await _memberSearchService.SearchMembersAsync(criteria);

            var result = new GenericSearchResult <Vendor>
            {
                TotalCount = searchResult.TotalCount,
                Results    = searchResult.Results.OfType <Vendor>().ToList()
            };

            return(Ok(result));
        }
        public IHttpActionResult SearchContacts(MembersSearchCriteria criteria)
        {
            if (criteria == null)
            {
                criteria = new MembersSearchCriteria();
            }

            criteria.MemberType  = typeof(Contact).Name;
            criteria.MemberTypes = new[] { criteria.MemberType };
            var searchResult = _memberSearchService.SearchMembers(criteria);

            var result = new GenericSearchResult <Contact>
            {
                TotalCount = searchResult.TotalCount,
                Results    = searchResult.Results.OfType <Contact>().ToList()
            };

            return(Ok(result));
        }