@@ -7,41 +7,32 @@ module NotificationReadable
77
88 # Marks notifications (for the current person) as read for events bound to a record
99 # via Noticed::Event#record_id (generic helper used across features).
10- def mark_notifications_read_for_record ( record , recipient : helpers . current_person ) # rubocop:todo Metrics/AbcSize
10+ def mark_notifications_read_for_record ( record , recipient : helpers . current_person )
1111 return unless recipient && record . respond_to? ( :id )
1212
13- nn = Noticed ::Notification . arel_table
14- ne = Noticed ::Event . arel_table
15-
16- join = nn . join ( ne ) . on ( ne [ :id ] . eq ( nn [ :event_id ] ) ) . join_sources
17-
18- relation = Noticed ::Notification
19- . where ( recipient :)
20- . where ( nn [ :read_at ] . eq ( nil ) )
21- . joins ( join )
22- . where ( ne [ :record_id ] . eq ( record . id ) )
13+ event_ids = Noticed ::Event . where ( record_id : record . id ) . select ( :id )
2314
24- relation . update_all ( read_at : Time . current )
15+ Noticed ::Notification
16+ . where ( recipient :)
17+ . where ( event_id : event_ids )
18+ . where ( read_at : nil )
19+ . update_all ( read_at : Time . current )
2520 end
2621
2722 # Marks notifications as read for a set of records associated to a given Noticed event class
2823 # using the event's record_id field.
29- def mark_notifications_read_for_event_records ( event_class , record_ids , recipient : helpers . current_person ) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
24+ def mark_notifications_read_for_event_records ( event_class , record_ids , recipient : helpers . current_person )
3025 return unless recipient && record_ids . present?
3126
32- nn = Noticed ::Notification . arel_table
33- ne = Noticed ::Event . arel_table
34-
35- join = nn . join ( ne ) . on ( ne [ :id ] . eq ( nn [ :event_id ] ) ) . join_sources
36-
37- relation = Noticed ::Notification
38- . where ( recipient :)
39- . where ( nn [ :read_at ] . eq ( nil ) )
40- . joins ( join )
41- . where ( ne [ :type ] . eq ( event_class . to_s ) )
42- . where ( ne [ :record_id ] . in ( Array ( record_ids ) ) )
27+ event_ids = Noticed ::Event
28+ . where ( type : event_class . to_s , record_id : Array ( record_ids ) )
29+ . select ( :id )
4330
44- relation . update_all ( read_at : Time . current )
31+ Noticed ::Notification
32+ . where ( recipient :)
33+ . where ( event_id : event_ids )
34+ . where ( read_at : nil )
35+ . update_all ( read_at : Time . current )
4536 end
4637
4738 # Marks Joatu match notifications as read for an Offer or Request record by matching
0 commit comments