public override void Process()
        {
            leadAdapterAndAccountMap = leadAdaptersRepository.GetLeadAdapterByID(LeadAdapterAccountMapID);

            ServiceProvider ServiceProviders = serviceProviderRepository
                                               .GetServiceProviders(1, CommunicationType.Mail, MailType.TransactionalEmail);

            var response = customFieldService.GetAllCustomFields(new GetAllCustomFieldsRequest(leadAdapterAndAccountMap.AccountID));
            IEnumerable <FieldViewModel> customFields = response.CustomFields;
            var dropdownfeildsresposne = cacheService.GetDropdownValues(leadAdapterAndAccountMap.AccountID);

            IEnumerable <DropdownValueViewModel> phoneFields = dropdownfeildsresposne.Where(x => x.DropdownID == (short)DropdownFieldTypes.PhoneNumberType).Select(x => x.DropdownValuesList).FirstOrDefault();

            var jobLogId = InsertJobLog(leadAdapterAndAccountMap);
            var contacts = this.GetContacts("lead_feed.xml", customFields, jobLogId, phoneFields);

            if (contacts.ContactData != null && contacts.ContactData.Any())
            {
                Logger.Current.Informational("Got contacts for inserting : " + contacts.ContactData.Count);
                /* Bulk Insert */
                Task.Factory.StartNew(() => { MailgunVerification(contacts.ContactData.ToList()); }, TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(() => { ContactsBulkinsert(contacts); }, TaskCreationOptions.LongRunning);
            }
            else
            {
                Logger.Current.Informational("No contacts for inserting, Account name : " + leadAdapterAndAccountMap.AccountName);
            }
        }
Exemple #2
0
        private IEnumerable <FieldViewModel> GetCustomFields()
        {
            var response = customFieldService.GetAllCustomFields(new GetAllCustomFieldsRequest(leadAdapterAndAccountMap.AccountID));
            IEnumerable <FieldViewModel> customFields = response.CustomFields;

            return(customFields);
        }
        private PersonViewModel GetUpdatedPersonData(PersonViewModel existingModel, PersonViewModel newModel)
        {
            Person person         = new Person();
            var    dropdownValues = _cachingService.GetDropdownValues(newModel.AccountID);

            existingModel.FirstName   = !string.IsNullOrEmpty(newModel.FirstName) ? newModel.FirstName : existingModel.FirstName;
            existingModel.LastName    = !string.IsNullOrEmpty(newModel.LastName) ? newModel.LastName : existingModel.LastName;
            existingModel.CompanyName = !string.IsNullOrEmpty(newModel.CompanyName) ? newModel.CompanyName : existingModel.CompanyName;
            existingModel.Title       = !string.IsNullOrEmpty(newModel.Title) ? newModel.Title : existingModel.Title;

            List <short> phoneTypeIds        = newModel.Phones.IsAny() ? newModel.Phones.Select(p => p.PhoneType).ToList() : new List <short>();
            List <short> dropdownValueTyeIds = new List <short>();

            if (phoneTypeIds.IsAny())
            {
                dropdownValueTyeIds = _formRepository.GetDropdownValueTypeIdsByPhoneTypes(phoneTypeIds, newModel.AccountID);
            }

            var mobilePhoneNumber = dropdownValueTyeIds.Contains((short)DropdownValueTypes.MobilePhone) ? newModel.Phones.Select(m => m.Number).FirstOrDefault() : null;
            var homePhoneNumber   = dropdownValueTyeIds.Contains((short)DropdownValueTypes.Homephone) ? newModel.Phones.Select(m => m.Number).FirstOrDefault() : null;
            var workPhoneNumber   = dropdownValueTyeIds.Contains((short)DropdownValueTypes.WorkPhone) ? newModel.Phones.Select(m => m.Number).FirstOrDefault() : null;

            var phoneTypes = dropdownValues.Where(s => s.DropdownID == (byte)DropdownFieldTypes.PhoneNumberType)
                             .Select(s => s.DropdownValuesList).FirstOrDefault().Where(d => d.IsActive == true);


            existingModel.Phones = new List <Phone>();

            Log.Informational("While Updating phones.");

            IEnumerable <Phone> existingPhones = _formRepository.GetPhoneFields(existingModel.ContactID);

            Phone mobilePhone = existingPhones.Where(w => w.DropdownValueTypeID == (short)DropdownValueTypes.MobilePhone).FirstOrDefault();

            if (!string.IsNullOrEmpty(mobilePhoneNumber) && !(mobilePhoneNumber.Length < 10 || mobilePhoneNumber.Length > 15))
            {
                string number = mobilePhoneNumber.TrimStart(new char[] { '0', '1' });
                Phone  phone  = new Phone();
                phone.Number    = number;
                phone.AccountID = existingModel.AccountID;
                phone.IsPrimary = mobilePhone != null ? mobilePhone.IsPrimary : (homePhoneNumber == null && workPhoneNumber == null && !existingPhones.IsAny()) ||
                                  phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.MobilePhone).Select(c => c.IsDefault).FirstOrDefault();
                phone.PhoneType     = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.MobilePhone).Select(c => c.DropdownValueID).FirstOrDefault();
                phone.PhoneTypeName = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.MobilePhone).Select(c => c.DropdownValue).FirstOrDefault();
                if (person.IsValidPhoneNumberLength(number))
                {
                    existingModel.Phones.Add(phone);
                }
            }
            else if (mobilePhone != null)
            {
                existingModel.Phones.Add(mobilePhone);
            }

            Phone homePhone = existingPhones.Where(w => w.DropdownValueTypeID == (short)DropdownValueTypes.Homephone).FirstOrDefault();

            if (!string.IsNullOrEmpty(homePhoneNumber) && !(homePhoneNumber.Length < 10 || homePhoneNumber.Length > 15))
            {
                string number = homePhoneNumber.TrimStart(new char[] { '0', '1' });
                Phone  phone  = new Phone();
                phone.Number    = number;
                phone.AccountID = existingModel.AccountID;
                phone.IsPrimary = homePhone != null ? homePhone.IsPrimary : (mobilePhoneNumber == null && workPhoneNumber == null && !existingPhones.IsAny()) ||
                                  phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.Homephone).Select(c => c.IsDefault).FirstOrDefault();
                phone.PhoneType     = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.Homephone).Select(c => c.DropdownValueID).FirstOrDefault();
                phone.PhoneTypeName = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.Homephone).Select(c => c.DropdownValue).FirstOrDefault();
                if (person.IsValidPhoneNumberLength(number))
                {
                    existingModel.Phones.Add(phone);
                }
            }
            else if (homePhone != null)
            {
                existingModel.Phones.Add(homePhone);
            }

            Phone workPhone = existingPhones.Where(w => w.DropdownValueTypeID == (short)DropdownValueTypes.WorkPhone).FirstOrDefault();

            if (!string.IsNullOrEmpty(workPhoneNumber) && !(workPhoneNumber.Length < 10 || workPhoneNumber.Length > 15))
            {
                string number = workPhoneNumber.TrimStart(new char[] { '0', '1' });
                Phone  phone  = new Phone();
                phone.Number    = number;
                phone.AccountID = existingModel.AccountID;
                phone.IsPrimary = workPhone != null ? workPhone.IsPrimary : (mobilePhoneNumber == null && homePhoneNumber == null && !existingPhones.IsAny()) ||
                                  phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.WorkPhone).Select(c => c.IsDefault).FirstOrDefault();
                phone.PhoneType     = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.WorkPhone).Select(c => c.DropdownValueID).FirstOrDefault();
                phone.PhoneTypeName = phoneTypes.Where(c => c.DropdownValueTypeID == (short)DropdownValueTypes.WorkPhone).Select(c => c.DropdownValue).FirstOrDefault();
                if (person.IsValidPhoneNumberLength(number))
                {
                    existingModel.Phones.Add(phone);
                }
            }
            else if (workPhone != null)
            {
                existingModel.Phones.Add(workPhone);
            }

            IEnumerable <Phone> existingNonDefaultPhones = existingPhones.Where(w => w.DropdownValueTypeID != 9 && w.DropdownValueTypeID != 10 && w.DropdownValueTypeID != 11 && !w.IsDeleted);

            if (existingNonDefaultPhones.IsAny())
            {
                existingNonDefaultPhones.Each(e =>
                {
                    if (phoneTypeIds.IsAny())
                    {
                        if (phoneTypeIds.Contains(e.PhoneType))
                        {
                            var nonDefaultPhone = newModel.Phones.Where(p => p.PhoneType == e.PhoneType).FirstOrDefault();//workPhoneNumber.TrimStart(new char[] { '0', '1' });
                            string number       = nonDefaultPhone.Number.TrimStart(new char[] { '0', '1' });
                            if (e.Number != number)
                            {
                                e.IsPrimary         = false;
                                Phone phone         = new Phone();
                                phone.Number        = number;
                                phone.AccountID     = existingModel.AccountID;
                                phone.IsPrimary     = true;
                                phone.PhoneType     = nonDefaultPhone.PhoneType;
                                phone.PhoneTypeName = nonDefaultPhone.PhoneTypeName;
                                if (person.IsValidPhoneNumberLength(number.TrimStart(new char[] { '0', '1' })))
                                {
                                    existingModel.Phones.Add(phone);
                                }
                            }
                        }
                    }
                    existingModel.Phones.Add(e);
                });
            }

            Log.Informational("While Updating Addresses.");
            existingModel.Addresses = new List <AddressViewModel>();
            var addressLine1 = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.AddressLine1).FirstOrDefault() : null;
            var addressLine2 = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.AddressLine2).FirstOrDefault() : null;
            var city         = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.City).FirstOrDefault() : null;
            var state        = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.State).FirstOrDefault() : null;
            var zip          = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.ZipCode).FirstOrDefault() : null;
            var country      = newModel.Addresses.IsAny() ? newModel.Addresses.Select(a => a.Country).FirstOrDefault() : null;

            if (addressLine1 != null || addressLine2 != null || city != null || zip != null || country != null || state != null)
            {
                existingModel.AddressTypes = dropdownValues.Where(s => s.DropdownID == (byte)DropdownFieldTypes.AddressType)
                                             .Select(s => s.DropdownValuesList).ToList().FirstOrDefault().Where(d => d.IsActive == true);

                AddressViewModel newAddress = new AddressViewModel();
                newAddress.AddressTypeID = existingModel.AddressTypes.SingleOrDefault(a => a.IsDefault).DropdownValueID;
                newAddress.AddressLine1  = addressLine1 != null && !string.IsNullOrEmpty(addressLine1) ? addressLine1 : "";
                newAddress.AddressLine2  = addressLine2 != null && !string.IsNullOrEmpty(addressLine2) ? addressLine2 : "";
                newAddress.City          = city != null && !string.IsNullOrEmpty(city) ? city : "";
                if (state != null)
                {
                    newAddress.State = new State()
                    {
                        Code = state.Code
                    }
                }
                ;
                else
                {
                    newAddress.State = new State();
                }
                if (country != null)
                {
                    newAddress.Country = new Country()
                    {
                        Code = country.Code
                    }
                }
                ;
                else
                {
                    newAddress.Country = new Country();
                }

                var zipCode = zip != null && !string.IsNullOrEmpty(zip) ? zip : "";
                newAddress.ZipCode   = zipCode;
                newAddress.IsDefault = true;

                if ((newAddress.State != null && !string.IsNullOrEmpty(newAddress.State.Code)) &&
                    (newAddress.Country == null || string.IsNullOrEmpty(newAddress.Country.Code)))
                {
                    newAddress.Country      = new Country();
                    newAddress.Country.Code = newAddress.State.Code.Substring(0, 2);
                }
                existingModel.Addresses.Add(newAddress);
            }

            existingModel.ContactType     = Entities.ContactType.Person.ToString();
            existingModel.SecondaryEmails = new List <dynamic>();

            Log.Informational("While Updating Life Cycle Stage.");
            existingModel.LifecycleStages = dropdownValues.Where(s => s.DropdownID == (byte)DropdownFieldTypes.LifeCycle)
                                            .Select(s => s.DropdownValuesList).ToList().FirstOrDefault().Where(d => d.IsActive == true);
            var defaultLifeCycleType = existingModel.LifecycleStages.SingleOrDefault(a => a.IsDefault);

            if (newModel.LifecycleStage > 0)
            {
                if (!existingModel.LifecycleStages.Where(l => l.DropdownValueID == newModel.LifecycleStage).IsAny())
                {
                    newModel.LifecycleStage = existingModel.LifecycleStages.Where(l => l.IsDefault).Select(s => s.DropdownValueID).FirstOrDefault();
                }
            }
            existingModel.LifecycleStage = newModel.LifecycleStage > 0 ? newModel.LifecycleStage : defaultLifeCycleType.DropdownValueID;

            existingModel.AccountID     = newModel.AccountID;
            existingModel.LastUpdatedOn = DateTime.Now.ToUniversalTime();
            GetAllCustomFieldsResponse accountCustomFields = new GetAllCustomFieldsResponse();
            GetAllCustomFieldsRequest  request             = new GetAllCustomFieldsRequest(newModel.AccountID);

            accountCustomFields.CustomFields = _customFieldService.GetAllCustomFields(request).CustomFields;
            if (newModel.CustomFields.IsAny())
            {
                Log.Informational("While Updating Custom fields.");
                foreach (ContactCustomFieldMapViewModel submittedField in newModel.CustomFields)
                {
                    try
                    {
                        var isCustomField = accountCustomFields.CustomFields.Where(c => c.FieldId == submittedField.CustomFieldId).FirstOrDefault();
                        if (isCustomField != null)
                        {
                            ContactCustomFieldMapViewModel contactCustomField = new ContactCustomFieldMapViewModel();
                            contactCustomField.CustomFieldId    = submittedField.CustomFieldId;
                            contactCustomField.Value            = submittedField.Value;
                            contactCustomField.FieldInputTypeId = (int)isCustomField.FieldInputTypeId;
                            contactCustomField.ContactId        = existingModel.ContactID;
                            var existingCustomField = existingModel.CustomFields.Where(c => c.CustomFieldId == isCustomField.FieldId).FirstOrDefault();
                            if (existingCustomField == null)
                            {
                                existingModel.CustomFields.Add(contactCustomField);
                            }
                            else
                            {
                                existingCustomField.Value = submittedField.Value;
                            }
                        }
                    }
                    catch
                    {
                        Log.Informational("While Update: Submitted customfieldId: " + submittedField.CustomFieldId + " cannot be Updated. Value: " + submittedField.Value);
                    }
                }
            }

            return(existingModel);
        }
        private void readLocalFiles(FtpService ftpManager)
        {
            DirectoryInfo d = new DirectoryInfo(leadAdapterAndAccountMap.LocalFilePath);

            FileInfo[] Files = d.GetFiles("*.xml");

            if (Files != null && Files.Count() > 0)
            {
                var response = customFieldService.GetAllCustomFields(new GetAllCustomFieldsRequest(leadAdapterAndAccountMap.AccountID));
                IEnumerable <FieldViewModel> customFields = response.CustomFields;
                var dropdownfeildsresposne = cacheService.GetDropdownValues(leadAdapterAndAccountMap.AccountID);

                IEnumerable <DropdownValueViewModel> phoneFields = dropdownfeildsresposne.Where(x => x.DropdownID == (short)DropdownFieldTypes.PhoneNumberType).Select(x => x.DropdownValuesList).FirstOrDefault();

                Logger.Current.Informational("Processing files for account : " + leadAdapterAndAccountMap.AccountName + " count of files : " + Files.LongLength);
                foreach (var file in Files)
                {
                    Logger.Current.Informational("Current Processed File Name: " + file.Name);
                    var localFilePath = Path.Combine(leadAdapterAndAccountMap.LocalFilePath, file.Name);

                    try
                    {
                        int jobid    = Convert.ToInt32(Path.GetFileNameWithoutExtension(localFilePath).Split('~')[1]);
                        var fileName = Path.GetFileNameWithoutExtension(localFilePath).Split('~')[0] + Path.GetExtension(localFilePath);

                        /* Get contacts */
                        var contacts = new ImportContactsData();
                        contacts = GetContacts(localFilePath, customFields, jobid, phoneFields);
                        if (contacts.ContactData != null && contacts.ContactData.Any())
                        {
                            Logger.Current.Informational("Got contacts for inserting : " + contacts.ContactData.Count);
                            /* Bulk Insert */
                            Task.Factory.StartNew(() => { MailgunVerification(contacts.ContactData.ToList()); }, TaskCreationOptions.LongRunning);
                            Task.Factory.StartNew(() => { ContactsBulkinsert(contacts); }, TaskCreationOptions.LongRunning);
                        }
                        else
                        {
                            Logger.Current.Informational("No contacts for inserting, Account name : " + leadAdapterAndAccountMap.AccountName);
                        }
                        /* File move to archive after processing */

                        var fileExtension = Path.GetExtension(localFilePath);
                        var newFileName   = Guid.NewGuid() + fileExtension;
                        importDataRepository.UpdateLeadAdapterJobLogsWithProcessedFileName(jobid, newFileName);
                        string archivedfile = string.Empty;
                        if (Directory.Exists(leadAdapterAndAccountMap.ArchivePath) && System.IO.File.Exists(localFilePath))
                        {
                            archivedfile = Path.Combine(leadAdapterAndAccountMap.ArchivePath, newFileName);
                            File.Move(localFilePath, archivedfile);
                        }
                        else if (File.Exists(localFilePath))
                        {
                            File.Delete(localFilePath);
                        }

                        bool isFolderExist = ftpManager.CreateFTPDirectory(leadAdapterAndAccountMap.RequestGuid);
                        if (isFolderExist)
                        {
                            ftpManager.MoveFile(leadAdapterAndAccountMap.RequestGuid, fileName, archivedfile, jobid);
                        }
                        else
                        {
                            ftpManager.Delete(leadAdapterAndAccountMap.RequestGuid, string.Empty, new List <string> {
                                fileName
                            });
                        }
                    }
                    catch (IndexOutOfRangeException indexExc)
                    {
                        Logger.Current.Error("Index out of range exception while accessing jobid and filename, account name : " + leadAdapterAndAccountMap.AccountName, indexExc);
                        var fileExtension = Path.GetExtension(localFilePath);
                        var newFileName   = Guid.NewGuid() + fileExtension;

                        string archivedfile = string.Empty;
                        archivedfile = Path.Combine(leadAdapterAndAccountMap.ArchivePath, newFileName);
                        File.Move(localFilePath, archivedfile);           //These are old files
                        continue;
                    }
                }
                Logger.Current.Informational("Files processed succcessfully for account : " + leadAdapterAndAccountMap.AccountName + " count of files : " + Files.LongLength);
            }
        }