Beispiel #1
0
        /// <summary>
        /// Pushes the information to the <c>crm</c>
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        public async Task PushAsync(AgileCRM crm)
        {
            var exists = await this.ExistsAsync(crm).ConfigureAwait(false);

            if (!exists)
            {
                var newPersonRequest = new NewPersonRequest(this);
                var updatedContact   = await crm.RequestAsync("contacts", HttpMethod.Post, JsonConvert.SerializeObject(newPersonRequest)).ConfigureAwait(false);

                this.Contact.Update(JsonConvert.DeserializeObject <Contact>(updatedContact));
            }
            else
            {
                // make sure the contact id is initialized
                if (this.Contact.Id == 0)
                {
                    var person = new Person(this.Email);
                    await person.PullAsync(crm).ConfigureAwait(false);

                    this.Contact.Id = person.Contact.Id;
                }

                // update properties
                var updatePropertiesRequest = new UpdateContactPropertiesRequest(this.Contact);
                await crm.RequestAsync("contacts/edit-properties", HttpMethod.Put, JsonConvert.SerializeObject(updatePropertiesRequest)).ConfigureAwait(false);

                // update lead score
                var updateLeadScoreRequest = new UpdateLeadScoreRequest(this.Contact);
                await crm.RequestAsync("contacts/edit/lead-score", HttpMethod.Put, JsonConvert.SerializeObject(updateLeadScoreRequest)).ConfigureAwait(false);

                // update star value
                var updateStarValueRequest = new UpdateStarValueRequest(this.Contact);
                await crm.RequestAsync("contacts/edit/add-star", HttpMethod.Put, JsonConvert.SerializeObject(updateStarValueRequest)).ConfigureAwait(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Pulls the information from the <c>crm</c>
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the read operation.
        /// </returns>
        public async Task <bool> PullAsync(AgileCRM crm)
        {
            if (this.Contact.Id != 0)
            {
                var response = await crm.RequestAsync($"contacts/{this.Contact.Id}", HttpMethod.Get, null).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(response))
                {
                    this.Contact.Update(JsonConvert.DeserializeObject <Contact>(response));
                }
            }

            if (this.Contact.Id == 0 && !string.IsNullOrEmpty(this.Email))
            {
                var response = await crm.RequestAsync($"contacts/search/email/{this.Email}", HttpMethod.Get, null).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(response))
                {
                    this.Contact.Update(JsonConvert.DeserializeObject <Contact>(response));
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Checks whether the person exists in the <c>CRM</c>
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance.
        /// </param>
        /// <returns>
        /// Condition whether the person exists in the <c>CRM</c>
        /// </returns>
        public async Task <bool> ExistsAsync(AgileCRM crm)
        {
            if (this.Contact.Id != 0)
            {
                var response = await crm.RequestAsync($"contacts/{this.Contact.Id}", HttpMethod.Get, null).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(response))
                {
                    return(true);
                }
            }

            if (this.Contact.Id == 0 && !string.IsNullOrEmpty(this.Email))
            {
                var response = await crm.RequestAsync($"contacts/search/email/{this.Email}", HttpMethod.Get, null).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(response))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Removes tags from the person
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance.
        /// </param>
        /// <param name="tags">
        /// The tags to be removed
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        public async Task RemoveTagsAsync(AgileCRM crm, List <string> tags)
        {
            await this.PullAsync(crm).ConfigureAwait(false);

            var updateTagsRequest = new UpdateContactTagsRequest(this.Contact, tags);
            var response          = await crm.RequestAsync("contacts/delete/tags", HttpMethod.Put, JsonConvert.SerializeObject(updateTagsRequest)).ConfigureAwait(false);

            await this.PullAsync(crm).ConfigureAwait(false);
        }
Beispiel #5
0
        /// <summary>
        /// Adds tags to the person
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance.
        /// </param>
        /// <param name="tags">
        /// The tags to be added
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        public async Task AddTagsAsync(AgileCRM crm, List <string> tags)
        {
            await this.PullAsync(crm).ConfigureAwait(false);

            var updateTagsRequest = new UpdateContactTagsRequest(this.Contact, tags);
            var response          = await crm.RequestAsync("contacts/edit/tags", HttpMethod.Put, JsonConvert.SerializeObject(updateTagsRequest)).ConfigureAwait(false);

            var updatedContact = JsonConvert.DeserializeObject <Contact>(response);

            this.Tags = updatedContact.Tags;
        }
Beispiel #6
0
        /// <summary>
        /// Creates a note in the <c>CRM</c>
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance to be used.
        /// </param>
        /// <param name="subject">
        /// The subject of the note.
        /// </param>
        /// <param name="description">
        /// The description of the note.
        /// </param>
        /// <param name="contactIds">
        /// The related contact identifiers.
        /// </param>
        /// <returns>
        /// The created note
        /// </returns>
        public static async Task <Note> CreateContactNoteAsync(AgileCRM crm, string subject, string description, List <long> contactIds)
        {
            var createNoteRequest = new CreateNoteRequest()
            {
                Subject     = subject,
                Description = description,
                ContactIds  = contactIds,
            };

            var response = await crm.RequestAsync($"notes", HttpMethod.Post, JsonConvert.SerializeObject(createNoteRequest)).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <Note>(response));
        }
Beispiel #7
0
        /// <summary>
        /// Delete this person.
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        public async Task DeletePersonAsync(AgileCRM crm)
        {
            if (this.Contact.Id == 0)
            {
                await this.PullAsync(crm).ConfigureAwait(false);
            }

            if (this.Contact.Id == 0)
            {
                throw new InvalidOperationException("The person identifier is not defined.");
            }

            await crm.RequestAsync($"contacts/{this.Contact.Id}", HttpMethod.Delete, null).ConfigureAwait(false);

            this.Contact.DisconnectFromCRM();
        }
Beispiel #8
0
        /// <summary>
        /// Get a list of persons from the <c>CRM</c>
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance.
        /// </param>
        /// <param name="maxPersons">
        /// The maximum number of persons to receive.
        /// </param>
        /// <param name="cursor">
        /// Points to the first person of the list (used for paging).
        /// </param>
        /// <returns>
        /// A list of persons.
        /// </returns>
        public static async Task <List <Person> > GetPersonsAsync(AgileCRM crm, int maxPersons, string cursor)
        {
            var response = await crm.RequestAsync($"contacts/?page_size={maxPersons}&cursor={cursor}", HttpMethod.Get, null).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <List <Contact> >(response).Select <Contact, Person>(c => new Person(c)).ToList());
        }
Beispiel #9
0
        /// <summary>
        /// Get all notes related to a contact.
        /// </summary>
        /// <param name="crm">
        /// The <see cref="AgileCRM"/> instance to be used.
        /// </param>
        /// <param name="contactId">
        /// The contact identifier for which to retrieve the note.
        /// </param>
        /// <returns>
        /// The notes related to a contact.
        /// </returns>
        public static async Task <List <Note> > GetContactNotesAsync(AgileCRM crm, long contactId)
        {
            var response = await crm.RequestAsync($"contacts/{contactId}/notes", HttpMethod.Get, null).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <List <Note> >(response));
        }