Prevent creating a point if it already exists

This commit is contained in:
Eugene Burmakin 2024-05-29 23:06:58 +02:00
parent de3339ae22
commit 8559449355
3 changed files with 20 additions and 1 deletions

View file

@ -7,6 +7,17 @@ class Owntracks::PointCreatingJob < ApplicationJob
def perform(point_params, user_id = nil)
parsed_params = OwnTracks::Params.new(point_params).call
return if point_exists?(parsed_params, user_id)
Point.create!(parsed_params.merge(user_id:))
end
def point_exists?(params, user_id)
Point.exists?(
latitude: params[:latitude],
longitude: params[:longitude],
timestamp: params[:timestamp],
user_id:
)
end
end

View file

@ -18,5 +18,13 @@ RSpec.describe Owntracks::PointCreatingJob, type: :job do
expect(Point.last.user_id).to eq(user.id)
end
context 'when point already exists' do
before { create(:point, latitude: 1.0, longitude: 1.0, timestamp: Time.now.to_i, user:) }
it 'does not create a point' do
expect { perform }.not_to(change { Point.count })
end
end
end
end

View file

@ -180,7 +180,7 @@ paths:
lat: 52.502397
lon: 13.356718
tid: Swagger
tst: 1716638918
tst: 1717016815
servers:
- url: http://{defaultHost}
variables: