spree_cm_commissioner 2.5.1.pre.pre3 → 2.5.1.pre.pre4

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.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test_and_build_gem.yml +2 -2
  3. data/Gemfile.lock +1 -1
  4. data/app/controllers/spree/api/v2/storefront/popular_route_places_controller.rb +2 -2
  5. data/app/controllers/spree/api/v2/storefront/transit/draft_orders_controller.rb +4 -4
  6. data/app/controllers/spree/api/v2/tenant/popular_route_places_controller.rb +60 -0
  7. data/app/controllers/spree/api/v2/tenant/routes_controller.rb +50 -0
  8. data/app/controllers/spree/api/v2/tenant/transit/draft_orders_controller.rb +46 -0
  9. data/app/controllers/spree/transit/trips_controller.rb +3 -3
  10. data/app/finders/spree_cm_commissioner/places/find_with_route.rb +12 -12
  11. data/app/finders/spree_cm_commissioner/route_metrics/find_popular.rb +44 -0
  12. data/app/finders/spree_cm_commissioner/routes/find.rb +94 -0
  13. data/app/finders/spree_cm_commissioner/routes/find_popular.rb +19 -35
  14. data/app/interactors/spree_cm_commissioner/stock/permanent_inventory_items_generator.rb +11 -4
  15. data/app/interactors/spree_cm_commissioner/stock/stock_movement_creator.rb +10 -1
  16. data/app/interactors/spree_cm_commissioner/trip_clone_creator.rb +4 -3
  17. data/app/interactors/spree_cm_commissioner/trip_stops_creator.rb +2 -2
  18. data/app/jobs/spree_cm_commissioner/route_metrics/decrease_trip_count_job.rb +10 -0
  19. data/app/jobs/spree_cm_commissioner/route_metrics/increase_fulfilled_order_count_job.rb +10 -0
  20. data/app/jobs/spree_cm_commissioner/route_metrics/increase_order_count_job.rb +10 -0
  21. data/app/jobs/spree_cm_commissioner/route_metrics/increase_trip_count_job.rb +10 -0
  22. data/app/jobs/spree_cm_commissioner/stock/permanent_inventory_items_generator_job.rb +2 -2
  23. data/app/models/concerns/spree_cm_commissioner/route_order_countable.rb +2 -2
  24. data/app/models/spree_cm_commissioner/place.rb +5 -8
  25. data/app/models/spree_cm_commissioner/product_decorator.rb +1 -0
  26. data/app/models/spree_cm_commissioner/route.rb +45 -5
  27. data/app/models/spree_cm_commissioner/route_metric.rb +21 -0
  28. data/app/models/spree_cm_commissioner/route_photo.rb +12 -0
  29. data/app/models/spree_cm_commissioner/trip.rb +8 -33
  30. data/app/models/spree_cm_commissioner/trip_stop.rb +16 -2
  31. data/app/models/spree_cm_commissioner/vendor_decorator.rb +3 -1
  32. data/app/queries/spree_cm_commissioner/trip_query.rb +2 -2
  33. data/app/serializers/spree/v2/tenant/transit_cart_serializer.rb +11 -0
  34. data/app/serializers/spree_cm_commissioner/v2/storefront/route_metric_serializer.rb +13 -0
  35. data/app/serializers/spree_cm_commissioner/v2/storefront/route_serializer.rb +2 -1
  36. data/app/serializers/spree_cm_commissioner/v2/storefront/transit_line_item_serializer.rb +17 -0
  37. data/app/serializers/spree_cm_commissioner/v2/storefront/trip_stop_serializer.rb +1 -1
  38. data/app/services/spree_cm_commissioner/route_metrics/decrease_trip_count.rb +31 -0
  39. data/app/services/spree_cm_commissioner/{routes/increment_fulfilled_order_count.rb → route_metrics/increase_fulfilled_order_count.rb} +3 -3
  40. data/app/services/spree_cm_commissioner/{routes/increment_order_count.rb → route_metrics/increase_order_count.rb} +3 -3
  41. data/app/services/spree_cm_commissioner/route_metrics/increase_trip_count.rb +31 -0
  42. data/app/services/spree_cm_commissioner/{routes/base_update_order_metrics.rb → route_metrics/update_route_metrics.rb} +11 -16
  43. data/app/services/spree_cm_commissioner/routes/create.rb +51 -0
  44. data/app/services/spree_cm_commissioner/routes/update.rb +25 -0
  45. data/app/{interactors/spree_cm_commissioner/transit/draft_order_creator.rb → services/spree_cm_commissioner/transit_order/create.rb} +13 -16
  46. data/app/services/spree_cm_commissioner/trips/create_single_leg.rb +123 -0
  47. data/app/services/spree_cm_commissioner/trips/service_calendars/create_or_update.rb +54 -0
  48. data/app/services/spree_cm_commissioner/trips/update_single_leg.rb +88 -0
  49. data/app/services/spree_cm_commissioner/trips/variants/create.rb +103 -0
  50. data/app/views/spree/transit/trip_stops/index.html.erb +4 -2
  51. data/app/views/spree_cm_commissioner/guest_mailer/send_ticket_to_guest.html.erb +0 -1
  52. data/config/initializers/spree_permitted_attributes.rb +11 -0
  53. data/config/routes.rb +6 -0
  54. data/db/migrate/20251224033103_migrate_cm_routes_to_cm_route_metrics.rb +17 -0
  55. data/db/migrate/20251224033910_migrate_cm_vendor_routes_to_cm_routes.rb +30 -0
  56. data/db/migrate/20260105072450_migrate_cm_trip_stops_to_support_trip_connection.rb +12 -0
  57. data/db/migrate/20260108101406_add_allow_booking_to_cm_trips.rb +5 -0
  58. data/lib/spree_cm_commissioner/test_helper/factories/route_factory.rb +7 -6
  59. data/lib/spree_cm_commissioner/test_helper/factories/route_metric_factory.rb +12 -0
  60. data/lib/spree_cm_commissioner/test_helper/factories/route_photo_factory.rb +5 -0
  61. data/lib/spree_cm_commissioner/test_helper/factories/trip_factory.rb +4 -1
  62. data/lib/spree_cm_commissioner/test_helper/factories/trip_stop_factory.rb +3 -1
  63. data/lib/spree_cm_commissioner/test_helper/factories/vendor_factory.rb +2 -0
  64. data/lib/spree_cm_commissioner/test_helper/factories/vendor_place_factory.rb +22 -0
  65. data/lib/spree_cm_commissioner/transit/route_stop.rb +61 -0
  66. data/lib/spree_cm_commissioner/transit/route_stop_collection.rb +175 -0
  67. data/lib/spree_cm_commissioner/transit/trip_form.rb +81 -0
  68. data/lib/spree_cm_commissioner/transit/trip_stop_form.rb +61 -0
  69. data/lib/spree_cm_commissioner/version.rb +1 -1
  70. data/lib/spree_cm_commissioner.rb +4 -0
  71. metadata +37 -17
  72. data/app/jobs/spree_cm_commissioner/transit/route_fulfilled_order_count_incrementer_job.rb +0 -10
  73. data/app/jobs/spree_cm_commissioner/transit/route_order_count_incrementer_job.rb +0 -10
  74. data/app/jobs/spree_cm_commissioner/transit/route_previous_trip_count_decrementer_job.rb +0 -13
  75. data/app/jobs/spree_cm_commissioner/transit/route_trip_count_decrementer_job.rb +0 -10
  76. data/app/jobs/spree_cm_commissioner/transit/route_trip_count_incrementer_job.rb +0 -10
  77. data/app/models/concerns/spree_cm_commissioner/route_trip_count_callbacks.rb +0 -48
  78. data/app/models/spree_cm_commissioner/trip_connection.rb +0 -36
  79. data/app/models/spree_cm_commissioner/vendor_route.rb +0 -9
  80. data/app/services/spree_cm_commissioner/routes/decrement_previous_trip_count.rb +0 -30
  81. data/app/services/spree_cm_commissioner/routes/decrement_trip_count.rb +0 -33
  82. data/app/services/spree_cm_commissioner/routes/increment_trip_count.rb +0 -33
  83. data/lib/spree_cm_commissioner/test_helper/factories/trip_connection_factory.rb +0 -6
