From 0ea6daccb33f04f9b2093e5facba2b1dab1855b4 Mon Sep 17 00:00:00 2001 From: redroot Date: Tue, 25 Jan 2022 17:41:39 +0000 Subject: [PATCH 1/9] adds deals service upsert --- lib/basecrm/services/deals_service.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/basecrm/services/deals_service.rb b/lib/basecrm/services/deals_service.rb index 581bd20..d062ff8 100644 --- a/lib/basecrm/services/deals_service.rb +++ b/lib/basecrm/services/deals_service.rb @@ -105,6 +105,28 @@ def update(deal) Deal.new(root[:data]) end + # Upsert a deal + # + # post '/deals/upsert?filter_param=filter_value + # + # Create a new deal or update an existing, based on a value of a filter or a set of filters. + # At least a single filter - query parameter - is required. If no parameters are present, the request will return an error. + # See full docs https://developers.getbase.com/docs/rest/reference/deals + # + # @param filters [Hash] - hash contain filters, one level deep e.g. { name: 'string', 'custom_fields[field]': 'value' } + # @param deal [Deal, Hash] - This object's attributes describe the object to be updated or created + # @return [Deal] The resulting object representing updated or created resource. + def upsert(filters, deal) + validate_upsert_filters!(filters) + validate_type!(deal) + + attributes = sanitize(deal) + query_string = URI.encode_www_form(filters) + _, _, root = @client.post("/deals/upsert?#{query_string}", attributes) + + Deal.new(root[:data]) + end + # Delete a deal # @@ -127,6 +149,11 @@ def validate_type!(deal) raise TypeError unless deal.is_a?(Deal) || deal.is_a?(Hash) end + def validate_upsert_filters!(filters) + raise TypeError unless filters.is_a?(Hash) + raise ArgumentError, "at least one filter is required" if filters.empty? + end + def extract_params!(deal, *args) params = deal.to_h.select{ |k, _| args.include?(k) } raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length From 06356c54d01794aca7e546316073dbef802508e1 Mon Sep 17 00:00:00 2001 From: redroot Date: Tue, 25 Jan 2022 20:28:25 +0000 Subject: [PATCH 2/9] adds deals service upsert --- spec/services/deals_service_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/services/deals_service_spec.rb b/spec/services/deals_service_spec.rb index 8f5b0c6..2d2f8ec 100644 --- a/spec/services/deals_service_spec.rb +++ b/spec/services/deals_service_spec.rb @@ -9,6 +9,7 @@ it { should respond_to :destroy } it { should respond_to :find } it { should respond_to :update } + it { should respond_to :upsert } it { should respond_to :where } end @@ -59,6 +60,14 @@ end end + describe :upsert do + it 'raises a TypeError if filters is nil' do + + end + it 'raises an ArgumentError if filters is empty' + it 'calls the upsert route with encoded filters' + end + describe :destroy do it "returns true on success" do @deal = create(:deal) From f96d4a9d41cc2d5d67222a63d9d0c31b55ad916b Mon Sep 17 00:00:00 2001 From: redroot Date: Wed, 26 Jan 2022 12:44:34 +0000 Subject: [PATCH 3/9] adds specs --- config.h | 1 + spec/services/deals_service_spec.rb | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 120000 config.h diff --git a/config.h b/config.h new file mode 120000 index 0000000..af63eb9 --- /dev/null +++ b/config.h @@ -0,0 +1 @@ +../../../../Headers/ruby/config.h \ No newline at end of file diff --git a/spec/services/deals_service_spec.rb b/spec/services/deals_service_spec.rb index 2d2f8ec..f413d91 100644 --- a/spec/services/deals_service_spec.rb +++ b/spec/services/deals_service_spec.rb @@ -62,10 +62,18 @@ describe :upsert do it 'raises a TypeError if filters is nil' do + expect(client.deals.upsert(nil, { name: 'unique_name' }).to raise_error(TypeError) + end + + it 'raises an ArgumentError if filters is empty' do + expect(client.deals.upsert({}, { name: 'unique_name' }).to raise_error(ArgumentError) + end + it 'calls the upsert route with encoded filters' do + filters = { name: 'unique_name', 'custom_fields[external_id]': 'unique-1' } + attributes = filters.merge('custom_fields[category]': 'bags') + expect(client.deals.upsert(filters, attributes)).to be_instance_of BaseCRM::Deal end - it 'raises an ArgumentError if filters is empty' - it 'calls the upsert route with encoded filters' end describe :destroy do From 84195f5d62d85a3e3e22997d494a0f357e718a1b Mon Sep 17 00:00:00 2001 From: redroot Date: Wed, 26 Jan 2022 12:46:48 +0000 Subject: [PATCH 4/9] remvoe wierd extra file --- config.h | 1 - 1 file changed, 1 deletion(-) delete mode 120000 config.h diff --git a/config.h b/config.h deleted file mode 120000 index af63eb9..0000000 --- a/config.h +++ /dev/null @@ -1 +0,0 @@ -../../../../Headers/ruby/config.h \ No newline at end of file From 0798f7bac90aae46ab9a0d3011f003fc2a09a5da Mon Sep 17 00:00:00 2001 From: redroot Date: Wed, 26 Jan 2022 14:19:48 +0000 Subject: [PATCH 5/9] adds contact upsert --- lib/basecrm/services/contacts_service.rb | 27 ++++++++++++++++++++++++ spec/services/contacts_service_spec.rb | 17 +++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/basecrm/services/contacts_service.rb b/lib/basecrm/services/contacts_service.rb index 19e1556..fe25c68 100644 --- a/lib/basecrm/services/contacts_service.rb +++ b/lib/basecrm/services/contacts_service.rb @@ -103,6 +103,28 @@ def update(contact) Contact.new(root[:data]) end + # Upsert a contact + # + # post '/contacts/upsert?filter_param=filter_value + # + # Create a new contact or update an existing, based on a value of a filter or a set of filters. + # At least a single filter - query parameter - is required. If no parameters are present, the request will return an error. + # See full docs https://developers.getbase.com/docs/rest/reference/contacts + # + # @param filters [Hash] - hash contain filters, one level deep e.g. { name: 'string', 'custom_fields[field]': 'value' } + # @param contact [Contact, Hash] - This object's attributes describe the object to be updated or created + # @return [Contact] The resulting object representing updated or created resource. + def upsert(filters, deal) + validate_upsert_filters!(filters) + validate_type!(deal) + + attributes = sanitize(deal) + query_string = URI.encode_www_form(filters) + _, _, root = @client.post("/contacts/upsert?#{query_string}", attributes) + + Contact.new(root[:data]) + end + # Delete a contact # @@ -125,6 +147,11 @@ def validate_type!(contact) raise TypeError unless contact.is_a?(Contact) || contact.is_a?(Hash) end + def validate_upsert_filters!(filters) + raise TypeError unless filters.is_a?(Hash) + raise ArgumentError, "at least one filter is required" if filters.empty? + end + def extract_params!(contact, *args) params = contact.to_h.select{ |k, _| args.include?(k) } raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length diff --git a/spec/services/contacts_service_spec.rb b/spec/services/contacts_service_spec.rb index 46b81ce..4900d00 100644 --- a/spec/services/contacts_service_spec.rb +++ b/spec/services/contacts_service_spec.rb @@ -9,6 +9,7 @@ it { should respond_to :destroy } it { should respond_to :find } it { should respond_to :update } + it { should respond_to :upsert } it { should respond_to :where } end @@ -48,6 +49,22 @@ end end + describe :upsert do + it 'raises a TypeError if filters is nil' do + expect(client.contacts.upsert(nil, { name: 'unique_name' }).to raise_error(TypeError) + end + + it 'raises an ArgumentError if filters is empty' do + expect(client.contacts.upsert({}, { name: 'unique_name' }).to raise_error(ArgumentError) + end + + it 'calls the upsert route with encoded filters' do + filters = { name: 'unique_name', 'custom_fields[external_id]': 'unique-1' } + contact = create(:contact) + expect(client.contacts.upsert(filters, contact)).to be_instance_of BaseCRM::Contact + end + end + describe :destroy do it "returns true on success" do @contact = create(:contact) From 804659211c0c133bf2c925d01d962a7ebd06659d Mon Sep 17 00:00:00 2001 From: redroot Date: Wed, 26 Jan 2022 14:21:34 +0000 Subject: [PATCH 6/9] improve specs --- spec/services/contacts_service_spec.rb | 4 ++-- spec/services/deals_service_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/services/contacts_service_spec.rb b/spec/services/contacts_service_spec.rb index 4900d00..a3532fb 100644 --- a/spec/services/contacts_service_spec.rb +++ b/spec/services/contacts_service_spec.rb @@ -59,8 +59,8 @@ end it 'calls the upsert route with encoded filters' do - filters = { name: 'unique_name', 'custom_fields[external_id]': 'unique-1' } - contact = create(:contact) + filters = { last_name: 'unique_name', 'custom_fields[external_id]': 'unique-1' } + contact = create(:contact, last_name: 'unique_name', custom_fields: { external_id: 'unique-1'}) expect(client.contacts.upsert(filters, contact)).to be_instance_of BaseCRM::Contact end end diff --git a/spec/services/deals_service_spec.rb b/spec/services/deals_service_spec.rb index f413d91..a8e187d 100644 --- a/spec/services/deals_service_spec.rb +++ b/spec/services/deals_service_spec.rb @@ -71,8 +71,8 @@ it 'calls the upsert route with encoded filters' do filters = { name: 'unique_name', 'custom_fields[external_id]': 'unique-1' } - attributes = filters.merge('custom_fields[category]': 'bags') - expect(client.deals.upsert(filters, attributes)).to be_instance_of BaseCRM::Deal + deal = create(:deal, name: 'unique_name', custom_fields: { external_id: 'unique-1'}) + expect(client.deals.upsert(filters, deal)).to be_instance_of BaseCRM::Deal end end From 29c8bd5031d035d0a1f7c117caac7a6b7ad4d634 Mon Sep 17 00:00:00 2001 From: Luke Williams <425213+redroot@users.noreply.github.com> Date: Fri, 28 Jan 2022 18:00:25 +0000 Subject: [PATCH 7/9] Update lib/basecrm/services/contacts_service.rb Co-authored-by: Emiliano Mancuso --- lib/basecrm/services/contacts_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/basecrm/services/contacts_service.rb b/lib/basecrm/services/contacts_service.rb index fe25c68..a2b2c8e 100644 --- a/lib/basecrm/services/contacts_service.rb +++ b/lib/basecrm/services/contacts_service.rb @@ -116,7 +116,7 @@ def update(contact) # @return [Contact] The resulting object representing updated or created resource. def upsert(filters, deal) validate_upsert_filters!(filters) - validate_type!(deal) + validate_type!(contact) attributes = sanitize(deal) query_string = URI.encode_www_form(filters) From a2917a332fa4f86685d2841d7a8438477f6a74d8 Mon Sep 17 00:00:00 2001 From: Luke Williams <425213+redroot@users.noreply.github.com> Date: Fri, 28 Jan 2022 18:01:13 +0000 Subject: [PATCH 8/9] Update lib/basecrm/services/contacts_service.rb Co-authored-by: Emiliano Mancuso --- lib/basecrm/services/contacts_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/basecrm/services/contacts_service.rb b/lib/basecrm/services/contacts_service.rb index a2b2c8e..3acf7df 100644 --- a/lib/basecrm/services/contacts_service.rb +++ b/lib/basecrm/services/contacts_service.rb @@ -118,7 +118,7 @@ def upsert(filters, deal) validate_upsert_filters!(filters) validate_type!(contact) - attributes = sanitize(deal) + attributes = sanitize(contact) query_string = URI.encode_www_form(filters) _, _, root = @client.post("/contacts/upsert?#{query_string}", attributes) From 70927ccfb881f62a4ecb00ece53f86d3a48bb578 Mon Sep 17 00:00:00 2001 From: Luke Williams <425213+redroot@users.noreply.github.com> Date: Fri, 28 Jan 2022 18:01:17 +0000 Subject: [PATCH 9/9] Update lib/basecrm/services/contacts_service.rb Co-authored-by: Emiliano Mancuso --- lib/basecrm/services/contacts_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/basecrm/services/contacts_service.rb b/lib/basecrm/services/contacts_service.rb index 3acf7df..d199519 100644 --- a/lib/basecrm/services/contacts_service.rb +++ b/lib/basecrm/services/contacts_service.rb @@ -114,7 +114,7 @@ def update(contact) # @param filters [Hash] - hash contain filters, one level deep e.g. { name: 'string', 'custom_fields[field]': 'value' } # @param contact [Contact, Hash] - This object's attributes describe the object to be updated or created # @return [Contact] The resulting object representing updated or created resource. - def upsert(filters, deal) + def upsert(filters, contact) validate_upsert_filters!(filters) validate_type!(contact)