2021-10-12 09:09:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module CustomerRelations
|
|
|
|
module Contacts
|
|
|
|
class UpdateService < BaseService
|
|
|
|
def execute(contact)
|
|
|
|
return error_no_permissions unless allowed?
|
2022-04-29 18:08:18 +00:00
|
|
|
|
|
|
|
handle_active_param
|
2022-05-11 12:09:03 +00:00
|
|
|
return error_organization_invalid unless organization_valid?
|
2021-10-12 09:09:35 +00:00
|
|
|
return error_updating(contact) unless contact.update(params)
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: contact)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-29 18:08:18 +00:00
|
|
|
def handle_active_param
|
|
|
|
return if params[:active].nil?
|
|
|
|
|
|
|
|
active = params.delete(:active)
|
|
|
|
params[:state] = active ? 'active' : 'inactive'
|
|
|
|
end
|
|
|
|
|
2021-10-12 09:09:35 +00:00
|
|
|
def error_updating(contact)
|
|
|
|
error(contact&.errors&.full_messages || 'Failed to update contact')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|