Example #1
0
        public SearchOpportunityResponse GetAllOpportunities(SearchOpportunityRequest request)
        {
            SearchOpportunityResponse response = new SearchOpportunityResponse();

            IEnumerable <Opportunity>          opportunities   = opportunityRepository.GetOpportunitiesWithBuyersList(request.AccountId, request.PageNumber, request.Limit, request.Query, request.SortField, request.UserIDs, request.StartDate, request.EndDate, request.SortDirection);
            IEnumerable <OpportunityViewModel> opportunityList = MapDomainToVM(opportunities);

            response.Opportunities = opportunityList;
            response.TotalHits     = opportunityList.IsAny() ? opportunityList.Select(t => t.TotalCount).FirstOrDefault() : 0;
            return(response);
        }
Example #2
0
        SearchOpportunityResponse search(SearchOpportunityRequest request, IEnumerable <Type> types, IList <string> fields, bool matchAll, bool autoComplete)
        {
            SearchOpportunityResponse response   = new SearchOpportunityResponse();
            SearchParameters          parameters = new SearchParameters();

            parameters.Limit      = request.Limit;
            parameters.PageNumber = request.PageNumber == 0 ? 1 : request.PageNumber;
            parameters.Types      = types;
            parameters.MatchAll   = matchAll;
            parameters.AccountId  = request.AccountId;
            parameters.Ids        = request.OpportunityIDs;
            parameters.UserID     = request.UserID;
            parameters.StartDate  = request.StartDate;
            parameters.EndDate    = request.EndDate;

            if (request.SortField != null)
            {
                List <string> sortFields = new List <string>();
                var           maps       = SmartTouch.CRM.ApplicationServices.ObjectMappers.MapperConfigurationProvider.Instance.FindTypeMapFor <OpportunityViewModel, Opportunity>();

                foreach (var propertyMap in maps.GetPropertyMaps())
                {
                    if (request.SortField.Equals(propertyMap.SourceMember.Name))
                    {
                        sortFields.Add(propertyMap.DestinationProperty.MemberInfo.Name);
                        break;
                    }
                }
                parameters.SortDirection = request.SortDirection;
                parameters.SortFields    = sortFields;
            }
            Logger.Current.Informational("Search string:" + request.Query);
            Logger.Current.Informational("Parameters:" + parameters.ToString());
            SearchResult <Opportunity> searchResult;

            bool isAccountAdmin = cachingService.IsAccountAdmin(request.RoleId, request.AccountId);
            bool isPrivate      = cachingService.IsModulePrivate(AppModules.Opportunity, request.AccountId);

            if (isPrivate && !isAccountAdmin)
            {
                int userId = (int)request.RequestedBy;
                searchResult = searchService.Search(request.Query, c => c.OwnerId == userId, parameters);
            }
            else if (request.UserID != null && request.StartDate != null && request.EndDate != null)
            {
                int userId = (int)request.UserID;
                searchResult = searchService.Search(request.Query, c => c.CreatedBy == userId, parameters);
            }
            else
            {
                searchResult = searchService.Search(request.Query, parameters);
            }

            IEnumerable <Opportunity> opportunities = searchResult.Results;

            Logger.Current.Informational("Search complete, total results:" + searchResult.Results.Count());

            if (opportunities == null)
            {
                response.Exception = GetOpportunitiesNotFoundException();
            }
            else
            {
                IEnumerable <OpportunityViewModel> list = Mapper.Map <IEnumerable <Opportunity>, IEnumerable <OpportunityViewModel> >(opportunities);

                IEnumerable <int>     contactIds      = searchResult.Results.SelectMany(o => o.Contacts).Distinct().ToList();
                SearchContactsRequest contactsRequest = new SearchContactsRequest();
                contactsRequest.ContactIDs  = contactIds.ToArray();
                contactsRequest.AccountId   = request.AccountId;
                contactsRequest.RoleId      = request.RoleId;
                contactsRequest.RequestedBy = request.RequestedBy;
                contactsRequest.Limit       = contactIds.Count();
                contactsRequest.PageNumber  = 1;
                contactsRequest.Module      = AppModules.Opportunity;
                SearchContactsResponse <ContactEntry> contactsResponse = contactService.GetAllContacts <ContactEntry>(contactsRequest);
                foreach (Opportunity opportunity in searchResult.Results)
                {
                    var opportunityViewModel = list.SingleOrDefault(o => o.OpportunityID == opportunity.Id);

                    IEnumerable <int> people = opportunity.Contacts;

                    //if (isPrivate && !isAccountAdmin)
                    //{
                    //    IList<int> owncontacts = contactRepository.GetContactByUserId((int)request.RequestedBy);

                    //    var peopleinvoled = opportunity.PeopleInvolved.Where(n => !owncontacts.Contains(n.ContactID));

                    //    foreach (var p in peopleinvoled)
                    //    {
                    //        p.IsPrivate = true;
                    //    }
                    //}

                    var contacts = contactsResponse.Contacts.Where(c => people.Contains(c.Id));
                    opportunityViewModel.Contacts = contacts;
                }


                response.Opportunities = list;
                response.TotalHits     = searchResult.TotalHits;
            }
            return(response);
        }