solidus_foudra 0.2.0 → 0.3.1
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/README.md +7 -4
- data/app/controllers/solidus_foudra/webhooks_controller.rb +52 -0
- data/app/jobs/solidus_foudra/application_job.rb +30 -0
- data/app/jobs/solidus_foudra/cancel_order_job.rb +14 -0
- data/app/jobs/solidus_foudra/full_sync_job.rb +13 -0
- data/app/jobs/solidus_foudra/listing_job.rb +14 -0
- data/app/jobs/solidus_foudra/send_order_job.rb +24 -0
- data/app/jobs/solidus_foudra/shipment_job.rb +14 -0
- data/app/jobs/solidus_foudra/stock_job.rb +13 -0
- data/app/models/solidus_foudra/webhook_event.rb +24 -0
- data/app/subscribers/solidus_foudra/order_subscriber.rb +27 -0
- data/config/locales/en.yml +1 -4
- data/config/routes.rb +8 -0
- data/db/migrate/20261124000001_create_solidus_foudra_webhook_events.rb +18 -0
- data/db/migrate/20261124000002_add_foudra_columns_to_spree_tables.rb +17 -0
- data/lib/generators/solidus_foudra/install/install_generator.rb +46 -0
- data/lib/generators/solidus_foudra/install/templates/initializer.rb +20 -0
- data/lib/solidus_foudra/client.rb +22 -3
- data/lib/solidus_foudra/customer_finder.rb +104 -0
- data/lib/solidus_foudra/engine.rb +5 -0
- data/lib/solidus_foudra/full_sync.rb +64 -0
- data/lib/solidus_foudra/order_sender.rb +80 -0
- data/lib/solidus_foudra/part_numbers.rb +28 -0
- data/lib/solidus_foudra/product_sync.rb +201 -0
- data/lib/solidus_foudra/shipment_sync.rb +126 -0
- data/lib/solidus_foudra/status.rb +71 -0
- data/lib/solidus_foudra/stock_sync.rb +65 -0
- data/lib/solidus_foudra/version.rb +1 -1
- data/lib/solidus_foudra.rb +8 -0
- data/lib/tasks/solidus_foudra.rake +25 -0
- metadata +25 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: da9f21463a1c51dcb19cdfe37eaa22a0118ace9e53532fd1c200e562b42c57c4
|
|
4
|
+
data.tar.gz: 0fa638bc44e6183fb49995f9d49e2f00f2030b683b42e751358ad5aac6fb209b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9409a301160ecc8d6c902d7899182a5935fd9d42ab76c2cdb894aba6ab1140f8a4ba738cf84fb26aa604637f01a028a27ac281edef3289ae57b10bed0e315732
|
|
7
|
+
data.tar.gz: db583a7c949abec9f8ffd56cfc55937cfe38fc4c74d5f7f36622d69e0b93a10ec0234ead031d06804bfb1fa10cf3cc02b1ec606315c4e291bb95c5c7f4d67fed
|
data/README.md
CHANGED
|
@@ -121,10 +121,13 @@ Check it all with `bin/rails solidus_foudra:status`.
|
|
|
121
121
|
Foudra cancels what hasn't shipped; an order that has all shipped is left
|
|
122
122
|
(409, logged).
|
|
123
123
|
|
|
124
|
-
Every write carries an `Idempotency-Key`
|
|
125
|
-
`solidus_foudra:
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
Every order and customer write carries an `Idempotency-Key`
|
|
125
|
+
(`solidus_foudra:order:<number>`, `solidus_foudra:cancel:<number>`,
|
|
126
|
+
`solidus_foudra:customer:<digest of the body>`,
|
|
127
|
+
`solidus_foudra:customer-ref:<customer>:<user>`: Foudra refuses one key with
|
|
128
|
+
two different bodies, so a customer's key changes with their details). The
|
|
129
|
+
listing write-back (`PUT /api/channels/{code}/listings/{part}`) takes none;
|
|
130
|
+
it is safe to repeat.
|
|
128
131
|
|
|
129
132
|
### Webhooks
|
|
130
133
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# POST /foudra/webhooks: checks X-Foudra-Signature over the raw body,
|
|
8
|
+
# records the event id (a repeat is answered 204 and dropped), queues a
|
|
9
|
+
# job with only the ids the event names, and answers 204. The job
|
|
10
|
+
# re-fetches everything from the API: an event is a nudge, never data.
|
|
11
|
+
class WebhooksController < ActionController::API
|
|
12
|
+
def create
|
|
13
|
+
body = request.raw_post
|
|
14
|
+
config = SolidusFoudra.config
|
|
15
|
+
unless WebhookSignature.valid?(header: request.headers["X-Foudra-Signature"], body:,
|
|
16
|
+
secret: config.webhook_secret, tolerance: config.webhook_tolerance)
|
|
17
|
+
SolidusFoudra.log(:warn, "webhook refused: bad or old signature")
|
|
18
|
+
return head :unauthorized
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
event = JSON.parse(body)
|
|
22
|
+
return head :bad_request unless event.is_a?(Hash) && event["id"].present?
|
|
23
|
+
|
|
24
|
+
type = event["type"].to_s
|
|
25
|
+
enqueue_jobs(type, event["data"] || {}) if WebhookEvent.record(event["id"], type)
|
|
26
|
+
head :no_content
|
|
27
|
+
rescue JSON::ParserError
|
|
28
|
+
head :bad_request
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def enqueue_jobs(type, data)
|
|
34
|
+
return unless SolidusFoudra.config.enabled?
|
|
35
|
+
|
|
36
|
+
case type
|
|
37
|
+
when "stock.changed"
|
|
38
|
+
id = data.dig("part", "id")
|
|
39
|
+
StockJob.perform_later([id]) if id
|
|
40
|
+
when "sales_order.created", "sales_order.cancelled"
|
|
41
|
+
ids = Array(data["lines"]).filter_map { |l| l.dig("part", "id") }.uniq
|
|
42
|
+
StockJob.perform_later(ids) if ids.any?
|
|
43
|
+
when "listing.changed"
|
|
44
|
+
id = data.dig("part", "id") || data.dig("listing", "part_id")
|
|
45
|
+
ListingJob.perform_later(id) if id && data.dig("channel", "code") == SolidusFoudra.config.channel
|
|
46
|
+
when "shipment.shipped", "shipment.updated"
|
|
47
|
+
id = data.dig("shipment", "id")
|
|
48
|
+
ShipmentJob.perform_later(id) if id
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Foudra busy or down (429, 5xx, network) after the client's own three
|
|
8
|
+
# attempts: retried with growing pauses (executions**4 seconds: 3s, 18s,
|
|
9
|
+
# 83s, … about two and a half hours in all). A refusal (401/402/403, 422,
|
|
10
|
+
# an unknown part number) is logged and dropped: retrying won't fix it.
|
|
11
|
+
class ApplicationJob < ActiveJob::Base
|
|
12
|
+
queue_as { SolidusFoudra.config.queue }
|
|
13
|
+
|
|
14
|
+
# Handlers declared later win, so the general refusal comes first and
|
|
15
|
+
# the retry for Unavailable (a kind of Client::Error) after it.
|
|
16
|
+
rescue_from Client::Error do |error|
|
|
17
|
+
SolidusFoudra.log(:error, "#{self.class.name} dropped: #{error.message}")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
retry_on Client::Unavailable, attempts: 9, wait: ->(executions) { (executions**4) + 2 } do |job, error|
|
|
21
|
+
SolidusFoudra.log(:error, "#{job.class.name} gave up: #{error.message}")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
discard_on ActiveJob::DeserializationError
|
|
25
|
+
|
|
26
|
+
around_perform do |_job, block|
|
|
27
|
+
block.call if SolidusFoudra.config.enabled?
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Cancels in Foudra what hasn't shipped of a cancelled order.
|
|
8
|
+
class CancelOrderJob < ApplicationJob
|
|
9
|
+
def perform(order_id)
|
|
10
|
+
order = Spree::Order.find_by(id: order_id)
|
|
11
|
+
OrderSender.new(order).cancel_order if order
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# The nightly pass (see FullSync), for a job scheduler.
|
|
8
|
+
class FullSyncJob < ApplicationJob
|
|
9
|
+
def perform
|
|
10
|
+
FullSync.new.call
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Re-fetches a part's listing on the channel and creates, updates or
|
|
8
|
+
# discontinues its product.
|
|
9
|
+
class ListingJob < ApplicationJob
|
|
10
|
+
def perform(part_id)
|
|
11
|
+
ProductSync.new.sync_part(part_id)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Sends a completed order to Foudra as a sales order. order_finalized is
|
|
8
|
+
# published inside the checkout's transaction, so this may run before
|
|
9
|
+
# the order is committed: it waits (retries) until it is.
|
|
10
|
+
class SendOrderJob < ApplicationJob
|
|
11
|
+
class NotYetComplete < StandardError; end
|
|
12
|
+
|
|
13
|
+
retry_on NotYetComplete, wait: 10.seconds, attempts: 10 do |job, _error|
|
|
14
|
+
SolidusFoudra.log(:warn, "order #{job.arguments.first}: never completed; not sent")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def perform(order_id)
|
|
18
|
+
order = Spree::Order.find_by(id: order_id)
|
|
19
|
+
raise NotYetComplete unless order&.completed_at && order.complete?
|
|
20
|
+
|
|
21
|
+
OrderSender.new(order).send_order
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Re-fetches a shipment and its sales order and ships the Solidus units
|
|
8
|
+
# (or corrects the tracking).
|
|
9
|
+
class ShipmentJob < ApplicationJob
|
|
10
|
+
def perform(shipment_id)
|
|
11
|
+
ShipmentSync.new.sync_id(shipment_id)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Re-fetches these parts and sets their stock.
|
|
8
|
+
class StockJob < ApplicationJob
|
|
9
|
+
def perform(part_ids)
|
|
10
|
+
StockSync.new.update_parts(part_ids)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# A Foudra webhook event already received: its id is unique, so recording
|
|
8
|
+
# a redelivered event fails and the event is dropped.
|
|
9
|
+
class WebhookEvent < ActiveRecord::Base
|
|
10
|
+
self.table_name = "solidus_foudra_webhook_events"
|
|
11
|
+
|
|
12
|
+
# true the first time an id is seen, false on a repeat.
|
|
13
|
+
def self.record(event_id, event_type)
|
|
14
|
+
create!(event_id: event_id.to_s, event_type: event_type.to_s, created_at: Time.current)
|
|
15
|
+
true
|
|
16
|
+
rescue ActiveRecord::RecordNotUnique
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.prune(older_than: 30.days)
|
|
21
|
+
where(created_at: ...older_than.ago).delete_all
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Spree::Bus events that send an order to Foudra or cancel it there. Only
|
|
8
|
+
# the order's id goes on the queue; the job reloads it.
|
|
9
|
+
class OrderSubscriber
|
|
10
|
+
include Omnes::Subscriber
|
|
11
|
+
|
|
12
|
+
handle :order_finalized, with: :send_order, id: :solidus_foudra_send_order
|
|
13
|
+
handle :order_canceled, with: :cancel_order, id: :solidus_foudra_cancel_order
|
|
14
|
+
|
|
15
|
+
def send_order(event)
|
|
16
|
+
return unless SolidusFoudra.config.enabled?
|
|
17
|
+
|
|
18
|
+
SendOrderJob.perform_later(event[:order].id)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def cancel_order(event)
|
|
22
|
+
return unless SolidusFoudra.config.enabled?
|
|
23
|
+
|
|
24
|
+
CancelOrderJob.perform_later(event[:order].id)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/config/locales/en.yml
CHANGED
data/config/routes.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
# One row per Foudra webhook event received, so a redelivery is dropped.
|
|
7
|
+
# The nightly pass prunes rows older than 30 days.
|
|
8
|
+
class CreateSolidusFoudraWebhookEvents < ActiveRecord::Migration[7.0]
|
|
9
|
+
def change
|
|
10
|
+
create_table :solidus_foudra_webhook_events do |t|
|
|
11
|
+
t.string :event_id, null: false
|
|
12
|
+
t.string :event_type
|
|
13
|
+
t.datetime :created_at, null: false
|
|
14
|
+
end
|
|
15
|
+
add_index :solidus_foudra_webhook_events, :event_id, unique: true
|
|
16
|
+
add_index :solidus_foudra_webhook_events, :created_at
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
# The part number that joins a variant to a Foudra part, and what an order
|
|
7
|
+
# became in Foudra.
|
|
8
|
+
class AddFoudraColumnsToSpreeTables < ActiveRecord::Migration[7.0]
|
|
9
|
+
def change
|
|
10
|
+
add_column :spree_variants, :foudra_part_number, :string
|
|
11
|
+
add_index :spree_variants, :foudra_part_number
|
|
12
|
+
|
|
13
|
+
add_column :spree_orders, :foudra_customer_id, :bigint
|
|
14
|
+
add_column :spree_orders, :foudra_sales_order_id, :bigint
|
|
15
|
+
add_index :spree_orders, :foudra_sales_order_id
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
module Generators
|
|
8
|
+
# bin/rails generate solidus_foudra:install
|
|
9
|
+
#
|
|
10
|
+
# Writes the initializer, mounts the engine at /foudra (the webhook is
|
|
11
|
+
# POST /foudra/webhooks) and copies the migrations.
|
|
12
|
+
class InstallGenerator < Rails::Generators::Base
|
|
13
|
+
class_option :auto_run_migrations, type: :boolean, default: false
|
|
14
|
+
source_root File.expand_path("templates", __dir__)
|
|
15
|
+
|
|
16
|
+
def self.exit_on_failure?
|
|
17
|
+
true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def copy_initializer
|
|
21
|
+
template "initializer.rb", "config/initializers/solidus_foudra.rb"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def mount_engine
|
|
25
|
+
routes = File.join(destination_root, "config/routes.rb")
|
|
26
|
+
return if File.exist?(routes) && File.read(routes).include?("SolidusFoudra::Engine")
|
|
27
|
+
|
|
28
|
+
route 'mount SolidusFoudra::Engine, at: "/foudra"'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def add_migrations
|
|
32
|
+
run "bin/rails railties:install:migrations FROM=solidus_foudra"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def run_migrations
|
|
36
|
+
run_migrations = options[:auto_run_migrations] ||
|
|
37
|
+
["", "y", "Y"].include?(ask("Would you like to run the migrations now? [Y/n]"))
|
|
38
|
+
if run_migrations
|
|
39
|
+
run "bin/rails db:migrate"
|
|
40
|
+
else
|
|
41
|
+
say "Skipping bin/rails db:migrate, don't forget to run it!"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Foudra MRP: see https://github.com/kalopa/solidus_foudra#configuration
|
|
4
|
+
# Nothing is sent or synced until base_url, api_key and channel are set.
|
|
5
|
+
SolidusFoudra.configure do |config|
|
|
6
|
+
config.base_url = ENV["FOUDRA_BASE_URL"] # https://<company>.foudra.com
|
|
7
|
+
config.api_key = ENV["FOUDRA_API_KEY"] ||
|
|
8
|
+
Rails.application.credentials.dig(:foudra, :api_key)
|
|
9
|
+
config.webhook_secret = ENV["FOUDRA_WEBHOOK_SECRET"] ||
|
|
10
|
+
Rails.application.credentials.dig(:foudra, :webhook_secret)
|
|
11
|
+
config.channel = ENV["FOUDRA_CHANNEL"] # the Foudra channel that is this shop, e.g. "SHOP"
|
|
12
|
+
|
|
13
|
+
# config.stock_location = nil # a stock location's name or id; nil = the default
|
|
14
|
+
# config.part_number_column = :foudra_part_number # the spree_variants column with the part number
|
|
15
|
+
# config.sync_content = false # true: Foudra's name/description/image always win
|
|
16
|
+
# config.send_orders_since = nil # nightly re-send of missed orders; set to go-live time
|
|
17
|
+
# config.wanted_after_days = 0 # sales order wanted date = order date + this
|
|
18
|
+
# config.queue = :default # ActiveJob queue
|
|
19
|
+
# config.markdown_renderer = nil # optional ->(markdown) { html }
|
|
20
|
+
end
|
|
@@ -81,10 +81,10 @@ module SolidusFoudra
|
|
|
81
81
|
get("/api/parts/#{Integer(id)}")["data"]
|
|
82
82
|
end
|
|
83
83
|
|
|
84
|
-
# A part by its exact part number
|
|
85
|
-
#
|
|
84
|
+
# A part by its exact part number (display or canonical form); nil
|
|
85
|
+
# when Foudra has no such part.
|
|
86
86
|
def find_part(part_number)
|
|
87
|
-
get("/api/parts",
|
|
87
|
+
get("/api/parts", part_number:, per: 10)["data"].first
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
def parts(&)
|
|
@@ -109,6 +109,12 @@ module SolidusFoudra
|
|
|
109
109
|
get("/api/channels/#{escape(channel)}/listings")["data"]
|
|
110
110
|
end
|
|
111
111
|
|
|
112
|
+
# The channel's listing of one part (listed or not); nil when the part
|
|
113
|
+
# has never been listed on it.
|
|
114
|
+
def listing(part_id, channel = config.channel)
|
|
115
|
+
get("/api/channels/#{escape(channel)}/listings", part_id: Integer(part_id))["data"].first
|
|
116
|
+
end
|
|
117
|
+
|
|
112
118
|
def put_listing(part_id, attrs, channel: config.channel)
|
|
113
119
|
put("/api/channels/#{escape(channel)}/listings/#{Integer(part_id)}", attrs)["data"]
|
|
114
120
|
end
|
|
@@ -117,10 +123,23 @@ module SolidusFoudra
|
|
|
117
123
|
get("/api/customers", email:, per: 10)["data"]
|
|
118
124
|
end
|
|
119
125
|
|
|
126
|
+
# The customer whose ref on the channel is ref (the shop's user id);
|
|
127
|
+
# nil when there is none.
|
|
128
|
+
def customer_by_channel_ref(ref, channel: config.channel)
|
|
129
|
+
get("/api/customers", channel:, channel_ref: ref.to_s, per: 10)["data"].first
|
|
130
|
+
end
|
|
131
|
+
|
|
120
132
|
def create_customer(attrs, idempotency_key:)
|
|
121
133
|
post("/api/customers", attrs, idempotency_key:)["data"]
|
|
122
134
|
end
|
|
123
135
|
|
|
136
|
+
# Adds the shop's ref to an existing customer (refs are added, never
|
|
137
|
+
# replaced; nothing else about the customer is changed).
|
|
138
|
+
def add_channel_ref(customer_id, ref, idempotency_key:, channel: config.channel)
|
|
139
|
+
put("/api/customers/#{Integer(customer_id)}", {channel_refs: [{channel:, ref: ref.to_s}]},
|
|
140
|
+
idempotency_key:)["data"]
|
|
141
|
+
end
|
|
142
|
+
|
|
124
143
|
def find_sales_order(channel_order_ref, channel: config.channel)
|
|
125
144
|
get("/api/sales-orders", channel:, channel_order_ref:, per: 10)["data"].first
|
|
126
145
|
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "digest"
|
|
7
|
+
|
|
8
|
+
module SolidusFoudra
|
|
9
|
+
# Finds the Foudra customer for an order, or creates one:
|
|
10
|
+
#
|
|
11
|
+
# 1. a signed-in shopper by their Spree user id, which Foudra keeps as the
|
|
12
|
+
# customer's ref on the channel;
|
|
13
|
+
# 2. anyone by the order's email, adding the user id to a customer found
|
|
14
|
+
# that way;
|
|
15
|
+
# 3. else a new customer with the bill-to name, phone, addresses and the
|
|
16
|
+
# user id as its channel ref.
|
|
17
|
+
#
|
|
18
|
+
# An existing customer's details are never changed.
|
|
19
|
+
class CustomerFinder
|
|
20
|
+
def initialize(order, client: Client.new)
|
|
21
|
+
@order = order
|
|
22
|
+
@client = client
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The Foudra customer's id.
|
|
26
|
+
def call
|
|
27
|
+
by_ref || by_email || create
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
attr_reader :order, :client
|
|
33
|
+
|
|
34
|
+
def user_ref
|
|
35
|
+
order.user_id&.to_s
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def by_ref
|
|
39
|
+
return unless user_ref
|
|
40
|
+
|
|
41
|
+
client.customer_by_channel_ref(user_ref)&.fetch("id")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def by_email
|
|
45
|
+
return if order.email.blank?
|
|
46
|
+
|
|
47
|
+
found = client.customers_by_email(order.email)
|
|
48
|
+
customer = found.find { |c| c["archived_at"].nil? } || found.first
|
|
49
|
+
return unless customer
|
|
50
|
+
|
|
51
|
+
if user_ref
|
|
52
|
+
client.add_channel_ref(customer["id"], user_ref,
|
|
53
|
+
idempotency_key: "solidus_foudra:customer-ref:#{customer["id"]}:#{user_ref}")
|
|
54
|
+
end
|
|
55
|
+
customer["id"]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def create
|
|
59
|
+
body = attributes
|
|
60
|
+
# Foudra refuses one key with two different bodies, so the key changes
|
|
61
|
+
# with the customer's details.
|
|
62
|
+
key = "solidus_foudra:customer:#{Digest::SHA256.hexdigest(JSON.generate(body))[0, 32]}"
|
|
63
|
+
client.create_customer(body, idempotency_key: key).fetch("id")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def attributes
|
|
67
|
+
bill = order.bill_address || order.ship_address
|
|
68
|
+
ship = order.ship_address
|
|
69
|
+
body = {
|
|
70
|
+
name: bill&.name.presence || order.email,
|
|
71
|
+
email: order.email.presence,
|
|
72
|
+
phone: bill&.phone.presence,
|
|
73
|
+
billing_address: AddressFormat.call(bill),
|
|
74
|
+
shipping_address: (AddressFormat.call(ship) if ship && bill && !same_place?(ship, bill))
|
|
75
|
+
}
|
|
76
|
+
body[:channel_refs] = [{channel: SolidusFoudra.config.channel, ref: user_ref}] if user_ref
|
|
77
|
+
body.compact
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def same_place?(one, other)
|
|
81
|
+
AddressFormat.call(one) == AddressFormat.call(other)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# A Spree::Address as Foudra writes one.
|
|
86
|
+
module AddressFormat
|
|
87
|
+
module_function
|
|
88
|
+
|
|
89
|
+
def call(address, with_name: false)
|
|
90
|
+
return unless address
|
|
91
|
+
|
|
92
|
+
out = {
|
|
93
|
+
line1: address.address1,
|
|
94
|
+
line2: address.address2.presence,
|
|
95
|
+
city: address.city,
|
|
96
|
+
region: address.state_text.presence,
|
|
97
|
+
postcode: address.zipcode.presence,
|
|
98
|
+
country: address.country&.iso
|
|
99
|
+
}
|
|
100
|
+
out = {name: address.name}.merge(out) if with_name
|
|
101
|
+
out.compact
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -16,6 +16,11 @@ module SolidusFoudra
|
|
|
16
16
|
|
|
17
17
|
engine_name "solidus_foudra"
|
|
18
18
|
|
|
19
|
+
# Before Solidus's to_prepare subscribes every class in the set.
|
|
20
|
+
initializer "solidus_foudra.subscribers" do
|
|
21
|
+
Spree::Config.environment.subscribers << "SolidusFoudra::OrderSubscriber"
|
|
22
|
+
end
|
|
23
|
+
|
|
19
24
|
config.generators do |g|
|
|
20
25
|
g.test_framework :rspec
|
|
21
26
|
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# The nightly pass: products, stock, shipments of sent orders not yet
|
|
8
|
+
# shipped here (last 60 days), orders since send_orders_since that never
|
|
9
|
+
# reached Foudra, and pruning de-duplication rows older than 30 days.
|
|
10
|
+
# Each step is independent: one failing doesn't stop the others.
|
|
11
|
+
class FullSync
|
|
12
|
+
STEPS = %i[products stock shipments resend prune].freeze
|
|
13
|
+
|
|
14
|
+
def initialize(client: Client.new)
|
|
15
|
+
@client = client
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# {step => :ok | error message}
|
|
19
|
+
def call(steps = STEPS)
|
|
20
|
+
steps.index_with do |step|
|
|
21
|
+
send(step)
|
|
22
|
+
:ok
|
|
23
|
+
rescue => e
|
|
24
|
+
SolidusFoudra.log(:error, "nightly #{step} failed: #{e.class}: #{e.message}")
|
|
25
|
+
e.message
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def products
|
|
30
|
+
ProductSync.new(client:).sync_all
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def stock
|
|
34
|
+
StockSync.new(client:).update_all
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def shipments
|
|
38
|
+
ShipmentSync.new(client:).sync_open_orders
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def resend
|
|
42
|
+
since = SolidusFoudra.config.send_orders_since
|
|
43
|
+
return unless since
|
|
44
|
+
|
|
45
|
+
orders = Spree::Order.complete.where(completed_at: since.., foudra_sales_order_id: nil)
|
|
46
|
+
.where.not(state: "canceled")
|
|
47
|
+
orders.find_each do |order|
|
|
48
|
+
OrderSender.new(order, client:).send_order
|
|
49
|
+
rescue Client::Unavailable
|
|
50
|
+
raise
|
|
51
|
+
rescue Client::Error => e
|
|
52
|
+
SolidusFoudra.log(:error, "order #{order.number}: not sent: #{e.message}")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def prune
|
|
57
|
+
WebhookEvent.prune
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
attr_reader :client
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Sends a completed Solidus order to Foudra as a sales order on the
|
|
8
|
+
# shop's channel, and cancels it there.
|
|
9
|
+
class OrderSender
|
|
10
|
+
def initialize(order, client: Client.new)
|
|
11
|
+
@order = order
|
|
12
|
+
@client = client
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# The Foudra sales order's id, or nil when the order has nothing Foudra
|
|
16
|
+
# makes (no line's variant has a part number).
|
|
17
|
+
def send_order
|
|
18
|
+
return order.foudra_sales_order_id if order.foudra_sales_order_id.present?
|
|
19
|
+
|
|
20
|
+
lines = sales_order_lines
|
|
21
|
+
if lines.empty?
|
|
22
|
+
SolidusFoudra.log(:info, "order #{order.number}: no line has a part number; not sent")
|
|
23
|
+
return
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
customer_id = order.foudra_customer_id.presence || CustomerFinder.new(order, client:).call
|
|
27
|
+
sales_order = client.create_sales_order(body(customer_id, lines),
|
|
28
|
+
idempotency_key: "solidus_foudra:order:#{order.number}")
|
|
29
|
+
order.update_columns(foudra_customer_id: customer_id, foudra_sales_order_id: sales_order["id"])
|
|
30
|
+
SolidusFoudra.log(:info, "order #{order.number}: Foudra sales order #{sales_order["number"]}")
|
|
31
|
+
sales_order["id"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Foudra cancels what hasn't shipped; an order that has all shipped is
|
|
35
|
+
# left (409, logged), as is one already cancelled there.
|
|
36
|
+
def cancel_order
|
|
37
|
+
id = order.foudra_sales_order_id.presence || client.find_sales_order(order.number)&.fetch("id")
|
|
38
|
+
unless id
|
|
39
|
+
SolidusFoudra.log(:info, "order #{order.number}: never reached Foudra; nothing to cancel")
|
|
40
|
+
return
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
client.cancel_sales_order(id, idempotency_key: "solidus_foudra:cancel:#{order.number}")
|
|
44
|
+
SolidusFoudra.log(:info, "order #{order.number}: cancelled in Foudra")
|
|
45
|
+
rescue Client::Conflict => e
|
|
46
|
+
SolidusFoudra.log(:warn, "order #{order.number}: not cancelled in Foudra: #{e.message}")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
attr_reader :order, :client
|
|
52
|
+
|
|
53
|
+
def body(customer_id, lines)
|
|
54
|
+
ordered = order.completed_at.to_date
|
|
55
|
+
{
|
|
56
|
+
channel: SolidusFoudra.config.channel,
|
|
57
|
+
channel_order_ref: order.number,
|
|
58
|
+
customer_id:,
|
|
59
|
+
customer_reference: order.number,
|
|
60
|
+
ordered_at: ordered.iso8601,
|
|
61
|
+
wanted_at: (ordered + SolidusFoudra.config.wanted_after_days.to_i).iso8601,
|
|
62
|
+
currency: order.currency,
|
|
63
|
+
ship_to: AddressFormat.call(order.ship_address, with_name: true),
|
|
64
|
+
lines:
|
|
65
|
+
}.compact
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# What the customer was charged per unit, before order-level promotions
|
|
69
|
+
# (line-level ones are in it). Information only: Foudra books no money.
|
|
70
|
+
def sales_order_lines
|
|
71
|
+
order.line_items.includes(:variant).filter_map do |item|
|
|
72
|
+
part_number = PartNumbers.of(item.variant)
|
|
73
|
+
next unless part_number
|
|
74
|
+
|
|
75
|
+
unit = (item.total_before_tax / item.quantity).round(4)
|
|
76
|
+
{part_number:, quantity: item.quantity.to_s, unit_price: unit.to_s("F")}
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# The variants a Foudra part number names, through the configured column.
|
|
8
|
+
module PartNumbers
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def column
|
|
12
|
+
SolidusFoudra.config.part_number_column
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def of(variant)
|
|
16
|
+
variant.public_send(column).presence
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Live (not deleted) variants with this part number.
|
|
20
|
+
def variants(part_number)
|
|
21
|
+
Spree::Variant.where(column => part_number.to_s)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def deleted_variants(part_number)
|
|
25
|
+
Spree::Variant.with_discarded.discarded.where(column => part_number.to_s)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "bigdecimal"
|
|
7
|
+
require "stringio"
|
|
8
|
+
|
|
9
|
+
module SolidusFoudra
|
|
10
|
+
# Makes the shop's products match the channel's listings:
|
|
11
|
+
#
|
|
12
|
+
# - listed, no variant with its number: a new product (master only);
|
|
13
|
+
# - listed, a variant has its number: adopted, its price set and any
|
|
14
|
+
# discontinuation cleared (name, description and image only with
|
|
15
|
+
# sync_content);
|
|
16
|
+
# - unlisted, never listed or the part archived: the product is
|
|
17
|
+
# discontinued, unless another of its variants is still listed.
|
|
18
|
+
#
|
|
19
|
+
# The shop's SKU is written back as the listing's channel_part_number.
|
|
20
|
+
# Nothing is ever deleted.
|
|
21
|
+
class ProductSync
|
|
22
|
+
def initialize(client: Client.new)
|
|
23
|
+
@client = client
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# One part, after listing.changed.
|
|
27
|
+
def sync_part(part_id)
|
|
28
|
+
listing = client.listing(part_id)
|
|
29
|
+
if listing
|
|
30
|
+
sync(listing)
|
|
31
|
+
else
|
|
32
|
+
# Never listed (or the listing removed): unlisted.
|
|
33
|
+
part = client.part(part_id)
|
|
34
|
+
unlist(part["part_number"].to_s, nil)
|
|
35
|
+
end
|
|
36
|
+
rescue Client::NotFound
|
|
37
|
+
SolidusFoudra.log(:warn, "part #{part_id}: not found in Foudra")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Every listing on the channel, then every variant whose part isn't on
|
|
41
|
+
# it at all.
|
|
42
|
+
def sync_all
|
|
43
|
+
listings = client.listings
|
|
44
|
+
@listed_numbers = listings.select { |l| listed?(l) }.map { |l| l["part_number"] }.to_set
|
|
45
|
+
listings.each do |listing|
|
|
46
|
+
sync(listing)
|
|
47
|
+
rescue Client::Unavailable
|
|
48
|
+
raise
|
|
49
|
+
rescue Client::Error, ActiveRecord::ActiveRecordError => e
|
|
50
|
+
SolidusFoudra.log(:error, "listing #{listing["part_number"]}: #{e.message}")
|
|
51
|
+
end
|
|
52
|
+
on_channel = listings.map { |l| l["part_number"] }.to_set
|
|
53
|
+
Spree::Variant.where.not(PartNumbers.column => [nil, ""]).find_each do |variant|
|
|
54
|
+
number = PartNumbers.of(variant)
|
|
55
|
+
unlist(number, nil) unless on_channel.include?(number)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def sync(listing)
|
|
60
|
+
number = listing["part_number"].to_s
|
|
61
|
+
return if number.empty?
|
|
62
|
+
|
|
63
|
+
if listed?(listing)
|
|
64
|
+
variants = PartNumbers.variants(number).to_a
|
|
65
|
+
variants.empty? ? create(listing) : variants.each { |v| adopt(v, listing) }
|
|
66
|
+
else
|
|
67
|
+
unlist(number, listing)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
attr_reader :client
|
|
74
|
+
|
|
75
|
+
def listed?(listing)
|
|
76
|
+
listing["is_listed"] && listing["part_is_active"] != false
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def shop_currency
|
|
80
|
+
@shop_currency ||= Spree::Store.default.try(:default_currency).presence || Spree::Config.currency
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def currency_of(listing)
|
|
84
|
+
listing["currency"].presence || shop_currency
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def price_of(listing)
|
|
88
|
+
BigDecimal(listing["price"].to_s) if listing["price"].present?
|
|
89
|
+
rescue ArgumentError
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def create(listing)
|
|
94
|
+
number = listing["part_number"]
|
|
95
|
+
price = price_of(listing)
|
|
96
|
+
if price.nil? || currency_of(listing) != shop_currency
|
|
97
|
+
return SolidusFoudra.log(:info, "#{number}: listed without a #{shop_currency} price; no product made")
|
|
98
|
+
end
|
|
99
|
+
if PartNumbers.deleted_variants(number).exists?
|
|
100
|
+
return SolidusFoudra.log(:info, "#{number}: a deleted variant had this number; no product made")
|
|
101
|
+
end
|
|
102
|
+
if Spree::Variant.with_discarded.exists?(sku: number)
|
|
103
|
+
return SolidusFoudra.log(:warn, "#{number}: another variant has this SKU; no product made")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
part = client.part(listing["part_id"])
|
|
107
|
+
product = Spree::Product.new(
|
|
108
|
+
name: part["name"].presence || listing["part_name"].presence || number,
|
|
109
|
+
description: Markdown.render(part["description"]),
|
|
110
|
+
price:,
|
|
111
|
+
sku: number,
|
|
112
|
+
available_on: Time.current,
|
|
113
|
+
shipping_category: shipping_category
|
|
114
|
+
)
|
|
115
|
+
product.master.public_send(:"#{PartNumbers.column}=", number)
|
|
116
|
+
product.save!
|
|
117
|
+
attach_image(product.master, part["id"])
|
|
118
|
+
write_back(listing, product.master)
|
|
119
|
+
SolidusFoudra.log(:info, "#{number}: product created")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def adopt(variant, listing)
|
|
123
|
+
set_price(variant, listing)
|
|
124
|
+
product = variant.product
|
|
125
|
+
product.update!(discontinue_on: nil) if product.discontinue_on.present?
|
|
126
|
+
if SolidusFoudra.config.sync_content && variant.is_master?
|
|
127
|
+
part = client.part(listing["part_id"])
|
|
128
|
+
product.update!(name: part["name"], description: Markdown.render(part["description"])) if part["name"].present?
|
|
129
|
+
attach_image(variant, part["id"]) if variant.images.empty?
|
|
130
|
+
end
|
|
131
|
+
write_back(listing, variant)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# In the shop's currency: the default price and its VAT prices. In
|
|
135
|
+
# another currency: a price in that currency.
|
|
136
|
+
def set_price(variant, listing)
|
|
137
|
+
amount = price_of(listing)
|
|
138
|
+
return unless amount
|
|
139
|
+
|
|
140
|
+
currency = currency_of(listing)
|
|
141
|
+
prices = variant.prices.where(currency:)
|
|
142
|
+
if currency == shop_currency && prices.exists?
|
|
143
|
+
prices.each { |p| p.update!(amount:) unless p.amount == amount }
|
|
144
|
+
else
|
|
145
|
+
price = prices.find_or_initialize_by(country_iso: nil)
|
|
146
|
+
price.update!(amount:) unless price.persisted? && price.amount == amount
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Solidus discontinues products, not variants.
|
|
151
|
+
def unlist(number, _listing)
|
|
152
|
+
return if number.blank?
|
|
153
|
+
|
|
154
|
+
PartNumbers.variants(number).includes(:product).find_each do |variant|
|
|
155
|
+
product = variant.product
|
|
156
|
+
next if product.discontinued?
|
|
157
|
+
|
|
158
|
+
others = product.variants_including_master.where.not(id: variant.id).filter_map { |v| PartNumbers.of(v) }
|
|
159
|
+
if others.any? { |n| listed_number?(n) }
|
|
160
|
+
SolidusFoudra.log(:info, "#{number}: unlisted, but #{product.slug} has another listed variant; left")
|
|
161
|
+
next
|
|
162
|
+
end
|
|
163
|
+
product.update!(discontinue_on: Time.current)
|
|
164
|
+
SolidusFoudra.log(:info, "#{number}: unlisted; #{product.slug} discontinued")
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def listed_number?(number)
|
|
169
|
+
@listed_numbers ||= client.listings.select { |l| listed?(l) }.map { |l| l["part_number"] }.to_set
|
|
170
|
+
@listed_numbers.include?(number)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def write_back(listing, variant)
|
|
174
|
+
sku = variant.sku.presence
|
|
175
|
+
return if sku.nil? || listing["channel_part_number"] == sku
|
|
176
|
+
|
|
177
|
+
client.put_listing(listing["part_id"], {channel_part_number: sku})
|
|
178
|
+
rescue Client::Conflict => e
|
|
179
|
+
SolidusFoudra.log(:warn, "#{listing["part_number"]}: SKU not written back: #{e.message}")
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# The part's own first image, never its category's.
|
|
183
|
+
def attach_image(variant, part_id)
|
|
184
|
+
image = client.images(part_id).find { |i| !i["from_category"] }
|
|
185
|
+
return unless image
|
|
186
|
+
|
|
187
|
+
bytes = client.download(image["url"])
|
|
188
|
+
Spree::Image.create!(viewable: variant, alt: variant.name,
|
|
189
|
+
attachment: {io: StringIO.new(bytes), filename: image["filename"].presence || "image",
|
|
190
|
+
content_type: image["content_type"]})
|
|
191
|
+
rescue Client::Unavailable
|
|
192
|
+
raise
|
|
193
|
+
rescue Client::Error, ActiveRecord::ActiveRecordError => e
|
|
194
|
+
SolidusFoudra.log(:warn, "part #{part_id}: image not added: #{e.message}")
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def shipping_category
|
|
198
|
+
Spree::ShippingCategory.order(:id).first || Spree::ShippingCategory.create!(name: "Default")
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "bigdecimal"
|
|
7
|
+
|
|
8
|
+
module SolidusFoudra
|
|
9
|
+
# A shipped Foudra shipment on this channel ships, in the Solidus order
|
|
10
|
+
# whose number is its sales order's channel_order_ref, as many
|
|
11
|
+
# not-yet-shipped units of each line's part as Foudra shipped: one carton
|
|
12
|
+
# per Solidus shipment, external_number = the Foudra shipment's number,
|
|
13
|
+
# tracking = its tracking number. Solidus then sends its shipped email.
|
|
14
|
+
# A repeat finds the cartons by external_number, ships only what they
|
|
15
|
+
# don't already hold and corrects the tracking (no second email).
|
|
16
|
+
class ShipmentSync
|
|
17
|
+
def initialize(client: Client.new)
|
|
18
|
+
@client = client
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def sync_id(shipment_id)
|
|
22
|
+
sync(client.shipment(shipment_id))
|
|
23
|
+
rescue Client::NotFound
|
|
24
|
+
SolidusFoudra.log(:warn, "shipment #{shipment_id}: not found in Foudra")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Shipments of orders sent to Foudra but not yet shipped here.
|
|
28
|
+
def sync_open_orders(since: 60.days.ago)
|
|
29
|
+
orders = Spree::Order.complete.where(completed_at: since..).where.not(foudra_sales_order_id: nil)
|
|
30
|
+
.where.not(shipment_state: "shipped").where.not(state: "canceled")
|
|
31
|
+
orders.find_each do |order|
|
|
32
|
+
client.shipments_for(order.foudra_sales_order_id).each { |s| sync(client.shipment(s["id"]), order:) }
|
|
33
|
+
rescue Client::Unavailable
|
|
34
|
+
raise
|
|
35
|
+
rescue Client::Error, ActiveRecord::ActiveRecordError => e
|
|
36
|
+
SolidusFoudra.log(:error, "order #{order.number}: shipments not synced: #{e.message}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def sync(shipment, order: nil)
|
|
41
|
+
return unless shipment["status"] == "shipped"
|
|
42
|
+
|
|
43
|
+
sales_order = client.sales_order(shipment["sales_order_id"])
|
|
44
|
+
return unless sales_order["channel"] == SolidusFoudra.config.channel
|
|
45
|
+
|
|
46
|
+
order ||= Spree::Order.find_by(number: sales_order["channel_order_ref"])
|
|
47
|
+
unless order
|
|
48
|
+
return SolidusFoudra.log(:warn, "shipment #{shipment["number"]}: no order #{sales_order["channel_order_ref"]}")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
Ship.new(order, shipment).call
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
attr_reader :client
|
|
57
|
+
|
|
58
|
+
class Ship
|
|
59
|
+
def initialize(order, shipment)
|
|
60
|
+
@order = order
|
|
61
|
+
@shipment = shipment
|
|
62
|
+
@number = shipment["number"].to_s
|
|
63
|
+
@tracking = shipment["tracking_number"].presence
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def call
|
|
67
|
+
correct_tracking
|
|
68
|
+
units = units_to_ship
|
|
69
|
+
if units.empty?
|
|
70
|
+
return SolidusFoudra.log(:info, "order #{order.number}: shipment #{number} already shipped here")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
shipped_at = Time.zone.parse(shipment["shipped_at"].to_s) || Time.current
|
|
74
|
+
units.group_by(&:shipment).each do |solidus_shipment, group|
|
|
75
|
+
Spree::OrderShipping.new(order).ship(
|
|
76
|
+
inventory_units: group,
|
|
77
|
+
stock_location: solidus_shipment.stock_location,
|
|
78
|
+
address: order.ship_address,
|
|
79
|
+
shipping_method: solidus_shipment.shipping_method,
|
|
80
|
+
shipped_at:,
|
|
81
|
+
external_number: number,
|
|
82
|
+
tracking_number: tracking
|
|
83
|
+
)
|
|
84
|
+
end
|
|
85
|
+
SolidusFoudra.log(:info, "order #{order.number}: shipped #{units.size} unit(s) as #{number}")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
attr_reader :order, :shipment, :number, :tracking
|
|
91
|
+
|
|
92
|
+
def cartons
|
|
93
|
+
@cartons ||= Spree::Carton.joins(:inventory_units)
|
|
94
|
+
.where(external_number: number, spree_inventory_units: {shipment_id: order.shipments.select(:id)}).distinct.to_a
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def correct_tracking
|
|
98
|
+
cartons.each do |carton|
|
|
99
|
+
next if carton.tracking == tracking
|
|
100
|
+
|
|
101
|
+
carton.update!(tracking:)
|
|
102
|
+
carton.shipments.each { |s| s.update!(tracking:) }
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Per part: what Foudra shipped, less what this shipment's cartons
|
|
107
|
+
# already hold, taken from the order's on-hand units. Backordered
|
|
108
|
+
# units wait for the stock update.
|
|
109
|
+
def units_to_ship
|
|
110
|
+
in_cartons = cartons.flat_map(&:inventory_units).group_by(&:variant_id).transform_values(&:size)
|
|
111
|
+
wanted = Hash.new(0)
|
|
112
|
+
Array(shipment["lines"]).each do |line|
|
|
113
|
+
wanted[line["part_number"].to_s] += BigDecimal(line["quantity"].to_s).floor
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
units = order.inventory_units.includes(:variant, :shipment).to_a
|
|
117
|
+
wanted.flat_map do |part_number, quantity|
|
|
118
|
+
mine = units.select { |u| PartNumbers.of(u.variant) == part_number }
|
|
119
|
+
already = mine.map(&:variant_id).uniq.sum { |id| in_cartons.fetch(id, 0) }
|
|
120
|
+
open = mine.select(&:on_hand?)
|
|
121
|
+
open.first([quantity - already, 0].max)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# What bin/rails solidus_foudra:status prints: the configuration (never
|
|
8
|
+
# the secrets) and whether Foudra agrees with it.
|
|
9
|
+
class Status
|
|
10
|
+
def initialize(config = SolidusFoudra.config, client: Client.new(config), out: $stdout)
|
|
11
|
+
@config = config
|
|
12
|
+
@client = client
|
|
13
|
+
@out = out
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# true when everything checked out.
|
|
17
|
+
def call
|
|
18
|
+
ok = true
|
|
19
|
+
line "base_url", config.base_url.presence || "(not set)"
|
|
20
|
+
line "channel", config.channel.presence || "(not set)"
|
|
21
|
+
line "api_key", config.api_key.present? ? "set" : "(not set)"
|
|
22
|
+
line "webhook_secret", config.webhook_secret.present? ? "set" : "(not set: webhooks will be refused)"
|
|
23
|
+
line "part_number_column", config.part_number_column
|
|
24
|
+
line "sync_content", config.sync_content.inspect
|
|
25
|
+
line "send_orders_since", config.send_orders_since.inspect
|
|
26
|
+
ok &= check("spree_variants.#{config.part_number_column}") do
|
|
27
|
+
raise "no such column" unless Spree::Variant.column_names.include?(config.part_number_column)
|
|
28
|
+
|
|
29
|
+
"#{Spree::Variant.where.not(config.part_number_column => [nil, ""]).count} variant(s) with a part number"
|
|
30
|
+
end
|
|
31
|
+
ok &= check("stock location") { config.stock_location_record.name }
|
|
32
|
+
return fail_with("not configured: base_url, api_key and channel are needed") unless config.enabled?
|
|
33
|
+
|
|
34
|
+
ok &= check("channel #{config.channel}") do
|
|
35
|
+
channel = client.channels.find { |c| c["code"] == config.channel }
|
|
36
|
+
raise "Foudra has no channel #{config.channel}" unless channel
|
|
37
|
+
raise "channel #{config.channel} is not active" if channel["is_active"] == false
|
|
38
|
+
|
|
39
|
+
channel["name"]
|
|
40
|
+
end
|
|
41
|
+
ok &= check("listings") do
|
|
42
|
+
listings = client.listings
|
|
43
|
+
listed = listings.select { |l| l["is_listed"] }
|
|
44
|
+
unpriced = listed.count { |l| l["price"].blank? }
|
|
45
|
+
"#{listed.size} listed of #{listings.size}, #{unpriced} without a price"
|
|
46
|
+
end
|
|
47
|
+
ok
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
attr_reader :config, :client, :out
|
|
53
|
+
|
|
54
|
+
def line(label, value)
|
|
55
|
+
out.puts format("%-20s %s", label, value)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def check(label)
|
|
59
|
+
line label, "ok: #{yield}"
|
|
60
|
+
true
|
|
61
|
+
rescue => e
|
|
62
|
+
line label, "FAILED: #{Redact.call(e.message)}"
|
|
63
|
+
false
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def fail_with(message)
|
|
67
|
+
out.puts message
|
|
68
|
+
false
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "bigdecimal"
|
|
7
|
+
|
|
8
|
+
module SolidusFoudra
|
|
9
|
+
# Sets each variant's count on hand, at the configured stock location, to
|
|
10
|
+
# Foudra's available quantity for its part: on hand less what open orders
|
|
11
|
+
# commit, rounded down, never below zero. Variants that don't track
|
|
12
|
+
# inventory are left alone.
|
|
13
|
+
class StockSync
|
|
14
|
+
def initialize(client: Client.new)
|
|
15
|
+
@client = client
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Re-fetches each part by id and sets its stock.
|
|
19
|
+
def update_parts(part_ids)
|
|
20
|
+
Array(part_ids).uniq.each do |id|
|
|
21
|
+
update(client.part(id))
|
|
22
|
+
rescue Client::NotFound
|
|
23
|
+
SolidusFoudra.log(:warn, "part #{id}: not found in Foudra")
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Every part, 100 a page. Returns how many variants changed.
|
|
28
|
+
def update_all
|
|
29
|
+
client.parts.sum { |part| update(part) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Sets the stock of the variants with this part's number. Returns how
|
|
33
|
+
# many changed.
|
|
34
|
+
def update(part)
|
|
35
|
+
number = part["part_number"].to_s
|
|
36
|
+
return 0 if number.empty?
|
|
37
|
+
|
|
38
|
+
count = self.class.available(part["available"])
|
|
39
|
+
PartNumbers.variants(number).sum do |variant|
|
|
40
|
+
next 0 unless variant.should_track_inventory?
|
|
41
|
+
|
|
42
|
+
item = location.stock_item_or_create(variant)
|
|
43
|
+
next 0 if item.count_on_hand == count
|
|
44
|
+
|
|
45
|
+
item.set_count_on_hand(count)
|
|
46
|
+
SolidusFoudra.log(:info, "#{number}: count on hand #{count}")
|
|
47
|
+
1
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.available(value)
|
|
52
|
+
[BigDecimal(value.presence || "0").floor, 0].max
|
|
53
|
+
rescue ArgumentError
|
|
54
|
+
0
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
attr_reader :client
|
|
60
|
+
|
|
61
|
+
def location
|
|
62
|
+
@location ||= SolidusFoudra.config.stock_location_record
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
data/lib/solidus_foudra.rb
CHANGED
|
@@ -14,6 +14,14 @@ require "solidus_foudra/redact"
|
|
|
14
14
|
require "solidus_foudra/client"
|
|
15
15
|
require "solidus_foudra/webhook_signature"
|
|
16
16
|
require "solidus_foudra/markdown"
|
|
17
|
+
require "solidus_foudra/part_numbers"
|
|
18
|
+
require "solidus_foudra/customer_finder"
|
|
19
|
+
require "solidus_foudra/order_sender"
|
|
20
|
+
require "solidus_foudra/stock_sync"
|
|
21
|
+
require "solidus_foudra/product_sync"
|
|
22
|
+
require "solidus_foudra/shipment_sync"
|
|
23
|
+
require "solidus_foudra/full_sync"
|
|
24
|
+
require "solidus_foudra/status"
|
|
17
25
|
require "solidus_foudra/engine"
|
|
18
26
|
|
|
19
27
|
module SolidusFoudra
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
namespace :solidus_foudra do
|
|
7
|
+
desc "Show the Foudra configuration (not the secrets) and check the channel and listings"
|
|
8
|
+
task status: :environment do
|
|
9
|
+
exit(1) unless SolidusFoudra::Status.new.call
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
desc "First import: products for every listing on the channel, then stock"
|
|
13
|
+
task import: :environment do
|
|
14
|
+
results = SolidusFoudra::FullSync.new.call(%i[products stock])
|
|
15
|
+
results.each { |step, result| puts "#{step}: #{result}" }
|
|
16
|
+
exit(1) unless results.values.all?(:ok)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
desc "The nightly pass: products, stock, shipments, missed orders, pruning"
|
|
20
|
+
task sync: :environment do
|
|
21
|
+
results = SolidusFoudra::FullSync.new.call
|
|
22
|
+
results.each { |step, result| puts "#{step}: #{result}" }
|
|
23
|
+
exit(1) unless results.values.all?(:ok)
|
|
24
|
+
end
|
|
25
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: solidus_foudra
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kalopa Robotics Limited
|
|
@@ -82,6 +82,16 @@ files:
|
|
|
82
82
|
- LICENSE
|
|
83
83
|
- README.md
|
|
84
84
|
- Rakefile
|
|
85
|
+
- app/controllers/solidus_foudra/webhooks_controller.rb
|
|
86
|
+
- app/jobs/solidus_foudra/application_job.rb
|
|
87
|
+
- app/jobs/solidus_foudra/cancel_order_job.rb
|
|
88
|
+
- app/jobs/solidus_foudra/full_sync_job.rb
|
|
89
|
+
- app/jobs/solidus_foudra/listing_job.rb
|
|
90
|
+
- app/jobs/solidus_foudra/send_order_job.rb
|
|
91
|
+
- app/jobs/solidus_foudra/shipment_job.rb
|
|
92
|
+
- app/jobs/solidus_foudra/stock_job.rb
|
|
93
|
+
- app/models/solidus_foudra/webhook_event.rb
|
|
94
|
+
- app/subscribers/solidus_foudra/order_subscriber.rb
|
|
85
95
|
- bin/console
|
|
86
96
|
- bin/rails
|
|
87
97
|
- bin/rails-engine
|
|
@@ -91,15 +101,29 @@ files:
|
|
|
91
101
|
- bin/sandbox
|
|
92
102
|
- bin/setup
|
|
93
103
|
- config/locales/en.yml
|
|
104
|
+
- config/routes.rb
|
|
105
|
+
- db/migrate/20261124000001_create_solidus_foudra_webhook_events.rb
|
|
106
|
+
- db/migrate/20261124000002_add_foudra_columns_to_spree_tables.rb
|
|
107
|
+
- lib/generators/solidus_foudra/install/install_generator.rb
|
|
108
|
+
- lib/generators/solidus_foudra/install/templates/initializer.rb
|
|
94
109
|
- lib/solidus_foudra.rb
|
|
95
110
|
- lib/solidus_foudra/client.rb
|
|
96
111
|
- lib/solidus_foudra/configuration.rb
|
|
112
|
+
- lib/solidus_foudra/customer_finder.rb
|
|
97
113
|
- lib/solidus_foudra/engine.rb
|
|
114
|
+
- lib/solidus_foudra/full_sync.rb
|
|
98
115
|
- lib/solidus_foudra/markdown.rb
|
|
116
|
+
- lib/solidus_foudra/order_sender.rb
|
|
117
|
+
- lib/solidus_foudra/part_numbers.rb
|
|
118
|
+
- lib/solidus_foudra/product_sync.rb
|
|
99
119
|
- lib/solidus_foudra/redact.rb
|
|
120
|
+
- lib/solidus_foudra/shipment_sync.rb
|
|
121
|
+
- lib/solidus_foudra/status.rb
|
|
122
|
+
- lib/solidus_foudra/stock_sync.rb
|
|
100
123
|
- lib/solidus_foudra/testing_support/factories.rb
|
|
101
124
|
- lib/solidus_foudra/version.rb
|
|
102
125
|
- lib/solidus_foudra/webhook_signature.rb
|
|
126
|
+
- lib/tasks/solidus_foudra.rake
|
|
103
127
|
- solidus_foudra.gemspec
|
|
104
128
|
homepage: https://foudra.com/developers
|
|
105
129
|
licenses:
|