Skip to content

Commit b583eff

Browse files
committed
Add LocationPositionUpdaterJob
This adds a job which calls the `LocationPositionUpdater` service class for a location, allowing it's position to be updated in the background. Jira-Issue: MAV-6379
1 parent 5150094 commit b583eff

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
class LocationPositionUpdaterJob
4+
include Sidekiq::Job
5+
6+
sidekiq_options queue: :third_party_data_imports, lock: :until_executing
7+
8+
def perform(location_id)
9+
location = Location.find(location_id)
10+
LocationPositionUpdater.call(location)
11+
rescue LocationPositionUpdater::NoResults => e
12+
Sentry.capture_exception(e, level: "warning")
13+
end
14+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
describe LocationPositionUpdaterJob do
4+
describe "#perform" do
5+
subject(:perform) { described_class.new.perform(location.id) }
6+
7+
let(:location) { create(:community_clinic, position: nil) }
8+
9+
context "when the location has an address" do
10+
let(:response) do
11+
{
12+
header: {
13+
total_results: 1
14+
},
15+
results: [{ dpa: { lat: 51.5074, lng: -0.1278 } }]
16+
}
17+
end
18+
19+
before do
20+
allow(OrdnanceSurvey::PlacesAPI).to receive(:find).and_return(response)
21+
end
22+
23+
it "calls LocationPositionUpdater with the location" do
24+
expect(LocationPositionUpdater).to receive(:call).with(location)
25+
perform
26+
end
27+
28+
it "updates the location position" do
29+
expect { perform }.to change { location.reload.position }.from(nil).to(
30+
an_instance_of(RGeo::Geographic::SphericalPointImpl)
31+
)
32+
end
33+
end
34+
35+
context "when there are no results" do
36+
let(:error) { LocationPositionUpdater::NoResults.new }
37+
38+
before do
39+
allow(LocationPositionUpdater).to receive(:call).and_raise(error)
40+
end
41+
42+
it "captures the exception in Sentry at warning level" do
43+
expect(Sentry).to receive(:capture_exception).with(
44+
error,
45+
level: "warning"
46+
)
47+
perform
48+
end
49+
50+
it "does not raise the error" do
51+
expect { perform }.not_to raise_error
52+
end
53+
end
54+
55+
context "when the location has no address" do
56+
let(:location) { create(:community_clinic, :without_address) }
57+
58+
it "re-raises the error" do
59+
expect { perform }.to raise_error(
60+
LocationPositionUpdater::MissingAddress
61+
)
62+
end
63+
end
64+
end
65+
end

0 commit comments

Comments
 (0)