Class: Lesli::UserService

Inherits:
ApplicationLesliService show all
Defined in:
app/services/lesli/user_service.rb

Instance Method Summary collapse

Methods inherited from ApplicationLesliService

#create, #delete, #error, #errors, #errors_as_sentence, #find, #found?, #index, #initialize, #result, #show, #successful?, #update

Constructor Details

This class inherits a constructor from Lesli::ApplicationLesliService

Instance Method Details

#list(params = nil) ⇒ Object

Return a list of users that belongs to the account of the current_user this list is meant to be used in selectors, autocomplets, etc



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/lesli/user_service.rb', line 39

def list params=nil
    users = current_user..users

    if params[:role].present?
        # add simple quotes to the roles so the sql can manage the query
        roles = params[:role].split(",").map { |role| "'#{role}'" }.join(", ")
        users = users.joins("
            inner join (
                select
                    ur.users_id, string_agg(r.\"name\", ', ') role_names
                from user_roles ur
                join roles r
                    on r.id = ur.role_id 
                    and r.name in ( #{ roles } )
                where ur.deleted_at is null
                group by ur.users_id
            ) roles on roles.users_id = users.id
        ")
    end

    users.order(name: :asc).select(
        :id,
        :email,
        "CONCAT_WS(' ', first_name, last_name) as name",
        "COALESCE(NULLIF(alias,''), email) as alias"
    ).as_json
end