public EditContactViewModel(Company company)
        {
            if (company == null)
                throw new ArgumentNullException("company");

            Company = new CompanySummaryViewModel(company);
        }
        private void InitializeTestEntities()
        {
            _jobsearch1 = new JobSearch { Companies = new List<Company>() };
            _jobsearch2 = new JobSearch { Companies = new List<Company>() };
            _company1 = new Company { Contacts = new List<Contact>(), Tasks = new List<Task>(), JobSearch = _jobsearch1 };
            _company2 = new Company { Contacts = new List<Contact>(), Tasks = new List<Task>(), JobSearch = _jobsearch2 };
            _contact1 = new Contact { Tasks = new List<Task>(), Company = _company1 };
            _contact2 = new Contact { Tasks = new List<Task>(), Company = _company2 };

            _contactTask1 = new Task { Contact = _contact1, CompletionDate = null, TaskDate = new DateTime(2011, 1, 2, 5, 4, 5) };
            _contactTask2 = new Task { Contact = _contact2, CompletionDate = null, TaskDate = new DateTime(2011, 1, 2, 6, 4, 5) };
            _companyTask1 = new Task { Company = _company1, CompletionDate = null, TaskDate = new DateTime(2011, 1, 2, 1, 4, 5) };
            _companyTask2 = new Task { Company = _company2, CompletionDate = null, TaskDate = new DateTime(2011, 1, 2, 2, 4, 5) };
            _closedTask1 = new Task { Company = _company1, CompletionDate = DateTime.Now, TaskDate = new DateTime(2011, 1, 2, 3, 4, 5) };
            _closedTask2 = new Task { Company = _company2, CompletionDate = DateTime.Now, TaskDate = new DateTime(2011, 1, 2, 4, 4, 5) };

            _unitOfWork.JobSearches.Add(_jobsearch1);
            _unitOfWork.JobSearches.Add(_jobsearch2);
            _unitOfWork.Companies.Add(_company1);
            _unitOfWork.Companies.Add(_company2);
            _unitOfWork.Contacts.Add(_contact1);
            _unitOfWork.Contacts.Add(_contact2);
            _unitOfWork.Tasks.Add(_contactTask1);
            _unitOfWork.Tasks.Add(_contactTask2);
            _unitOfWork.Tasks.Add(_companyTask1);
            _unitOfWork.Tasks.Add(_companyTask2);
            _unitOfWork.Tasks.Add(_closedTask1);
            _unitOfWork.Tasks.Add(_closedTask2);
            _unitOfWork.Commit();
            
        }
        public void Position_Creation_Returns_Display_View_Model()
        {
            // Setup
            var positionAuthMock = new Mock<IProcess<PositionAuthorizationParams, AuthorizationResultViewModel>>();
            positionAuthMock.Setup(x => x.Execute(It.IsAny<PositionAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            var companyAuthMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            companyAuthMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            IProcess<CreatePositionParams, PositionDisplayViewModel> process = new PositionProcesses(_context, positionAuthMock.Object, companyAuthMock.Object);

            User user = new User();
            Company company = new Company();
            _context.Users.Add(user);
            _context.Companies.Add(company);
            _context.SaveChanges();

            // Act
            PositionDisplayViewModel result = process.Execute(new CreatePositionParams
            {
                CompanyId = company.Id,
                Title = "title",
                HasApplied = true,
                Notes = "Notes",
                LinkedInId = "ABC123",
                RequestingUserId = user.Id
            });

            // Verify
            Assert.AreEqual("title", result.Title, "Position had an incorrect title");
            Assert.IsTrue(result.HasApplied, "Position had an incorrect HasApplied value");
            Assert.AreEqual("Notes", result.Notes, "Position had an incorrect note");
            Assert.AreEqual("ABC123", result.LinkedInId, "Position had an incorrect LinkedIn id");
        }
        public void Index(Company company)
        {
            if (company == null)
                throw new ArgumentNullException("Cannot index a null company");

            // Create a document for the company
            var document = new Document();
            document.Add(new Field(Constants.COMPANY_ID, company.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field(Constants.COMPANY_NAME, company.Name ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_CITY, company.City ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_INDUSTRY, company.Industry ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_METRO, company.MetroArea ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_NOTES, company.Notes ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_PHONE, company.Phone ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_STATE, company.State ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.COMPANY_ZIP, company.Zip ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field(Constants.JOBSEARCH_ID, company.JobSearch.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            // Remove any previous documents for the company and add the new one
            Remove(company);

            var writer = OpenIndex();
            try
            {
                writer.AddDocument(document);
                writer.Optimize();
            }
            finally 
            {
                // Make sure the writer attempts to close even if an exception occurs, to prevent stale locks
                writer.Close(); 
            }
        }
        public void User_Not_Authorized_When_Contact_Doesnt_Belong_To_User_Jobsearch()
        {
            // Setup
            var user = new User();
            var user2 = new User();
            var search = new JobSearch { User = user };
            var company = new Company { JobSearch = search };
            var contact = new Contact { Company = company };

            _context.Contacts.Add(contact);
            _context.Users.Add(user2);
            _context.SaveChanges();

            IProcess<ContactAutorizationParams, AuthorizationResultViewModel> process = new AuthorizationProcesses(_context);

            // Act
            AuthorizationResultViewModel result = process.Execute(new ContactAutorizationParams
            {
                ContactId = contact.Id,
                RequestingUserId = user2.Id
            });

            // Verify
            Assert.IsNotNull(result, "Result was null");
            Assert.IsFalse(result.UserAuthorized, "User was incorrectly authorized");
        }
        private void InitializeTestEntities()
        {
            _changedDate = new DateTime(2011, 1, 2, 3, 4, 5);
            _startDate = new DateTime(2011, 2, 3, 4, 5, 6);

            _jobSearch = new JobSearch();
            Company company = new Company { JobSearch = _jobSearch };
            _user = new User();
            _contact = new Contact { Company = company };

            _task = new Task
            {
                Name = "Starting Name",
                CompletionDate = null,
                TaskDate = _startDate,
                Category = "Starting Category",
                Company = company,
                Notes = "Starting Notes",
                History = new List<TaskHistory>()
            };

            _unitOfWork.Users.Add(_user);
            _unitOfWork.Tasks.Add(_task);
            _unitOfWork.Contacts.Add(_contact);
            _unitOfWork.Commit();

            // Mocks
            _contactAuthMock = new Mock<IProcess<ContactAutorizationParams, AuthorizationResultViewModel>>();
            _contactAuthMock.Setup(x => x.Execute(It.IsAny<ContactAutorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            _taskAuthMock = new Mock<IProcess<TaskAuthorizationParams, AuthorizationResultViewModel>>();
            _taskAuthMock.Setup(x => x.Execute(It.IsAny<TaskAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            _serviceFactory = new Mock<IServiceFactory>();
            _serviceFactory.Setup(x => x.GetService<IUnitOfWork>()).Returns(_unitOfWork);

            _searchProvider = new Mock<ISearchProvider>();
            _serviceFactory.Setup(x => x.GetService<ISearchProvider>()).Returns(_searchProvider.Object);

            _userQuery = new Mock<UserByIdQuery>(_unitOfWork);
            _userQuery.Setup(x => x.Execute()).Returns(_user);
            _serviceFactory.Setup(x => x.GetService<UserByIdQuery>()).Returns(_userQuery.Object);

            _contactQuery = new Mock<ContactByIdQuery>(_unitOfWork, _contactAuthMock.Object);
            _contactQuery.Setup(x => x.Execute()).Returns(_contact);
            _serviceFactory.Setup(x => x.GetService<ContactByIdQuery>()).Returns(_contactQuery.Object);

            _taskQuery = new Mock<TaskByIdQuery>(_unitOfWork, _taskAuthMock.Object);
            _taskQuery.Setup(x => x.Execute()).Returns(_task);
            _serviceFactory.Setup(x => x.GetService<TaskByIdQuery>()).Returns(_taskQuery.Object);

            _updateMetricsCmd = new Mock<UpdateJobSearchMetricsCommand>(_serviceFactory.Object);
            _serviceFactory.Setup(x => x.GetService<UpdateJobSearchMetricsCommand>()).Returns(_updateMetricsCmd.Object);

            _validator = new Mock<IValidator<Task>>();
            _validator.Setup(x => x.Validate(It.IsAny<Task>())).Returns(new ValidationResult());
            _serviceFactory.Setup(x => x.GetService<IValidator<Task>>()).Returns(_validator.Object);
        }
        public void Execute_Returns_Null_Company_When_Id_Not_Found()
        {
            // Setup
            var authProcessMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            authProcessMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            Company company = new Company();
            _unitOfWork.Companies.Add(company);
            _unitOfWork.Commit();

            int id = company.Id + 1;

            // Act
            Company result = new CompanyByIdQuery(_unitOfWork, authProcessMock.Object).WithCompanyId(id).Execute();

            // Verify
            Assert.IsNull(result, "Query returned a non-null company");
        }
        public EditCompanyViewModel(Company company)
        {
            if (company == null)
                throw new ArgumentNullException("company");

            Id = company.Id;
            Name = company.Name;
            Phone = company.Phone;
            City = company.City;
            State = company.State;
            Zip = company.Zip;
            MetroArea = company.MetroArea;
            Industry = company.Industry;
            LeadStatus = company.LeadStatus;
            Notes = company.Notes;
            Website = company.Website;

            JobSearchId = Convert.ToInt32(company.JobSearchID);
            AvailableLeadStatuses = new List<string>();
        }
        public void Execute_Returns_Null_Company_When_User_Not_Authorized()
        {
            // Setup
            var authProcessMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            authProcessMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = false });

            Company company = new Company();
            _unitOfWork.Companies.Add(company);
            _unitOfWork.Commit();

            // Act
            Company result = new CompanyByIdQuery(_unitOfWork, authProcessMock.Object)
                                    .WithCompanyId(company.Id)
                                    .RequestedByUserId(23)
                                    .Execute();

            // Verify
            Assert.IsNull(result, "A non-null company was returned");
            authProcessMock.Verify(x => x.Execute(It.Is<CompanyQueryAuthorizationParams>(y => y.RequestingUserId == 23)));
        }
        public void Can_Index_Company()
        {
            // Setup
            Company company = new Company
            {
                Id = 3,
                City = "City",
                Industry = "Industry",
                MetroArea = "Metro",
                Name = "Name",
                Notes = "Notes",
                Phone = "Phone",
                State = "State",
                Zip = "Zip",

                JobSearch = new JobSearch { Id = 5 }
            };

            // Act
            _provider.Index(company);

            // Verify
            IndexSearcher searcher;
            TopDocs hits = SearchIndex(LuceneSearchProvider.Constants.COMPANY_ID, "3", out searcher);
            Assert.AreEqual(1, hits.scoreDocs.Length, "Incorrect number of documents returned");

            Document doc = searcher.Doc(hits.scoreDocs[0].doc);
            Assert.AreEqual("3", doc.Get(LuceneSearchProvider.Constants.COMPANY_ID), "Document had an incorrect company id value");
            Assert.AreEqual("City", doc.Get(LuceneSearchProvider.Constants.COMPANY_CITY), "Document had an incorrect company city value");
            Assert.AreEqual("Industry", doc.Get(LuceneSearchProvider.Constants.COMPANY_INDUSTRY), "Document had an incorrect company industry value");
            Assert.AreEqual("Metro", doc.Get(LuceneSearchProvider.Constants.COMPANY_METRO), "Document had an incorrect metro area value ");
            Assert.AreEqual("Name", doc.Get(LuceneSearchProvider.Constants.COMPANY_NAME), "Document had an incorrect name value");
            Assert.AreEqual("Notes", doc.Get(LuceneSearchProvider.Constants.COMPANY_NOTES), "Document had an incorrect notes value");
            Assert.AreEqual("Phone", doc.Get(LuceneSearchProvider.Constants.COMPANY_PHONE), "Document had an incorrect phone value");
            Assert.AreEqual("State", doc.Get(LuceneSearchProvider.Constants.COMPANY_STATE), "Document had an incorrect state value");
            Assert.AreEqual("Zip", doc.Get(LuceneSearchProvider.Constants.COMPANY_ZIP), "Document had an incorrect zip value");
            Assert.AreEqual("5", doc.Get(LuceneSearchProvider.Constants.JOBSEARCH_ID), "Document had an incorrect job search id value ");
        }
        public void Can_Retrieve_Company_By_Its_Id()
        {
            // Setup
            var authProcessMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            authProcessMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            Company company1 = new Company { Name = "Company 1" };
            Company company2 = new Company { Name = "Company 2" };
            Company company3 = new Company { Name = "Company 3" };

            _unitOfWork.Companies.Add(company1);
            _unitOfWork.Companies.Add(company2);
            _unitOfWork.Companies.Add(company3);

            _unitOfWork.Commit();

            // Act
            Company result = new CompanyByIdQuery(_unitOfWork, authProcessMock.Object).WithCompanyId(company2.Id).Execute();

            // Verify
            Assert.IsNotNull(result, "Returned company entity was null");
            Assert.AreEqual(company2.Id, result.Id, "The returned company had an incorrect id value");
            Assert.AreEqual(company2.Name, result.Name, "The returned company had an incorrect name value");
        }
        public void Search_Finds_Entity_With_Multiple_Search_Words_In_Separate_Fields()
        {
            // Setup 
            Company company = new Company { Id = 2, Name = "My Name", Notes = "is Andrew", JobSearch = new JobSearch { Id = 4 } };
            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrew Name", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
        public void InitializeTestEntities()
        {
            var search = new JobSearch { User = new User { FullName = "Full Name" } };
            _comp1 = new Company
            {
                Name = "company",
                Phone = "555-333-2323",
                State = "FL",
                Zip = "33445",
                City = "City",
                LeadStatus = "Status1",
                Notes = "notes 1",
                JobSearch = search
            };

            _comp2 = new Company
            {
                Name = "company2",
                Phone = "666-333-2323",
                State = "NY",
                Zip = "23456",
                City = "City2",
                LeadStatus = "Status2",
                Notes = "notes 2",
                JobSearch = search
            };

            _contact1 = new Contact
            {
                Company = _comp1,
                Name = "Contact 1",
                DirectPhone = "111-222-3333",
                Extension = "23",
                MobilePhone = "222-333-4444",
                Assistant = "Assistant 1",
                Email = "email 1",
                Notes = "notes 1",
                ReferredBy = "referred 1",
                Title = "title 1"
            };

            _contact2 = new Contact
            {
                Company = _comp2,
                Name = "Contact 2",
                DirectPhone = "666-222-3333",
                Extension = "25",
                MobilePhone = "777-333-4444",
                Assistant = "Assistant 2",
                Email = "email 2",
                Notes = "notes 2",
                ReferredBy = "referred 2",
                Title = "title 2"
            };

            _context.Contacts.Add(_contact1);
            _context.Contacts.Add(_contact2);
            _context.SaveChanges();

            // Create the export process class
            _jsQueryMock = new Mock<JobSearchByIdQuery>(_unitOfWork);
            _jsQueryMock.Setup(x => x.Execute()).Returns(search);
            _jsQueryMock.Setup(x => x.WithJobSearchId(It.IsAny<int>())).Returns(_jsQueryMock.Object);
            _process = new JobSearchExportProcess(_jsQueryMock.Object);
        }
        public void Search_Finds_Company_By_Notes()
        {
            // Setup 
            Company company = new Company { Id = 2, Notes = "This is my note", JobSearch = new JobSearch { Id = 4 } };
            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("note", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
        public void Search_Returns_No_Results_With_No_Match()
        {
            // Setup
            Company company = new Company { Id = 2, JobSearch = new JobSearch { Id = 5 } };
            Contact contact = new Contact { Id = 3, Company = new Company { JobSearch = new JobSearch { Id = 5 } } };
            Task task = new Task { Id = 4, Company = new Company { JobSearch = new JobSearch { Id = 5 } } };

            _provider.Index(company);
            _provider.Index(contact);
            _provider.Index(task);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Test", 5);

            // Verify
            Assert.IsNotNull(result, "Search provider result was null");
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Search provider result's found company list item count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Search provider result's found contact list item count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Search provider result's found task list item count was incorrect");
        }
        public void Search_Returns_No_Results_When_JobSearchId_Doesnt_Match()
        {
            // Setup
            Company company = new Company { Id = 2, Name = "Test", JobSearch = new JobSearch { Id = 5 } };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Test", 6);

            // Verify
            Assert.IsNotNull(result, "Search provider result was null");
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Search provider result's found company list item count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Search provider result's found contact list item count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Search provider result's found task list item count was incorrect");
        }
        public void ReIndexing_Company_Updates_Existing_Company_Document()
        {
            // Setup
            Company company = new Company { Id = 3, Name = "Name1", JobSearch = new JobSearch { Id = 5 } };
            _provider.Index(company);
            company.Name = "Name2";

            // Act
            _provider.Index(company);

            // Verify
            IndexSearcher searcher;
            TopDocs hits = SearchIndex(LuceneSearchProvider.Constants.COMPANY_ID, "3", out searcher);
            Assert.AreEqual(1, hits.scoreDocs.Length, "Incorrect number of documents returned");

            Document doc = searcher.Doc(hits.scoreDocs[0].doc);
            Assert.AreEqual("Name2", doc.Get(LuceneSearchProvider.Constants.COMPANY_NAME), "Document had an incorrect name");
        }
        public void Can_Remove_Company_From_Index()
        {
            // Setup
            Company company = new Company { Id = 3, JobSearch = new JobSearch { Id = 5 } };
            _provider.Index(company);

            // Act
            _provider.Remove(company);

            // Verify
            IndexSearcher searcher;
            TopDocs hits = SearchIndex(LuceneSearchProvider.Constants.COMPANY_ID, "3", out searcher);

            Assert.AreEqual(0, hits.scoreDocs.Length, "Incorrect number of documents returned");
        }
        public void Position_Gets_Created_With_History_Record()
        {
            // Setup
            var positionAuthMock = new Mock<IProcess<PositionAuthorizationParams, AuthorizationResultViewModel>>();
            positionAuthMock.Setup(x => x.Execute(It.IsAny<PositionAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            var companyAuthMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            companyAuthMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            IProcess<CreatePositionParams, PositionDisplayViewModel> process = new PositionProcesses(_context, positionAuthMock.Object, companyAuthMock.Object);

            User user = new User();
            Company company = new Company();
            _context.Users.Add(user);
            _context.Companies.Add(company);
            _context.SaveChanges();

            DateTime start, end;

            // Act
            start = DateTime.Now;
            process.Execute(new CreatePositionParams
            {
                CompanyId = company.Id,
                Title = "title",
                HasApplied = true,
                Notes = "Notes",
                LinkedInId = "ABC123",
                RequestingUserId = user.Id
            });
            end = DateTime.Now;

            // Verify
            var result = _context.Positions.Single().History.SingleOrDefault();
            Assert.IsNotNull(result, "No history record was created");
            Assert.AreEqual("title", result.Title, "History record's title was incorrect");
            Assert.AreEqual(true, result.HasApplied, "History record's applied value was incorrect");
            Assert.AreEqual("Notes", result.Notes, "History record's notes value was incorrect");
            Assert.AreEqual("ABC123", result.LinkedInId, "History record's linkedin id was incorrect");

            Assert.AreEqual(MJLConstants.HistoryInsert, result.HistoryAction, "History action was incorrect");
            Assert.AreEqual(user.Id, result.AuthoringUserId, "History record's authoring user id was incorrect");
            Assert.IsTrue(result.DateModified >= start && result.DateModified <= end, "History's date was incorrect");
        }
        public void Can_Index_Multiple_Companies()
        {
            // Setup
            var company1 = new Company { Id = 3, Name = "Name", JobSearch = new JobSearch { Id = 5 } };
            var company2 = new Company { Id = 4, Name = "Name", JobSearch = new JobSearch { Id = 5 } };

            // Act
            _provider.Index(company1);
            _provider.Index(company2);

            // Verify
            IndexSearcher searcher;
            TopDocs hits = SearchIndex(LuceneSearchProvider.Constants.COMPANY_NAME, "Name", out searcher);

            Assert.AreEqual(2, hits.scoreDocs.Length, "Incorrect number of documents returned");
        }
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <returns></returns>
        /// <exception cref="MJLEntityNotFoundException"></exception>
        public virtual Company Execute()
        {
            // Retrieve the user creating this company
            
            var user = _serviceFactory.GetService<UserByIdQuery>().WithUserId(_callingUserId).Execute();
            if (user == null)
                throw new MJLEntityNotFoundException(typeof(User), _callingUserId);

            // Retrieve the job search
            var search = _serviceFactory.GetService<JobSearchByIdQuery>().WithJobSearchId(_searchId).Execute();
            if (search == null)
                throw new MJLEntityNotFoundException(typeof(JobSearch), _searchId);

            // Create the company entity
            var company = new Company
            {
                JobSearch = search,
                Name = _name,
                Phone = _phone,
                City = _city,
                State = _state,
                Zip = _zip,
                MetroArea = _metro,
                Industry = _industry,
                Notes = _notes,

                Tasks = new List<Task>(),
                Contacts = new List<Contact>(),
                History = new List<CompanyHistory>()
            };

            // Create the history record
            company.History.Add(new CompanyHistory
            {
                Name = _name,
                Phone = _phone,
                City = _city,
                State = _state,
                Zip = _zip,
                MetroArea = _metro,
                Industry = _industry,
                Notes = _notes,

                AuthoringUser = user,
                DateModified = DateTime.Now,
                HistoryAction = MJLConstants.HistoryInsert
            });

            var unitOfWork = _serviceFactory.GetService<IUnitOfWork>();
            unitOfWork.Companies.Add(company);
            unitOfWork.Commit();

            // Index this new company for searching
            _serviceFactory.GetService<ISearchProvider>().Index(company);

            // Update the job search metrics
            var updateCommand = _serviceFactory.GetService<UpdateJobSearchMetricsCommand>();
            updateCommand.Execute(new UpdateJobSearchMetricsCmdParams { JobSearchId = search.Id });

            return company;
        }
Example #22
0
 protected void CreateCompanyContactList(int selectedContactId, Company company, EditTaskViewModel model)
 {
     model.CompanyContactList = _serviceFactory.GetService<ContactsByCompanyIdQuery>().WithCompanyId(company.Id)
                                                                                     .RequestedByUserId(CurrentUserId)
                                                                                     .Execute()
                                                                                     .Select(x => new SelectListItem
                                                                                     {
                                                                                         Text = x.Name,
                                                                                         Value = x.Id.ToString(),
                                                                                         Selected = x.Id == selectedContactId
                                                                                     })
                                                                                     .ToList();
     model.CompanyContactList.Insert(0, new SelectListItem { Text = "---------------------------", Value = "-1", Selected = false });
     model.CompanyContactList.Insert(0, new SelectListItem { Text = "Company: " + company.Name, Value = "0", Selected = selectedContactId == 0 });
 }
        public void Indexed_Companys_Null_Properties_Become_Empty_Strings()
        {
            // Setup
            Company company = new Company { Id = 3, JobSearch = new JobSearch { Id = 5 } };

            // Act
            _provider.Index(company);

            // Verify
            IndexSearcher searcher;
            TopDocs hits = SearchIndex(LuceneSearchProvider.Constants.COMPANY_ID, "3", out searcher);
            Assert.AreEqual(1, hits.scoreDocs.Length, "Incorrect number of documents returned");

            Document doc = searcher.Doc(hits.scoreDocs[0].doc);
            Assert.AreEqual("3", doc.Get(LuceneSearchProvider.Constants.COMPANY_ID), "Document had an incorrect company id value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_CITY), "Document had an incorrect company city value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_INDUSTRY), "Document had an incorrect company industry value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_METRO), "Document had an incorrect metro area value ");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_NAME), "Document had an incorrect name value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_NOTES), "Document had an incorrect notes value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_PHONE), "Document had an incorrect phone value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_STATE), "Document had an incorrect state value");
            Assert.AreEqual(string.Empty, doc.Get(LuceneSearchProvider.Constants.COMPANY_ZIP), "Document had an incorrect zip value");
        }
        public void Search_Doesnt_Find_Entity_When_All_Words_Not_Matched()
        {
            // Setup 
            Company company = new Company { Id = 2, Name = "My Name", JobSearch = new JobSearch { Id = 4 } };
            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrew Name", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
        public void Remove(Company company)
        {
            if (company == null)
                throw new ArgumentNullException("Cannot remove the index for a null company");

            // Delete all documents that have a company id matching the passed in company
            var writer = OpenIndex();
            writer.DeleteDocuments(new Term(Constants.COMPANY_ID, company.Id.ToString()));
            writer.Optimize();
            writer.Close();
        }
        public void Search_Allows_For_Fuzzy_Searching()
        {
            // Setup 
            Company company = new Company { Id = 2, Name = "My Name is Andrew", JobSearch = new JobSearch { Id = 4 } };
            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrw Nme", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
        public void Process_Throws_EntityNotFoundException_When_Company_Not_Found()
        {
            // Setup
            var positionAuthMock = new Mock<IProcess<PositionAuthorizationParams, AuthorizationResultViewModel>>();
            positionAuthMock.Setup(x => x.Execute(It.IsAny<PositionAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            var companyAuthMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            companyAuthMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            IProcess<CreatePositionParams, PositionDisplayViewModel> process = new PositionProcesses(_context, positionAuthMock.Object, companyAuthMock.Object);
            Company company = new Company();
            _context.Companies.Add(company);
            _context.SaveChanges();
            int id = company.Id + 1;

            // Act
            try
            {
                process.Execute(new CreatePositionParams { CompanyId = id });
                Assert.Fail("No exception was thrown");
            }

            // Verify
            catch (MJLEntityNotFoundException ex)
            {
                Assert.AreEqual(typeof(Company), ex.EntityType, "MJLEntityNotFoundException's entity type was incorrect");
                Assert.AreEqual(id.ToString(), ex.IdValue, "MJLEntityNotFoundException's id value was incorrect");
            }
        }
Example #28
0
 public EditTaskViewModel(Company company) 
 {
     Company = new CompanySummaryViewModel(company);
     AssociatedCompanyId = company.Id;
     AvailableCategoryList = new List<string>();
 }
        public void Process_Throws_UserNotAuthorizedForEntityException_When_User_Not_Authorized_For_Company()
        {
            // Setup
            var positionAuthMock = new Mock<IProcess<PositionAuthorizationParams, AuthorizationResultViewModel>>();
            positionAuthMock.Setup(x => x.Execute(It.IsAny<PositionAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = true });

            var companyAuthMock = new Mock<IProcess<CompanyQueryAuthorizationParams, AuthorizationResultViewModel>>();
            companyAuthMock.Setup(x => x.Execute(It.IsAny<CompanyQueryAuthorizationParams>())).Returns(new AuthorizationResultViewModel { UserAuthorized = false });

            IProcess<CreatePositionParams, PositionDisplayViewModel> process = new PositionProcesses(_context, positionAuthMock.Object, companyAuthMock.Object);
            Company company = new Company();
            _context.Companies.Add(company);
            _context.SaveChanges();

            // Act
            try
            {
                process.Execute(new CreatePositionParams
                {
                    CompanyId = company.Id,
                    Title = "title",
                    HasApplied = true,
                    Notes = "Notes",
                    RequestingUserId = 15
                });

                Assert.Fail("No exception was thrown");
            }

            // Verify
            catch (UserNotAuthorizedForEntityException ex)
            {
                Assert.AreEqual(typeof(Company), ex.EntityType, "Exception's entity type was incorrect");
                Assert.AreEqual(company.Id, ex.IdValue, "Exception's id value was incorrect");
                Assert.AreEqual(15, ex.UserId, "Exception's user id value was incorrect");
                companyAuthMock.Verify(x => x.Execute(It.Is<CompanyQueryAuthorizationParams>(y => y.RequestingUserId == 15 && y.CompanyId == company.Id)));
            }
        }