Exemplo n.º 1
0
        private static void PopulatePatients()
        {
            List <Patient> patients = JsonConvert.DeserializeObject <List <Patient> >(File.ReadAllText(@"C:\Users\Robert\Documents\patient.json"));
            List <Patient> success  = new List <Patient>();
            List <Patient> errors   = new List <Patient>();
            List <Patient> skipped  = new List <Patient>();
            int            count    = 0;

            List <string>          emails    = new List <string>();
            List <string>          phones    = new List <string>();
            IEnumerable <Customer> customers = Shopify.GetCustomers();

            foreach (Customer c in customers)
            {
                if (!string.IsNullOrEmpty(c.Email))
                {
                    emails.Add(c.Email);
                }

                if (!string.IsNullOrEmpty(c.Phone))
                {
                    phones.Add(c.Phone);
                }
            }


            Patient patient;

            while ((patient = patients.FirstOrDefault()) != null)
            {
                patients.Remove(patient);
                File.WriteAllText(@"C:\Users\Robert\Documents\patient.json", JsonConvert.SerializeObject(patients));
                //				patient.EMail = isValidEmail(patient.EMail) ? patient.EMail : null;
                if (emails.Contains(patient.EMail))
                {
                    skipped.Add(patient);
                    File.WriteAllText(@"C:\Users\Robert\Documents\skipped.json", JsonConvert.SerializeObject(skipped));
                    System.Console.WriteLine($"\rSkipping Patient {patient.PatientNo} duplicate email");
                    continue;
                }
                if (!string.IsNullOrEmpty(patient.EMail))
                {
                    emails.Add(patient.EMail);
                }
                else
                {
                }
                ShopifySharp.Address address = new Address
                {
                    Address1     = patient.Address1,
                    Address2     = patient.Address2,
                    City         = patient.City,
                    Default      = true,
                    Zip          = patient.Zip,
                    CountryCode  = "US",
                    ProvinceCode = patient.State
                };

                ShopifySharp.Customer customer = new ShopifySharp.Customer
                {
                    Addresses = new List <Address>()
                    {
                        address
                    },
                    Email     = patient.EMail,
                    FirstName = patient.FirstName,
                    LastName  = patient.LastName
                };
                if (patient.CellPhone != null && IsValidUSPhoneNumber(patient.CellPhone))
                {
                    customer.Phone = patient.CellPhone;
                }
                else if (patient.HomePhone != null && IsValidUSPhoneNumber(patient.HomePhone))
                {
                    customer.Phone = patient.HomePhone;
                }
                else if (patient.WorkPhone != null && IsValidUSPhoneNumber(patient.WorkPhone))
                {
                    customer.Phone = patient.WorkPhone;
                }
                else if (patient.OtherPhone != null && IsValidUSPhoneNumber(patient.OtherPhone))
                {
                    customer.Phone = patient.OtherPhone;
                }
                if (!string.IsNullOrEmpty(customer.Phone))
                {
                    if (phones.Contains(customer.Phone))
                    {
                        skipped.Add(patient);
                        File.WriteAllText(@"C:\Users\Robert\Documents\skipped.json", JsonConvert.SerializeObject(skipped));
                        System.Console.WriteLine($"\rSkipping Patient {patient.PatientNo} duplicate phone");
                        continue;
                    }
                    phones.Add(customer.Phone);
                }

                try
                {
                    Customer c = Shopify.CreateCustomer(customer);
                    bool     b = Shopify.CreateCustomerMetadata(c.Id.Value, "ownCustomer", "PatientNumber", patient.PatientNo);
                    success.Add(patient);
                    File.WriteAllText(@"C:\Users\Robert\Documents\success.json", JsonConvert.SerializeObject(success));
                }
                catch (Exception)
                {
                    errors.Add(patient);
                    File.WriteAllText(@"C:\Users\Robert\Documents\errors.json", JsonConvert.SerializeObject(errors));
                }
                count++;
                System.Console.Write($"{count}\r");
                System.Threading.Thread.Sleep(1100);
            }
        }