diff --git a/app/services/tracks/generator.rb b/app/services/tracks/generator.rb index 4191a286..45610a50 100644 --- a/app/services/tracks/generator.rb +++ b/app/services/tracks/generator.rb @@ -163,15 +163,18 @@ class Tracks::Generator scope = user.tracks scope = scope.where(start_at: time_range) if time_range_defined? - deleted_count = scope.delete_all - Rails.logger.info "Deleted #{deleted_count} existing tracks for user #{user.id}" + deleted_count = scope.destroy_all + + Rails.logger.info "Deleted #{deleted_count} existing tracks for user #{user.id}" end def clean_daily_tracks day_range = daily_time_range range = Time.zone.at(day_range.begin)..Time.zone.at(day_range.end) - deleted_count = user.tracks.where(start_at: range).delete_all + scope = user.tracks.where(start_at: range) + deleted_count = scope.destroy_all + Rails.logger.info "Deleted #{deleted_count} daily tracks for user #{user.id}" end diff --git a/app/services/tracks/incremental_processor.rb b/app/services/tracks/incremental_processor.rb index 1d714e9e..3f3bcd8b 100644 --- a/app/services/tracks/incremental_processor.rb +++ b/app/services/tracks/incremental_processor.rb @@ -66,7 +66,7 @@ class Tracks::IncrementalProcessor end def find_end_time - previous_point ? Time.at(previous_point.timestamp) : nil + previous_point ? Time.zone.at(previous_point.timestamp) : nil end def exceeds_thresholds?(previous_point, current_point) diff --git a/spec/services/tracks/generator_spec.rb b/spec/services/tracks/generator_spec.rb index 0b53c5f5..6f352b86 100644 --- a/spec/services/tracks/generator_spec.rb +++ b/spec/services/tracks/generator_spec.rb @@ -31,6 +31,24 @@ RSpec.describe Tracks::Generator do generator.call expect(points.map(&:reload).map(&:track)).to all(be_present) end + + it 'properly handles point associations when cleaning existing tracks' do + # Create existing tracks with associated points + existing_track = create(:track, user: user) + existing_points = create_list(:point, 3, user: user, track: existing_track) + + # Verify points are associated + expect(existing_points.map(&:reload).map(&:track_id)).to all(eq(existing_track.id)) + + # Run generator which should clean existing tracks and create new ones + generator.call + + # Verify the old track is deleted + expect(Track.exists?(existing_track.id)).to be false + + # Verify the points are no longer associated with the deleted track + expect(existing_points.map(&:reload).map(&:track_id)).to all(be_nil) + end end context 'with insufficient points' do @@ -118,6 +136,24 @@ RSpec.describe Tracks::Generator do generator.call expect(Track.exists?(existing_track.id)).to be false end + + it 'properly handles point associations when cleaning daily tracks' do + # Create existing tracks with associated points for today + existing_track = create(:track, user: user, start_at: today.beginning_of_day) + existing_points = create_list(:point, 3, user: user, track: existing_track) + + # Verify points are associated + expect(existing_points.map(&:reload).map(&:track_id)).to all(eq(existing_track.id)) + + # Run generator which should clean existing tracks for the day and create new ones + generator.call + + # Verify the old track is deleted + expect(Track.exists?(existing_track.id)).to be false + + # Verify the points are no longer associated with the deleted track + expect(existing_points.map(&:reload).map(&:track_id)).to all(be_nil) + end end context 'with empty points' do diff --git a/spec/services/tracks/incremental_processor_spec.rb b/spec/services/tracks/incremental_processor_spec.rb index f3b66499..a2d21bd5 100644 --- a/spec/services/tracks/incremental_processor_spec.rb +++ b/spec/services/tracks/incremental_processor_spec.rb @@ -47,7 +47,7 @@ RSpec.describe Tracks::IncrementalProcessor do it 'processes when time threshold exceeded' do expect(Tracks::CreateJob).to receive(:perform_later) - .with(user.id, start_at: nil, end_at: Time.at(previous_point.timestamp), mode: :none) + .with(user.id, start_at: nil, end_at: Time.zone.at(previous_point.timestamp), mode: :none) processor.call end end @@ -65,7 +65,7 @@ RSpec.describe Tracks::IncrementalProcessor do it 'uses existing track end time as start_at' do expect(Tracks::CreateJob).to receive(:perform_later) - .with(user.id, start_at: existing_track.end_at, end_at: Time.at(previous_point.timestamp), mode: :none) + .with(user.id, start_at: existing_track.end_at, end_at: Time.zone.at(previous_point.timestamp), mode: :none) processor.call end end @@ -88,7 +88,7 @@ RSpec.describe Tracks::IncrementalProcessor do it 'processes when distance threshold exceeded' do expect(Tracks::CreateJob).to receive(:perform_later) - .with(user.id, start_at: nil, end_at: Time.at(previous_point.timestamp), mode: :none) + .with(user.id, start_at: nil, end_at: Time.zone.at(previous_point.timestamp), mode: :none) processor.call end end diff --git a/spec/services/users/import_data_spec.rb b/spec/services/users/import_data_spec.rb index 5d57b97f..1fcf9cfd 100644 --- a/spec/services/users/import_data_spec.rb +++ b/spec/services/users/import_data_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Users::ImportData, type: :service do let(:import_directory) { Rails.root.join('tmp', "import_#{user.email.gsub(/[^0-9A-Za-z._-]/, '_')}_1234567890") } before do - allow(Time).to receive(:current).and_return(Time.at(1234567890)) + allow(Time).to receive(:current).and_return(Time.zone.at(1234567890)) allow(FileUtils).to receive(:mkdir_p) allow(FileUtils).to receive(:rm_rf) allow(File).to receive(:directory?).and_return(true)