public void ChainedOptionsRelationships()
        {
            using (var client = TestHelper.ClientGet())
            {
                // Create a contact with two relationships.

                var relationshipRequest1 = new RelationshipRequest
                {
                    RelationshipTypeId = 5, // Works for
                    ContactIdAValueExpression = "$value.id",
                    ContactIdB = 1,         // Default organization
                    StartDate = DateTime.Now.Date.AddYears(-2),
                    EndDate = DateTime.Now.Date.AddYears(-1),
                    IsActive = false
                };
                var relationshipRequest2 = new RelationshipRequest
                {
                    RelationshipTypeId = 5, // Works for
                    ContactIdAValueExpression = "$value.id",
                    ContactIdB = 1,         // Default organization
                    StartDate = DateTime.Now.Date,
                    EndDate = DateTime.Now.Date.AddYears(1),
                    IsActive = true
                };

                var saveResult = client.ContactSave(TestHelper.ApiKey, TestHelper.SiteKey,
                    new ContactRequest
                    {
                        ContactType = ContactType.Individual,
                        LastName = "Smurf",
                        FirstName = "Smul",
                        ExternalIdentifier = "Test_External_Smurf",
                        ApiOptions = new ApiOptions { Match = "external_identifier" },
                        RelationshipSaveRequest = new List<RelationshipRequest> { relationshipRequest1, relationshipRequest2 },
                    });
                Assert.IsNotNull(saveResult.Id);

                // Get contact with relationships, order them backwards, and retrieve only one.
                var contact = client.ContactGetSingle(TestHelper.ApiKey, TestHelper.SiteKey,
                    new ContactRequest
                    {
                        Id = saveResult.Id.Value,
                        RelationshipGetRequest = new RelationshipRequest
                        {
                            ContactIdAValueExpression = "$value.id",
                            ApiOptions = new ApiOptions {Sort = "end_date DESC", Limit = 1}
                        }
                    });

                // Delete contact before doing assertions.
                client.ContactDelete(TestHelper.ApiKey, TestHelper.SiteKey, new DeleteRequest(saveResult.Id.Value), 1);

                Assert.AreEqual(1, contact.RelationshipResult.Count);
                Assert.AreEqual(relationshipRequest2.StartDate, contact.RelationshipResult.Values.First().StartDate);
            }
        }
        public void RelationshipChainedGetDelete()
        {
            using (var client = TestHelper.ClientGet())
            {
                var request = new RelationshipRequest
                {
                    ContactIdB = _myCompanyId,
                    EndDateFilter = new Filter<DateTime?>(WhereOperator.Gt, new DateTime(2016, 8, 7)),
                    RelationshipDeleteRequest = new List<DeleteRequest> { new DeleteRequest { IdValueExpression = "$value.id"} }
                };

                // Find and remove relationship.
                var result = client.RelationshipGet(TestHelper.ApiKey, TestHelper.SiteKey, request);

                var result2 = client.RelationshipGet(TestHelper.ApiKey, TestHelper.SiteKey,
                    new RelationshipRequest {Id = _myOtherRelationshipId});

                Assert.AreEqual(0, result2.Count);
            }
        }
        public void CreateWithChainedRelationship()
        {
            using (var client = TestHelper.ClientGet())
            {
                var relationshipSaveRequest = new RelationshipRequest
                {
                    RelationshipTypeId = 5,
                    ContactIdB = 1,
                    ContactIdAValueExpression = "$value.id"
                };

                var contact = new ContactRequest
                {
                    ContactType = ContactType.Individual,
                    FirstName = "Lucky",
                    LastName = "Luke",
                    BirthDate = new DateTime(1946, 3, 3),
                    Gender = Gender.Male,
                    ExternalIdentifier = "test_ext_id_yep",
                    RelationshipSaveRequest = new[] {relationshipSaveRequest},
                    ApiOptions = new ApiOptions {Match = "external_identifier"}
                };

                var saveResult = client.ContactSave(TestHelper.ApiKey, TestHelper.SiteKey, contact);
                var getResult = client.ContactGetSingle(TestHelper.ApiKey, TestHelper.SiteKey,
                    new ContactRequest
                    {
                        Id = saveResult.Id,
                        RelationshipGetRequest = new RelationshipRequest {ContactIdAValueExpression = "$value.id"}
                    });

                // Delete first (cleanup), check afterward.
                client.ContactDelete(TestHelper.ApiKey, TestHelper.SiteKey, new DeleteRequest(saveResult.Id.Value), 1);

                Assert.AreEqual(0, saveResult.IsError);
                Assert.AreEqual(1, getResult.RelationshipResult.Count);

                var relationShip = getResult.RelationshipResult.Values.First();

                Assert.AreEqual(relationshipSaveRequest.RelationshipTypeId, relationShip.RelationshipTypeId);
                Assert.AreEqual(relationshipSaveRequest.ContactIdB, relationShip.ContactIdB);
            }
        }
        public void RelationshipDateFilter2()
        {
            using (var client = TestHelper.ClientGet())
            {
                var request = new RelationshipRequest
                {
                    ContactIdB = _myCompanyId,
                    EndDateFilter = new Filter<DateTime?>(WhereOperator.Lt, new DateTime(2016, 8, 7))
                };

                var result = client.RelationshipGet(TestHelper.ApiKey, TestHelper.SiteKey, request);

                Assert.AreEqual(0, result.Count);
            }
        }
        public void RelationshipChainedContact()
        {
            using (var client = TestHelper.ClientGet())
            {
                var request = new RelationshipRequest
                {
                    Id = _myRelationshipId,
                    ContactGetRequest = new ContactRequest
                    {
                        IdValueExpression = "$value.contact_id_a"
                    }
                };

                var result = client.RelationshipGetSingle(TestHelper.ApiKey, TestHelper.SiteKey, request);

                Assert.IsNotNull(result.ContactResult);
                Assert.AreEqual(1, result.ContactResult.Count);
                Assert.AreEqual(_myContactId, result.ContactResult.Values.First().Id);
            }
        }
        public void ReactivateRelationship()
        {
            using (var client = TestHelper.ClientGet())
            {
                // reactivate the inactive relationship.
                var saveRequest = new RelationshipRequest
                {
                    Id = _myOtherRelationshipId,
                    IsActive = true,
                    // This will be the workaround to unset a date:
                    EndDate = DateTime.MinValue
                };
                client.RelationshipSave(TestHelper.ApiKey, TestHelper.SiteKey, saveRequest);

                var result = client.RelationshipGetSingle(TestHelper.ApiKey, TestHelper.SiteKey,
                    new RelationshipRequest {Id = _myOtherRelationshipId});

                Assert.IsNull(result.EndDate);
                Assert.IsTrue(result.IsActive);
            }
        }
        public void NewRelationship()
        {
            using (var client = TestHelper.ClientGet())
            {
                var relationshipRequest = new RelationshipRequest
                {
                    RelationshipTypeId = 5, // Works for
                    ContactIdA = _myContactId,
                    ContactIdB = 1, // Default organization
                    StartDate = DateTime.Now.Date,
                    EndDate = DateTime.Now.Date.AddYears(1),
                    IsActive = true
                };

                var result = client.RelationshipSave(TestHelper.ApiKey, TestHelper.SiteKey, relationshipRequest);
                var newRelationship = result.Values.First();

                Assert.AreEqual(relationshipRequest.ContactIdA, newRelationship.ContactIdA);
                Assert.AreEqual(relationshipRequest.ContactIdB, newRelationship.ContactIdB);
                Assert.AreEqual(relationshipRequest.StartDate, newRelationship.StartDate);
                Assert.AreEqual(relationshipRequest.EndDate, newRelationship.EndDate);
                Assert.AreEqual(relationshipRequest.IsActive, newRelationship.IsActive);
                Assert.AreEqual(relationshipRequest.RelationshipTypeId, newRelationship.RelationshipTypeId);
            }
        }
        public void RelationshipChainedSave()
        {
            using (var client = TestHelper.ClientGet())
            {
                DateTime today = DateTime.Today;

                // Fire all contacts working for the company.
                var request = new RelationshipRequest
                {
                    RelationshipTypeId = 5, // Works for
                    ContactIdB = _myCompanyId,
                    IsActive = true,
                    RelationshipSaveRequest = new[]
                    {
                        new RelationshipRequest
                        {
                            IdValueExpression = "$value.id",
                            IsActive = false,
                            EndDate = today
                        }
                    }
                };
                // Get relationship to see if it worked.
                var result = client.RelationshipGet(TestHelper.ApiKey, TestHelper.SiteKey, request);
                var relationship = client.RelationshipGetSingle(TestHelper.ApiKey, TestHelper.SiteKey,
                    new RelationshipRequest {Id = _myRelationshipId});
                Assert.IsFalse(relationship.IsActive);
                Assert.AreEqual(today, relationship.EndDate);
            }
        }