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, #found?, #initialize, #result, #successful?

Constructor Details

This class inherits a constructor from Lesli::ApplicationLesliService

Instance Method Details

#find(id) ⇒ Object



36
37
38
39
# File 'app/services/lesli/user_service.rb', line 36

def find id
    #super(current_user.account.users.joins(:detail).find_by(id: id))
    super(current_user..users.find_by(id: id))
end

#index(params) ⇒ Array

TODO: Implement pg_search

Returns:

  • (Array)

    Paginated index of users.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/services/lesli/user_service.rb', line 90

def index params

    # sql string to join to user_roles and get all the roles assigned to a user
    sql_string_for_user_roles = "left join (
        select
            ur.user_id, string_agg(r.\"name\", ', ') rolenames
        from lesli_user_roles ur
        join lesli_roles r
            on r.id = ur.role_id
        where ur.deleted_at is null
        group by ur.user_id
    ) roles on roles.user_id = lesli_users.id"

    # sql string to joing to user_sessions and get all the active sessions of a user
    sql_string_for_user_sessions = "left join (
        select
            max(last_used_at) as last_action_performed_at,
            user_id
        from lesli_user_sessions us
        where us.deleted_at is null
        group by(us.user_id)
    ) sessions on sessions.user_id = lesli_users.id"

    current_user..users
    .joins(sql_string_for_user_roles)
    .joins(sql_string_for_user_sessions)
    .page(query[:pagination][:page])
    .per(query[:pagination][:perPage])
    .select(
        :id,
        "CONCAT(COALESCE(lesli_users.first_name, ''), ' ', COALESCE(lesli_users.last_name, '')) as fullname",
        :email,
        :active,
        :rolenames,
        Date2.new.date_time.db_column("current_sign_in_at")
    )
end

#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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/lesli/user_service.rb', line 44

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
        # ")

        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
        ")
        .where('r.name IN (:roles)', roles: roles)

    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

#showObject

Creates a query that selects all user information from several tables if CloudLock is present



129
130
131
# File 'app/services/lesli/user_service.rb', line 129

def show
    resource
end

#update(params) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/services/lesli/user_service.rb', line 133

def update params

    # old_attributes = resource.detail.attributes.merge({
    #     active: resource.active
    # })

    if resource.update(params)
        # new_attributes = resource.detail.attributes.merge({
        #     active: resource.active
        # })
        #resource.log_activity_update(current_user, resource, old_attributes, new_attributes)
    else
        self.error(resource.errors.full_messages.to_sentence)
    end
    
    self
end