Sha256: 4055ac74a61f10889a287d29277b7c4085ed3c2a9887a4bc78c296a22da06b30

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module GDSZendesk
  class Users
    def initialize(client)
      @client = client
    end

    def create_or_update_user(requested_user)
      existing_users = find_by_email(requested_user.email)
      if existing_users.empty?
        create(requested_user)
      else
        existing_user_in_zendesk = existing_users.first
        update(existing_user_in_zendesk, requested_user)
      end
    end

    def suspended?(user_email)
      existing_users = find_by_email(user_email)
      if existing_users.empty?
        false
      else
        existing_user_in_zendesk = existing_users.first
        existing_user_in_zendesk["suspended"]
      end
    end

  protected

    def find_by_email(email)
      @client.users.search(query: email).to_a
    end

    def create(requested_user)
      attributes = { email: requested_user.email, name: requested_user.name, verified: true }
      attributes[:details] = "Job title: #{requested_user.job}" if requested_user.respond_to?(:job)
      attributes[:phone] = requested_user.phone if requested_user.respond_to?(:phone)
      @client.users.create!(attributes)
    end

    def update(existing_user_in_zendesk, requested_user)
      attributes = {}
      attributes[:details] = "Job title: #{requested_user.job}" if requested_user.respond_to?(:job)
      if requested_user.respond_to?(:phone) && !requested_user.phone.nil? && !requested_user.phone.empty?
        attributes[:phone] = requested_user.phone
      end
      existing_user_in_zendesk.update(attributes)
      existing_user_in_zendesk.save
      existing_user_in_zendesk
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gds_zendesk-3.7.0 lib/gds_zendesk/users.rb
gds_zendesk-3.6.0 lib/gds_zendesk/users.rb