mattbeedle / capsule_crm

Ruby CapsuleCRM API consumer
MIT License
26 stars 16 forks source link

Multiple saves duplicate contact information for an organization #89

Closed madleech closed 9 years ago

madleech commented 9 years ago

If I create a new organization and save it multiple times, the contact details get duplicated. Looks the same as #46?

Example code:

org = CapsuleCRM::Organization.new(name: "Test org")
org.phones << CapsuleCRM::Phone.new(phone_number: 1234, type: 'Work')
org.save
org.save
org.save

Log output:

First save
CapsuleCRM: POST /api/organisation with {"organisation":{"name":"Test org","contacts":{"phone":[{"type":"Work","phoneNumber":"1234"}]}}}
CapsuleCRM Response:
CapsuleCRM: GET /api/party/73750163/customfields with {}
CapsuleCRM Response: {"customFields":{"@size":"0"}}

Second save
CapsuleCRM: PUT /api/organisation/73750163 with {"organisation":{"name":"Test org","contacts":{"phone":[{"type":"Work","phoneNumber":"1234"}]}}}
CapsuleCRM Response: {"organisation":{"id":"73750163","contacts":{"phone":[{"id":"142214664","type":"Work","phoneNumber":"1234"},{"id":"142214668","type":"Work","phoneNumber":"1234"}]},"pictureURL":"https:\/\/d365sd3k9yw37.cloudfront.net\/a\/1420623822\/theme\/default\/images\/org_avatar_70.png","createdOn":"2015-01-13T01:32:56Z","updatedOn":"2015-01-13T01:33:00.025Z","name":"Test org"}}

Third save
CapsuleCRM: PUT /api/organisation/73750163 with {"organisation":{"name":"Test org","contacts":{"phone":[{"type":"Work","phoneNumber":"1234"}]}}}
CapsuleCRM Response: {"organisation":{"id":"73750163","contacts":{"phone":[{"id":"142214664","type":"Work","phoneNumber":"1234"},{"id":"142214668","type":"Work","phoneNumber":"1234"},{"id":"142214669","type":"Work","phoneNumber":"1234"}]},"pictureURL":"https:\/\/d365sd3k9yw37.cloudfront.net\/a\/1420623822\/theme\/default\/images\/org_avatar_70.png","createdOn":"2015-01-13T01:32:56Z","updatedOn":"2015-01-13T01:33:02.232Z","name":"Test org"}}

Each PUT is the same, but each response includes duplicate contact details.

# ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

# bundle show capsule_crm
/Library/Ruby/Gems/2.0.0/gems/capsule_crm-1.10.0
mattbeedle commented 9 years ago

hmmm, this does look the same as #46. There must have been a regression at some point. I can see in the requests there that no ID is being sent with any of the contact details so the capsule api thinks they are new every time.

mattbeedle commented 9 years ago

Looking into this now @madleech. At the moment when a record is created or updated none of the attributes are refreshed, which means that the various types of contact details don't get assigned an ID. You can workaround this bug at the moment by completely reloading the record:

org = CapsuleCRM::Organization.new(name: "Test org")
org.phones << CapsuleCRM::Phone.new(phone_number: 1234, type: 'Work')
org.save
org = CapsuleCRM::Organization.find(org.id)
org.phones << CapsuleCRM::Phone.new(phone_number: 5678, type: 'Home')
org.save

I'll try to get a fix in place today though.

mattbeedle commented 9 years ago

Ok, you can at least use #reload now if you upgrade to ~> 1.10.1. The other fix is a bit trickier mainly due to some pretty terrible design decisions I made early in the development of this gem.

madleech commented 9 years ago

Great, #reload makes things a lot cleaner. Thanks heaps.