summaryrefslogtreecommitdiff
path: root/app/services/customer_relations/contacts/update_service.rb
blob: 66eb5731bc998de8a40acabaf8f96ec9806d6570 (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
29
30
# frozen_string_literal: true

module CustomerRelations
  module Contacts
    class UpdateService < BaseService
      def execute(contact)
        return error_no_permissions unless allowed?

        handle_active_param
        return error_organization_invalid unless organization_valid?
        return error_updating(contact) unless contact.update(params)

        ServiceResponse.success(payload: contact)
      end

      private

      def handle_active_param
        return if params[:active].nil?

        active = params.delete(:active)
        params[:state] = active ? 'active' : 'inactive'
      end

      def error_updating(contact)
        error(contact&.errors&.full_messages || 'Failed to update contact')
      end
    end
  end
end