summaryrefslogtreecommitdiff
path: root/app/controllers/services/update_address_service.rb
blob: 26a596ed20c61737e60f6955f90fa650bdb5217c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module Services
  # The service for address creation.
  class UpdateAddressService < AddressServicesBase
    attr_reader :address

    def initialize(user_account, address_id, params)
      super(user_account, params)
      @address_id = address_id
    end

    def call
      @address = Address.find_by(id: @address_id)

      return :not_found if @address.nil?

      if already_existing_address.nil?
        return :unprocessable_entity unless @address.update(@params)
      else
        add_address_to_user_addresses
        @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy
        @address.destroy if @address.user_accounts.count.zero?
        @address = already_existing_address
      end
    end
  end
end