diff --git a/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee b/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee index 085a992a36..a9aa6148bf 100644 --- a/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee +++ b/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee @@ -1,5 +1,6 @@ # Used in enterprise new and edit forms to reset the state when the country is changed -angular.module("admin.enterprises").controller 'countryCtrl', ($scope, availableCountries) -> +angular.module("admin.enterprises").controller 'countryCtrl', ($scope, $timeout, availableCountries) -> + $scope.address_type = "address" $scope.countries = availableCountries $scope.countriesById = $scope.countries.reduce (obj, country) -> @@ -7,12 +8,13 @@ angular.module("admin.enterprises").controller 'countryCtrl', ($scope, available obj , {} - $scope.$watch 'Enterprise.address.country_id', (newID, oldID) -> - $scope.clearState() unless $scope.addressStateMatchesCountry() + $timeout -> + $scope.$watch 'Enterprise.' + $scope.address_type + '.country_id', (newID, oldID) -> + $scope.clearState() unless $scope.addressStateMatchesCountry() $scope.clearState = -> - $scope.Enterprise.address.state_id = null + $scope.Enterprise[$scope.address_type].state_id = null $scope.addressStateMatchesCountry = -> - $scope.countriesById[$scope.Enterprise.address.country_id].states.some (state) -> - state.id == $scope.Enterprise.address.state_id + $scope.countriesById[$scope.Enterprise[$scope.address_type].country_id].states.some (state) -> + state.id == $scope.Enterprise[$scope.address_type].state_id diff --git a/app/assets/javascripts/templates/admin/modals/business_address_info.html.haml b/app/assets/javascripts/templates/admin/modals/business_address_info.html.haml new file mode 100644 index 0000000000..9a0945e90c --- /dev/null +++ b/app/assets/javascripts/templates/admin/modals/business_address_info.html.haml @@ -0,0 +1,7 @@ +%div + .margin-bottom-30 + %p + {{ 'js.admin.modals.business_address_info.message' | t }} + + .text-center + %input.button.red.icon-plus{ type: 'button', value: '{{ "js.admin.modals.got_it" | t }}', ng: { click: 'close()' } } diff --git a/app/assets/stylesheets/admin/disabled.scss b/app/assets/stylesheets/admin/disabled.scss index d7c7805036..b454754f2a 100644 --- a/app/assets/stylesheets/admin/disabled.scss +++ b/app/assets/stylesheets/admin/disabled.scss @@ -10,6 +10,12 @@ input[type='button'], input[type='submit'] { background-color: $disabled-background; color: #ffffff; } + + &.secondary:disabled { + background-color: #ebf3fb; + border: 1px solid #ebf3fb; + color: #AFCFEF; + } } .select2-container-disabled { diff --git a/app/assets/stylesheets/admin/shared/forms.scss b/app/assets/stylesheets/admin/shared/forms.scss index d034595fd3..8f4ee24dde 100644 --- a/app/assets/stylesheets/admin/shared/forms.scss +++ b/app/assets/stylesheets/admin/shared/forms.scss @@ -85,6 +85,20 @@ button, .button { width: 100%; text-align: center; } + + &.secondary { + background-color: transparent; + border: 1px solid $color-btn-bg; + color: $color-btn-bg; + + &:hover, &:active, &:focus { + background-color: #ebf3fb; + } + + &:active:focus { + box-shadow: none; + } + } } span.info { diff --git a/app/models/concerns/set_unused_address_fields.rb b/app/models/concerns/set_unused_address_fields.rb new file mode 100644 index 0000000000..cb6f563f25 --- /dev/null +++ b/app/models/concerns/set_unused_address_fields.rb @@ -0,0 +1,14 @@ +require 'active_support/concern' + +module SetUnusedAddressFields + extend ActiveSupport::Concern + + included do + self.before_validation :set_unused_address_fields + end + + def set_unused_address_fields + ship_address.company = 'unused' if ship_address.present? + bill_address.company = 'unused' if bill_address.present? + end +end diff --git a/app/models/customer.rb b/app/models/customer.rb index 7f23c60589..fc030bd1a1 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Customer < ApplicationRecord + include SetUnusedAddressFields + acts_as_taggable searchable_attributes :name, :email, :code diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index aef90188da..1d2af37a91 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -5,7 +5,6 @@ require 'spree/core/s3_support' class Enterprise < ApplicationRecord SELLS = %w(unspecified none own any).freeze ENTERPRISE_SEARCH_RADIUS = 100 - searchable_attributes :sells, :is_primary_producer searchable_associations :properties searchable_scopes :is_primary_producer, :is_distributor, :is_hub, :activated, :visible, @@ -41,6 +40,7 @@ class Enterprise < ApplicationRecord dependent: :destroy has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id' belongs_to :address, class_name: 'Spree::Address' + belongs_to :business_address, class_name: 'Spree::Address', dependent: :destroy has_many :enterprise_fees has_many :enterprise_roles, dependent: :destroy has_many :users, through: :enterprise_roles @@ -59,6 +59,7 @@ class Enterprise < ApplicationRecord delegate :latitude, :longitude, :city, :state_name, to: :address accepts_nested_attributes_for :address + accepts_nested_attributes_for :business_address, reject_if: :business_address_empty?, allow_destroy: true accepts_nested_attributes_for :producer_properties, allow_destroy: true, reject_if: lambda { |pp| pp[:property_name].blank? @@ -210,6 +211,13 @@ class Enterprise < ApplicationRecord ", one, one, others) } + def business_address_empty?(attributes) + attributes_exists = attributes['id'].present? + attributes_empty = attributes.slice(:company, :address1, :city, :phone, :zipcode).values.all?(&:blank?) + attributes.merge!(_destroy: 1) if attributes_exists and attributes_empty + !attributes_exists && attributes_empty + end + # Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554 def self.distinct_count count(distinct: true) @@ -414,7 +422,8 @@ class Enterprise < ApplicationRecord end def set_unused_address_fields - address.firstname = address.lastname = address.phone = 'unused' if address.present? + address.firstname = address.lastname = address.phone = address.company = 'unused' if address.present? + business_address.first_name = business_address.last_name = 'unused' if business_address.present? end def ensure_owner_is_manager diff --git a/app/models/enterprise_group.rb b/app/models/enterprise_group.rb index 7d100ea2c6..1c3b435d9e 100644 --- a/app/models/enterprise_group.rb +++ b/app/models/enterprise_group.rb @@ -54,7 +54,7 @@ class EnterpriseGroup < ApplicationRecord } def set_unused_address_fields - address.firstname = address.lastname = I18n.t(:unused) + address.firstname = address.lastname = address.company = I18n.t(:unused) end def set_undefined_address_fields diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index 24ed00ef5c..cff38fab0e 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -13,7 +13,9 @@ module Spree has_one :enterprise, dependent: :restrict_with_exception has_many :shipments - validates :firstname, :lastname, :address1, :city, :country, presence: true + validates :address1, :city, :country, :phone, presence: true + validates :company, presence: true, unless: -> { first_name.blank? || last_name.blank?} + validates :firstname, :lastname, presence: true, unless: -> { company.present? } validates :zipcode, presence: true, if: :require_zipcode? validate :state_validate diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index e7af441b7e..e081e45559 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -10,6 +10,7 @@ module Spree include OrderShipment include Checkout include Balance + include SetUnusedAddressFields searchable_attributes :number, :state, :shipment_state, :payment_state, :distributor_id, :order_cycle_id, :email, :total diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index ccd8975f6f..c25634777f 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -2,6 +2,8 @@ module Spree class User < ApplicationRecord + include SetUnusedAddressFields + searchable_attributes :email devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable, diff --git a/app/models/subscription.rb b/app/models/subscription.rb index f533b617f5..8c219b6c3a 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Subscription < ApplicationRecord + include SetUnusedAddressFields + ALLOWED_PAYMENT_METHOD_TYPES = ["Spree::PaymentMethod::Check", "Spree::Gateway::StripeConnect", "Spree::Gateway::StripeSCA"].freeze diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index cc2a962f7e..6d45133e4c 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -17,6 +17,7 @@ module Api has_one :owner, serializer: Api::Admin::UserSerializer has_many :users, serializer: Api::Admin::UserSerializer has_one :address, serializer: Api::AddressSerializer + has_one :business_address, serializer: Api::AddressSerializer def logo attachment_urls(object.logo, [:thumb, :small, :medium]) diff --git a/app/services/permitted_attributes/business_address.rb b/app/services/permitted_attributes/business_address.rb new file mode 100644 index 0000000000..24348a353b --- /dev/null +++ b/app/services/permitted_attributes/business_address.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module PermittedAttributes + class BusinessAddress + def self.attributes + [ + :company, :address1, :address2, + :city, :country_id, :state_id, :zipcode, + :phone, :_destroy, :id + ] + end + end +end diff --git a/app/services/permitted_attributes/enterprise.rb b/app/services/permitted_attributes/enterprise.rb index a3f688c64d..dbae9dee32 100644 --- a/app/services/permitted_attributes/enterprise.rb +++ b/app/services/permitted_attributes/enterprise.rb @@ -17,6 +17,7 @@ module PermittedAttributes group_ids: [], user_ids: [], shipping_method_ids: [], payment_method_ids: [], address_attributes: PermittedAttributes::Address.attributes, + business_address_attributes: PermittedAttributes::BusinessAddress.attributes, producer_properties_attributes: [:id, :property_name, :value, :_destroy] ] end diff --git a/app/views/admin/enterprises/form/_business_address.html.haml b/app/views/admin/enterprises/form/_business_address.html.haml new file mode 100644 index 0000000000..be99ce6cfd --- /dev/null +++ b/app/views/admin/enterprises/form/_business_address.html.haml @@ -0,0 +1,46 @@ +.row + .three.columns.alpha + = bf.label :company, t(".company_legal_name") + %i.text-big.icon-question-sign.help-modal{ template: 'admin/modals/business_address_info.html' } + .eight.columns.omega + = bf.text_field :company, { placeholder: t(".company_placeholder") } + +.row + .three.columns.alpha + = bf.label :address1, t('.address1') + %i.text-big.icon-question-sign.help-modal{ template: 'admin/modals/business_address_info.html' } + .eight.columns.omega + = bf.text_field :address1, { placeholder: t(".address1_placeholder") } +.row + .alpha.three.columns + = bf.label :address2, t(".address2") + .eight.columns.omega + = bf.text_field :address2 +.row + .three.columns.alpha + = bf.label :city, t(:city) + \/ + = bf.label :zipcode, t(:postcode) + .four.columns + = bf.text_field :city, { placeholder: t(:city_placeholder) } + .four.columns.omega + = bf.text_field :zipcode, { placeholder: t(:postcode_placeholder) } +.row + .three.columns.alpha + = bf.label :country_id, t(:country) + \/ + = bf.label :state_id, t(:state) + %div{ "ng-controller": "countryCtrl", "ng-init": "address_type='business_address'" } + .four.columns + %input.ofn-select2.fullwidth#enterprise_business_address_attributes_country_id{ name: 'enterprise[business_address_attributes][country_id]', type: 'number', data: 'countries', placeholder: t('admin.choose'), ng: { model: 'Enterprise.business_address.country_id' } } + .four.columns.omega + %input.ofn-select2.fullwidth#enterprise_business_address_attributes_state_id{ name: 'enterprise[business_address_attributes][state_id]', type: 'number', data: 'countriesById[Enterprise.address.country_id].states', placeholder: t('admin.choose'), ng: { model: 'Enterprise.business_address.state_id' } } + + +.row + .three.columns.alpha + = bf.label :phone, t(".legal_phone_number") + %i.text-big.icon-question-sign.help-modal{ template: 'admin/modals/business_address_info.html' } + .eight.columns.omega + = bf.text_field :phone, { placeholder: t(".phone_placeholder") } + diff --git a/app/views/admin/enterprises/form/_business_details.html.haml b/app/views/admin/enterprises/form/_business_details.html.haml index 1bade5b580..b5a36a527e 100644 --- a/app/views/admin/enterprises/form/_business_details.html.haml +++ b/app/views/admin/enterprises/form/_business_details.html.haml @@ -47,3 +47,13 @@ .pad-top %a.button.red{ href: '', ng: {click: 'removeTermsAndConditions()', if: 'Enterprise.terms_and_conditions'} } = t('.remove_terms_and_conditions') + + += f.fields_for :business_address, @enterprise.business_address || @enterprise.build_business_address do |bf| + %fieldset.alpha.no-border-bottom + %legend= t('business_address') + = render 'admin/enterprises/form/business_address', bf: bf + + .row{"data-controller": "updateinput"} + = bf.hidden_field :_destroy, {"data-updateinput-target": "input"} + = f.submit t(".reset_form"), {class: 'secondary', "data-action": "click->updateinput#update", "data-updateinput-value": "true"} diff --git a/app/views/spree/admin/orders/invoice.html.haml b/app/views/spree/admin/orders/invoice.html.haml index 73629cea18..1ee17ea165 100644 --- a/app/views/spree/admin/orders/invoice.html.haml +++ b/app/views/spree/admin/orders/invoice.html.haml @@ -18,12 +18,18 @@ %h4= @order.order_cycle&.name %tr{ valign: "top" } %td{ align: "left", colspan: 3 } - %strong= "#{t('.from')}: #{@order.distributor.name}" + - if @order.distributor.business_address.blank? + %strong= "#{t('.from')}: #{@order.distributor.name}" + - else + %strong= "#{t('.from')}: #{@order.distributor.business_address.company}" - if @order.distributor.abn.present? %br = "#{t(:abn)} #{@order.distributor.abn}" %br - = @order.distributor.address.full_address + - if @order.distributor.business_address.blank? + = @order.distributor.address.full_address + - else + = @order.distributor.business_address.full_address %br = @order.distributor.contact.email %tr{ valign: "top" } diff --git a/app/views/spree/admin/orders/invoice2.html.haml b/app/views/spree/admin/orders/invoice2.html.haml index c38472e0d9..9428d62eca 100644 --- a/app/views/spree/admin/orders/invoice2.html.haml +++ b/app/views/spree/admin/orders/invoice2.html.haml @@ -12,16 +12,28 @@ = wicked_pdf_image_tag @order.distributor.logo(:small), width: 150, height: 150 %tr{ valign: "top" } %td{ :align => "left" } - %strong= @order.distributor.name - %br - = @order.distributor.address.address_part1 - %br - = @order.distributor.address.address_part2 - %br - = @order.distributor.email_address - - if @order.distributor.phone.present? + - if @order.distributor.business_address.blank? + %strong= @order.distributor.name %br - = @order.distributor.phone + = @order.distributor.address.address_part1 + %br + = @order.distributor.address.address_part2 + %br + = @order.distributor.email_address + - if @order.distributor.phone.present? + %br + = @order.distributor.phone + - else + %strong= @order.distributor.business_address.company + %br + = @order.distributor.business_address.address_part1 + %br + = @order.distributor.business_address.address_part2 + %br + = @order.distributor.email_address + - if @order.distributor.business_address.phone.present? + %br + = @order.distributor.business_address.phone - if @order.distributor.abn.present? %br = "#{t :abn} #{@order.distributor.abn}" diff --git a/app/views/spree/admin/shared/_head.html.haml b/app/views/spree/admin/shared/_head.html.haml index e783e2f07e..becc6d0309 100644 --- a/app/views/spree/admin/shared/_head.html.haml +++ b/app/views/spree/admin/shared/_head.html.haml @@ -18,6 +18,8 @@ = render "spree/admin/shared/translations" = render "spree/admin/shared/routes" += javascript_pack_tag "admin", "data-turbo-track": "reload" + %script = raw "var AUTH_TOKEN = \"#{form_authenticity_token}\";" diff --git a/app/webpacker/controllers/updateinput_controller.js b/app/webpacker/controllers/updateinput_controller.js new file mode 100644 index 0000000000..aa72ae2086 --- /dev/null +++ b/app/webpacker/controllers/updateinput_controller.js @@ -0,0 +1,13 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["input"]; + + update(event) { + const value = event.currentTarget.dataset.updateinputValue; + + this.inputTargets.forEach((t) => { + t.value = value; + }); + } +} diff --git a/app/webpacker/packs/admin.js b/app/webpacker/packs/admin.js new file mode 100644 index 0000000000..38fb32259e --- /dev/null +++ b/app/webpacker/packs/admin.js @@ -0,0 +1,6 @@ +import { Application } from "stimulus" +import { definitionsFromContext } from "stimulus/webpack-helpers" + +const application = Application.start() +const context = require.context("controllers", true, /.js$/) +application.load(definitionsFromContext(context)) diff --git a/config/locales/en.yml b/config/locales/en.yml index d9e0955a23..562227021a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -265,6 +265,7 @@ en: producers: 'Australian Producers' producers_join: Australian producers are now welcome to join the Open Food Network. #FIXME charges_sales_tax: Charges GST? + business_address: "Business Address" print_invoice: "Print Invoice" print_ticket: "Print Ticket" select_ticket_printer: "Select printer for tickets" @@ -774,6 +775,15 @@ en: terms_and_conditions: "Terms and Conditions" remove_terms_and_conditions: "Remove File" uploaded_on: "uploaded on" + reset_form: "Reset Form" + business_address: + company_legal_name: Company Legal Name + company_placeholder: Example Inc. + address1: Legal address + address1_placeholder: 123 High St. + address2: Address(contd.) + legal_phone_number: Legal phone number + phone_placeholder: "98 123 4565" contact: name: Name name_placeholder: eg. Gustav Plum @@ -2739,6 +2749,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using title: "Uploading Terms and Conditions" message_1: "All your buyers will have to agree to them once at checkout. If you update the file, all your buyers will have to agree to them again at checkout." message_2: "For buyers with subscriptions, you need to email them the Terms and Conditions (or the changes to them) for now, nothing will notify them about these new Terms and Conditions." + business_address_info: + message: "Company Legal Name, Legal Address and Legal Phone number are used for businesses that invoice from a legal entity registered with different details to their public trading information. These details will ONLY be used on invoices. If these details are blank your public Name, Address and Phone Number will be used on invoices." panels: save: SAVE saved: SAVED diff --git a/db/migrate/20210726132054_add_business_address_id_to_enterprises.rb b/db/migrate/20210726132054_add_business_address_id_to_enterprises.rb new file mode 100644 index 0000000000..2fc63c138f --- /dev/null +++ b/db/migrate/20210726132054_add_business_address_id_to_enterprises.rb @@ -0,0 +1,5 @@ +class AddBusinessAddressIdToEnterprises < ActiveRecord::Migration[6.1] + def change + add_column :enterprises, :business_address_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 97dd60cf72..8f91507988 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -209,6 +209,7 @@ ActiveRecord::Schema.define(version: 2021_09_27_091723) do t.string "terms_and_conditions_content_type", limit: 255 t.integer "terms_and_conditions_file_size" t.datetime "terms_and_conditions_updated_at" + t.integer "business_address_id" t.index ["address_id"], name: "index_enterprises_on_address_id" t.index ["is_primary_producer", "sells"], name: "index_enterprises_on_is_primary_producer_and_sells" t.index ["name"], name: "index_enterprises_on_name", unique: true diff --git a/spec/factories/address_factory.rb b/spec/factories/address_factory.rb index 3a2dadf090..036318c6f3 100644 --- a/spec/factories/address_factory.rb +++ b/spec/factories/address_factory.rb @@ -4,7 +4,7 @@ FactoryBot.define do factory :address, aliases: [:bill_address, :ship_address], class: Spree::Address do firstname { 'John' } lastname { 'Doe' } - company { 'Company' } + company { 'unused' } address1 { '10 Lovely Street' } address2 { 'Northwest' } city { 'Herndon' } diff --git a/spec/features/admin/enterprises/business_address_form_spec.rb b/spec/features/admin/enterprises/business_address_form_spec.rb new file mode 100644 index 0000000000..38c9681db7 --- /dev/null +++ b/spec/features/admin/enterprises/business_address_form_spec.rb @@ -0,0 +1,143 @@ +require "spec_helper" + +describe "Business Address" do + include WebHelper + include AuthenticationHelper + + context "as an Enterprise user", js: true do + let(:enterprise_user) { create(:user, enterprise_limit: 1) } + let(:distributor) { create(:distributor_enterprise, name: "First Distributor") } + + before do + enterprise_user.enterprise_roles.build(enterprise: distributor).save! + + login_as enterprise_user + visit edit_admin_enterprise_path(distributor) + end + + describe "Business Address form" do + def go_to_business_details + within(".side_menu") do + click_link "Business Details" + end + end + + before do + go_to_business_details + end + + it 'adds a business address' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + expect(page).to have_content("Enterprise \"First Distributor\" has been successfully updated!") + end + + it 'is missing company field' do + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + expect(page).to have_content("Business address company can't be blank") + end + + it 'is missing address field' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + expect(page).to have_content("Business address address1 can't be blank") + end + + it 'is missing city field' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + expect(page).to have_content("Business address city can't be blank") + end + + it 'is missing zipcode field' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + expect(page).to have_content("Business address zipcode can't be blank") + end + + it 'is missing phone field' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + + click_button "Update" + expect(page).to have_content("Business address phone can't be blank") + end + + it 'destroys business address when Reset Form button is clicked' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + + go_to_business_details + + click_button "Reset Form" + expect(page).to have_content("Enterprise \"First Distributor\" has been successfully updated!") + end + + it 'clears form when all fields are empty' do + fill_in 'enterprise_business_address_attributes_company', with: 'Company' + fill_in 'enterprise_business_address_attributes_address1', with: '35 Ballantyne St' + fill_in 'enterprise_business_address_attributes_city', with: 'Thornbury' + fill_in 'enterprise_business_address_attributes_zipcode', with: '3072' + select2_select 'Australia', from: 'enterprise_business_address_attributes_country_id' + select2_select 'Victoria', from: 'enterprise_business_address_attributes_state_id' + fill_in 'enterprise_business_address_attributes_phone', with: '0123456789' + + click_button "Update" + + go_to_business_details + + fill_in 'enterprise_business_address_attributes_company', with: '' + fill_in 'enterprise_business_address_attributes_address1', with: '' + fill_in 'enterprise_business_address_attributes_city', with: '' + fill_in 'enterprise_business_address_attributes_zipcode', with: '' + fill_in 'enterprise_business_address_attributes_phone', with: '' + + click_button "Update" + expect(page).to have_content("Enterprise \"First Distributor\" has been successfully updated!") + end + end + end +end diff --git a/spec/javascripts/stimulus/update_controller_test.js b/spec/javascripts/stimulus/update_controller_test.js new file mode 100644 index 0000000000..33c85645c7 --- /dev/null +++ b/spec/javascripts/stimulus/update_controller_test.js @@ -0,0 +1,30 @@ +/** + * @jest-environment jsdom + */ + +import { Application } from "stimulus"; +import updateinput_controller from "../../../app/webpacker/controllers/updateinput_controller"; + +describe("updateInput controller", () => { + describe("#update", () => { + beforeEach(() => { + document.body.innerHTML = `
`; + + const application = Application.start(); + application.register("updateinput", updateinput_controller); + }); + + it("update the input value", () => { + const submit = document.getElementById("submit"); + const input = document.getElementById("input"); + expect(input.value).toBe("false"); + + submit.click(); + + expect(input.value).toBe("true"); + }); + }); +}); diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 98e920f0c5..94130fa444 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -23,6 +23,7 @@ describe Enterprise do it { is_expected.to have_many(:supplied_products) } it { is_expected.to have_many(:distributed_orders) } it { is_expected.to belong_to(:address) } + it { is_expected.to belong_to(:business_address) } it "destroys enterprise roles upon its own demise" do e = create(:enterprise) diff --git a/spec/models/spree/address_spec.rb b/spec/models/spree/address_spec.rb index cf3987ebba..a1cb4fa8dd 100644 --- a/spec/models/spree/address_spec.rb +++ b/spec/models/spree/address_spec.rb @@ -14,7 +14,7 @@ describe Spree::Address do country: state.country, firstname: 'firstname', lastname: 'lastname', - company: 'company', + company: 'unused', phone: 'phone', state_id: state.id, state_name: state.name, @@ -110,10 +110,10 @@ describe Spree::Address do expect(address).to be_valid end - it "does not require phone" do + it "requires phone" do address.phone = "" address.valid? - expect(address.errors[:phone]).to be_empty + expect(address.errors[:phone].first).to eq "can't be blank" end it "requires zipcode" do