@@ -0,0 +1,123 @@
1
+ module SpreeCmCommissioner
2
+ module Trips
3
+ # Service class responsible for creating a single-leg trip in the booking system.
4
+ # Orchestrates the creation of product variant, trip record, stops, calendar, and inventory.
5
+ class CreateSingleLeg
6
+ prepend Spree::ServiceModule::Base
7
+
8
+ # Main service method that validates the trip form and orchestrates creation.
9
+ # Creates variant, trip, calendar, and inventory within a database transaction.
10
+ # Returns success with created objects or failure with error message on exceptions.
11
+ def call(vendor:, trip_form:)
12
+ return failure(nil, trip_form.errors.full_messages.to_sentence) unless trip_form.valid_data?
13
+
14
+ ApplicationRecord.transaction do
15
+ variant = create_variant!(vendor, trip_form)
16
+ trip = create_trip!(vendor, trip_form, variant)
17
+ calendar = setup_calendar!(variant.product, trip_form.service_calendar)
18
+ generate_inventory!(variant)
19
+
20
+ success(trip: trip, variant: variant, calendar: calendar)
21
+ rescue StandardError => e
22
+ CmAppLogger.error(
23
+ label: 'SpreeCmCommissioner::Trips::CreateSingleLeg#call',
24
+ data: {
25
+ error_class: e.class.name,
26
+ error_message: e.message
27
+ }
28
+ )
29
+ failure(nil, e.message)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ # Creates a product variant for the trip using the vehicle type's seat capacity.
36
+ def create_variant!(vendor, trip_form)
37
+ first_stop = trip_form.trip_stops.first
38
+ vehicle_type = SpreeCmCommissioner::VehicleType.find(first_stop.vehicle_type_id)
39
+ capacity = vehicle_type.number_of_seats
40
+
41
+ result = SpreeCmCommissioner::Trips::Variants::Create.call(
42
+ vendor: vendor,
43
+ trip_form: trip_form,
44
+ price: trip_form.price || 0.0,
45
+ capacity: capacity
46
+ )
47
+
48
+ raise result.error unless result.success?
49
+
50
+ result.value[:variant]
51
+ end
52
+
53
+ # Creates the main trip record with details extracted from the trip form.
54
+ # Associates the trip with vendor, product, vehicle, route, and time.
55
+ def create_trip!(vendor, trip_form, variant)
56
+ first_stop = trip_form.trip_stops.first
57
+ last_stop = trip_form.trip_stops.last
58
+
59
+ locations = vendor.locations.where(id: trip_form.trip_stops.map(&:location_id)).index_by(&:id)
60
+ stops = vendor.stops.where(id: trip_form.trip_stops.map(&:stop_id)).index_by(&:id)
61
+
62
+ trip = vendor.trips.create!(
63
+ product: variant.product,
64
+ vehicle_type: SpreeCmCommissioner::VehicleType.find_by(id: first_stop.vehicle_type_id),
65
+ vehicle: SpreeCmCommissioner::Vehicle.find_by(id: first_stop.vehicle_id),
66
+ origin_place: locations[first_stop.location_id].place,
67
+ destination_place: locations[last_stop.location_id].place,
68
+ route: vendor.routes.find(trip_form.route_id),
69
+ departure_time: first_stop.departure_time,
70
+ duration: trip_form.total_duration_seconds,
71
+ allow_booking: trip_form.allow_booking,
72
+ allow_seat_selection: trip_form.allow_seat_selection,
73
+ route_type: trip_form.route_type || vendor.routes.find(trip_form.route_id)&.route_type
74
+ )
75
+
76
+ create_trip_stops(trip, trip_form, locations, stops)
77
+ trip
78
+ end
79
+
80
+ # Creates individual trip stops based on the trip form stops.
81
+ # Each stop includes arrival/departure times and boarding/drop-off permissions.
82
+ def create_trip_stops(trip, trip_form, locations, stops)
83
+ previous_departure = nil
84
+
85
+ trip_form.trip_stops.each do |ts|
86
+ arrival_time = previous_departure || ts.departure_time
87
+
88
+ trip.trip_stops.create!(
89
+ stop_place: stops[ts.stop_id].place,
90
+ location_place: locations[ts.location_id].place,
91
+ allow_boarding: ts.allow_boarding?,
92
+ allow_drop_off: ts.allow_drop_off,
93
+ arrival_time: arrival_time,
94
+ departure_time: ts.departure_time
95
+ )
96
+
97
+ previous_departure = ts.departure_time
98
+ end
99
+ end
100
+
101
+ # Sets up the service calendar for the trip, defining operational dates, weekdays, and exceptions.
102
+ def setup_calendar!(product, calendar_form)
103
+ res = SpreeCmCommissioner::Trips::ServiceCalendars::CreateOrUpdate.call(
104
+ calendarable: product,
105
+ name: calendar_form.name,
106
+ start_date: calendar_form.start_date,
107
+ end_date: calendar_form.end_date,
108
+ weekdays: calendar_form.weekdays,
109
+ exception_rules: calendar_form.exception_rules || []
110
+ )
111
+
112
+ raise res.error unless res.success?
113
+
114
+ res.value[:calendar]
115
+ end
116
+
117
+ # Triggers asynchronous generation of permanent inventory items for the variant.
118
+ def generate_inventory!(variant)
119
+ SpreeCmCommissioner::Stock::PermanentInventoryItemsGeneratorJob.perform_later(variant_ids: [variant[:id]])
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,54 @@
1
+ module SpreeCmCommissioner
2
+ module Trips
3
+ module ServiceCalendars
4
+ # Service to create or update a service calendar for a calendarable entity.
5
+ class CreateOrUpdate
6
+ prepend ::Spree::ServiceModule::Base
7
+
8
+ def call(calendarable:, name:, start_date:, end_date:, weekdays:, exception_rules: []) # rubocop:disable Metrics/ParameterLists
9
+ return failure(nil, 'calendarable must be present') if calendarable.blank?
10
+ return failure(nil, 'name must be present') if name.blank?
11
+ return failure(nil, 'start_date must be present') if start_date.blank?
12
+ return failure(nil, 'end_date must be present') if end_date.blank?
13
+ return failure(nil, 'weekdays must be present') if weekdays.blank?
14
+
15
+ ApplicationRecord.transaction do
16
+ calendar = create_or_update_calendar(calendarable, name, start_date, end_date, weekdays, exception_rules)
17
+ success(calendar: calendar)
18
+ end
19
+ rescue StandardError => e
20
+ CmAppLogger.error(
21
+ label: 'SpreeCmCommissioner::Trips::ServiceCalendars::CreateOrUpdate#call',
22
+ data: {
23
+ error_class: e.class.name,
24
+ error_message: e.message
25
+ }
26
+ )
27
+ failure(nil, 'Failed to create service calendar')
28
+ end
29
+
30
+ private
31
+
32
+ # Create or update calendar
33
+ def create_or_update_calendar(calendarable, name, start_date, end_date, weekdays, exception_rules) # rubocop:disable Metrics/ParameterLists
34
+ calendar = calendarable.service_calendar || calendarable.build_service_calendar
35
+
36
+ calendar.assign_attributes(
37
+ name: name,
38
+ start_date: start_date,
39
+ end_date: end_date,
40
+ exception_rules: exception_rules
41
+ )
42
+
43
+ # Set weekday attributes
44
+ weekdays.each do |day, enabled|
45
+ calendar.send("#{day}=", enabled)
46
+ end
47
+
48
+ calendar.save!
49
+ calendar
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,88 @@
1
+ module SpreeCmCommissioner
2
+ module Trips
3
+ # Service class responsible for updating an existing single-leg trip.
4
+ # Handles updates to trip, product, variant, stops, and calendar in the booking system.
5
+ class UpdateSingleLeg
6
+ prepend Spree::ServiceModule::Base
7
+
8
+ # Main service method that orchestrates trip updates within a database transaction.
9
+ # Updates trip/product, variant if price provided, stops, and calendar conditionally.
10
+ # Returns success with updated objects or failure with error message.
11
+ def call(trip:, trip_form:)
12
+ ApplicationRecord.transaction do
13
+ update_trip_and_product(trip, trip_form)
14
+ update_variant(trip, trip_form) if trip_form.price.present?
15
+ update_trip_stops(trip, trip_form)
16
+ calendar = update_calendar(trip.product, trip_form.service_calendar) if trip_form.service_calendar.present?
17
+
18
+ success(trip: trip, variant: trip.product.variants.first, calendar: calendar)
19
+ rescue StandardError => e
20
+ CmAppLogger.error(
21
+ label: 'SpreeCmCommissioner::Trips::UpdateSingleLeg#call',
22
+ data: {
23
+ error_class: e.class.name,
24
+ error_message: e.message
25
+ }
26
+ )
27
+ failure(nil, 'Failed to update trip')
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # Updates the trip record and associated product with attributes from the form.
34
+ def update_trip_and_product(trip, trip_form)
35
+ trip.update!(trip_attrs(trip_form))
36
+ trip.product.update!(product_attrs(trip_form))
37
+ end
38
+
39
+ # Updates the variant's price if a new price is provided in the form.
40
+ def update_variant(trip, trip_form)
41
+ variant = trip.product.variants.where.not(id: trip.product.master.id).first
42
+ return unless variant
43
+
44
+ variant.update!(price: trip_form.price)
45
+ trip.product.update!(price: trip_form.price)
46
+ end
47
+
48
+ # Iterates through stops and applies updates only if attributes are present.
49
+ def update_trip_stops(trip, trip_form)
50
+ trip.trip_stops.each_with_index do |stop, idx|
51
+ ts_form = trip_form.trip_stops[idx]
52
+ next unless ts_form
53
+
54
+ updates = {
55
+ allow_boarding: ts_form.allow_boarding?,
56
+ allow_drop_off: ts_form.allow_drop_off
57
+ }.compact
58
+
59
+ stop.update!(updates) if updates.any?
60
+ end
61
+ end
62
+
63
+ # Updates or creates the service calendar for the product using calendar form data.
64
+ def update_calendar(product, calendar_form)
65
+ result = SpreeCmCommissioner::Trips::ServiceCalendars::CreateOrUpdate.call(
66
+ calendarable: product,
67
+ name: calendar_form.name,
68
+ start_date: calendar_form.start_date,
69
+ end_date: calendar_form.end_date,
70
+ weekdays: calendar_form.weekdays,
71
+ exception_rules: calendar_form.exception_rules || []
72
+ )
73
+
74
+ raise result.error unless result.success?
75
+
76
+ result.value[:calendar]
77
+ end
78
+
79
+ def trip_attrs(form)
80
+ { allow_booking: form.allow_booking, allow_seat_selection: form.allow_seat_selection }.compact
81
+ end
82
+
83
+ def product_attrs(form)
84
+ { name: form.name, short_name: form.short_name }.compact
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,103 @@
1
+ module SpreeCmCommissioner
2
+ module Trips
3
+ module Variants
4
+ # Service class responsible for creating product and variant for a trip.
5
+ # Handles validations, product creation with options, variant pricing, and stock setup.
6
+ class Create
7
+ prepend ::Spree::ServiceModule::Base
8
+
9
+ # Main method that validates inputs and creates product, options, variant, and stock in transaction.
10
+ # Returns success with product and variant or failure with error message.
11
+ def call(vendor:, trip_form:, price:, capacity:)
12
+ return failure(nil, 'vendor must be present') if vendor.blank?
13
+ return failure(nil, 'trip_form must be present') if trip_form.blank?
14
+ return failure(nil, 'price must be present') if price.blank?
15
+ return failure(nil, 'capacity must be present') if capacity.blank?
16
+ return failure(nil, 'capacity must be greater than 0') if capacity <= 0
17
+
18
+ ApplicationRecord.transaction do
19
+ product = create_product!(vendor, trip_form)
20
+ option_values = setup_option_values!(product)
21
+ variant = create_variant!(product, price, option_values)
22
+
23
+ create_stock_item!(variant, capacity)
24
+
25
+ success(product: product, variant: variant)
26
+ end
27
+ rescue StandardError => e
28
+ CmAppLogger.error(
29
+ label: 'SpreeCmCommissioner::Trips::Variants::Create#call',
30
+ data: {
31
+ error_class: e.class.name,
32
+ error_message: e.message
33
+ }
34
+ )
35
+ failure(nil, e.message)
36
+ end
37
+
38
+ private
39
+
40
+ # Creates a transit product with name based on stops, assigns vendor, type, and options.
41
+ # Includes seat-type option type and default store association.
42
+ def create_product!(vendor, trip_form)
43
+ vendor_stops = stops(vendor, trip_form)
44
+ first_stop = vendor_stops[trip_form.trip_stops.first.stop_id].place
45
+ last_stop = vendor_stops[trip_form.trip_stops.last.stop_id].place
46
+
47
+ Spree::Product.create!(
48
+ name: trip_form.name.presence || "#{first_stop.name} - #{last_stop.name}",
49
+ short_name: trip_form.short_name,
50
+ vendor: vendor,
51
+ product_type: 'transit',
52
+ status: 'active',
53
+ available_on: Time.current,
54
+ price: trip_form.price,
55
+ shipping_category: Spree::ShippingCategory.find_or_create_by!(name: 'Default'),
56
+ option_types: [Spree::OptionType.find_by(name: 'seat-type')].compact,
57
+ stores: [Spree::Store.default].compact
58
+ )
59
+ end
60
+
61
+ # Retrieves and indexes stops from the vendor for the trip form's stop IDs.
62
+ def stops(vendor, trip_form)
63
+ vendor.stops.where(id: trip_form.trip_stops.map(&:stop_id)).index_by(&:id)
64
+ end
65
+
66
+ # Sets up or finds the seat-type option type and creates normal option value.
67
+ # Associates the option type with the product if not already present.
68
+ def setup_option_values!(product)
69
+ option_type = Spree::OptionType.find_or_create_by!(
70
+ name: 'seat-type',
71
+ presentation: 'Seat Type'
72
+ )
73
+
74
+ product.option_types << option_type unless product.option_types.include?(option_type)
75
+
76
+ option_value = option_type.option_values.find_or_create_by_name!(option_type, 'Normal')
77
+
78
+ [option_value]
79
+ end
80
+
81
+ # Creates a product variant with the given price and associated option values.
82
+ # Saves the variant to the database.
83
+ def create_variant!(product, price, option_values)
84
+ variant = product.variants.new(price: price)
85
+ variant.option_values = option_values
86
+ variant.save!
87
+ variant
88
+ end
89
+
90
+ # Creates a stock item for the variant with the specified capacity.
91
+ # Uses stock movement creator to initialize inventory.
92
+ def create_stock_item!(variant, capacity)
93
+ result = SpreeCmCommissioner::Stock::StockMovementCreator.call(
94
+ variant_id: variant.id,
95
+ current_store: variant.product.stores.first,
96
+ stock_movement_params: { quantity: capacity }
97
+ )
98
+ result.stock_item
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -9,7 +9,8 @@
9
9
  <tr data-hook="option_header">
10
10
  <th class="no-border handel-head"></th>
11
11
  <th><%= Spree.t(:name) %></th>
12
- <th><%= Spree.t(:stop_type) %></th>
12
+ <th><%= Spree.t(:allow_boarding) %></th>
13
+ <th><%= Spree.t(:allow_drop_off) %></th>
13
14
  <th><%= Spree.t(:sequence) %></th>
14
15
  <th><%= Spree.t(:created_at) %></th>
15
16
  </tr>
@@ -21,7 +22,8 @@
21
22
  <%= svg_icon name: "grip-vertical.svg", width: '18', height: '18' %>
22
23
  </td>
23
24
  <td><%= stop.stop_name %></td>
24
- <td> <%= stop.stop_type %></td>
25
+ <td> <%= stop.allow_boarding ? 'Yes' : 'No' %></td>
26
+ <td> <%= stop.allow_drop_off ? 'Yes' : 'No' %></td>
25
27
  <td> <%= stop.sequence %></td>
26
28
  <td> <%= stop.created_at.to_date %></td>
27
29
  </tr>
@@ -151,7 +151,6 @@
151
151
  <% @trip.trip_stops.order(:sequence).each do |stop| %>
152
152
  <tr>
153
153
  <td>
154
- <span class="stop-type <%= stop.stop_type %>"><%= stop.stop_type.to_s.titleize %></span>
155
154
  <div class="stop-name"><strong><%= stop.stop_name %></strong></div>
