dulvie / emmy

Emmy - organize sales for small retail businesses
MIT License
0 stars 0 forks source link

Unable to send email due to issue with contact relation #322

Open qzio opened 6 years ago

qzio commented 6 years ago

While trying to send the invoice using an email template, the browser is redirected to a 404 and this is found in the logs:

2018-06-07T18:15:17.311756 #25909]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"****", "return_path"=>"/AN_ORGANIZATION/sales?locale=se", "sale"=>{"mail_template"=>"1"}, "commit"=>"Bekräfta", "locale"=>"se", "organization_slug"=>"AN_ORGANIZATION", "id"=>"1234"}
I, [2018-06-07T18:15:17.338744 #25909]  INFO -- : Completed 401 Unauthorized in 27ms (ActiveRecord: 3.4ms)
F, [2018-06-07T18:15:17.340440 #25909] FATAL -- :
ActiveRecord::RecordNotFound (Couldn't find all Contacts with 'id': (#<Contact:0x0000563dc8fee500>, #<Contact:0x0000563dc8fee2a8>) [WHERE "contacts"."organization_id" = ?] (found 1 results, but was looking for 2)):
  app/mailers/invoice_mailer.rb:42:in `set_text' 
  app/mailers/invoice_mailer.rb:13:in `invoice_email'
  app/models/sale.rb:342:in `send_invoice!'
  app/controllers/sales_controller.rb:105:in `send_email'
qzio commented 6 years ago

I did some debugging and it seems as this particular user actually have two "contact" object for this particular organization. The objects have the same creation date so I assume this is a mistake, maybe due to a "double-tap" on the create button or something.

qzio commented 6 years ago

Further look, when I tried to remove the extra "contact" object surfaced UsersController#show which checks for nil, on the contact_relations, but then uses the contact.

The issue is NOT that there's multiple Contact object, but rather multiple ContactRelation objects!

  1. It should not be possible to submit a form twice by double-tapping the submit button
  2. we need to be careful when using habtm relations