Skip to content

Commit

Permalink
Do not attempt to update calendar for properties not previously synced.
Browse files Browse the repository at this point in the history
It is common for the calendar endpoint of supplier APIs to be different
from that to get property data. Therefore, it is possible for APIs to
return the availabilities calendar of properties that were not
previously synchronised.

Since this is a possible scenarion across different suppliers, this
changes the `Workers::CalendarSynchronisation` class to skip updating
the calendar (that is, making an API call to Roomorama) in case the
property identifier is unknown.

In case the property is indeed created by Concierge on a later run of
the `metadata` worker, then the next `calendar` worker run should update
its calendar as usual.
  • Loading branch information
Renato Mascarenhas committed Aug 11, 2016
1 parent 8c1497d commit bbaa974
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apps/workers/calendar_synchronisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def finish!
private

def process(calendar)
# if the property trying to have its calendar synchronised was not
# synchronised by Concierge, then do not attempt to update its calendar,
# since the API call to Roomorama is going to fail (the +identifier+
# will not be recognised.)
return unless synchronised?(calendar.identifier)

calendar.validate!
update_counters(calendar)
operation = Roomorama::Client::Operations.update_calendar(calendar)
Expand Down Expand Up @@ -126,6 +132,10 @@ def run_operation(operation)
announce_failure(result) unless result.success?
end

def synchronised?(property_identifier)
!!(PropertyRepository.from_host(host).identified_by(property_identifier).first)
end

def missing_data(message, attributes)
missing_data = Concierge::Context::MissingBasicData.new(
error_message: message,
Expand Down
17 changes: 17 additions & 0 deletions spec/workers/calendar_synchronisation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
operation = nil
expect(subject).to receive(:run_operation) { |op| operation = op }

create_property(identifier: "prop1", host_id: host.id)
subject.start("prop1") { Result.new(calendar) }

expect(operation).to be_a Roomorama::Client::Operations::UpdateCalendar
expect(operation.calendar).to eq calendar
end

context "error handling" do
it "announces an error if the calendar returned does not pass validations" do
calendar.entries.first.date = nil

create_property(identifier: "prop1", host_id: host.id)
subject.start("prop1") { Result.new(calendar) }

error = ExternalErrorRepository.last
Expand Down Expand Up @@ -82,6 +86,8 @@
[422, {}, read_fixture("roomorama/invalid_start_date.json")]
}

create_property(identifier: "prop1", host_id: host.id)

expect {
subject.start("prop1") { Result.new(calendar) }
}.to change { ExternalErrorRepository.count }.by(1)
Expand All @@ -104,6 +110,14 @@
subject.start("prop1") { Result.new(calendar) }
}.not_to change { ExternalErrorRepository.count }
end

it "skips calendar synchronisation if the property was not previously created by Concierge" do
expect {
subject.start("prop1") { Result.new(calendar) }
}.not_to change { ExternalErrorRepository.count }

expect(Roomorama::Client::Operations).not_to receive(:update_calendar)
end
end
end

Expand All @@ -113,6 +127,9 @@
end

it "creates a sync_process record when there is an error with the synchronisation" do
create_property(identifier: "prop1", host_id: host.id)
create_property(identifier: "prop2", host_id: host.id)

subject.start("prop1") { Result.new(calendar) }
subject.start("prop2") { Result.error(:http_status_500) }

Expand Down

0 comments on commit bbaa974

Please sign in to comment.