11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
|
# File 'lib/monban/repository/account/search.rb', line 11
def search(limit:, offset:, sort:, query:)
search = Getto::Repository::Sequel::Search.new(limit: limit, sort: sort, query: query)
where = search.where do |w|
w.search "login_id.cont", &w.cont(::Sequel[:account_login_ids][:login_id])
end
order = search.order do |o|
o.order :login_id, ::Sequel[:account_login_ids][:login_id]
o.force ::Sequel[:account_login_ids][:login_id]
end
pages = search.pages(
db[:accounts]
.where(where)
.count
)
accounts =
db[:accounts]
.left_join(:account_login_ids, account_id: :id)
.where(where)
.limit(limit)
.offset(offset)
.order(*order)
.select(
::Sequel[:accounts][:id],
::Sequel[:account_login_ids][:login_id],
)
.all
emails = db[:account_reset_password_emails]
.where(account_id: accounts.map{|row| row[:id]})
.group_and_count(:account_id)
.map{|email| [email[:account_id], email[:count] > 0] }.to_h
roles = db[:account_roles]
.where(account_id: accounts.map{|row| row[:id]})
.select(:account_id,:role)
.all.group_by{|role| role[:account_id]}
.map{|id,rows| [id, rows.map{|role| role[:role]}] }.to_h
{
pages: pages,
accounts: accounts.map{|account|
account.tap{
id = account[:id]
account[:reset] = !!emails[id]
account[:roles] = roles[id] || []
}
}
}
end
|