Class: HasHelpers::Mutations::MarkNotificationsRead

Inherits:
Base
  • Object
show all
Defined in:
app/graphql/has_helpers/mutations/mark_notifications_read.rb

Instance Method Summary collapse

Methods included from HasHelpers::Middleware::ApplicationAccess

#authorized?

Instance Method Details

#resolve(group_title: nil, alert_type: nil) ⇒ Object



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
# File 'app/graphql/has_helpers/mutations/mark_notifications_read.rb', line 11

def resolve(group_title: nil, alert_type: nil)
  organization = context[:current_organization]
  return { success: false } unless organization

  if alert_type.present?
    alert_type_record = ::HasHelpers::NotificationAlertType.find_by(name: alert_type)
    return { success: false } unless alert_type_record

    scope = ::HasHelpers::NotificationAlert.
      where(organization_id: organization.id).
      where(alert_type: alert_type_record).
      joins(:status).
      where(options: { name: "Unread" })

    Rails.logger.info "Marking all #{alert_type} alerts as read for organization #{organization.id}"
  elsif group_title.present?
    scope = ::HasHelpers::NotificationAlert.
      where(organization_id: organization.id).
      joins(:status).
      where(options: { name: "Unread" }).
      where("payload ->> 'groupTitle' = ?", group_title)

    Rails.logger.info "Marking notifications with groupTitle '#{group_title}' as read for organization #{organization.id}"
  else
    return { success: false }
  end

  read_status = ::HasHelpers::NotificationAlertStatus::READ
  updated_count = scope.update_all(status_id: read_status.id)

  Rails.logger.info "Successfully marked #{updated_count} notifications as read"

  { success: true }
rescue => e
  Rails.logger.error "Error marking notifications as read: #{e.message}"
  { success: false }
end