From 39f0c5b5b05179eb52534850ea84bae03d6774b6 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Thu, 21 Dec 2017 15:56:37 +0000 Subject: [PATCH] Rename contact name field and revert contact name specs --- app/jobs/update_account_invoices.rb | 2 + .../admin/enterprises/_new_form.html.haml | 5 +++ .../admin/enterprises/form/_contact.html.haml | 5 +++ .../registration/steps/_contact.html.haml | 6 +++ ...728140134_remove_email_from_enterprises.rb | 37 +++++++++++-------- db/schema.rb | 1 + lib/tasks/enterprises.rake | 4 +- .../api/enterprises_controller_spec.rb | 2 +- spec/features/admin/enterprises_spec.rb | 2 + spec/features/consumer/registration_spec.rb | 2 + spec/jobs/update_account_invoices_spec.rb | 10 ++++- 11 files changed, 56 insertions(+), 20 deletions(-) diff --git a/app/jobs/update_account_invoices.rb b/app/jobs/update_account_invoices.rb index f042ddb9ef..a2a726df3e 100644 --- a/app/jobs/update_account_invoices.rb +++ b/app/jobs/update_account_invoices.rb @@ -37,7 +37,9 @@ class UpdateAccountInvoices if billable_periods.any? oldest_enterprise = billable_periods.first.enterprise address = oldest_enterprise.address.dup + first, _space, last = (oldest_enterprise.contact_name || "").partition(' ') address.update_attributes(phone: oldest_enterprise.phone) if oldest_enterprise.phone.present? + address.update_attributes(firstname: first, lastname: last) if first.present? && last.present? account_invoice.order.update_attributes(bill_address: address, ship_address: address) end diff --git a/app/views/admin/enterprises/_new_form.html.haml b/app/views/admin/enterprises/_new_form.html.haml index 3ca2c6c8a9..9d25fbff7a 100644 --- a/app/views/admin/enterprises/_new_form.html.haml +++ b/app/views/admin/enterprises/_new_form.html.haml @@ -44,6 +44,11 @@   = f.label :sells, t('admin.enterprises.form.primary_details.any'), value: "any" +.row + .alpha.three.columns + = f.label :contact_name, t('.contact_name') + .omega.nine.columns + = f.text_field :contact_name, { placeholder: t('admin.enterprises.form.contact.name_placeholder')} .row .alpha.three.columns = f.label :email_address, t('admin.enterprises.form.contact.email_address') diff --git a/app/views/admin/enterprises/form/_contact.html.haml b/app/views/admin/enterprises/form/_contact.html.haml index 12b0c562c0..61f7eb341d 100644 --- a/app/views/admin/enterprises/form/_contact.html.haml +++ b/app/views/admin/enterprises/form/_contact.html.haml @@ -1,3 +1,8 @@ +.row + .alpha.three.columns + = f.label :contact_name, t('.name') + .omega.eight.columns + = f.text_field :contact_name, { placeholder: t('.name_placeholder') } .row .alpha.three.columns = f.label :email_address, t('.email_address') diff --git a/app/views/registration/steps/_contact.html.haml b/app/views/registration/steps/_contact.html.haml index cf8bb510a8..746503282d 100644 --- a/app/views/registration/steps/_contact.html.haml +++ b/app/views/registration/steps/_contact.html.haml @@ -10,6 +10,12 @@ %form{ name: 'contact', novalidate: true, ng: { controller: "RegistrationFormCtrl", submit: "selectIfValid('type',contact)" } } .row.content .small-12.medium-12.large-7.columns + .row + .small-12.columns.field + %label{ for: 'enterprise_contact' } {{'enterprise.registration.modal.steps.contact.contact_field' | t}}: + %input.chunky.small-12.columns{ id: 'enterprise_contact', name: 'contact_name', required: true, placeholder: "{{'enterprise.registration.modal.steps.contact.contact_field_placeholder' | t}}", ng: { model: 'enterprise.contact_name' } } + %span.error.small-12.columns{ ng: { show: "contact.contact_name.$error.required && submitted" } } + {{'enterprise.registration.modal.steps.contact.contact_field_required' | t}} .row .small-12.columns.field %label{ for: 'enterprise_email_address' } {{'admin.enterprises.form.contact.email_address' | t}}: diff --git a/db/migrate/20170728140134_remove_email_from_enterprises.rb b/db/migrate/20170728140134_remove_email_from_enterprises.rb index 332daef87f..9e9acb936a 100644 --- a/db/migrate/20170728140134_remove_email_from_enterprises.rb +++ b/db/migrate/20170728140134_remove_email_from_enterprises.rb @@ -13,22 +13,11 @@ class RemoveEmailFromEnterprises < ActiveRecord::Migration EnterpriseRole.reset_column_information Enterprise.select([:id, :email, :owner_id]).each do |enterprise| - contact_user = Spree::User.find_by_email enterprise.email - unless contact_user - password = Devise.friendly_token.first(8) - contact_user = Spree::User.create(email: enterprise.email, password: password, password_confirmation: password) - contact_user.send_reset_password_instructions if contact_user.persisted? - end - - unless contact_user.persisted? - contact_user = Spree::User.find enterprise.owner_id - end - - manager = EnterpriseRole.find_or_initialize_by_user_id_and_enterprise_id(contact_user.id, enterprise.id) - manager.update_attribute :receives_notifications, true + update_enterprise_contact enterprise end - remove_columns :enterprises, :email, :contact + remove_column :enterprises, :email + rename_column :enterprises, :contact, :contact_name end def down @@ -37,7 +26,7 @@ class RemoveEmailFromEnterprises < ActiveRecord::Migration EnterpriseRole.reset_column_information add_column :enterprises, :email, :string - add_column :enterprises, :contact, :string + rename_column :enterprises, :contact_name, :contact Enterprise.select(:id).each do |e| manager = EnterpriseRole.find_by_enterprise_id_and_receives_notifications(e.id, true) @@ -45,4 +34,22 @@ class RemoveEmailFromEnterprises < ActiveRecord::Migration e.update_attribute :email, user.email end end + + def update_enterprise_contact(enterprise) + contact_user = Spree::User.find_by_email(enterprise.email) || create_contact_user(enterprise) + + unless contact_user.persisted? + contact_user = Spree::User.find enterprise.owner_id + end + + manager = EnterpriseRole.find_or_initialize_by_user_id_and_enterprise_id(contact_user.id, enterprise.id) + manager.update_attribute :receives_notifications, true + end + + def create_contact_user(enterprise) + password = Devise.friendly_token.first(8) + contact_user = Spree::User.create(email: enterprise.email, password: password, password_confirmation: password) + contact_user.send_reset_password_instructions if contact_user.persisted? + contact_user + end end diff --git a/db/schema.rb b/db/schema.rb index 7afa8b554a..be414f9187 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -247,6 +247,7 @@ ActiveRecord::Schema.define(:version => 20170921065259) do t.text "invoice_text" t.boolean "display_invoice_logo", :default => false t.boolean "allow_order_changes", :default => false, :null => false + t.string "contact_name" end add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id" diff --git a/lib/tasks/enterprises.rake b/lib/tasks/enterprises.rake index f974e4875e..62d56fe8a5 100644 --- a/lib/tasks/enterprises.rake +++ b/lib/tasks/enterprises.rake @@ -20,11 +20,11 @@ namespace :openfoodnetwork do end def enterprise_header - ['name', 'description', 'long_description', 'is_primary_producer', 'is_distributor', 'phone', 'email', 'website', 'twitter', 'abn', 'acn', 'pickup_times', 'next_collection_at', 'distributor_info', 'visible', 'facebook', 'instagram', 'linkedin', 'address1', 'address2', 'city', 'zipcode', 'state', 'country'] + ['name', 'description', 'long_description', 'is_primary_producer', 'is_distributor', 'contact_name', 'phone', 'email', 'website', 'twitter', 'abn', 'acn', 'pickup_times', 'next_collection_at', 'distributor_info', 'visible', 'facebook', 'instagram', 'linkedin', 'address1', 'address2', 'city', 'zipcode', 'state', 'country'] end def enterprise_row(enterprise) - [enterprise.name, enterprise.description, enterprise.long_description, enterprise.is_primary_producer, enterprise.is_distributor, enterprise.phone, enterprise.contact.email, enterprise.website, enterprise.twitter, enterprise.abn, enterprise.acn, enterprise.pickup_times, enterprise.next_collection_at, enterprise.distributor_info, enterprise.visible, enterprise.facebook, enterprise.instagram, enterprise.linkedin, enterprise.address.address1, enterprise.address.address2, enterprise.address.city, enterprise.address.zipcode, enterprise.address.state_name, enterprise.address.country.andand.name] + [enterprise.name, enterprise.description, enterprise.long_description, enterprise.is_primary_producer, enterprise.is_distributor, enterprise.contact_name, enterprise.phone, enterprise.email, enterprise.website, enterprise.twitter, enterprise.abn, enterprise.acn, enterprise.pickup_times, enterprise.next_collection_at, enterprise.distributor_info, enterprise.visible, enterprise.facebook, enterprise.instagram, enterprise.linkedin, enterprise.address.address1, enterprise.address.address2, enterprise.address.city, enterprise.address.zipcode, enterprise.address.state_name, enterprise.address.country.andand.name] end end end diff --git a/spec/controllers/api/enterprises_controller_spec.rb b/spec/controllers/api/enterprises_controller_spec.rb index 61502cc4ed..a16bfcbac2 100644 --- a/spec/controllers/api/enterprises_controller_spec.rb +++ b/spec/controllers/api/enterprises_controller_spec.rb @@ -20,7 +20,7 @@ module Api let(:new_enterprise_params) do { enterprise: { - name: 'name', email: 'email@example.com', address_attributes: { + name: 'name', contact_name: 'Sheila', address_attributes: { address1: '123 Abc Street', city: 'Northcote', zipcode: '3070', diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index d3e9ca8dfb..282bbe0713 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -45,6 +45,7 @@ feature %q{ select2_search admin.email, from: 'Owner' select2_search admin.email, from: 'Owner' + fill_in 'enterprise_contact_name', :with => 'Kirsten or Ren' fill_in 'enterprise_phone', :with => '0413 897 321' fill_in 'enterprise_email_address', :with => 'info@eaterprises.com.au' fill_in 'enterprise_website', :with => 'http://eaterprises.com.au' @@ -138,6 +139,7 @@ feature %q{ check "enterprise_shipping_method_ids_#{shipping_method.id}" click_link "Contact" + fill_in 'enterprise_contact_name', :with => 'Kirsten or Ren' fill_in 'enterprise_phone', :with => '0413 897 321' fill_in 'enterprise_email_address', :with => 'info@eaterprises.com.au' fill_in 'enterprise_website', :with => 'http://eaterprises.com.au' diff --git a/spec/features/consumer/registration_spec.rb b/spec/features/consumer/registration_spec.rb index 37caa9123e..70d132b11b 100644 --- a/spec/features/consumer/registration_spec.rb +++ b/spec/features/consumer/registration_spec.rb @@ -54,6 +54,7 @@ feature "Registration", js: true do # Filling in Contact Details + fill_in 'enterprise_contact', with: 'Saskia Munroe' page.should have_field 'enterprise_email_address', with: user.email fill_in 'enterprise_phone', with: '12 3456 7890' perform_and_ensure(:click_button, "Continue", lambda { page.has_content? 'Last step to add My Awesome Enterprise!' }) @@ -68,6 +69,7 @@ feature "Registration", js: true do expect(e.sells).to eq "unspecified" expect(e.is_primary_producer).to eq true expect(e.contact.id).to eq e.owner_id + expect(e.contact_name).to eq "Saskia Munroe" # Filling in about fill_in 'enterprise_description', with: 'Short description' diff --git a/spec/jobs/update_account_invoices_spec.rb b/spec/jobs/update_account_invoices_spec.rb index 1487a4a6c0..be9411d25c 100644 --- a/spec/jobs/update_account_invoices_spec.rb +++ b/spec/jobs/update_account_invoices_spec.rb @@ -157,7 +157,7 @@ describe UpdateAccountInvoices do context "where the order is not complete" do before do allow(invoice_order).to receive(:complete?) { false } - june_billable_period1.enterprise.update_attributes(phone: '12345') + june_billable_period1.enterprise.update_attributes(contact_name: "Firstname Lastname Something Else", phone: '12345') updater.update(june_account_invoice) end @@ -175,6 +175,8 @@ describe UpdateAccountInvoices do [:address1, :address2, :city, :zipcode, :state_id, :country_id].each do |attr| expect(invoice_order.billing_address[attr]).to eq june_billable_period1.enterprise.address[attr] end + expect(invoice_order.billing_address.firstname).to eq "Firstname" + expect(invoice_order.billing_address.lastname).to eq "Lastname Something Else" expect(invoice_order.billing_address.phone).to eq "12345" end @@ -352,7 +354,7 @@ describe UpdateAccountInvoices do before do Spree::Config.set({ accounts_distributor_id: accounts_distributor.id }) - july_billable_period2.enterprise.update_attributes(phone: '3433523') + july_billable_period2.enterprise.update_attributes(contact_name: 'Anna Karenina', phone: '3433523') end context "when no invoice_order currently exists" do @@ -375,6 +377,8 @@ describe UpdateAccountInvoices do [:address1, :address2, :city, :zipcode, :state_id, :country_id].each do |attr| expect(invoice_order.billing_address[attr]).to eq july_billable_period2.enterprise.address[attr] end + expect(invoice_order.billing_address.firstname).to eq "Anna" + expect(invoice_order.billing_address.lastname).to eq "Karenina" expect(invoice_order.billing_address.phone).to eq "3433523" end end @@ -417,6 +421,8 @@ describe UpdateAccountInvoices do [:address1, :address2, :city, :zipcode, :state_id, :country_id].each do |attr| expect(invoice_order.billing_address[attr]).to eq july_billable_period2.enterprise.address[attr] end + expect(invoice_order.billing_address.firstname).to eq "Anna" + expect(invoice_order.billing_address.lastname).to eq "Karenina" expect(invoice_order.billing_address.phone).to eq "3433523" end end