156
155
  <% if stop.respond_to?(:location_place) && stop.location_place.present? %>
157
156
  <% if stop.location_place.respond_to?(:address) && stop.location_place.address.present? %>
@@ -1,5 +1,8 @@
1
1
  module Spree
2
2
  module PermittedAttributes
3
+ ATTRIBUTES << :route_attributes unless ATTRIBUTES.include?(:route_attributes)
4
+ mattr_reader :route_attributes
5
+
3
6
  @@vendor_attributes << :logo
4
7
 
5
8
  # Permitted all guest attributes for now as permitting only some guest attributes is not working by design
@@ -86,5 +89,13 @@ module Spree
86
89
  originator_type
87
90
  originator_id
88
91
  ]
92
+
93
+ @@route_attributes = %i[
94
+ route_name
95
+ short_name
96
+ vendor_id
97
+ route_type
98
+ route_stops
99
+ ]
89
100
  end
90
101
  end
data/config/routes.rb CHANGED
@@ -560,6 +560,12 @@ Spree::Core::Engine.add_routes do
560
560
  resources :trip_places, only: :index
561
561
  resources :trip_search, only: [:index]
562
562
  resources :trips, only: %i[show]
563
+ resources :popular_route_places, only: [:index]
564
+ resources :routes, only: [:index]
565
+
566
+ namespace :transit do
567
+ resources :draft_orders, only: %i[create]
568
+ end
563
569
 
