Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Customer Same as Caller to the Job Model #227

Open
bradbergeron-us opened this issue Mar 7, 2018 · 1 comment
Open

Move Customer Same as Caller to the Job Model #227

bradbergeron-us opened this issue Mar 7, 2018 · 1 comment

Comments

@bradbergeron-us
Copy link
Contributor

No description provided.

@bradbergeron-us bradbergeron-us changed the title Move Customer Same Move Customer Same as Caller to the Job Model Mar 7, 2018
@bradbergeron-us
Copy link
Contributor Author

bradbergeron-us commented Mar 7, 2018

FIXME: Move to Job model from Customer Model

def self.same_as_caller(job)
    caller = job.caller
    return if caller.nil?
# check if old customer exists
    if job.customer_id
      # FIXME: it's not a good idea to destroy object from database. Maybe just inactive?
      Customer.find_by(id: job.customer_id).destroy
    end
    caller_phones = caller.phones
    caller_address = Address.find_by(id: caller.address_id)

    customer = Customer.new(first_name: caller.first_name, last_name: caller.last_name, email: caller.email)
    customer.address = Address.new(address_1: caller_address.address_1, address_2: caller_address.address_2, zip_code: caller_address.zip_code, city: caller_address.city, county: caller_address.county, state_id: caller_address.state_id) if caller_address.present?

    job.customer = customer

    caller_phones.each do |phone|
      customer.phones.build(number: phone.number, extension: phone.extension, type_id: phone.type_id)
    end

    job.save
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant