spree_cm_commissioner 2.13.7 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/assets/javascripts/spree_cm_commissioner/backend.js +0 -1
- data/app/assets/stylesheets/spree_cm_commissioner/backend/calendar.scss +0 -39
- data/app/controllers/concerns/spree/admin/rates_availability_cells.rb +16 -0
- data/app/controllers/concerns/spree/api/v2/product_list_includes_decorator.rb +1 -0
- data/app/controllers/spree/admin/guests_controller.rb +2 -1
- data/app/controllers/spree/admin/integrations_controller.rb +16 -7
- data/app/controllers/spree/admin/inventory_items_controller.rb +33 -62
- data/app/controllers/spree/admin/product_option_types_controller.rb +88 -0
- data/app/controllers/spree/admin/rate_plans_controller.rb +125 -0
- data/app/controllers/spree/admin/rates_availabilities_controller.rb +142 -0
- data/app/controllers/spree/admin/stock_managements_controller.rb +1 -53
- data/app/controllers/spree/api/switch_cm/base_controller.rb +109 -0
- data/app/controllers/spree/api/switch_cm/tokens_controller.rb +68 -0
- data/app/controllers/spree/api/switch_cm/xml/availabilities_controller.rb +39 -0
- data/app/controllers/spree/api/switch_cm/xml/base_controller.rb +37 -0
- data/app/controllers/spree/api/switch_cm/xml/reservation_summaries_controller.rb +28 -0
- data/app/controllers/spree/api/switch_cm/xml/reservations_controller.rb +57 -0
- data/app/controllers/spree/api/switch_cm/xml/room_rate_availabilities_controller.rb +56 -0
- data/app/controllers/spree/api/switch_cm/xml/room_rates_controller.rb +45 -0
- data/app/controllers/spree/api/v2/operator/guests_controller.rb +1 -1
- data/app/controllers/spree/api/v2/storefront/accommodations/variants_controller.rb +31 -5
- data/app/controllers/spree/api/v2/storefront/inventory_items_controller.rb +2 -2
- data/app/controllers/spree/api/v2/tenant/intercity_taxi/draft_orders_controller.rb +0 -1
- data/app/factory/spree_cm_commissioner/order_telegram_message_factory.rb +1 -1
- data/app/finders/spree_cm_commissioner/accommodations/find.rb +10 -8
- data/app/finders/spree_cm_commissioner/accommodations/find_variant.rb +5 -11
- data/app/finders/spree_cm_commissioner/rates_availabilities/find_bookings.rb +116 -0
- data/app/helpers/spree_cm_commissioner/admin/inventory_items_helper.rb +36 -0
- data/app/helpers/spree_cm_commissioner/admin/rates_availabilities_helper.rb +247 -0
- data/app/interactors/spree_cm_commissioner/intercity_taxi_order_creator.rb +0 -1
- data/app/jobs/spree_cm_commissioner/option_type_variants_public_metadata_updater_job.rb +1 -0
- data/app/jobs/spree_cm_commissioner/option_value_variants_public_metadata_updater_job.rb +1 -0
- data/app/jobs/spree_cm_commissioner/product_variants_public_metadata_updater_job.rb +12 -0
- data/app/models/concerns/spree_cm_commissioner/kyc_bitwise.rb +4 -0
- data/app/models/concerns/spree_cm_commissioner/line_item_durationable.rb +1 -10
- data/app/models/concerns/spree_cm_commissioner/line_item_guests_concern.rb +33 -3
- data/app/models/concerns/spree_cm_commissioner/line_item_rate_plannable.rb +55 -0
- data/app/models/concerns/spree_cm_commissioner/line_item_transitable.rb +0 -16
- data/app/models/concerns/spree_cm_commissioner/option_type_attr_type.rb +22 -4
- data/app/models/concerns/spree_cm_commissioner/rate_plan_options_concern.rb +62 -0
- data/app/models/concerns/spree_cm_commissioner/variant_options_concern.rb +12 -0
- data/app/models/concerns/spree_cm_commissioner/variant_rate_plannable.rb +81 -0
- data/app/models/spree_cm_commissioner/address_decorator.rb +2 -0
- data/app/models/spree_cm_commissioner/configuration.rb +0 -6
- data/app/models/spree_cm_commissioner/exports/import_order_guests_csv.rb +2 -1
- data/app/models/spree_cm_commissioner/exports/operator_guest_json_gzip.rb +8 -2
- data/app/models/spree_cm_commissioner/guest.rb +1 -1
- data/app/models/spree_cm_commissioner/integration.rb +15 -0
- data/app/models/spree_cm_commissioner/integrations/switch_cm.rb +96 -0
- data/app/models/spree_cm_commissioner/inventory_item.rb +32 -3
- data/app/models/spree_cm_commissioner/line_item_decorator.rb +7 -7
- data/app/models/spree_cm_commissioner/maintenance_tasks/cache_invalidation.rb +20 -4
- data/app/models/spree_cm_commissioner/maintenance_tasks/event.rb +3 -2
- data/app/models/spree_cm_commissioner/option_type_decorator.rb +2 -1
- data/app/models/spree_cm_commissioner/option_value_decorator.rb +3 -0
- data/app/models/spree_cm_commissioner/option_value_rate_plan.rb +12 -0
- data/app/models/spree_cm_commissioner/order_decorator.rb +1 -0
- data/app/models/spree_cm_commissioner/price_decorator.rb +13 -0
- data/app/models/spree_cm_commissioner/product_decorator.rb +18 -2
- data/app/models/spree_cm_commissioner/product_option_type_decorator.rb +42 -0
- data/app/models/spree_cm_commissioner/rate_plan.rb +139 -0
- data/app/models/spree_cm_commissioner/rate_plan_options.rb +36 -0
- data/app/models/spree_cm_commissioner/rate_restriction.rb +45 -0
- data/app/models/spree_cm_commissioner/taxon_decorator.rb +5 -2
- data/app/models/spree_cm_commissioner/variant_decorator.rb +36 -5
- data/app/models/spree_cm_commissioner/variant_options.rb +3 -2
- data/app/models/spree_cm_commissioner/vendor_decorator.rb +1 -0
- data/app/overrides/spree/admin/option_types/_form/checkbox.html.erb.deface +0 -8
- data/app/overrides/spree/admin/option_types/_form/name.html.erb.deface +1 -1
- data/app/overrides/spree/admin/products/_filters/vendor_filter.html.erb.deface +6 -0
- data/app/overrides/spree/admin/shared/_product_tabs/rates_availabilities.html.erb.deface +8 -0
- data/app/overrides/spree/admin/shared/_product_tabs/stock_managements.html.erb.deface +1 -1
- data/app/overrides/spree/admin/variants/index/discontinued_badge.html.erb.deface +13 -0
- data/app/overrides/spree/admin/variants/index/option_types_empty_state_link.html.erb.deface +3 -0
- data/app/overrides/spree/admin/variants/index/option_types_summary.html.erb.deface +24 -0
- data/app/presenters/spree/variants/visible_options_presenter.rb +34 -1
- data/app/queries/spree_cm_commissioner/multi_leg_trips_query.rb +40 -22
- data/app/queries/spree_cm_commissioner/single_leg_trips_query.rb +28 -14
- data/app/request_schemas/spree_cm_commissioner/intercity_taxi_draft_order_update_schema.rb +0 -1
- data/app/serializers/spree/v2/storefront/line_item_serializer_decorator.rb +2 -0
- data/app/serializers/spree/v2/storefront/option_type_serializer_decorator.rb +1 -1
- data/app/serializers/spree/v2/storefront/product_option_type_serializer.rb +15 -0
- data/app/serializers/spree/v2/storefront/product_serializer_decorator.rb +15 -0
- data/app/serializers/spree/v2/tenant/line_item_serializer.rb +1 -1
- data/app/serializers/spree/v2/tenant/option_type_serializer.rb +1 -1
- data/app/serializers/spree_cm_commissioner/v2/operator/dynamic_field_option_serializer.rb +9 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/dynamic_field_serializer.rb +11 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/guest_dynamic_field_serializer.rb +14 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb +1 -0
- data/app/serializers/spree_cm_commissioner/v2/storefront/accommodation_variant_serializer.rb +44 -0
- data/app/serializers/spree_cm_commissioner/v2/storefront/inventory_item_serializer.rb +4 -4
- data/app/serializers/spree_cm_commissioner/v2/storefront/rate_plan_serializer.rb +16 -0
- data/app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/accept_push.rb +111 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/accommodation_lock.rb +43 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/apply_updates.rb +133 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/availability_snapshot.rb +127 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/inventory_update.rb +35 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/pushed_nights.rb +82 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/unseen_room_nights.rb +49 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/validate_updates.rb +153 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/feed.rb +209 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/mark_seen.rb +62 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/reservation.rb +171 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/room_catalog.rb +88 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_manager.rb +16 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_strategies/full_sync_strategy.rb +39 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_strategies/incremental_sync_strategy.rb +51 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/availability_parser.rb +103 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/push_response.rb +45 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/reservation_serializer.rb +83 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/response_builder.rb +19 -0
- data/app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/intercity_taxi_order/update.rb +1 -3
- data/app/services/spree_cm_commissioner/inventory_items/bulk_adjust_quantities_by_variant.rb +4 -0
- data/app/services/spree_cm_commissioner/inventory_items/reset.rb +4 -0
- data/app/services/spree_cm_commissioner/rate_plans/save.rb +65 -0
- data/app/services/spree_cm_commissioner/rates_availabilities/bulk_update.rb +225 -0
- data/app/services/spree_cm_commissioner/reschedule_requests/split_line_item.rb +5 -0
- data/app/services/spree_cm_commissioner/room_types/create.rb +87 -0
- data/app/services/spree_cm_commissioner/room_types/update_properties.rb +32 -0
- data/app/services/spree_cm_commissioner/room_types.rb +40 -0
- data/app/services/spree_cm_commissioner/route_prices/update_price_range.rb +3 -1
- data/app/services/spree_cm_commissioner/trips/search.rb +14 -7
- data/app/services/spree_cm_commissioner/trips/update_single_leg.rb +5 -1
- data/app/views/spree/admin/guests/_check_in_status.html.erb +28 -22
- data/app/views/spree/admin/guests/edit.html.erb +29 -26
- data/app/views/spree/admin/integrations/_form.html.erb +4 -0
- data/app/views/spree/admin/integrations/_switch_cm_fields.html.erb +53 -0
- data/app/views/spree/admin/inventory_items/_details.html.erb +56 -0
- data/app/views/spree/admin/product_option_types/index.html.erb +79 -0
- data/app/views/spree/admin/rate_plans/_form.html.erb +134 -0
- data/app/views/spree/admin/rate_plans/_order_form.html.erb +37 -0
- data/app/views/spree/admin/rates_availabilities/_block.html.erb +34 -0
- data/app/views/spree/admin/rates_availabilities/_bookings.html.erb +52 -0
- data/app/views/spree/admin/rates_availabilities/_filters.html.erb +48 -0
- data/app/views/spree/admin/rates_availabilities/_legend.html.erb +27 -0
- data/app/views/spree/admin/rates_availabilities/_list_pages.html.erb +14 -0
- data/app/views/spree/admin/rates_availabilities/_modal_header.html.erb +11 -0
- data/app/views/spree/admin/rates_availabilities/_names.html.erb +62 -0
- data/app/views/spree/admin/rates_availabilities/_script.html.erb +676 -0
- data/app/views/spree/admin/rates_availabilities/_styles.html.erb +159 -0
- data/app/views/spree/admin/rates_availabilities/index.html.erb +112 -0
- data/app/views/spree/admin/shared/sub_menu/_homepage.html.erb +0 -1
- data/app/views/spree/admin/stock_managements/_header.html.erb +0 -14
- data/app/views/spree/admin/stock_managements/_inventory_item_stats.html.erb +28 -0
- data/app/views/spree/admin/stock_managements/index.html.erb +1 -56
- data/config/initializers/spree_permitted_attributes.rb +1 -0
- data/config/locales/en.yml +241 -3
- data/config/locales/km.yml +241 -4
- data/config/routes.rb +36 -17
- data/db/migrate/20260928000000_add_oauth_application_id_to_cm_integrations.rb +5 -0
- data/db/migrate/20260929000000_create_cm_rate_plans.rb +22 -0
- data/db/migrate/20260929000001_add_rate_plan_to_spree_prices.rb +24 -0
- data/db/migrate/20260929000002_add_rate_plan_to_spree_line_items.rb +7 -0
- data/db/migrate/20260929000003_create_cm_option_value_rate_plans.rb +16 -0
- data/db/migrate/20261009000000_create_cm_rate_restrictions.rb +29 -0
- data/db/migrate/20261013000001_add_hidden_and_presentation_to_spree_product_option_types.rb +6 -0
- data/db/migrate/20261013000002_move_hidden_from_spree_option_types_to_product_option_types.rb +36 -0
- data/db/migrate/20261016000001_add_index_to_spree_line_items_number.rb +8 -0
- data/lib/spree_cm_commissioner/booking_dates.rb +43 -0
- data/lib/spree_cm_commissioner/calendar_event.rb +0 -25
- data/lib/spree_cm_commissioner/rate.rb +74 -0
- data/lib/spree_cm_commissioner/rate_closings.rb +48 -0
- data/lib/spree_cm_commissioner/rates_availability_grid.rb +113 -0
- data/lib/spree_cm_commissioner/stay_price.rb +30 -0
- data/lib/spree_cm_commissioner/stay_prices.rb +58 -0
- data/lib/spree_cm_commissioner/test_helper/factories/integration_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/option_type_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/option_value_rate_plan_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/rate_plan_factory.rb +14 -0
- data/lib/spree_cm_commissioner/test_helper/factories/variant_factory.rb +9 -0
- data/lib/spree_cm_commissioner/version.rb +1 -1
- data/lib/spree_cm_commissioner.rb +6 -0
- metadata +94 -32
- data/app/assets/javascripts/spree_cm_commissioner/calendar.js +0 -59
- data/app/controllers/spree/admin/calendars/base_controller.rb +0 -27
- data/app/controllers/spree/admin/calendars/orders_controller.rb +0 -27
- data/app/controllers/spree/admin/homepage_feed_controller.rb +0 -36
- data/app/controllers/spree/admin/metafields_controller.rb +0 -69
- data/app/models/concerns/spree_cm_commissioner/metafield.rb +0 -15
- data/app/models/spree_cm_commissioner/homepage_feed.rb +0 -4
- data/app/overrides/spree/admin/shared/_main_menu/sidebar_calendars.html.erb.deface +0 -5
- data/app/overrides/spree/admin/shared/_product_tabs/metafields.html.erb.deface +0 -15
- data/app/serializers/spree_cm_commissioner/v2/storefront/taxon_include_product_serializer.rb +0 -13
- data/app/serializers/spree_cm_commissioner/v2/storefront/vendor_include_product_serializer.rb +0 -14
- data/app/services/spree_cm_commissioner/feed/taxon_include_product.rb +0 -20
- data/app/services/spree_cm_commissioner/feed/taxon_product.rb +0 -54
- data/app/services/spree_cm_commissioner/feed/vendor_include_product.rb +0 -20
- data/app/services/spree_cm_commissioner/feed/vendor_product.rb +0 -52
- data/app/services/spree_cm_commissioner/homepage_data_loader.rb +0 -112
- data/app/services/spree_cm_commissioner/metafields/product_metadata_service.rb +0 -32
- data/app/views/shared/_calendar.html.erb +0 -27
- data/app/views/shared/calendar/_order_popover.html.erb +0 -18
- data/app/views/shared/calendar/_orders_sidebar.html.erb +0 -41
- data/app/views/spree/admin/calendars/orders/index.html.erb +0 -22
- data/app/views/spree/admin/homepage_feed/_form.html.erb +0 -24
- data/app/views/spree/admin/homepage_feed/edit.html.erb +0 -17
- data/app/views/spree/admin/inventory_items/prices.html.erb +0 -43
- data/app/views/spree/admin/inventory_items/stocks.html.erb +0 -34
- data/app/views/spree/admin/metafields/_private_metafields_form.html.erb +0 -71
- data/app/views/spree/admin/metafields/_public_metafields_form.html.erb +0 -64
- data/app/views/spree/admin/metafields/edit.html.erb +0 -27
- data/app/views/spree/admin/shared/sub_menu/_calendar.html.erb +0 -3
- data/app/views/spree/admin/stock_managements/calendar.html.erb +0 -310
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Reservations
|
|
3
|
+
# The reservation streams a channel manager pulls, over one or more of its connected hotels.
|
|
4
|
+
#
|
|
5
|
+
# Delivery state is the order's IntegrationMapping for the hotel: its `seen_status` is the last
|
|
6
|
+
# status the channel manager has seen (returned in B.XML), and its
|
|
7
|
+
# `seen_modified_at` the latest reschedule it has seen. A reservation stays in its stream until
|
|
8
|
+
# those match its current state, so nothing drops out unseen.
|
|
9
|
+
#
|
|
10
|
+
# A reservation is modified when one of this hotel's reschedule requests completed after the
|
|
11
|
+
# channel manager last saw it. One rescheduled before the channel manager first saw it goes out
|
|
12
|
+
# as new, with its current rooms and dates. A booking from before the hotel connected counts as
|
|
13
|
+
# seen (the summary imported it), so its reschedules since connecting are modifications too.
|
|
14
|
+
#
|
|
15
|
+
# Reservations completed before the hotel's `connected_at` are never streamed as new: the
|
|
16
|
+
# channel manager imports those from the reservation summary while setting the hotel up. Their
|
|
17
|
+
# later cancellations still are.
|
|
18
|
+
class Feed
|
|
19
|
+
DEFAULT_LIMIT = 100
|
|
20
|
+
LIMIT_RANGE = (10..200)
|
|
21
|
+
RESERVATION_INCLUDES = [
|
|
22
|
+
:user, :bill_address, :reschedule_requests,
|
|
23
|
+
{ payments: :payment_method, line_items: [:guests, :rate_plan, :vendor, { variant: :product }] }
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
# Booking.com reads `last_change` in Amsterdam time and serves at most two weeks back.
|
|
27
|
+
LAST_CHANGE_TIME_ZONE = 'Europe/Amsterdam'.freeze
|
|
28
|
+
LAST_CHANGE_WINDOW = 14.days
|
|
29
|
+
|
|
30
|
+
# StoreMetadata's coder JSON.dumps the hash, so the jsonb column holds a JSON *string* and a plain
|
|
31
|
+
# `->> 'seen_status'` would always be NULL. `#>> '{}'` unwraps it (and is a no-op for a real
|
|
32
|
+
# object), and the cast makes it an object again.
|
|
33
|
+
SEEN_STATUS_SQL = "COALESCE((switch_cm_seen.public_metadata #>> '{}')::jsonb ->> 'seen_status', '')".freeze
|
|
34
|
+
# Kept in UTC, which is how Rails stores timestamps, so it compares with them as is.
|
|
35
|
+
SEEN_MODIFIED_AT_SQL = "COALESCE(((switch_cm_seen.public_metadata #>> '{}')::jsonb ->> 'seen_modified_at')::timestamp, '-infinity')".freeze
|
|
36
|
+
CANCELLED_AT_SQL = 'COALESCE(spree_orders.canceled_at, spree_orders.updated_at)'.freeze
|
|
37
|
+
|
|
38
|
+
# Each order's seen status for this hotel, as `SEEN_STATUS_SQL`. MarkSeen keeps one mapping
|
|
39
|
+
# row per hotel and order, so the join never multiplies rows.
|
|
40
|
+
def self.seen_join_sql(integration)
|
|
41
|
+
ActiveRecord::Base.sanitize_sql_array([<<~SQL.squish, integration.id])
|
|
42
|
+
LEFT JOIN cm_integration_mappings switch_cm_seen
|
|
43
|
+
ON switch_cm_seen.internal_type = 'Spree::Order'
|
|
44
|
+
AND switch_cm_seen.internal_id = spree_orders.id
|
|
45
|
+
AND switch_cm_seen.integration_id = ?
|
|
46
|
+
SQL
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The order's latest completed reschedule for the integration's hotel, NULL if none.
|
|
50
|
+
def self.modified_at_sql(integration)
|
|
51
|
+
ActiveRecord::Base.sanitize_sql_array([<<~SQL.squish, integration.vendor_id])
|
|
52
|
+
(SELECT MAX(cm_reschedule_requests.completed_at) FROM cm_reschedule_requests
|
|
53
|
+
WHERE cm_reschedule_requests.order_id = spree_orders.id
|
|
54
|
+
AND cm_reschedule_requests.vendor_id = ?
|
|
55
|
+
AND cm_reschedule_requests.state = 'completed')
|
|
56
|
+
SQL
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Reservations the channel manager has as booked: seen as new, or imported from the summary.
|
|
60
|
+
def self.seen_as_booked_sql(integration)
|
|
61
|
+
ActiveRecord::Base.sanitize_sql_array(
|
|
62
|
+
["(#{SEEN_STATUS_SQL} = 'confirmed' OR (#{SEEN_STATUS_SQL} = '' AND spree_orders.completed_at < ?))", integration.connected_at]
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Confirmed reservations the channel manager has as booked, but not since their latest reschedule.
|
|
67
|
+
def self.unseen_modification_sql(integration)
|
|
68
|
+
modified_at = modified_at_sql(integration)
|
|
69
|
+
ActiveRecord::Base.sanitize_sql_array(
|
|
70
|
+
["#{seen_as_booked_sql(integration)} AND #{modified_at} > #{SEEN_MODIFIED_AT_SQL} AND #{modified_at} >= ?", integration.connected_at]
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.parse_last_change(value)
|
|
75
|
+
return nil if value.blank?
|
|
76
|
+
|
|
77
|
+
ActiveSupport::TimeZone[LAST_CHANGE_TIME_ZONE].parse(value.to_s)
|
|
78
|
+
rescue ArgumentError
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.normalize_limit(value)
|
|
83
|
+
return DEFAULT_LIMIT if value.blank?
|
|
84
|
+
|
|
85
|
+
value.to_i.clamp(LIMIT_RANGE.min, LIMIT_RANGE.max)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def initialize(integrations)
|
|
89
|
+
@integrations = Array(integrations)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Confirmed reservations not yet seen.
|
|
93
|
+
def new_reservations(id: nil, last_change: nil, limit: nil)
|
|
94
|
+
collect(limit) { |integration| new_scope(integration, id: id, last_change: last_change) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Cancellations and modifications not yet seen.
|
|
98
|
+
def modified_reservations(id: nil, last_change: nil, limit: nil)
|
|
99
|
+
merge(limit) do |integration, scope_limit|
|
|
100
|
+
[
|
|
101
|
+
build(integration, cancelled_scope(integration, id: id, last_change: last_change).limit(scope_limit)),
|
|
102
|
+
build(integration, modified_scope(integration, id: id, last_change: last_change).limit(scope_limit), modified: true)
|
|
103
|
+
]
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# B.XML reservations: all three in one stream. With `id`, the one reservation in its current
|
|
108
|
+
# status, which is modified once it's been rescheduled.
|
|
109
|
+
def changed_reservations(id: nil, last_change: nil, limit: nil)
|
|
110
|
+
merge(limit) do |integration, scope_limit|
|
|
111
|
+
modified = build(integration, modified_scope(integration, id: id, last_change: last_change).limit(scope_limit), modified: true)
|
|
112
|
+
new = build(integration, new_scope(integration, id: id, last_change: last_change).limit(scope_limit))
|
|
113
|
+
new = new.reject { |reservation| modified.any? { |other| other.id == reservation.id } } if id.present?
|
|
114
|
+
|
|
115
|
+
[build(integration, cancelled_scope(integration, id: id, last_change: last_change).limit(scope_limit)), modified, new]
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# B.XML reservationssummary: confirmed stays checking out from tomorrow on, whenever booked.
|
|
120
|
+
# It's how the channel manager imports what the hotel sold before connecting.
|
|
121
|
+
def summary
|
|
122
|
+
@integrations.flat_map do |integration|
|
|
123
|
+
upcoming = line_items(integration).where('spree_line_items.to_date >= ?', Time.zone.today.beginning_of_day)
|
|
124
|
+
scope = orders(integration).where(state: Reservation::CONFIRMED_STATES)
|
|
125
|
+
.where(id: upcoming.select(:order_id))
|
|
126
|
+
.order(:completed_at, :id)
|
|
127
|
+
build(integration, scope)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
# The streams the block returns per integration, oldest change first, up to the limit.
|
|
134
|
+
def merge(limit)
|
|
135
|
+
limit = self.class.normalize_limit(limit)
|
|
136
|
+
reservations = @integrations.flat_map { |integration| yield(integration, limit).flatten }
|
|
137
|
+
|
|
138
|
+
reservations.sort_by { |reservation| [reservation.changed_at, reservation.id] }.first(limit)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def collect(limit)
|
|
142
|
+
limit = self.class.normalize_limit(limit)
|
|
143
|
+
|
|
144
|
+
@integrations.each_with_object([]) do |integration, reservations|
|
|
145
|
+
remaining = limit - reservations.size
|
|
146
|
+
break reservations if remaining <= 0
|
|
147
|
+
|
|
148
|
+
reservations.concat(build(integration, yield(integration).limit(remaining)))
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def new_scope(integration, id:, last_change:)
|
|
153
|
+
scope = orders(integration).where(state: Reservation::CONFIRMED_STATES).order(:completed_at, :id)
|
|
154
|
+
return scope.where(id: id) if id.present?
|
|
155
|
+
|
|
156
|
+
scope = scope.where('spree_orders.completed_at >= ?', integration.connected_at)
|
|
157
|
+
return scope.where('spree_orders.completed_at >= ?', clamp_last_change(last_change)) if last_change.present?
|
|
158
|
+
|
|
159
|
+
scope.where("#{SEEN_STATUS_SQL} <> 'confirmed'")
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def cancelled_scope(integration, id:, last_change:)
|
|
163
|
+
scope = orders(integration).where(state: Reservation::CANCELLED_STATES).order(Arel.sql("#{CANCELLED_AT_SQL}, spree_orders.id"))
|
|
164
|
+
return scope.where(id: id) if id.present?
|
|
165
|
+
|
|
166
|
+
scope = scope.where("spree_orders.completed_at >= :at OR #{CANCELLED_AT_SQL} >= :at", at: integration.connected_at)
|
|
167
|
+
return scope.where("#{CANCELLED_AT_SQL} >= ?", clamp_last_change(last_change)) if last_change.present?
|
|
168
|
+
|
|
169
|
+
scope.where("#{SEEN_STATUS_SQL} <> 'cancelled'")
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Confirmed reservations the channel manager has as booked whose latest reschedule came after what it saw.
|
|
173
|
+
# `id` and `last_change` re-read ones rescheduled at all, or since then.
|
|
174
|
+
def modified_scope(integration, id:, last_change:)
|
|
175
|
+
modified_at = self.class.modified_at_sql(integration)
|
|
176
|
+
scope = orders(integration).where(state: Reservation::CONFIRMED_STATES)
|
|
177
|
+
.where(self.class.seen_as_booked_sql(integration))
|
|
178
|
+
.where("#{modified_at} IS NOT NULL")
|
|
179
|
+
.order(Arel.sql("#{modified_at}, spree_orders.id"))
|
|
180
|
+
return scope.where(id: id) if id.present?
|
|
181
|
+
return scope.where("#{modified_at} >= ?", clamp_last_change(last_change)) if last_change.present?
|
|
182
|
+
|
|
183
|
+
scope.where(self.class.unseen_modification_sql(integration))
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def orders(integration)
|
|
187
|
+
Spree::Order.where(id: line_items(integration).select(:order_id)).joins(self.class.seen_join_sql(integration))
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Joined by hand rather than through the associations, whose default scopes would hide
|
|
191
|
+
# reservations for rooms deleted since they were booked.
|
|
192
|
+
def line_items(integration)
|
|
193
|
+
Spree::LineItem.joins('INNER JOIN spree_variants ON spree_variants.id = spree_line_items.variant_id')
|
|
194
|
+
.joins('INNER JOIN spree_products ON spree_products.id = spree_variants.product_id')
|
|
195
|
+
.where(vendor_id: integration.vendor_id, product_type: :accommodation)
|
|
196
|
+
.where(spree_products: { need_confirmation: [false, nil] })
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def clamp_last_change(last_change)
|
|
200
|
+
[last_change, LAST_CHANGE_WINDOW.ago].max
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def build(integration, scope, modified: false)
|
|
204
|
+
scope.includes(*RESERVATION_INCLUDES)
|
|
205
|
+
.map { |order| Reservation.new(integration: integration, order: order, modified: modified) }
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Reservations
|
|
3
|
+
# Records that the channel manager has seen a reservation in the given status, which takes it
|
|
4
|
+
# out of that status's stream (Feed) and out of the rooms we hold back from its
|
|
5
|
+
# availability pushes.
|
|
6
|
+
#
|
|
7
|
+
# `modified_at` is the latest reschedule the channel manager now has. 'modified' records only
|
|
8
|
+
# that; 'confirmed' records it when given.
|
|
9
|
+
#
|
|
10
|
+
# Each (hotel, order) has one mapping row. The mapping's unique index can't enforce that, since
|
|
11
|
+
# its `date` column is NULL here and Postgres treats NULLs as distinct, so a transaction-scoped
|
|
12
|
+
# advisory lock serializes the find-or-create instead. It's per order, so it never waits on
|
|
13
|
+
# the hotel's push lock.
|
|
14
|
+
class MarkSeen
|
|
15
|
+
prepend ::Spree::ServiceModule::Base
|
|
16
|
+
|
|
17
|
+
STATUSES = %w[confirmed modified cancelled].freeze
|
|
18
|
+
|
|
19
|
+
def call(integration:, order:, status:, modified_at: nil)
|
|
20
|
+
status = status.to_s
|
|
21
|
+
return failure(nil, "unknown status #{status}") unless STATUSES.include?(status)
|
|
22
|
+
|
|
23
|
+
mapping = SpreeCmCommissioner::IntegrationMapping.transaction do
|
|
24
|
+
lock!(integration, order)
|
|
25
|
+
upsert_mapping!(integration, order, status, modified_at)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
success(mapping)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def lock!(integration, order)
|
|
34
|
+
key = Zlib.crc32("spree_cm_commissioner:switch_cm_seen:#{integration.id}:#{order.id}")
|
|
35
|
+
SpreeCmCommissioner::IntegrationMapping.connection.execute("SELECT pg_advisory_xact_lock(#{key})")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def upsert_mapping!(integration, order, status, modified_at)
|
|
39
|
+
mapping = SpreeCmCommissioner::IntegrationMapping.find_or_initialize_by(
|
|
40
|
+
integration: integration,
|
|
41
|
+
internal_type: 'Spree::Order',
|
|
42
|
+
internal_id: order.id
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
# An acknowledgement of the new reservation arriving after its cancellation was already seen
|
|
46
|
+
# mustn't put the cancellation back in the stream.
|
|
47
|
+
return mapping if mapping.persisted? && mapping.public_metadata['seen_status'] == 'cancelled' && status == 'confirmed'
|
|
48
|
+
|
|
49
|
+
mapping.external_id = order.number
|
|
50
|
+
mapping.status = :active
|
|
51
|
+
mapping.last_synced_at = Time.current
|
|
52
|
+
metadata = (mapping.public_metadata || {}).merge('seen_at' => Time.current.iso8601)
|
|
53
|
+
metadata['seen_status'] = status unless status == 'modified'
|
|
54
|
+
# Microseconds, so a reschedule completing within the same second still compares as later.
|
|
55
|
+
metadata['seen_modified_at'] = modified_at.utc.iso8601(6) if modified_at
|
|
56
|
+
mapping.public_metadata = metadata
|
|
57
|
+
mapping.save!
|
|
58
|
+
mapping
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Reservations
|
|
3
|
+
# One connected hotel's part of an order, as the channel manager receives it. An order can hold
|
|
4
|
+
# rooms from several hotels; each hotel sees only its own rooms, under the same reservation id.
|
|
5
|
+
#
|
|
6
|
+
# A line item of quantity N becomes N rooms, like Booking.com's one RoomStay per booked room:
|
|
7
|
+
# a connector counts rooms by room stays, so one stay with a unit count would undercount them.
|
|
8
|
+
# We don't keep per-night prices, so each room's paid total is spread evenly over its nights.
|
|
9
|
+
#
|
|
10
|
+
# A guest changes dates or rooms through a reschedule request, which rewrites the line items in place.
|
|
11
|
+
# `modified: true` is the Feed saying the channel manager saw this reservation before its latest
|
|
12
|
+
# completed reschedule, so it's sent again, whole, as modified.
|
|
13
|
+
class Reservation
|
|
14
|
+
CONFIRMED_STATES = %w[complete resumed awaiting_return].freeze
|
|
15
|
+
CANCELLED_STATES = %w[canceled].freeze
|
|
16
|
+
|
|
17
|
+
Room = Struct.new(
|
|
18
|
+
:room_reservation_id, :room_id, :room_name, :rate_id, :arrival_date, :departure_date,
|
|
19
|
+
:adults, :children, :nightly_prices, :total, :commission, :guest_name,
|
|
20
|
+
keyword_init: true
|
|
21
|
+
)
|
|
22
|
+
Customer = Struct.new(
|
|
23
|
+
:first_name, :last_name, :email, :phone, :country_code, :remarks,
|
|
24
|
+
keyword_init: true
|
|
25
|
+
) do
|
|
26
|
+
def full_name = [first_name, last_name].compact_blank.join(' ')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
attr_reader :integration, :order
|
|
30
|
+
|
|
31
|
+
def initialize(integration:, order:, modified: false)
|
|
32
|
+
@integration = integration
|
|
33
|
+
@order = order
|
|
34
|
+
@modified = modified
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.status_of(order)
|
|
38
|
+
return :cancelled if CANCELLED_STATES.include?(order.state)
|
|
39
|
+
|
|
40
|
+
:confirmed if CONFIRMED_STATES.include?(order.state)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
delegate :id, :number, to: :order
|
|
44
|
+
|
|
45
|
+
def hotel_id = integration.vendor_id
|
|
46
|
+
def hotel_name = integration.vendor.name
|
|
47
|
+
def booked_at = order.completed_at
|
|
48
|
+
def cancelled_at = order.canceled_at || order.updated_at
|
|
49
|
+
def cancelled? = self.class.status_of(order) == :cancelled
|
|
50
|
+
def modified? = @modified && !cancelled? && modified_at.present?
|
|
51
|
+
|
|
52
|
+
# :confirmed, :modified or :cancelled, as the channel manager receives it.
|
|
53
|
+
def status
|
|
54
|
+
modified? ? :modified : self.class.status_of(order)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# When this hotel's latest reschedule completed, nil if never rescheduled.
|
|
58
|
+
def modified_at
|
|
59
|
+
return @modified_at if defined?(@modified_at)
|
|
60
|
+
|
|
61
|
+
@modified_at = order.reschedule_requests.select { |request| request.vendor_id == hotel_id && request.completed? }
|
|
62
|
+
.filter_map(&:completed_at).max
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# When this reservation last changed from the channel manager's point of view.
|
|
66
|
+
def changed_at
|
|
67
|
+
return cancelled_at if cancelled?
|
|
68
|
+
|
|
69
|
+
modified? ? modified_at : booked_at
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def currency
|
|
73
|
+
line_items.first&.currency.presence || order.currency
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def total
|
|
77
|
+
rooms.sum(&:total)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# :pay_at_hotel when the guest chose "Pay on site" at checkout, else :prepaid. That's a Check method
|
|
81
|
+
# that doesn't auto-capture, leaving the money to be collected later; other Check methods ("Cash On",
|
|
82
|
+
# counter sales, invoices) capture on the spot, so they're paid.
|
|
83
|
+
#
|
|
84
|
+
# Read from the payment, not the order's payment_state: online payments stuck in processing also
|
|
85
|
+
# leave a completed order balance_due, and a pay-on-site one turns paid once the hotel marks it so.
|
|
86
|
+
def payment_type
|
|
87
|
+
payments = order.payments.sort_by(&:created_at)
|
|
88
|
+
payment = payments.reject { |candidate| %w[failed void invalid].include?(candidate.state) }.last || payments.last
|
|
89
|
+
method = payment&.payment_method
|
|
90
|
+
|
|
91
|
+
method&.type_check? && !method.auto_capture? ? :pay_at_hotel : :prepaid
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The country is the one picked for the phone number at checkout: checkout asks for no postal
|
|
95
|
+
# address, and the billing address the app sends is a fixed placeholder, so it isn't passed on.
|
|
96
|
+
def customer
|
|
97
|
+
@customer ||= begin
|
|
98
|
+
source = [order.bill_address, order.user, first_guest].compact.find { |record| record.first_name.present? || record.last_name.present? }
|
|
99
|
+
|
|
100
|
+
Customer.new(
|
|
101
|
+
first_name: source&.first_name,
|
|
102
|
+
last_name: source&.last_name,
|
|
103
|
+
email: order.email,
|
|
104
|
+
phone: order.intel_phone_number.presence || order.phone_number,
|
|
105
|
+
country_code: order.country_code.presence&.downcase,
|
|
106
|
+
remarks: order.special_instructions.presence
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def rooms
|
|
112
|
+
@rooms ||= line_items.flat_map { |line_item| rooms_for(line_item) }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def line_items
|
|
116
|
+
@line_items ||= order.line_items.select do |line_item|
|
|
117
|
+
line_item.vendor_id == hotel_id && line_item.accommodation? && !line_item.variant.product.need_confirmation?
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
private
|
|
122
|
+
|
|
123
|
+
def rooms_for(line_item)
|
|
124
|
+
quantity = line_item.quantity
|
|
125
|
+
nights = (line_item.from_date.to_date..line_item.to_date.to_date).to_a
|
|
126
|
+
adults = split_count(line_item.number_of_adults, quantity)
|
|
127
|
+
children = split_count(line_item.number_of_kids, quantity)
|
|
128
|
+
guests = line_item.guests.to_a
|
|
129
|
+
|
|
130
|
+
commissions = split_amount(commission_of(line_item), quantity)
|
|
131
|
+
|
|
132
|
+
split_amount(line_item.total, quantity).each_with_index.map do |room_total, index|
|
|
133
|
+
Room.new(
|
|
134
|
+
room_reservation_id: (line_item.id * 1000) + index + 1,
|
|
135
|
+
room_id: line_item.variant_id,
|
|
136
|
+
room_name: RoomCatalog.room_name(line_item.variant),
|
|
137
|
+
rate_id: line_item.rate_plan&.code || line_item.variant_id, # the base rate's id is the room's (RoomCatalog)
|
|
138
|
+
arrival_date: nights.first,
|
|
139
|
+
departure_date: nights.last + 1,
|
|
140
|
+
adults: adults[index],
|
|
141
|
+
children: children[index],
|
|
142
|
+
nightly_prices: nights.zip(split_amount(room_total, nights.size)),
|
|
143
|
+
total: room_total,
|
|
144
|
+
commission: commissions[index],
|
|
145
|
+
guest_name: guests[index]&.full_name.presence || customer.full_name
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# What BookMe+ keeps of the line, at its commission rate (the product's, else the hotel's).
|
|
151
|
+
def commission_of(line_item)
|
|
152
|
+
(line_item.total.to_d * line_item.commission_rate.to_d / 100).round(2)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def first_guest
|
|
156
|
+
line_items.lazy.map { |line_item| line_item.guests.first }.find(&:present?)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Splits cents so the parts add up to the amount exactly; the first parts take the remainder.
|
|
160
|
+
def split_amount(amount, parts)
|
|
161
|
+
base, remainder = (amount.to_d * 100).round.to_i.divmod(parts)
|
|
162
|
+
Array.new(parts) { |index| (base + (index < remainder ? 1 : 0)).to_d / 100 }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def split_count(count, parts)
|
|
166
|
+
base, remainder = count.to_i.divmod(parts)
|
|
167
|
+
Array.new(parts) { |index| base + (index < remainder ? 1 : 0) }
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
# The rooms and rates a channel manager maps for one hotel. Each accommodation variant is its
|
|
3
|
+
# own inventory pool (cm_inventory_items is unique per variant and night), so each variant is
|
|
4
|
+
# one room, and its rates share that room's stock, as Booking.com's rates share a room's BookingLimit.
|
|
5
|
+
#
|
|
6
|
+
# Rates follow Booking.com's parent/child model:
|
|
7
|
+
# - the base rate, the variant's own prices, under the room's id. Always listed, even once the room
|
|
8
|
+
# has rate plans: derived rate plans are priced from it, a client that sends no rate plan still books
|
|
9
|
+
# it, and mappings made before rate plans keep working;
|
|
10
|
+
# - a derived rate plan, as a child rate of the base rate under its code. It follows the base rate's
|
|
11
|
+
# price and closings, so price pushes go to the base rate (ValidateUpdates);
|
|
12
|
+
# - an independent rate plan, as a rate of its own under its code.
|
|
13
|
+
#
|
|
14
|
+
# Variants that need hotel confirmation are left out: a channel manager can't express
|
|
15
|
+
# request-to-book, so it must never push availability that would make them instantly bookable.
|
|
16
|
+
class RoomCatalog
|
|
17
|
+
BASE_RATE_NAME = 'Standard'.freeze
|
|
18
|
+
|
|
19
|
+
Room = Struct.new(:id, :name, :max_persons, :max_children, :variant, :rates, keyword_init: true) do
|
|
20
|
+
def base_rate = rates.first
|
|
21
|
+
|
|
22
|
+
# nil for a rate this room doesn't sell, e.g. an archived rate plan's code.
|
|
23
|
+
def find_rate(rate_id)
|
|
24
|
+
rates.find { |rate| rate.id.to_s == rate_id.to_s }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
Rate = Struct.new(:id, :name, :room_variant, :rate_plan, keyword_init: true) do
|
|
29
|
+
def room_id = room_variant.id
|
|
30
|
+
def base? = rate_plan.nil?
|
|
31
|
+
def child? = rate_plan&.kind_derived? || false
|
|
32
|
+
def parent_id = (room_id if child?)
|
|
33
|
+
|
|
34
|
+
# Booking.com's rate relation: a percentage of the parent's price (90 is 10% off), or an amount added to it.
|
|
35
|
+
def percentage = (100 + rate_plan.adjustment_value if child? && rate_plan.adjustment_percent?)
|
|
36
|
+
def additional = (rate_plan.adjustment_value if child? && rate_plan.adjustment_amount?)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Spree's descriptive_name leaves a dangling " - " when every option type is hidden (as
|
|
40
|
+
# number-of-adults is), so the options are appended only when there are any to show.
|
|
41
|
+
def self.room_name(variant)
|
|
42
|
+
options = variant.options_text
|
|
43
|
+
options.present? ? "#{variant.name} - #{options}" : variant.name
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def initialize(vendor)
|
|
47
|
+
@vendor = vendor
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def rooms
|
|
51
|
+
@rooms ||= variants.map do |variant|
|
|
52
|
+
Room.new(
|
|
53
|
+
id: variant.id,
|
|
54
|
+
name: self.class.room_name(variant),
|
|
55
|
+
max_persons: variant.number_of_guests + variant.allowed_extra_adults + variant.allowed_extra_kids,
|
|
56
|
+
max_children: variant.number_of_kids + variant.allowed_extra_kids,
|
|
57
|
+
variant: variant,
|
|
58
|
+
rates: rates(variant)
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def find(room_id)
|
|
64
|
+
rooms_by_id[room_id.to_s]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def rates(variant)
|
|
70
|
+
base = Rate.new(id: variant.id, name: BASE_RATE_NAME, room_variant: variant, rate_plan: nil)
|
|
71
|
+
[base] + variant.active_rate_plans.map do |rate_plan|
|
|
72
|
+
Rate.new(id: rate_plan.code, name: rate_plan.display_name, room_variant: variant, rate_plan: rate_plan)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def rooms_by_id
|
|
77
|
+
@rooms_by_id ||= rooms.index_by { |room| room.id.to_s }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def variants
|
|
81
|
+
Spree::Variant.joins(:product)
|
|
82
|
+
.where(vendor_id: @vendor.id, is_master: false, product_type: :accommodation)
|
|
83
|
+
.where(spree_products: { need_confirmation: [false, nil], deleted_at: nil })
|
|
84
|
+
.includes(:product, option_values: :option_type, active_rate_plans: { option_values: :option_type })
|
|
85
|
+
.order(:product_id, :position, :id)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
# switch.cm calls us, so polling has nothing to pull. It runs our own periodic checks for the
|
|
3
|
+
# hotel instead, logged as sync sessions like any other integration's: incremental alerts ops
|
|
4
|
+
# about reservations switch.cm hasn't picked up, full keeps the pushed-night bookkeeping tidy.
|
|
5
|
+
class SyncManager < SpreeCmCommissioner::Integrations::Base::SyncManager
|
|
6
|
+
# override
|
|
7
|
+
def sync_full!
|
|
8
|
+
run_execution(:full) { SyncStrategies::FullSyncStrategy.new(integration: @integration).call }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# override
|
|
12
|
+
def sync_incremental!
|
|
13
|
+
run_execution(:incremental) { SyncStrategies::IncrementalSyncStrategy.new(integration: @integration).call }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_strategies/full_sync_strategy.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module SyncStrategies
|
|
3
|
+
# Daily upkeep of the pushed-night bookkeeping (Inventory::PushedNights):
|
|
4
|
+
#
|
|
5
|
+
# - Reconciles unpushed nights with who owns the rooms: closed while switch.cm owns them, open
|
|
6
|
+
# otherwise. The connection's own save does this when ownership changes; this catches nights
|
|
7
|
+
# that slipped past it, e.g. written by a path that skips callbacks.
|
|
8
|
+
# - Deletes mappings for past nights, which nothing reads and would otherwise grow by rooms ×
|
|
9
|
+
# 365 rows a year per hotel.
|
|
10
|
+
#
|
|
11
|
+
# Reconciling takes the hotel's push lock, so it never races a push that is opening a night.
|
|
12
|
+
# A busy lock fails the session, and PollingScheduler retries a failed full sync on its next tick.
|
|
13
|
+
class FullSyncStrategy
|
|
14
|
+
LOCK_WAIT = 5.seconds
|
|
15
|
+
|
|
16
|
+
class LockBusyError < StandardError; end
|
|
17
|
+
|
|
18
|
+
def initialize(integration:)
|
|
19
|
+
@integration = integration
|
|
20
|
+
@sync_result = SpreeCmCommissioner::Integrations::Base::SyncResult.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call
|
|
24
|
+
pushed_nights = Inventory::PushedNights.new(@integration)
|
|
25
|
+
|
|
26
|
+
@sync_result.increment_metric(:pushed_nights, :pruned, pushed_nights.prune_past!)
|
|
27
|
+
|
|
28
|
+
reconciled = Inventory::AccommodationLock.with_lock(@integration, wait: LOCK_WAIT) { pushed_nights.reconcile_unpushed! }
|
|
29
|
+
raise LockBusyError, 'Another push for this hotel held the lock' if reconciled == Inventory::AccommodationLock::BUSY
|
|
30
|
+
|
|
31
|
+
@sync_result.increment_metric(:unpushed_nights, @integration.owns_inventory? ? :closed : :reopened, reconciled)
|
|
32
|
+
@sync_result
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
@sync_result.record_error(e)
|
|
35
|
+
@sync_result
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module SyncStrategies
|
|
3
|
+
# switch.cm pulls reservations, so a connector that stops polling fails silently: the hotel
|
|
4
|
+
# never hears about new bookings, changes or cancellations. This alerts ops about the hotel's
|
|
5
|
+
# reservations still unseen after UNSEEN_AFTER, the same point at which Booking.com falls back
|
|
6
|
+
# to other delivery. It alerts on every run until they're picked up, so the hotel's
|
|
7
|
+
# incremental interval (SwitchCm defaults it to UNSEEN_AFTER) is also the alert's repeat rate.
|
|
8
|
+
class IncrementalSyncStrategy
|
|
9
|
+
UNSEEN_AFTER = 30.minutes
|
|
10
|
+
LISTED = 10
|
|
11
|
+
|
|
12
|
+
def initialize(integration:)
|
|
13
|
+
@integration = integration
|
|
14
|
+
@sync_result = SpreeCmCommissioner::Integrations::Base::SyncResult.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call
|
|
18
|
+
stale = stale_reservations
|
|
19
|
+
stale.group_by(&:status).each { |status, reservations| @sync_result.increment_metric(:unseen_reservations, status, reservations.size) }
|
|
20
|
+
alert(stale) if stale.any?
|
|
21
|
+
|
|
22
|
+
@sync_result
|
|
23
|
+
rescue StandardError => e
|
|
24
|
+
@sync_result.record_error(e)
|
|
25
|
+
@sync_result
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def stale_reservations
|
|
31
|
+
feed = Reservations::Feed.new([@integration])
|
|
32
|
+
limit = Reservations::Feed::LIMIT_RANGE.max
|
|
33
|
+
cutoff = UNSEEN_AFTER.ago
|
|
34
|
+
|
|
35
|
+
(feed.new_reservations(limit: limit) + feed.modified_reservations(limit: limit)).select { |reservation| reservation.changed_at <= cutoff }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def alert(reservations)
|
|
39
|
+
data = {
|
|
40
|
+
integration_id: @integration.id,
|
|
41
|
+
hotel: "#{@integration.vendor.name} (#{@integration.vendor_id})",
|
|
42
|
+
reservations: reservations.first(LISTED).map { |reservation| "#{reservation.number} #{reservation.status}" }.join(', ')
|
|
43
|
+
}
|
|
44
|
+
message = "switch.cm hasn't picked up #{reservations.size} reservation(s) for #{UNSEEN_AFTER.inspect} or more"
|
|
45
|
+
|
|
46
|
+
CmAppLogger.error(label: "#{self.class.name}#alert", data: data.merge(message: message))
|
|
47
|
+
SpreeCmCommissioner::TelegramAlerts::IntegrationSyncFailure.call(error_message: message, data: data)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|