564
570
  namespace :intercity_taxi do
565
571
  resource :draft_orders, only: %i[create update]
@@ -0,0 +1,17 @@
1
+ class MigrateCmRoutesToCmRouteMetrics < ActiveRecord::Migration[7.0]
2
+ def change
3
+ # 1. Rename cm_routes which we use to track order, trip count to cm_route_metrics instead.
4
+ remove_index :cm_routes, [:origin_place_id, :destination_place_id] if index_exists?(:cm_routes, [:origin_place_id, :destination_place_id])
5
+ rename_table :cm_routes, :cm_route_metrics unless table_exists?(:cm_route_metrics)
6
+ add_column :cm_route_metrics, :route_type, :integer, default: 0, null: false unless column_exists?(:cm_route_metrics, :route_type)
7
+
8
+ # 2. Drop the old index before renaming table to avoid index name length issues & re-add the index with a shorter name
9
+ unless index_exists?(:cm_route_metrics, [:route_type, :origin_place_id, :destination_place_id], name: 'index_cm_route_metrics_on_origin_dest')
10
+ add_index :cm_route_metrics, [:route_type, :origin_place_id, :destination_place_id], unique: true, name: 'index_cm_route_metrics_on_origin_dest'
11
+ end
12
+
13
+ # 3. Remove old route_id column from trips.
14
+ # it has route_metrics value, and trip don't need to have association with metric directly.
15
+ remove_column :cm_trips, :route_id if column_exists?(:cm_trips, :route_id)
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ class MigrateCmVendorRoutesToCmRoutes < ActiveRecord::Migration[7.0]
2
+ def change
3
+ # 1. Remove cm_vendor_routes and add cm_routes instead for routes that associated with vendor.
4
+ drop_table :cm_vendor_routes, if_exists: true
5
+ create_table :cm_routes, if_not_exists: true do |t|
6
+ t.string :route_name
7
+ t.string :short_name
8
+ t.integer :order_count
9
+ t.integer :fulfilled_order_count
10
+ t.integer :route_type
11
+ t.jsonb :route_stops, default: "[]"
12
+ t.references :vendor, foreign_key: { to_table: :spree_vendors }, null: false
13
+ t.references :tenant, foreign_key: { to_table: :cm_tenants }, null: true
14
+
15
+ t.references :origin_place, foreign_key: { to_table: :cm_places }, null: false
16
+ t.references :destination_place, foreign_key: { to_table: :cm_places }, null: false
17
+
18
+ t.integer :lock_version, default: 0, null: false
19
+ t.timestamps
20
+ end
21
+
22
+ # 2. Add indexes for popular_routes association (vendor_id + order by fulfilled_order_count, order_count)
23
+ add_index :cm_routes, [:vendor_id, :route_type, :fulfilled_order_count, :order_count],
24
+ order: { fulfilled_order_count: :desc, order_count: :desc },
25
+ name: 'index_cm_routes_on_vendor_id_route_type_and_popular' unless index_exists?(:cm_routes, [:vendor_id, :route_type, :fulfilled_order_count, :order_count])
26
+
27
+ # 3. Add new route reference to cm_trips
28
+ add_reference :cm_trips, :route, foreign_key: { to_table: :cm_routes }, null: true unless column_exists?(:cm_trips, :route_id)
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ class MigrateCmTripStopsToSupportTripConnection < ActiveRecord::Migration[7.0]
2
+ def change
3
+ # 1. Replace stop_type with allow_boarding & allow_drop_off instead, it is more flexible.
4
+ # Useful for middle stops where both boarding & drop off can be allowed or both can be disallowed.
5
+ remove_column :cm_trip_stops, :stop_type if column_exists?(:cm_trip_stops, :stop_type)
6
+ add_column :cm_trip_stops, :allow_boarding, :boolean unless column_exists?(:cm_trip_stops, :allow_boarding)
7
+ add_column :cm_trip_stops, :allow_drop_off, :boolean unless column_exists?(:cm_trip_stops, :allow_drop_off)
8
+
9
+ # 2. Add references to link trip stops to trips for boarding, this will enable trip connections. Where current trip is main trip.
10
+ add_reference :cm_trip_stops, :board_to_trip, foreign_key: { to_table: :cm_trips }, null: true unless column_exists?(:cm_trip_stops, :board_to_trip_id)
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class AddAllowBookingToCmTrips < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :cm_trips, :allow_booking, :boolean, default: true, if_not_exists: true
4
+ end
5
+ end
@@ -1,10 +1,11 @@
1
1
  FactoryBot.define do
