/// <summary> /// Adds a new company with data from the specified jigsaw company /// </summary> /// <param name="procParams"></param> /// <returns></returns> public CompanyAddedViewResult Execute(AddJigsawCompanyParams procParams) { var user = _context.Users .Where(x => x.Id == procParams.RequestingUserId) .Include(x => x.LastVisitedJobSearch) .SingleOrDefault(); if (user == null) { throw new MJLEntityNotFoundException(typeof(User), procParams.RequestingUserId); } // Get the jigsaw details and create the company with those details var json = GetJigsawCompany(procParams.JigsawId, procParams.RequestingUserId); // 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); // Form the notes string notes = string.Format("Ownership: {1}{0}Revenue: {2}{0}Employees: {3}", Environment.NewLine, json.ownership, string.Format("${0:n0}", json.revenue), string.Format("{0:n0}", json.employeeCount)); var company = _createCompanyCmd.CalledByUserId(procParams.RequestingUserId) .WithJobSearch((int)user.LastVisitedJobSearchId) .SetName(json.name) .SetPhone(json.phone) .SetCity(json.city) .SetState(json.state) .SetZip(json.zip) .SetIndustry(industries) .SetNotes(notes) .SetWebsite(json.website) .SetJigsawId(Convert.ToInt32(json.companyId)) .Execute(); return(new CompanyAddedViewResult { CompanyId = company.Id, CompanyName = company.Name }); }
/// <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 }); } }