Exemplo n.º 1
0
        public static List <PCOPerson> GetPeople(bool importing, DateTime?updatedAfter, string apiEndPoint)
        {
            // Create variables to store the results
            var dataItems     = new List <PCOData>();
            var includedItems = new List <PCOData>();

            // Set the endpoint if not passed from parameter
            apiEndPoint = apiEndPoint ?? "https://api.planningcenteronline.com/people/v2/people?include=addresses,emails,field_data,households,inactive_reason,marital_status,name_prefix,name_suffix,phone_numbers,social_profiles,school,primary_campus&per_page=100";

            // If not importing and querying only changes, add a parameter to specify that the results should be sored in descending order of last update
            if (!importing && updatedAfter.HasValue)
            {
                apiEndPoint = apiEndPoint + "&order=-updated_at";
            }

            // Query Planning Center for people
            if (PCOGetItems(apiEndPoint, dataItems, includedItems, updatedAfter,
                            !importing, "", ""))
            {
                // Create variable to store the people
                var people = new List <PCOPerson>();

                // Loop through each item in the result of api call
                foreach (var item in dataItems)
                {
                    PCOPerson person;
                    if (apiEndPoint.Contains("giving"))
                    {
                        person = new PCOPerson(item, true);
                    }
                    else
                    {
                        person = new PCOPerson(item);
                        if (person != null)
                        {
                            // Update the contact information
                            person.UpdateContactInfo(item, includedItems);
                            person.UpdateHouseHold(item, includedItems);
                            person.UpdateFieldData(item, includedItems);
                            person.UpdateCampus(item, includedItems);
                            person.UpdateProperties(item, includedItems);
                            person.UpdateSocialProfiles(item, includedItems);
                        }
                    }
                    // Create the person record

                    if (person != null)
                    {
                        // Add to list of results
                        people.Add(person);
                    }
                }

                // return the list of people
                return(people);
            }

            // An error occurred trying to query people so return null
            return(null);
        }