2
- factory :cm_route, class: 'SpreeCmCommissioner::Route' do
2
+ factory :cm_route, class: SpreeCmCommissioner::Route do
3
+ association :vendor, factory: :vendor
4
+ association :tenant, factory: :cm_tenant
5
+ association :origin_place, factory: :cm_place
6
+ association :destination_place, factory: :cm_place
7
+
3
8
  sequence(:route_name) { |n| "Route #{n}" }
4
- origin_place_id { create(:cm_place).id }
5
- destination_place_id { create(:cm_place).id }
6
- trip_count { 0 }
7
- order_count { 0 }
8
- fulfilled_order_count { 0 }
9
+ sequence(:short_name) { |n| "R#{n}" }
9
10
  end
10
11
  end
@@ -0,0 +1,12 @@
1
+ FactoryBot.define do
2
+ factory :cm_route_metric, class: SpreeCmCommissioner::RouteMetric do
3
+ association :origin_place, factory: :cm_place
4
+ association :destination_place, factory: :cm_place
5
+
6
+ route_name { "#{origin_place.name} - #{destination_place.name}" }
7
+ route_type { :bus }
8
+ trip_count { 0 }
9
+ order_count { 0 }
10
+ fulfilled_order_count { 0 }
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ FactoryBot.define do
2
+ factory :cm_route_photo, class: SpreeCmCommissioner::RoutePhoto do
3
+ attachment { Rack::Test::UploadedFile.new(SpreeMultiVendor::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg'), 'image/jpeg') }
4
+ end
5
+ end
@@ -11,7 +11,10 @@ FactoryBot.define do
11
11
  association :vehicle, factory: :cm_vehicle
12
12
  association :origin_place, factory: :cm_place
13
13
  association :destination_place, factory: :cm_place
14
- association :route, factory: :cm_route
14
+
15
+ # Don't create route by default to avoid uniqueness conflicts
16
+ # Tests that need these should create them explicitly
17
+ route { nil }
15
18
  end
16
19
 
17
20
  factory :cm_trip_with_seat_counts, parent: :cm_trip do
@@ -4,7 +4,9 @@ FactoryBot.define do
4
4
  association :stop_place, factory: :cm_place
5
5
  association :location_place, factory: :cm_place
6
6
 
7
- stop_type { :boarding }
7
+ allow_boarding { true }
8
+ allow_drop_off { true }
9
+
8
10
  stop_name { stop_place.name }
9
11
 
10
12
  arrival_time { Time.current }
@@ -1,5 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :cm_transit_vendor, parent: :vendor do
3
+ from_email { FFaker::Internet.email } # required only when tenant exist.
3
4
  sequence(:name) { |n| "#{FFaker::Company.name} #{n}#{Kernel.rand(9999)}" }
4
5
  state { :active }
5
6
  primary_product_type { :transit }
@@ -7,6 +8,7 @@ FactoryBot.define do
7
8
 
8
9
  factory :cm_vendor, parent: :vendor do
9
10
  sequence(:name) { |n| "#{FFaker::Company.name} #{n}#{Kernel.rand(9999)}" }
11
+ from_email { FFaker::Internet.email } # required only when tenant exist.
10
12
  state { :active }
11
13
  default_state_id { Spree::State.first&.id }
12
14
  primary_product_type { :ecommerce }
@@ -7,6 +7,17 @@ FactoryBot.define do
7
7
  position { FFaker::Number.number }
8
8
 
9
9
  place_type { :location }
10
+
11
+ transient do
12
+ place_name { nil }
13
+ end
14
+
15
+ before :create do |vendor_place, evaluator|
16
+ if evaluator.place_name.present?
17
+ vendor_place.place.name = evaluator.place_name
18
+ vendor_place.place.save!
19
+ end
20
+ end
10
21
  end
11
22
 
12
23
  factory :cm_vendor_place, class: SpreeCmCommissioner::VendorPlace do
@@ -30,5 +41,16 @@ FactoryBot.define do
30
41
  place_type { :location }
31
42
  location { nil }
32
43
  end
44
+
45
+ transient do
46
+ place_name { nil }
47
+ end
48
+
49
+ before :create do |vendor_place, evaluator|
50
+ if evaluator.place_name.present?
51
+ vendor_place.place.name = evaluator.place_name
52
+ vendor_place.place.save!
53
+ end
54
+ end
33
55
  end
34
56
  end