Class: Users::WorkingHourServices

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_audit/users/working_hour_services.rb

Instance Method Summary collapse

Instance Method Details

#indexHash

Returns Paginated list of the records.

Parameters:

  • Has (query)

    of the formated queries/filters that will be applied to filter data

Returns:

  • (Hash)

    Paginated list of the records



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
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/lesli_audit/users/working_hour_services.rb', line 41

def index 
    
    #Get string for search query
    search_string = query[:search].downcase.gsub(" ","%") unless query[:search].blank?

    #Order by column in table of users
    order_string = "date"
    unless query.dig(:order, :by).blank?
        order_string = query.dig(:order, :by)
    end

    user_requests = current_user..users.joins(:requests).joins(:roles)

    user_requests = user_requests.select(
        :id,
        "users.id as userid",
        "users.email",
        "DATE_TRUNC('day', user_requests.created_at) as date",
        "min(user_requests.created_at) as first_activity", 
        "max(user_requests.created_at) as last_activity",
        "roles.name"
    ).order(order_string.concat(" ").concat(query.dig(:order, :dir)))

                
    user_requests = user_requests.group(:userid, "DATE_TRUNC('day', user_requests.created_at)", "roles.name")

    unless search_string.blank?
        user_requests = user_requests.where([
            "lower(users.email) like :search_string or lower(roles.name) like :search_string",
        ].join(" or "), { 
            search_string: "%#{search_string}%"
        }) 
    end

    user_requests
end