Skip to content

Commit

Permalink
Set default minimum_stay equal 1 to property, keep nil minimum_stays …
Browse files Browse the repository at this point in the history
…for calendar entries
  • Loading branch information
Sharipov Ruslan committed Oct 19, 2016
1 parent e87e19f commit 2448b94
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 70 deletions.
7 changes: 3 additions & 4 deletions lib/concierge/suppliers/kigo/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ def perform(pricing, availabilities: [], reservations: [])
set_reservations(reservations)

entries.each do |entry|
min_stay_result = Kigo::Mappers::MinStay.new(
min_stay = Kigo::Mappers::MinStay.new(
property.data.get("minimum_stay"),
entry.minimum_stay
).value
return min_stay_result unless min_stay_result.success?
)

entry.minimum_stay = min_stay_result.value
entry.minimum_stay = min_stay.value
calendar.add(entry)
end

Expand Down
13 changes: 2 additions & 11 deletions lib/concierge/suppliers/kigo/mappers/min_stay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@ def initialize(prop_min_stay, cal_min_stay)
end

# Compare and return the most strict min_stay value.
# Return nil if both prop_min_stay and cal_min_stay are zero
def value
min_stay = [prop_min_stay.to_i, cal_min_stay.to_i].max

if min_stay.zero?
invalid_min_stay_error
else
Result.new(min_stay)
end
end

private
def invalid_min_stay_error
desc = "Min stay was not defined both for property and calendar entry"
Result.error(:invalid_min_stay, desc)
min_stay.zero? ? nil : min_stay
end
end
end
32 changes: 13 additions & 19 deletions lib/concierge/suppliers/kigo/mappers/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,12 @@ def prepare(property_data, pricing_data)
return Result.error(:no_prices_provided) unless pricing_mapper.valid?

set_price(pricing_mapper)
set_minimum_stay(pricing_mapper)
set_images

set_deposit
set_cleaning_service

min_stay_result = Kigo::Mappers::MinStay.new(
property.minimum_stay.to_i,
pricing_mapper.minimum_stay
).value
return min_stay_result unless min_stay_result.success?

set_minimum_stay(min_stay_result.value)

Result.new(property)
end

Expand Down Expand Up @@ -81,9 +74,6 @@ def set_base_info
property.check_out_time = info['PROP_COUT_TIME']
property.check_in_instructions = info['PROP_ARRIVAL_SHEET']

# min_stay is set here, but maybe overided with a stricter minimum from pricingSetup
property.minimum_stay = stay_length(info['PROP_STAYTIME_MIN'])

# Kigo properties are available by default, but most of them has a periodical rate
# which covers almost all days. The days which not in periodical rates
# have unavailable availabilities for these days
Expand All @@ -103,12 +93,6 @@ def set_base_info
end
end

# Parse the minimum stay from the given interval,
# or use 1 to statisfy Roomorama's property validation
def stay_length(interval)
Kigo::TimeInterval.new(interval).days || 1
end

def set_description
description = strip(info['PROP_DESCRIPTION']) || strip(info['PROP_SHORTDESCRIPTION'])
area_description = strip(info['PROP_AREADESCRIPTION'])
Expand Down Expand Up @@ -173,8 +157,18 @@ def set_cleaning_service
property.services_cleaning_rate = get_fee_amount(cleaning_fee)
end

def set_minimum_stay(minimum_stay)
property.minimum_stay = minimum_stay
def set_minimum_stay(pricing_mapper)
min_stay = Kigo::Mappers::MinStay.new(
stay_length(info['PROP_STAYTIME_MIN']),
pricing_mapper.minimum_stay
)

# Use 1 if min_stay is nil to satisfy Roomorama's property validation
property.minimum_stay = min_stay.value || 1
end

def stay_length(interval)
Kigo::TimeInterval.new(interval).days
end

# STAYLENGTH unit means deposit has different prices for night, week, month
Expand Down
19 changes: 13 additions & 6 deletions spec/lib/concierge/suppliers/kigo/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@
end

context "when property hasn't minimum_stay value" do
it 'returns invalid_min_stay error' do
it 'returns entries with nil min stay' do
availabilities.concat([availability, unavailable_availability])
result = subject.perform(pricing, availabilities: availabilities)

expect(result).not_to be_success
expect(result.error.code).to eq(:invalid_min_stay)
expect(result.error.data).to eq(
"Min stay was not defined both for property and calendar entry"
)
expect(result).to be_success

calendar = result.value
available_entry = calendar.entries.find { |entry| entry.date.to_s == availability['DATE'] }
unavailable_entry = calendar.entries.find { |entry| entry.date.to_s == unavailable_availability['DATE'] }

expect(available_entry.available).to eq true
expect(unavailable_entry.available).to eq false

calendar.entries.each do |entry|
expect(entry.minimum_stay).to be_nil
end
end
end
end
Expand Down
43 changes: 16 additions & 27 deletions spec/lib/concierge/suppliers/kigo/mappers/min_stay_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,65 @@
prop_min_stay = 20
cal_min_stay = 19

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(20)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(20)
end

it "selects calendars's minimum_stay if it's bigger than property's minimum_stay" do
prop_min_stay = 16
cal_min_stay = 17

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(17)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(17)
end

it "selects any of minimum_stay values if they equal to each other" do
prop_min_stay = 15
cal_min_stay = 15

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(15)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(15)
end

it "selects property's minimum_stay when calendar's one is nil" do
prop_min_stay = 13
cal_min_stay = nil

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(13)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(13)
end

it "selects property's minimum_stay when calendar's one is nil" do
prop_min_stay = nil
cal_min_stay = 11

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(11)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(11)
end

it "applies .to_i to property's minimum_stay values" do
prop_min_stay = "20"
cal_min_stay = 19

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(20)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(20)
end

it "applies .to_i to calendars's minimum_stay values" do
prop_min_stay = 10
cal_min_stay = "19"

min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).to be_success
expect(min_stay_result.value).to eq(19)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to eq(19)
end

it "returns error when both minimum_stay values are nil or 0" do
null_values = [nil, 0]

null_values.each do |prop_min_stay|
null_values.each do |cal_min_stay|
min_stay_result = described_class.new(prop_min_stay, cal_min_stay).value
expect(min_stay_result).not_to be_success
expect(min_stay_result.error.code).to eq(:invalid_min_stay)
expect(min_stay_result.error.data).to eq(
"Min stay was not defined both for property and calendar entry"
)
min_stay = described_class.new(prop_min_stay, cal_min_stay)
expect(min_stay.value).to be_nil
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/workers/suppliers/kigo/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
context "when property has no minimum_stay value" do
let(:property_attrs) {{ host_id: host.id, identifier: identifier, data: { minimum_stay: nil, nightly_rate: 10 }}}

it "returns external error" do
it "doesn't creates an external error" do
allow_any_instance_of(Kigo::Importer).to receive(:fetch_prices) { Result.new(empty_prices) }
allow_any_instance_of(Kigo::Importer).to receive(:fetch_availabilities) { Result.new(availabilities) }

Expand All @@ -112,8 +112,8 @@
stats = sync_process.stats

expect(stats[:properties_processed]).to eq 1
expect(stats[:available_records]).to eq 0
expect(stats[:unavailable_records]).to eq 0
expect(stats[:available_records]).to eq 1
expect(stats[:unavailable_records]).to eq 7
end
end
end
Expand Down

0 comments on commit 2448b94

Please sign in to comment.