Skip to content

Commit

Permalink
User join query to get max date (#2169)
Browse files Browse the repository at this point in the history
* User join query to get max date

* CV2-5856: fix tests
  • Loading branch information
melsawy committed Jan 9, 2025
1 parent 4ba21df commit 7d23b7a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
22 changes: 18 additions & 4 deletions app/models/concerns/project_media_cached_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,24 @@ def recalculate_demand
def recalculate_last_seen
# If it’s a main/parent item, last_seen is related to any tipline request to that own ProjectMedia or any similar/child ProjectMedia
# If it’s not a main item (so, single or child, a.k.a. “confirmed match” or “suggestion”), then last_seen is related only to tipline requests related to that ProjectMedia.
ids = self.is_parent ? self.related_items_ids : self.id
v1 = TiplineRequest.where(associated_type: 'ProjectMedia', associated_id: ids).order('created_at DESC').first&.created_at || 0
v2 = ProjectMedia.where(id: ids).order('created_at DESC').first&.created_at || 0
[v1, v2].max.to_i
v1 = [0]
v2 = [0]
parent = self
if self.is_parent
parent = Relationship.confirmed.where(target_id: self.id).last&.source || self
result = Relationship.select('MAX(pm.created_at) as pm_c, MAX(tr.created_at) as tr_c')
.where(relationship_type: Relationship.confirmed_type, source_id: parent.id)
.joins("INNER JOIN project_medias pm ON pm.id = relationships.target_id")
.joins("LEFT JOIN tipline_requests tr ON tr.associated_id = relationships.target_id AND tr.associated_type = 'ProjectMedia'")
v1.concat(result.map(&:tr_c))
v2.concat(result.map(&:pm_c))
end
result = ProjectMedia.select('MAX(project_medias.created_at) as pm_c, MAX(tr.created_at) as tr_c')
.where(id: parent.id)
.joins("INNER JOIN tipline_requests tr ON tr.associated_id = project_medias.id AND tr.associated_type = 'ProjectMedia'")
v1.concat(result.map(&:tr_c))
v2.concat(result.map(&:pm_c))
[v1, v2].flatten.map(&:to_i).max
end

def recalculate_fact_check_id
Expand Down
1 change: 1 addition & 0 deletions test/models/bot/alegre_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def teardown
end

test "should set similarity relationship based on date threshold" do
RequestStore.store[:skip_cached_field_update] = false
create_verification_status_stuff
p = create_project team: @team
pm1 = create_project_media project: p, quote: "This is also a long enough Title so as to allow an actual check of other titles", team: @team
Expand Down
2 changes: 1 addition & 1 deletion test/models/tipline_newsletter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setup
rss_feed_url: 'https://example.com/feed',
number_of_articles: 3,
send_every: ['monday'],
send_on: Time.parse('2025-01-01'),
send_on: Time.parse('2030-01-01'),
timezone: 'UTC',
time: Time.parse('10:00'),
footer: 'Test',
Expand Down

0 comments on commit 7d23b7a

Please sign in to comment.