Exemplo n.º 2
0
 public static Person AddBackgroundCheckResult(Person person, PCOPerson pcoPerson)
 {
     if (pcoPerson.passed_background_check.HasValue)
     {
         string value;
         if (pcoPerson.passed_background_check.Value)
         {
             value = "Pass";
         }
         else
         {
             value = "Fail";
         }
         person.Attributes.Add(new PersonAttributeValue
         {
             AttributeKey   = "BackgroundCheckResult",
             AttributeValue = value,
             PersonId       = person.Id
         });
     }
     return(person);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Exports the individuals.
        /// </summary>
        /// <param name="modifiedSince">The modified since.</param>
        /// <param name="peoplePerPage">The people per page.</param>
        public static void ExportIndividuals(DateTime modifiedSince, int peoplePerPage = 100)
        {
            var personAttributes = WritePersonAttributes();

            var PCOPeople        = GetPeople(false, modifiedSince, API_PEOPLE + "?include=emails,addresses,phone_numbers,field_data,households,inactive_reason,martial_status,name_prefix,name_suffix,primary_campus,school,social_profiles&per_page=" + peoplePerPage);
            var PCOServicePeople = GetPeople(false, modifiedSince, "https://api.planningcenteronline.com/services/v2/people?per_page=" + peoplePerPage);
            var PCONotes         = GetNotes(false, modifiedSince, null);

            foreach (PCOPerson p in PCOPeople)
            {
                /*  For debugging issues with a given person
                 * if ( p.id == 29004938 )
                 * {
                 *  //Debug this profile
                 *  Console.WriteLine();
                 * }
                 */

                PCOPerson headOfHouse;
                if (p.household is null)
                {
                    headOfHouse = p;
                }
                else
                {
                    headOfHouse = PCOPeople.Where(x => x.id == p.household.primary_contact_id).FirstOrDefault();
                }

                if (headOfHouse == null)
                {
                    headOfHouse = p;
                }
                var importPerson = PCOImportPerson.Translate(p, personAttributes, headOfHouse);

                if (importPerson != null)
                {
                    if (PCOServicePeople != null)
                    {
                        PCOPerson backgroundCheckPerson = PCOServicePeople.Where(x => x.id == p.id).FirstOrDefault();
                        if (backgroundCheckPerson != null &&
                            backgroundCheckPerson.passed_background_check.HasValue &&
                            backgroundCheckPerson.passed_background_check.Value
                            )
                        {
                            importPerson = PCOImportPerson.AddBackgroundCheckResult(importPerson, backgroundCheckPerson);
                        }
                    }

                    ImportPackage.WriteToPackage(importPerson);
                }

                // save person image
                if (p.avatar.IsNotNullOrWhitespace())
                {
                    WebClient client = new WebClient();

                    var path = Path.Combine(ImportPackage.ImageDirectory, "Person_" + p.id + ".png");

                    try
                    {
                        client.DownloadFile(new Uri(p.avatar), path);
                        ApiCounter++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            // save notes.
            if (PCONotes != null)
            {
                foreach (PCONote note in PCONotes)
                {
                    PersonNote importNote = PCOImportPersonNote.Translate(note);
                    if (importNote != null)
                    {
                        ImportPackage.WriteToPackage(importNote);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static Person Translate(PCOPerson inputPerson, List <PCOFieldDefinition> personAttributes, PCOPerson HeadOfHouse)
        {
            var person = new Core.Model.Person();
            var notes  = new List <string>();

            if (inputPerson.id > 0)
            {
                person.Id = inputPerson.id;

                // names
                person.FirstName  = inputPerson.first_name;
                person.NickName   = inputPerson.nickname;
                person.MiddleName = inputPerson.middle_name;
                person.LastName   = inputPerson.last_name;


                if (!string.IsNullOrWhiteSpace(inputPerson.name_prefix))
                {
                    person.Salutation = System
                                        .Threading
                                        .Thread
                                        .CurrentThread
                                        .CurrentCulture
                                        .TextInfo
                                        .ToTitleCase(inputPerson.name_prefix.ToLower());
                }

                if (!string.IsNullOrWhiteSpace(inputPerson.name_suffix))
                {
                    if (inputPerson.name_suffix.Equals("Sr.", StringComparison.OrdinalIgnoreCase))
                    {
                        person.Suffix = "Sr.";
                    }
                    else if (inputPerson.name_suffix.Equals("Jr.", StringComparison.OrdinalIgnoreCase))
                    {
                        person.Suffix = "Jr.";
                    }
                    else if (inputPerson.name_suffix.Equals("Ph.D.", StringComparison.OrdinalIgnoreCase))
                    {
                        person.Suffix = "Ph.D.";
                    }
                    else
                    {
                        person.Suffix = inputPerson.name_suffix;
                    }
                }

                // communcations (phone & email)
                foreach (var number in inputPerson.contact_data.phone_numbers)
                {
                    person.PhoneNumbers.Add(new PersonPhone
                    {
                        PersonId    = person.Id,
                        PhoneType   = number.location,
                        PhoneNumber = number.number
                    });
                }

                foreach (var email in inputPerson.contact_data.email_addresses)
                {
                    person.Email = email.address;
                }


                // addresses
                foreach (var address in inputPerson.contact_data.addresses)
                {
                    var importAddress = new PersonAddress();
                    importAddress.PersonId   = person.Id;
                    importAddress.Street1    = address.street;
                    importAddress.City       = address.city;
                    importAddress.State      = address.state;
                    importAddress.PostalCode = address.zip;

                    var addressType = address.location;

                    switch (addressType)
                    {
                    case "Home":
                        importAddress.AddressType = AddressType.Home;
                        break;

                    case "Previous":
                        importAddress.AddressType = AddressType.Previous;
                        break;

                    case "Work":
                        importAddress.AddressType = AddressType.Work;
                        break;

                    case "Other":
                        importAddress.AddressType = AddressType.Other;
                        break;
                    }

                    person.Addresses.Add(importAddress);
                }

                // gender
                var gender = inputPerson.gender;

                if (gender == "M" || gender == "Male")
                {
                    person.Gender = Gender.Male;
                }
                else if (gender == "F" || gender == "Female")
                {
                    person.Gender = Gender.Female;
                }
                else
                {
                    person.Gender = Gender.Unknown;
                }

                // marital status
                switch (inputPerson.marital_status)
                {
                case "Married":
                    person.MaritalStatus = MaritalStatus.Married;
                    break;

                case "Single":
                    person.MaritalStatus = MaritalStatus.Single;
                    break;

                case "Divorced":
                    person.MaritalStatus = MaritalStatus.Divorced;
                    break;

                default:
                    person.MaritalStatus = MaritalStatus.Unknown;
                    if (inputPerson.marital_status.IsNotNullOrWhitespace())
                    {
                        notes.Add("maritalStatus: " + inputPerson.marital_status);
                    }
                    break;
                }


                // connection status
                person.ConnectionStatus = inputPerson.member;

                // record status
                if (inputPerson.status == "active")
                {
                    person.RecordStatus = RecordStatus.Active;
                }
                else
                {
                    person.RecordStatus = RecordStatus.Inactive;
                }

                // dates
                person.Birthdate        = inputPerson.birthdate;
                person.AnniversaryDate  = inputPerson.anniversary;
                person.CreatedDateTime  = inputPerson.created_at;
                person.ModifiedDateTime = inputPerson.updated_at;

                // family

                if (inputPerson.household != null)
                {
                    person.FamilyId = inputPerson.household.Id;
                }


                if (inputPerson.child.HasValue && inputPerson.child.Value)
                {
                    person.FamilyRole = FamilyRole.Child;
                }
                else
                {
                    person.FamilyRole = FamilyRole.Adult;
                }

                if (HeadOfHouse.campus != null)
                {
                    person.Campus = new Campus
                    {
                        CampusId   = HeadOfHouse.campus.id,
                        CampusName = HeadOfHouse.campus.name
                    };
                }

                // person attributes
                // Make sure there are no duplicate entries for a given field
                var attributes = inputPerson.field_data
                                 .GroupBy(a => a.field_definition_id)
                                 .Select(g => g.First())
                                 .ToList();
                var usedAttributeKeys = new List <string>();

                foreach (var attribute in attributes)
                {
                    var field_definition = personAttributes.Where(f => f.id == attribute.field_definition_id).FirstOrDefault();
                    if (field_definition != null)
                    {
                        person.Attributes.Add(new PersonAttributeValue
                        {
                            AttributeKey   = field_definition.id + "_" + field_definition.slug,
                            AttributeValue = attribute.value,
                            PersonId       = person.Id
                        });
                    }
                }

                // Make sure there are no duplicate entries
                var profiles = inputPerson.socialProfiles
                               .GroupBy(a => a.site)
                               .Select(g => g.First())
                               .ToList();
                // add social profiles to attributes
                foreach (var profile in profiles)
                {
                    if (profile.url.IsNotNullOrWhitespace())
                    {
                        person.Attributes.Add(new PersonAttributeValue
                        {
                            AttributeKey   = profile.site,
                            AttributeValue = profile.url,
                            PersonId       = person.Id
                        });
                    }
                }

                // Add School Attribute
                if (!string.IsNullOrWhiteSpace(inputPerson.school))
                {
                    person.Attributes.Add(new PersonAttributeValue
                    {
                        AttributeKey   = "School",
                        AttributeValue = inputPerson.school,
                        PersonId       = person.Id
                    });
                }

                // write out person notes
                if (notes.Count() > 0)
                {
                    person.Note = string.Join(",", notes);
                }
            }

            return(person);
        }