Class: Customer::UpdateProfile

Inherits:
BaseService show all
Defined in:
app/services/customer/update_profile.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#create_update_identification_number(customer_id, identification_numbers_attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/customer/update_profile.rb', line 56

def create_update_identification_number(customer_id, identification_numbers_attributes)
  return if identification_numbers_attributes.blank?

  if IdentificationNumber.where(customer_id: customer_id).present?
    identification_numbers_attributes.each do |k,v|
      identification_numbers_id = v[:id].to_i
      IdentificationNumber.where(id: identification_numbers_id).update_all(category: v[:category], number: v[:number], updated_at: Time.current)
    end
  else
    identification_numbers_attributes.each do |k,v|
      @identification_number = IdentificationNumber.create(customer_id: customer_id, category: v[:category], number: v[:number])
    end
  end
end

#process(customer, attributes) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/customer/update_profile.rb', line 6

def process(customer, attributes)
  profile_id = attributes[:profile_id]
  buying_group_id = attributes[:buying_group_id]
  rating = attributes[:rating]

  create_update_identification_number(customer.id,attributes[:identification_numbers_attributes])

  customer.profile_id = profile_id
  customer.update_product_interests(attributes[:product_line_ids])
  customer.update_buying_group(buying_group_id)

  update_parties_rating(customer.id,rating) if rating.present?

  profile_changed = (customer.profile_id_changed? || customer.buying_group_id_changed?)
  if profile_changed
    if customer.profile_id == ProfileConstants::HOMEOWNER
      # Name fixes
      pnp = PersonNameParser.new(customer.full_name)
      pnp.to_party(customer)
    elsif customer.profile_id_was == ProfileConstants::HOMEOWNER
      # Switch from homeowner to customer, person_name and other data might change
      customer.full_name = attributes[:full_name]
      if attributes[:person_name].present?
        pnp = PersonNameParser.new(attributes[:person_name])
        cnt = customer.contacts.new
        pnp.to_party(cnt)
        cnt.save!
        if attributes[:move_contact_points].to_b
          customer.contact_points.each{|cp| cp.update_attribute(:party_id, cnt.id)}
        end
      end

    end
    # This is to force callback to run
    customer.updated_at = Time.current

    if customer.save
      return Result.new(success: true, profile_changed: profile_changed)
    else
      return Result.new(success: false, profile_changed: profile_changed)
    end
  end
  return Result.new(success: true, profile_changed: profile_changed)

end

#update_parties_rating(customer_id, rating) ⇒ Object



52
53
54
# File 'app/services/customer/update_profile.rb', line 52

def update_parties_rating(customer_id,rating)
  Party.where(id: customer_id).update_all(rating: rating)
end