public virtual ActionResult Sync(int companyId, int jigsawId)
        {
            var jigsawData = _companyDetailsProcess.Execute(new JigsawCompanyDetailsParams {
                RequestingUserId = CurrentUserId, JigsawId = jigsawId
            });
            var company = _companyByIdQuery.RequestedByUserId(CurrentUserId).WithCompanyId(companyId).Execute();

            if (company == null)
            {
                throw new InvalidOperationException("Cannot sync jigsaw data with a null company");
            }

            var model = Mapper.Map <Company, SyncCompanyViewModel>(company);

            model = Mapper.Map <ExternalCompanyDetailsViewModel, SyncCompanyViewModel>(jigsawData, model);
            return(View(model));
        }
        /// <summary>
        /// Adds a jigsaw contact to the user's job search
        /// </summary>
        /// <param name="procParams"></param>
        /// <returns></returns>
        public ExternalContactAddedResultViewModel Execute(AddJigsawContactToJobSearchParams procParams)
        {
            bool jigsawContactOwned = true;
            var  credentials        = JigsawAuthProcesses.GetMyLeadsAccountCredentials();

            var user = _context.Users
                       .Where(x => x.Id == procParams.RequestingUserId)
                       .Include(x => x.LastVisitedJobSearch)
                       .FirstOrDefault();

            if (user == null)
            {
                throw new MJLEntityNotFoundException(typeof(User), procParams.RequestingUserId);
            }

            // If we are using an existing company, make sure it exists in the user's job search
            if (!procParams.CreateCompanyFromJigsaw)
            {
                if (!_context.Companies.Any(x => x.Id == procParams.ExistingCompanyId && x.JobSearch.Id == user.LastVisitedJobSearchId))
                {
                    throw new MJLEntityNotFoundException(typeof(Company), procParams.ExistingCompanyId);
                }
            }

            // Perform the API query to get the jigsaw contact only if the user has access to the contact
            var    client     = new RestClient("https://www.jigsaw.com/");
            string contactUrl = string.Format("rest/contacts/{0}.json", procParams.JigsawContactId);
            var    request    = new RestRequest(contactUrl, Method.GET);

            request.AddParameter("token", JigsawAuthProcesses.GetAuthToken());
            request.AddParameter("username", credentials.JigsawUsername);
            request.AddParameter("password", credentials.JigsawPassword);
            request.AddParameter("purchaseFlag", false);
            var response = client.Execute(request);

            try
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    JigsawAuthProcesses.ThrowInvalidResponse(response.Content, procParams.RequestingUserId, procParams.JigsawContactId);
                }
            }
            catch (JigsawContactNotOwnedException)
            {
                jigsawContactOwned = false;
            }

            // If the user owns the contact, update the info from Jigsaw
            if (jigsawContactOwned)
            {
                // Convert the json string into an object and evaluate it
                var json = JsonConvert.DeserializeObject <ContactDetailsResponseJson>(response.Content);
                if (json.Unrecognized.ContactId.Count > 0)
                {
                    throw new JigsawContactNotFoundException(procParams.JigsawContactId);
                }

                procParams.Name  = json.Contacts[0].FirstName + " " + json.Contacts[0].LastName;
                procParams.Title = json.Contacts[0].Title;
                procParams.Phone = json.Contacts[0].Phone;
                procParams.Email = json.Contacts[0].Email;
            }

            // Use a transaction to prevent creating a company without the contact
            using (var transaction = new TransactionScope())
            {
                // Retrieve the company from the database, or get the information to create a company from Jigsaw
                Company company;
                if (procParams.CreateCompanyFromJigsaw)
                {
                    // Create the company
                    var jsComp = JigsawCompanySearchProcesses.GetJigsawCompany(Convert.ToInt32(procParams.JigsawCompanyId), procParams.RequestingUserId);
                    company = _createCompanyCmd.CalledByUserId(procParams.RequestingUserId)
                              .WithJobSearch((int)user.LastVisitedJobSearchId)
                              .SetName(jsComp.name)
                              .SetIndustry(jsComp.industry1)
                              .SetCity(jsComp.city)
                              .SetState(jsComp.state)
                              .SetZip(jsComp.zip)
                              .SetPhone(jsComp.phone)
                              .SetJigsawId(Convert.ToInt32(jsComp.companyId))
                              .Execute();
                }

                // Retrieve the company from the database
                else
                {
                    // Already verified the company exists in the database, so no need for null checking
                    company = _companyByIdQuery.RequestedByUserId(procParams.RequestingUserId)
                              .WithCompanyId(procParams.ExistingCompanyId)
                              .Execute();
                }

                // Create the contact
                var contact = _createContactCmd.RequestedByUserId(procParams.RequestingUserId)
                              .WithCompanyId(company.Id)
                              .SetName(procParams.Name)
                              .SetTitle(procParams.Title)
                              .SetDirectPhone(procParams.Phone)
                              .SetEmail(procParams.Email)
                              .SetJigsawId(Convert.ToInt32(procParams.JigsawContactId))
                              .SetHasJigsawAccess(jigsawContactOwned)
                              .Execute();

                transaction.Complete();

                return(new ExternalContactAddedResultViewModel
                {
                    CompanyId = company.Id,
                    CompanyName = company.Name,
                    ContactId = contact.Id,
                    ContactName = contact.Name
                });
            }
        }
