dscf-marketplace 0.7.3 → 0.7.5
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/app/controllers/dscf/marketplace/supplier_products_controller.rb +53 -1
- data/app/models/dscf/marketplace/supplier_product.rb +1 -1
- data/app/policies/dscf/marketplace/supplier_product_policy.rb +7 -0
- data/app/serializers/dscf/marketplace/supplier_product_serializer.rb +2 -1
- data/app/services/dscf/marketplace/delivery_order_service.rb +12 -6
- data/app/services/dscf/marketplace/route_optimization_service.rb +49 -61
- data/config/locales/en.yml +4 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20260501000002_add_supplier_contact_fields_to_supplier_products.rb +7 -0
- data/lib/dscf/marketplace/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e66e6a4fac1c4edcfd47fee2305ae1f1003e0f5b67d1de4ed92437efab52c51d
|
|
4
|
+
data.tar.gz: 938e70e67ec267163bdb2414da889a90ecb5d46c4c72c016af6b26bac160a4cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 74555ff90a3951e9ba75d3fc451da200458a4b15c6eb4cd227f48dee569fe14e70174c292e7e7f1454200a6765431f3b3e66cd2862e4885dc474e0a1eddfc6eb
|
|
7
|
+
data.tar.gz: 999a3fa20ea0ee8db77b9d39c48efcffb9f5f9d19c73d1fd7555c7a9b0b9a77c9cf76ee363564e5acfabf1ed3cfbfe5b84f6bda1525fdb018903429495f3a21f
|
|
@@ -16,12 +16,64 @@ module Dscf
|
|
|
16
16
|
render_success("supplier_products.success.index", data: products, serializer_options: options)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
# Override create so supplier submissions default to inactive (require approval)
|
|
20
|
+
def create
|
|
21
|
+
authorize @clazz.new, :create?
|
|
22
|
+
|
|
23
|
+
obj = @clazz.new(model_params)
|
|
24
|
+
# Ensure newly created supplier products are inactive until approved by platform admin
|
|
25
|
+
obj.status = :inactive
|
|
26
|
+
|
|
27
|
+
ActiveRecord::Base.transaction do
|
|
28
|
+
if obj.save
|
|
29
|
+
@obj = obj
|
|
30
|
+
after_save_hook(obj) if respond_to?(:after_save_hook, true)
|
|
31
|
+
|
|
32
|
+
obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?
|
|
33
|
+
@obj = obj
|
|
34
|
+
|
|
35
|
+
includes = serializer_includes_for_action(:create)
|
|
36
|
+
options = {}
|
|
37
|
+
options[:include] = includes if includes.present?
|
|
38
|
+
|
|
39
|
+
render_success(data: obj, serializer_options: options, status: :created)
|
|
40
|
+
else
|
|
41
|
+
render_error(errors: obj.errors.full_messages[0], status: :unprocessable_entity)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Admin action to approve a supplier product (makes it active)
|
|
47
|
+
def approve
|
|
48
|
+
set_object
|
|
49
|
+
authorize @obj, :approve?
|
|
50
|
+
|
|
51
|
+
if @obj.update(status: :active)
|
|
52
|
+
render_success("supplier_products.success.approve", data: @obj)
|
|
53
|
+
else
|
|
54
|
+
render_error("supplier_products.errors.approve", errors: @obj.errors.full_messages, status: :unprocessable_entity)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Admin action to reject a supplier product (makes it inactive)
|
|
59
|
+
def reject
|
|
60
|
+
set_object
|
|
61
|
+
authorize @obj, :approve?
|
|
62
|
+
|
|
63
|
+
if @obj.update(status: :inactive)
|
|
64
|
+
render_success("supplier_products.success.reject", data: @obj)
|
|
65
|
+
else
|
|
66
|
+
render_error("supplier_products.errors.reject", errors: @obj.errors.full_messages, status: :unprocessable_entity)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
19
70
|
private
|
|
20
71
|
|
|
21
72
|
def model_params
|
|
22
73
|
params.require(:supplier_product).permit(
|
|
23
74
|
:business_id, :product_id, :supplier_price, :available_quantity,
|
|
24
|
-
:minimum_order_quantity, :status
|
|
75
|
+
:minimum_order_quantity, :status,
|
|
76
|
+
:supplier_name, :supplier_address, :supplier_contact_phone
|
|
25
77
|
)
|
|
26
78
|
end
|
|
27
79
|
|
|
@@ -20,7 +20,7 @@ module Dscf::Marketplace
|
|
|
20
20
|
scope :by_product, ->(product_id) { where(product_id: product_id) }
|
|
21
21
|
|
|
22
22
|
def self.ransackable_attributes(_auth_object = nil)
|
|
23
|
-
%w[id business_id product_id supplier_price available_quantity minimum_order_quantity status created_at updated_at]
|
|
23
|
+
%w[id business_id product_id supplier_price available_quantity minimum_order_quantity status created_at updated_at supplier_name supplier_address supplier_contact_phone]
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def self.ransackable_associations(_auth_object = nil)
|
|
@@ -4,6 +4,13 @@ module Dscf
|
|
|
4
4
|
def my_products?
|
|
5
5
|
user.has_permission?("supplier_products.my_products")
|
|
6
6
|
end
|
|
7
|
+
|
|
8
|
+
def approve?
|
|
9
|
+
return true if user.super_admin?
|
|
10
|
+
|
|
11
|
+
# Only allow users who belong to a system business to approve supplier products
|
|
12
|
+
user.businesses.where(is_system: true).exists?
|
|
13
|
+
end
|
|
7
14
|
end
|
|
8
15
|
end
|
|
9
16
|
end
|
|
@@ -4,7 +4,8 @@ module Dscf
|
|
|
4
4
|
attributes :id, :business_id, :product_id, :supplier_price, :available_quantity,
|
|
5
5
|
:minimum_order_quantity, :status, :created_at, :updated_at,
|
|
6
6
|
:in_stock?, :price_per_unit, :display_name, :name, :description,
|
|
7
|
-
:thumbnail_url, :images_urls
|
|
7
|
+
:thumbnail_url, :images_urls,
|
|
8
|
+
:supplier_name, :supplier_address, :supplier_contact_phone
|
|
8
9
|
|
|
9
10
|
belongs_to :business
|
|
10
11
|
belongs_to :product
|
|
@@ -9,7 +9,7 @@ module Dscf::Marketplace
|
|
|
9
9
|
def assign_driver(delivery_order, driver)
|
|
10
10
|
raise ArgumentError, "Delivery order is required" unless delivery_order
|
|
11
11
|
raise ArgumentError, "Driver is required" unless driver
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
validate_assignment(delivery_order)
|
|
14
14
|
|
|
15
15
|
delivery_order.driver_id = driver.id
|
|
@@ -35,9 +35,15 @@ module Dscf::Marketplace
|
|
|
35
35
|
delivery_order = create_delivery_order(vehicle_type, pickup_address, delivery_notes)
|
|
36
36
|
create_delivery_stops_and_items(delivery_order, orders, pickup_address)
|
|
37
37
|
associate_orders_with_delivery(delivery_order, orders)
|
|
38
|
-
|
|
39
|
-
#
|
|
40
|
-
|
|
38
|
+
|
|
39
|
+
# Best-effort route optimization using Gebeta Maps.
|
|
40
|
+
begin
|
|
41
|
+
RouteOptimizationService.new(delivery_order).optimize!
|
|
42
|
+
rescue StandardError => e
|
|
43
|
+
Rails.logger.warn(
|
|
44
|
+
"[DeliveryOrderService] Route optimization skipped for delivery_order=#{delivery_order.id}: #{e.class} - #{e.message}"
|
|
45
|
+
)
|
|
46
|
+
end
|
|
41
47
|
|
|
42
48
|
delivery_order
|
|
43
49
|
end
|
|
@@ -79,7 +85,7 @@ module Dscf::Marketplace
|
|
|
79
85
|
def create_delivery_stops_and_items(delivery_order, orders, pickup_address)
|
|
80
86
|
# Group orders by dropoff address to create stops
|
|
81
87
|
orders_by_address = orders.group_by(&:dropoff_address_id)
|
|
82
|
-
|
|
88
|
+
|
|
83
89
|
orders_by_address.each do |dropoff_address_id, address_orders|
|
|
84
90
|
# Create stop
|
|
85
91
|
stop = DeliveryStop.create!(
|
|
@@ -87,7 +93,7 @@ module Dscf::Marketplace
|
|
|
87
93
|
dropoff_address_id: dropoff_address_id,
|
|
88
94
|
status: :pending
|
|
89
95
|
)
|
|
90
|
-
|
|
96
|
+
|
|
91
97
|
# Create items for this stop
|
|
92
98
|
address_orders.each do |order|
|
|
93
99
|
order.order_items.reload.each do |order_item|
|
|
@@ -10,83 +10,71 @@ module Dscf
|
|
|
10
10
|
def optimize!
|
|
11
11
|
return unless delivery_order.pickup_address && delivery_order.delivery_stops.any?
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
stops
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
# ],
|
|
36
|
-
# "total_distance": 13.474, # In KM? Diagnostic showed 13.474 for small distance. Need to verify unit.
|
|
37
|
-
# "time_taken": 1033.263,
|
|
38
|
-
# "Direction": [[lat,lon], ...]
|
|
39
|
-
# }
|
|
40
|
-
|
|
41
|
-
# 'best_order' includes the start point at index 0 (usually without original_index or it's 0/1 based?)
|
|
42
|
-
# Based on diagnostic: "original_index": 2 for the 3rd point (index 2).
|
|
43
|
-
# So original_index is 0-based index from the input array.
|
|
44
|
-
|
|
45
|
-
best_order = response["best_order"]
|
|
46
|
-
|
|
47
|
-
# Filter out the pickup location (which should be the first one, or original_index == 0)
|
|
48
|
-
# We only want to re-sequence the STOPS.
|
|
49
|
-
# Stops in 'stops' array correspond to input indices 1..N.
|
|
50
|
-
|
|
51
|
-
ordered_stops_data = best_order.select { |node| node["original_index"].to_i > 0 }
|
|
52
|
-
|
|
13
|
+
stops = []
|
|
14
|
+
locations = []
|
|
15
|
+
|
|
16
|
+
stops = ordered_stops
|
|
17
|
+
locations = build_locations(stops)
|
|
18
|
+
|
|
19
|
+
apply_remote_optimization(stops, locations)
|
|
20
|
+
rescue StandardError => e
|
|
21
|
+
Rails.logger.warn(
|
|
22
|
+
"[RouteOptimizationService] Falling back to local route for delivery_order=#{delivery_order.id}: #{e.class} - #{e.message}"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
apply_local_fallback(stops, locations)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def apply_remote_optimization(stops, locations)
|
|
31
|
+
response = GebetaService.new.tsp(locations)
|
|
32
|
+
best_order = Array(response["best_order"])
|
|
33
|
+
ordered_stops_data = best_order.select { |node| node["original_index"].to_i.positive? }
|
|
34
|
+
|
|
53
35
|
ActiveRecord::Base.transaction do
|
|
54
36
|
ordered_stops_data.each_with_index do |node, seq_num|
|
|
55
|
-
original_idx = node["original_index"]
|
|
56
|
-
# stops array is 0-indexed, corresponding to input indices 1, 2, 3...
|
|
57
|
-
# If original_idx is 1, it means stops[0]. If 2, stops[1].
|
|
37
|
+
original_idx = node["original_index"].to_i
|
|
58
38
|
stop = stops[original_idx - 1]
|
|
59
|
-
|
|
60
|
-
if stop
|
|
61
|
-
stop.update!(sequence_number: seq_num + 1)
|
|
62
|
-
end
|
|
39
|
+
stop.update!(sequence_number: seq_num + 1) if stop
|
|
63
40
|
end
|
|
64
41
|
|
|
65
|
-
# Update DeliveryOrder metrics
|
|
66
|
-
# Check units:
|
|
67
|
-
# time_taken: 1033.263 (likely seconds ~ 17 mins for decent distance).
|
|
68
|
-
# total_distance: 13.474. If this is KM, it's reasonable. (Distance between 9.02,38.80 and 9.028,38.75 is ~ small).
|
|
69
|
-
# If meters, 13 meters is too small. 1000 seconds for 13 meters is wrong.
|
|
70
|
-
# So total_distance is likely KM.
|
|
71
|
-
|
|
72
42
|
distance_km = response["total_distance"].to_f
|
|
73
|
-
|
|
43
|
+
|
|
74
44
|
delivery_order.update!(
|
|
75
45
|
estimated_delivery_time: Time.current + response["time_taken"].to_f.seconds,
|
|
76
|
-
estimated_delivery_price: calculate_price(distance_km * 1000),
|
|
77
|
-
optimized_route: response["Direction"]
|
|
46
|
+
estimated_delivery_price: calculate_price(distance_km * 1000),
|
|
47
|
+
optimized_route: response["Direction"]
|
|
78
48
|
)
|
|
79
49
|
end
|
|
80
50
|
end
|
|
81
51
|
|
|
82
|
-
|
|
52
|
+
def apply_local_fallback(stops, locations)
|
|
53
|
+
ActiveRecord::Base.transaction do
|
|
54
|
+
stops.each_with_index do |stop, index|
|
|
55
|
+
stop.update!(sequence_number: index + 1)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
delivery_order.update!(optimized_route: locations)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def ordered_stops
|
|
63
|
+
delivery_order.delivery_stops.includes(:dropoff_address).order(:id).to_a
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def build_locations(stops)
|
|
67
|
+
pickup_coords = [ delivery_order.pickup_address.latitude.to_f, delivery_order.pickup_address.longitude.to_f ]
|
|
68
|
+
stop_coords = stops.map { |stop| [ stop.dropoff_address.latitude.to_f, stop.dropoff_address.longitude.to_f ] }
|
|
69
|
+
|
|
70
|
+
[ pickup_coords ] + stop_coords
|
|
71
|
+
end
|
|
83
72
|
|
|
84
|
-
# Placeholder pricing logic
|
|
85
73
|
def calculate_price(distance_meters)
|
|
86
74
|
base_rate = 50.0
|
|
87
|
-
km_rate = 10.0
|
|
75
|
+
km_rate = 10.0
|
|
88
76
|
distance_km = distance_meters / 1000.0
|
|
89
|
-
|
|
77
|
+
|
|
90
78
|
base_rate + (distance_km * km_rate)
|
|
91
79
|
end
|
|
92
80
|
end
|
data/config/locales/en.yml
CHANGED
|
@@ -77,12 +77,16 @@ en:
|
|
|
77
77
|
supplier_products:
|
|
78
78
|
success:
|
|
79
79
|
index: "Supplier products retrieved successfully"
|
|
80
|
+
approve: "Supplier product approved successfully"
|
|
81
|
+
reject: "Supplier product rejected successfully"
|
|
80
82
|
show: "Supplier product details retrieved successfully"
|
|
81
83
|
create: "Supplier product created successfully"
|
|
82
84
|
update: "Supplier product updated successfully"
|
|
83
85
|
destroy: "Supplier product deleted successfully"
|
|
84
86
|
errors:
|
|
85
87
|
index: "Failed to retrieve supplier products"
|
|
88
|
+
approve: "Failed to approve supplier product"
|
|
89
|
+
reject: "Failed to reject supplier product"
|
|
86
90
|
show: "Failed to retrieve supplier product details"
|
|
87
91
|
create: "Failed to create supplier product"
|
|
88
92
|
update: "Failed to update supplier product"
|
data/config/routes.rb
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
class AddSupplierContactFieldsToSupplierProducts < ActiveRecord::Migration[8.0]
|
|
2
|
+
def change
|
|
3
|
+
add_column :dscf_marketplace_supplier_products, :supplier_name, :string
|
|
4
|
+
add_column :dscf_marketplace_supplier_products, :supplier_address, :text
|
|
5
|
+
add_column :dscf_marketplace_supplier_products, :supplier_contact_phone, :string
|
|
6
|
+
end
|
|
7
|
+
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dscf-marketplace
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.7.
|
|
4
|
+
version: 0.7.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Asrat
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-05-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rails
|
|
@@ -532,6 +532,7 @@ files:
|
|
|
532
532
|
- db/migrate/20251130144500_add_delivery_stop_to_delivery_order_items.rb
|
|
533
533
|
- db/migrate/20260310120000_add_active_and_description_to_dscf_core_roles.rb
|
|
534
534
|
- db/migrate/20260310122000_add_missing_fields_to_dscf_core_user_roles.rb
|
|
535
|
+
- db/migrate/20260501000002_add_supplier_contact_fields_to_supplier_products.rb
|
|
535
536
|
- db/seeds.rb
|
|
536
537
|
- lib/dscf/marketplace.rb
|
|
537
538
|
- lib/dscf/marketplace/engine.rb
|