Class: LesliBell::NotificationService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_bell/notification_service.rb

Instance Method Summary collapse

Instance Method Details

#countObject



4
5
6
7
8
9
10
11
12
13
14
# File 'app/services/lesli_bell/notification_service.rb', line 4

def count 
    current_user..bell.notifications
    .where(:user => current_user)
    .where(status: nil)
    .or(
        current_user..bell.notifications
        .where(user: current_user)
        .where.not(status: 'read')
    )
    .count
end

#create(notification_params, send_to_role_ids: nil, send_to_user_ids: nil, send_to_user_emails: nil) ⇒ Object



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
# File 'app/services/lesli_bell/notification_service.rb', line 43

def create(
    notification_params,
    send_to_role_ids:nil, 
    send_to_user_ids:nil, 
    send_to_user_emails:nil
)

    users = []
    notifications = []

    unless Notification.categories.key?(notification_params[:category])
        notification_params[:category] = :info
    end

    unless Notification.channels.key?(notification_params[:channel])
        notification_params[:channel] = :web
    end

    send_to_user_ids.each do |user|
        notifications.push({
            **notification_params,
            user_id: user,
        })
    end

    # "bulk insert" all the notifications
    current_user..bell.notifications.create!(notifications)

    self.resource = { id: notifications.map{ |n| n[:user_id] } }

    self
end

#create_for_me(notification_params) ⇒ Object



39
40
41
# File 'app/services/lesli_bell/notification_service.rb', line 39

def create_for_me notification_params
    self.create(notification_params, send_to_user_ids: [current_user.id])
end

#index(only_own_notifications = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/lesli_bell/notification_service.rb', line 16

def index only_own_notifications=true

    notifications = []

    # work with all notifications
    notifications = current_user..bell
    .notifications
    .order(created_at: :DESC)

    # work only with notifications that belongs to the user
    if only_own_notifications
        notifications = notifications.where(:user => current_user, :status => ["created", "sent", nil]) 
    end

    # add pagination
    notifications = notifications
    .page(query[:pagination][:page])
    .per(query[:pagination][:perPage])
    .order(:updated_at)

    return notifications
end

#read(id) ⇒ Object



76
77
78
79
80
# File 'app/services/lesli_bell/notification_service.rb', line 76

def read id
    noti = current_user..bell.notifications.where(:id => id, :user => current_user)
    noti.update(:status => :read)
    self
end