Skip to content

Commit

Permalink
Add changes to support notifications for diff comments
Browse files Browse the repository at this point in the history
Notifiable type and notifiable id were miss matched. Because of that the
notification links were broken
  • Loading branch information
rubhanazeem committed Mar 30, 2023
1 parent a7cc5d5 commit 0c0f661
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def call
private

def bs_request
@bs_request ||= @notification.notifiable_type == 'BsRequest' ? @notification.notifiable : @notification.notifiable.commentable
@bs_request ||= if @notification.notifiable_type == 'BsRequest'
@notification.notifiable
elsif @notification.notifiable.commentable.is_a?(BsRequestAction)
@notification.notifiable.commentable.bs_request
else
@notification.notifiable.commentable
end
end
end
19 changes: 16 additions & 3 deletions src/api/app/components/notification_notifiable_link_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def notifiable_link_text
when 'Event::RequestStatechange', 'Event::RequestCreate', 'Event::ReviewWanted'
"#{helpers.request_type_of_action(@notification.notifiable)} Request ##{@notification.notifiable.number}"
when 'Event::CommentForRequest'
bs_request = @notification.notifiable.commentable
"Comment on #{helpers.request_type_of_action(bs_request)} Request ##{bs_request.number}"
when 'Event::CommentForProject'
'Comment on Project'
Expand Down Expand Up @@ -55,8 +54,12 @@ def notifiable_link_path
when 'Event::CommentForRequest'
# TODO: It would be better to eager load the commentable association with `includes(...)`,
# but it's complicated since this isn't for all notifications and it's nested 2 levels deep.
bs_request = @notification.notifiable.commentable
Rails.application.routes.url_helpers.request_show_path(bs_request.number, notification_id: @notification.id, anchor: 'comments-list')
anchor = if @notification.notifiable.commentable.is_a?(BsRequestAction)
'tab-pane-changes'
else
'comments-list'
end
Rails.application.routes.url_helpers.request_show_path(bs_request.number, notification_id: @notification.id, anchor: anchor)
when 'Event::CommentForProject'
Rails.application.routes.url_helpers.project_show_path(@notification.notifiable.commentable, notification_id: @notification.id, anchor: 'comments-list')
when 'Event::CommentForPackage'
Expand All @@ -80,4 +83,14 @@ def notifiable_link_path
end
end
# rubocop:enable Metrics/CyclomaticComplexity

def bs_request
return unless @notification.event_type == 'Event::CommentForRequest'

if @notification.notifiable.commentable.is_a?(BsRequestAction)
@notification.notifiable.commentable.bs_request
else
@notification.notifiable.commentable
end
end
end
2 changes: 1 addition & 1 deletion src/api/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def create_event
when 'BsRequest'
Event::CommentForRequest.create(event_parameters)
when 'BsRequestAction'
Event::CommentForRequest.create(event_parameters.merge({ id: commentable.bs_request.id, diff_ref: diff_ref }))
Event::CommentForRequest.create(event_parameters.merge({ id: id, diff_ref: diff_ref }))
end
end

Expand Down
13 changes: 12 additions & 1 deletion src/api/spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@
let(:comment) { create(:comment, :bs_request_action) }

it 'creates the corresponding event' do
expect { comment.save! }.to change(Event::CommentForRequest, :count).by(1)
expect { comment }.to change(Event::CommentForRequest, :count).by(1)
end
end

context 'valid event data' do
let!(:comment) { create(:comment, :bs_request_action) }

it 'adds correct parameters_for_notification' do
event = Event::CommentForRequest.last
expect(event.parameters_for_notification[:event_payload]['id']).to eq(comment.id)
expect(event.parameters_for_notification[:notifiable_id]).to eq(comment.id)
expect(event.parameters_for_notification[:notifiable_type]).to eq('Comment')
end
end
end
Expand Down

0 comments on commit 0c0f661

Please sign in to comment.