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

#create(subject, url: nil, body: nil, media: nil, payload: nil, channel: nil, category: nil, user_receiver_id: nil, notification_type: nil, role_receiver_names: nil, user_receiver_emails: nil) ⇒ Object



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_bell/notification_service.rb', line 46

def create(
    subject, 
    url:nil, 
    body:nil, 
    media:nil,
    payload:nil,
    channel:nil,
    category:nil, 
    user_receiver_id:nil, 
    notification_type:nil, 
    role_receiver_names:nil, 
    user_receiver_emails:nil
)

    # validate that the notifications has a valid category
    category = 'info' if not ['info', 'danger', 'warning', 'success'].include?(category)

    # set push (web and mobile) notifications as default channel
    channel = 'push' unless channel

    # base notification data
    notification_params = {
        url: url,
        body: body,
        media: media,
        payload: payload,
        channel: channel,
        subject: subject,
        category: category,
        notification_type: notification_type,
        status: 'created',
        user: current_user
    }

    # "bulk insert" all the notifications
    notifications = current_user..bell.notifications.create([notification_params])

    return { id: notifications.map(&:id) }

end

#index(only_own_notifications = true) ⇒ Object



4
5
6
7
8
9
10
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
# File 'app/services/lesli_bell/notification_service.rb', line 4

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

    # LC::Response.pagination(
    #     notifications.current_page,
    #     notifications.total_pages,
    #     notifications.total_count,
    #     notifications.length,
    #     notifications.map do |notification|
    #         {
    #             id: notification[:id],
    #             url: notification[:url],
    #             body: notification[:body],
    #             subject: notification[:subject],
    #             category: notification[:category],
    #             created_at: LC::Date.distance_to_words(notification[:created_at]),
    #             status: notification[:status],
    #         }
    #     end
    # )

end