spree_cm_commissioner 2.5.1.pre.pre4 → 2.5.1.pre.pre5
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/services/spree_cm_commissioner/trips/create_single_leg.rb +19 -14
- data/app/services/spree_cm_commissioner/trips/variants/create.rb +6 -6
- data/lib/spree_cm_commissioner/transit/route_stop_collection.rb +28 -0
- data/lib/spree_cm_commissioner/transit/trip_form.rb +24 -0
- data/lib/spree_cm_commissioner/transit/trip_stop_form.rb +7 -1
- data/lib/spree_cm_commissioner/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c04c1030a47ee44d31fbec31880fae41a769bcf41e7b700d9b35cbdceb71d36d
|
|
4
|
+
data.tar.gz: 2d729e1df109b0e69ad397b3b80a8212162772caaf6bdaf19b25e06f55876ab5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1d176ff6f8ab61f5b1e57983f864da966b0e90200bf1abc8cc6f57f69b25fdf8aa2d6a9eabfb8a23da96075a05082491e9be97611ba034675f91faf7c42a32d9
|
|
7
|
+
data.tar.gz: 5e569ceb12a0d2e533957ca83679e3738720cdb48e64e25d584bb4bac3ef2f9ab9bfab3ae89d325a1e070e4dc53d1ff068843a99ed08e6867d4e45d851257092
|
data/Gemfile.lock
CHANGED
|
@@ -36,7 +36,10 @@ module SpreeCmCommissioner
|
|
|
36
36
|
def create_variant!(vendor, trip_form)
|
|
37
37
|
first_stop = trip_form.trip_stops.first
|
|
38
38
|
vehicle_type = SpreeCmCommissioner::VehicleType.find(first_stop.vehicle_type_id)
|
|
39
|
-
capacity = vehicle_type.number_of_seats
|
|
39
|
+
capacity = vehicle_type.number_of_seats.to_i
|
|
40
|
+
|
|
41
|
+
raise StandardError, 'Vehicle type must be selected' if vehicle_type.blank?
|
|
42
|
+
raise StandardError, 'Vehicle type seat capacity must be greater than 0' if capacity <= 0
|
|
40
43
|
|
|
41
44
|
result = SpreeCmCommissioner::Trips::Variants::Create.call(
|
|
42
45
|
vendor: vendor,
|
|
@@ -45,7 +48,7 @@ module SpreeCmCommissioner
|
|
|
45
48
|
capacity: capacity
|
|
46
49
|
)
|
|
47
50
|
|
|
48
|
-
raise result.error unless result.success?
|
|
51
|
+
raise StandardError, result.error unless result.success?
|
|
49
52
|
|
|
50
53
|
result.value[:variant]
|
|
51
54
|
end
|
|
@@ -57,14 +60,18 @@ module SpreeCmCommissioner
|
|
|
57
60
|
last_stop = trip_form.trip_stops.last
|
|
58
61
|
|
|
59
62
|
locations = vendor.locations.where(id: trip_form.trip_stops.map(&:location_id)).index_by(&:id)
|
|
60
|
-
|
|
63
|
+
|
|
64
|
+
# vendor_places are either stops or branches
|
|
65
|
+
vendor_places = SpreeCmCommissioner::VendorPlace
|
|
66
|
+
.where(id: trip_form.trip_stops.map(&:stop_id), vendor: vendor)
|
|
67
|
+
.index_by(&:id)
|
|
61
68
|
|
|
62
69
|
trip = vendor.trips.create!(
|
|
63
70
|
product: variant.product,
|
|
64
71
|
vehicle_type: SpreeCmCommissioner::VehicleType.find_by(id: first_stop.vehicle_type_id),
|
|
65
72
|
vehicle: SpreeCmCommissioner::Vehicle.find_by(id: first_stop.vehicle_id),
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
origin_place_id: locations[first_stop.location_id.to_i].place_id,
|
|
74
|
+
destination_place_id: locations[last_stop.location_id.to_i].place_id,
|
|
68
75
|
route: vendor.routes.find(trip_form.route_id),
|
|
69
76
|
departure_time: first_stop.departure_time,
|
|
70
77
|
duration: trip_form.total_duration_seconds,
|
|
@@ -73,23 +80,21 @@ module SpreeCmCommissioner
|
|
|
73
80
|
route_type: trip_form.route_type || vendor.routes.find(trip_form.route_id)&.route_type
|
|
74
81
|
)
|
|
75
82
|
|
|
76
|
-
create_trip_stops(trip, trip_form, locations,
|
|
83
|
+
create_trip_stops(trip, trip_form, locations, vendor_places)
|
|
77
84
|
trip
|
|
78
85
|
end
|
|
79
86
|
|
|
80
87
|
# Creates individual trip stops based on the trip form stops.
|
|
81
88
|
# Each stop includes arrival/departure times and boarding/drop-off permissions.
|
|
82
|
-
def create_trip_stops(trip, trip_form, locations,
|
|
89
|
+
def create_trip_stops(trip, trip_form, locations, vendor_places)
|
|
83
90
|
previous_departure = nil
|
|
84
|
-
|
|
85
|
-
trip_form.trip_stops.each do |ts|
|
|
91
|
+
Array(trip_form.trip_stops).each do |ts|
|
|
86
92
|
arrival_time = previous_departure || ts.departure_time
|
|
87
|
-
|
|
88
93
|
trip.trip_stops.create!(
|
|
89
|
-
stop_place:
|
|
90
|
-
location_place: locations[ts.location_id].place,
|
|
94
|
+
stop_place: vendor_places[ts.stop_id.to_i].place,
|
|
95
|
+
location_place: locations[ts.location_id.to_i].place,
|
|
91
96
|
allow_boarding: ts.allow_boarding?,
|
|
92
|
-
allow_drop_off: ts.allow_drop_off
|
|
97
|
+
allow_drop_off: ts.allow_drop_off?,
|
|
93
98
|
arrival_time: arrival_time,
|
|
94
99
|
departure_time: ts.departure_time
|
|
95
100
|
)
|
|
@@ -109,7 +114,7 @@ module SpreeCmCommissioner
|
|
|
109
114
|
exception_rules: calendar_form.exception_rules || []
|
|
110
115
|
)
|
|
111
116
|
|
|
112
|
-
raise res.error unless res.success?
|
|
117
|
+
raise StandardError, res.error unless res.success?
|
|
113
118
|
|
|
114
119
|
res.value[:calendar]
|
|
115
120
|
end
|
|
@@ -40,12 +40,12 @@ module SpreeCmCommissioner
|
|
|
40
40
|
# Creates a transit product with name based on stops, assigns vendor, type, and options.
|
|
41
41
|
# Includes seat-type option type and default store association.
|
|
42
42
|
def create_product!(vendor, trip_form)
|
|
43
|
-
vendor_stops =
|
|
44
|
-
first_stop = vendor_stops[trip_form.trip_stops.first.stop_id]
|
|
45
|
-
last_stop = vendor_stops[trip_form.trip_stops.last.stop_id]
|
|
43
|
+
vendor_stops = branches(vendor, trip_form)
|
|
44
|
+
first_stop = vendor_stops[trip_form.trip_stops.first.stop_id]
|
|
45
|
+
last_stop = vendor_stops[trip_form.trip_stops.last.stop_id]
|
|
46
46
|
|
|
47
47
|
Spree::Product.create!(
|
|
48
|
-
name: trip_form.name.presence || "#{first_stop.
|
|
48
|
+
name: trip_form.name.presence || "#{first_stop.id} - #{last_stop.id}",
|
|
49
49
|
short_name: trip_form.short_name,
|
|
50
50
|
vendor: vendor,
|
|
51
51
|
product_type: 'transit',
|
|
@@ -59,8 +59,8 @@ module SpreeCmCommissioner
|
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
# Retrieves and indexes stops from the vendor for the trip form's stop IDs.
|
|
62
|
-
def
|
|
63
|
-
vendor.
|
|
62
|
+
def branches(vendor, trip_form)
|
|
63
|
+
vendor.branches.where(id: trip_form.trip_stops.map(&:stop_id)).index_by(&:id)
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
# Sets up or finds the seat-type option type and creates normal option value.
|
|
@@ -97,6 +97,34 @@ module SpreeCmCommissioner::Transit
|
|
|
97
97
|
grouped
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
+
def by_location_id_with_data
|
|
101
|
+
return {} if @stops.empty?
|
|
102
|
+
|
|
103
|
+
# Extract unique vendor_place_ids from all stops
|
|
104
|
+
vendor_place_ids = @stops.map(&:vendor_place_id).compact.uniq
|
|
105
|
+
return {} if vendor_place_ids.empty?
|
|
106
|
+
|
|
107
|
+
# Single query: fetch all vendor places with their locations
|
|
108
|
+
# index_by(:id) creates a hash for O(1) lookup: { vendor_place_id => vendor_place_obj }
|
|
109
|
+
vendor_places = SpreeCmCommissioner::VendorPlace.where(id: vendor_place_ids).includes(:location).index_by(&:id)
|
|
110
|
+
|
|
111
|
+
grouped = {}
|
|
112
|
+
@stops.each do |stop|
|
|
113
|
+
# Set the vendor_place object on the stop for later use
|
|
114
|
+
stop.vendor_place = vendor_places[stop.vendor_place_id]
|
|
115
|
+
|
|
116
|
+
# Get the location from the vendor_place (e.g., "Bangkok" location)
|
|
117
|
+
location = stop.vendor_place&.location
|
|
118
|
+
next if location.nil?
|
|
119
|
+
|
|
120
|
+
# Group stops by location id
|
|
121
|
+
grouped[location.id] ||= []
|
|
122
|
+
grouped[location.id] << stop
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
grouped
|
|
126
|
+
end
|
|
127
|
+
|
|
100
128
|
# Groups route stops by their location in sequence order
|
|
101
129
|
# Returns an array of hashes where each hash contains:
|
|
102
130
|
# - location: the VendorPlace representing the location
|
|
@@ -12,6 +12,30 @@ module SpreeCmCommissioner::Transit
|
|
|
12
12
|
:trip_stops, # TripStopForm[]
|
|
13
13
|
:service_calendar # monday, tuesday, wednesday, thursday, friday, saturday, sunday, start_date, end_date
|
|
14
14
|
|
|
15
|
+
# Convert params hashes into TripStopForm objects
|
|
16
|
+
def trip_stops_attributes=(arr)
|
|
17
|
+
items = arr.is_a?(Hash) ? arr.values : Array(arr)
|
|
18
|
+
@trip_stops = items.map do |h|
|
|
19
|
+
h = h.to_h.symbolize_keys
|
|
20
|
+
TripStopForm.new.tap do |ts|
|
|
21
|
+
ts.location_id = h[:location_id]
|
|
22
|
+
ts.stop_id = h[:stop_id]
|
|
23
|
+
ts.departure_time = h[:departure_time]
|
|
24
|
+
ts.duration_in_hours = h[:duration_in_hours]
|
|
25
|
+
ts.route_type = h[:route_type]
|
|
26
|
+
ts.vehicle_type_id = h[:vehicle_type_id]
|
|
27
|
+
ts.vehicle_id = h[:vehicle_id]
|
|
28
|
+
ts.board_to_trip_id = h[:board_to_trip_id]
|
|
29
|
+
ts.allow_boarding = h[:allow_boarding]
|
|
30
|
+
ts.allow_drop_off = h[:allow_drop_off]
|
|
31
|
+
ts.allow_seat_selection = h[:allow_seat_selection]
|
|
32
|
+
ts.allow_booking = h[:allow_booking]
|
|
33
|
+
ts.sequence = h[:sequence]
|
|
34
|
+
ts.vendor_id = h[:vendor_id]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
15
39
|
def multi_leg?
|
|
16
40
|
trip_stops.map(&:location_id).uniq.size > 2
|
|
17
41
|
end
|
|
@@ -46,12 +46,18 @@ module SpreeCmCommissioner::Transit
|
|
|
46
46
|
:sequence,
|
|
47
47
|
#
|
|
48
48
|
# set from create service
|
|
49
|
-
:vendor_id
|
|
49
|
+
:vendor_id,
|
|
50
|
+
# branch or stop
|
|
51
|
+
:stop_type
|
|
50
52
|
|
|
51
53
|
def allow_boarding?
|
|
52
54
|
ActiveModel::Type::Boolean.new.cast(allow_boarding)
|
|
53
55
|
end
|
|
54
56
|
|
|
57
|
+
def allow_drop_off?
|
|
58
|
+
ActiveModel::Type::Boolean.new.cast(allow_drop_off)
|
|
59
|
+
end
|
|
60
|
+
|
|
55
61
|
def duration_in_minutes
|
|
56
62
|
return nil if duration_in_hours.blank?
|
|
57
63
|
|