Exemple #3
0
        /// <summary>
        /// Merges the specified data from a jigsaw company to an existing MyLeads company
        /// </summary>
        /// <param name="procParams"></param>
        /// <returns></returns>
        public GeneralSuccessResultViewModel Execute(MergeJigsawCompanyParams procParams)
        {
            var company = _companyByIdQuery.RequestedByUserId(procParams.RequestingUserId).WithCompanyId(procParams.MyLeadsCompayId).Execute();

            if (company == null)
            {
                throw new MJLEntityNotFoundException(typeof(Company), procParams.MyLeadsCompayId);
            }

            var json = GetJigsawCompany(procParams.JigsawCompanyId, procParams.RequestingUserId);

            _editCompanyCmd.WithCompanyId(procParams.MyLeadsCompayId);
            _editCompanyCmd.RequestedByUserId(procParams.RequestingUserId);

            // Update the requested data
            if (procParams.MergeAddress)
            {
                _editCompanyCmd.SetCity(json.city);
                _editCompanyCmd.SetState(json.state);
                _editCompanyCmd.SetZip(json.zip);
            }

            if (procParams.MergeIndustry)
            {
                // Concat all the industries together
                string industries = string.Format("{0}: {1}, {2}: {3}, {4}: {5}",
                                                  json.industry1, json.subIndustry1, json.industry2, json.subIndustry2, json.industry3, json.subIndustry3);
                _editCompanyCmd.SetIndustry(industries);
            }

            if (procParams.MergeName)
            {
                _editCompanyCmd.SetName(json.name);
            }

            if (procParams.MergePhone)
            {
                _editCompanyCmd.SetPhone(json.phone);
            }

            // The rest of the merge fields are prepended into the company notes
            string notes = string.Empty;

            if (procParams.MergeWebsite)
            {
                _editCompanyCmd.SetWebsite(json.website);
            }

            if (procParams.MergeOwnership)
            {
                notes += string.Concat("Ownership: ", json.ownership, Environment.NewLine);
            }

            if (procParams.MergeRevenue)
            {
                notes += string.Concat("Revenue: ", string.Format("${0:n0}", json.revenue), Environment.NewLine);
            }

            if (procParams.MergeEmployeeCount)
            {
                notes += string.Concat("Employees: ", string.Format("{0:n0}", json.employeeCount), Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(notes))
            {
                notes += Environment.NewLine;
            }

            _editCompanyCmd.SetNotes(notes + company.Notes);

            // Save the changes
            _editCompanyCmd.Execute();
            return(new GeneralSuccessResultViewModel {
                WasSuccessful = true
            });
        }