mailjet / mailjet-gem

[API v3] Mailjet official Ruby GEM
https://dev.mailjet.com
Other
130 stars 72 forks source link

Cannot create a user in a contacts list #48

Closed EtienneDepaulis closed 9 years ago

EtienneDepaulis commented 9 years ago

Hello,

I'm experiencing a very weird and annoying bug using the "rails_fix branch of this gem. When I run:

user = User.find 11
contact_params = {id: 1178, action: "addforce", email: user.email, first_name: user.first_name, last_name: user.last_name}
contact = Mailjet::Contactslist_managecontact.create(contact_params)

I get:

 #<Mailjet::Contactslist_managecontact:0x007fcac506ea90
 @attributes=
  {"persisted"=>false,
   "id"=>1178,
   "action"=>"addforce",
   "email"=>"my_email@gmail.com",
   "first_name"=>"First",
   "last_name"=>"Last",
   "contact_id"=>1505363094,
   "name"=>""},
 @persisted=true>

But then if I want to add another user, I get this error every time:

Mailjet::ApiError: error 400 while sending #<RestClient::Resource:0x007fcac0649348 @url="https://api.mailjet.com/v3/REST/contactslist/1178/managecontact", @block=nil, @options={:public_operations=>[:post], :read_only=>nil, :user=>"d6...", :password=>"35...", :content_type=>"application/json"}> to https://api.mailjet.com/v3/REST/contactslist/1178/managecontact with {}

"{ \"ErrorMessage\" : \"Object properties invalid\", \"StatusCode\" : 400, \"ErrorInfo\" : { \"Email\" : \"MJ08 Property email is invalid: MJ03 A non-empty value is required\", \"Action\" : \"Invalid action: \" } }"

Please see http://api.mailjet.com/0.1/HelpStatus for more informations on error numbers.

What is really weird is I quit the rails console, then it works again but only for the first try. Then the error again ...

What am I doing wrong ??

EtienneDepaulis commented 9 years ago

Looks like the problem could be coming from the fact that if I do (after having tried at least once to save the contact):

contact = Mailjet::Contactslist_managecontact.new(contact_params)
contact.class.properties

I get: {} Meaning that https://github.com/mailjet/mailjet-gem/blob/master/lib/mailjet/resource.rb#L235 then erase all the properties while formating the payload...

WeshGuillaume commented 9 years ago

Hi @EtienneDepaulis ! thank you for using Mailjet ! I faced the issue a few days ago! strangely, it doesn't happen without rails. I will investigate and get back to you ! Thank you ! Best, Guillaume

EtienneDepaulis commented 9 years ago

Here is the dirty fix I found:

contact_params = {id: main_contact_list_id, action: "addnoforce", email: user.email}
contact = Mailjet::Contactslist_managecontact.create(contact_params)

Mailjet::Contactslist_managecontact.properties = [:email, :name, :action, :properties]

Mailjet::Contactdata.create(:contact_id => contact.contact_id, :data => [...])
WeshGuillaume commented 9 years ago

Hahah ! dirty indeed ! I guess you could use that until I come up with a solution !

EtienneDepaulis commented 9 years ago

I had too, looks like Mailjet support don't give a shit about support request sadly :/

WeshGuillaume commented 9 years ago

Sorry you have this feeling. we're working hard on this issue. I manage to identify the bug. I'll keep you up to date on that ! Best, Guillaume

WeshGuillaume commented 9 years ago

I found out why it didn't work. A naming conflict in our wrapper. Could you tell me if this works for you on the rails_fix: branch ?

EtienneDepaulis commented 9 years ago

Thanks for looking into it. However, we already lost 3 hours today (and few more 2 weeks ago) with this bug. This gem should really have specs in my opinion ...

WeshGuillaume commented 9 years ago

Hey ! I started implementing specs to test the wrapper. Thank you for your time. If you need anything else, I would be happy to help, otherwise, can you please close the issue ? Best, Guillaume

EtienneDepaulis commented 9 years ago

Has this gem been updated on Rubygems ? If not, the issue still exists ...

WeshGuillaume commented 9 years ago

Hey ! @EtienneDepaulis

I am just letting you know that we released a new version on RubyGem with specs this time, and the rails_fix branch is now merged into master. We are planning to constantly grow our tests over time. Feel free to contribute if you think it lacks some useful checks !

Best, Guillaume from Mailjet !

EtienneDepaulis commented 9 years ago

awesome thx :)