spree_cm_commissioner 2.16.0 → 2.16.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/Gemfile.lock +1 -1
- data/app/controllers/spree/admin/inventory_holds_controller.rb +10 -3
- data/app/controllers/spree/admin/inventory_monitorings_controller.rb +1 -1
- data/app/controllers/spree/api/v2/storefront/user_device_token_registrations_controller.rb +1 -0
- data/app/controllers/spree/api/v2/tenant/user_device_tokens_controller.rb +1 -0
- data/app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb +2 -0
- data/app/factory/spree_cm_commissioner/order_telegram_message_factory.rb +15 -1
- data/app/helpers/spree_cm_commissioner/admin/transit_line_items_helper.rb +89 -0
- data/app/interactors/spree_cm_commissioner/account_linkage.rb +2 -1
- data/app/interactors/spree_cm_commissioner/firebase_email_fetcher.rb +34 -11
- data/app/interactors/spree_cm_commissioner/firebase_email_fetcher_cron_executor.rb +42 -12
- data/app/interactors/spree_cm_commissioner/firebase_id_token_provider.rb +13 -14
- data/app/interactors/spree_cm_commissioner/user_device_token_register.rb +2 -1
- data/app/interactors/spree_cm_commissioner/user_id_token_authenticator.rb +29 -11
- data/app/interactors/spree_cm_commissioner/user_id_token_checker.rb +7 -5
- data/app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb +10 -9
- data/app/jobs/spree_cm_commissioner/firebase_email_fetcher_job.rb +3 -2
- data/app/models/concerns/spree_cm_commissioner/line_item_integration.rb +13 -0
- data/app/models/concerns/spree_cm_commissioner/route_type.rb +1 -1
- data/app/models/concerns/spree_cm_commissioner/service_type.rb +1 -1
- data/app/models/spree_cm_commissioner/integration.rb +7 -0
- data/app/models/spree_cm_commissioner/integrations/book_me_bus_v1.rb +8 -0
- data/app/models/spree_cm_commissioner/integrations/larryta.rb +7 -0
- data/app/models/spree_cm_commissioner/taxon_decorator.rb +1 -0
- data/app/models/spree_cm_commissioner/taxonomy_decorator.rb +11 -0
- data/app/models/spree_cm_commissioner/tenant.rb +52 -0
- data/app/models/spree_cm_commissioner/vendor_decorator.rb +6 -0
- data/app/notifications/delivery_methods/firebase_project_fcm.rb +55 -0
- data/app/notifications/noticed_fcm_base.rb +6 -1
- data/app/overrides/spree/admin/orders/_line_items/_line_items.html.erb.deface +21 -1
- data/app/queries/spree_cm_commissioner/inventory_hold_insights_query.rb +29 -11
- data/app/queries/spree_cm_commissioner/inventory_hold_overview_query.rb +6 -6
- data/app/request_schemas/spree_cm_commissioner/trip_search_request_schema.rb +8 -0
- data/app/serializers/spree/v2/storefront/user_device_token_serializer.rb +1 -1
- data/app/serializers/spree/v2/tenant/user_device_token_serializer.rb +1 -1
- data/app/services/spree_cm_commissioner/device_tokens/prune_other_projects.rb +24 -0
- data/app/services/spree_cm_commissioner/firebase_emails/resolve.rb +40 -0
- data/app/services/spree_cm_commissioner/firebase_projects.rb +39 -0
- data/app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb +11 -3
- data/app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_trip_stop.rb +57 -18
- data/app/services/spree_cm_commissioner/inventory_items/reset.rb +13 -0
- data/app/services/spree_cm_commissioner/rates_availabilities/bulk_update.rb +11 -3
- data/app/services/spree_cm_commissioner/trips/update_single_leg.rb +2 -1
- data/app/services/spree_cm_commissioner/waiting_room/sessions/issue.rb +18 -4
- data/app/views/spree/admin/inventory_holds/_overview.html.erb +4 -1
- data/app/views/spree/admin/inventory_holds/_search.html.erb +10 -0
- data/app/views/spree/admin/orders/_transit_line_item.html.erb +103 -0
- data/app/views/spree/admin/rates_availabilities/_script.html.erb +6 -2
- data/app/views/spree/admin/tenants/_form.html.erb +21 -0
- data/app/views/spree/admin/tenants/form/_firebase.html.erb +32 -0
- data/config/initializers/user_manager_decorator.rb +3 -1
- data/config/locales/en.yml +5 -0
- data/config/locales/km.yml +5 -0
- data/db/migrate/20261119000001_add_firebase_project_to_cm_tenants.rb +12 -0
- data/db/migrate/20261119000002_add_firebase_project_id_to_cm_device_tokens.rb +6 -0
- data/db/migrate/20261120000001_add_internal_index_to_cm_integration_mappings.rb +10 -0
- data/lib/spree_cm_commissioner/test_helper/factories/user_factory.rb +3 -1
- data/lib/spree_cm_commissioner/test_helper/firebase_id_token_helper.rb +53 -0
- data/lib/spree_cm_commissioner/version.rb +1 -1
- data/lib/tasks/fetch_email.rake +4 -3
- data/lib/tasks/prune_device_tokens.rake +10 -0
- metadata +14 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d29d6b80065878b170a4ca7ab8e957f2dc6d5747c4557b2c654d2327447bb34e
|
|
4
|
+
data.tar.gz: 1c0457a12209666df41598b8425601da196f83845dd92ab5d4f9a7922ba2a1c5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 40e2177f733abd6bcb6e809fe5d2256cfbb7378ce064b1a36fe6de1c95c76caa927eccd210a752985beeed19feb3586e7a8e73d22d678e65b68de53f2cd47233
|
|
7
|
+
data.tar.gz: 1e1a52a3e10dc6ddd1c4c768c184d7329ebbb1c436161ce5b0b0826436c268cb079234f2ae548e7def638cdf73fa0c078496e53406cfe90c3944c1fece050008
|
data/Gemfile.lock
CHANGED
|
@@ -21,13 +21,15 @@ module Spree
|
|
|
21
21
|
.reorder(nil).group(:status).count
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
# Same filters as the table
|
|
24
|
+
# Same filters as the table. Unfiltered, it covers only the last 24h so the GROUP BYs stay cheap;
|
|
25
|
+
# a narrowing filter (event, vendor, order, ...) already bounds the rows, so it covers all time.
|
|
25
26
|
def overview
|
|
26
27
|
authorize! :manage, SpreeCmCommissioner::InventoryHold
|
|
27
28
|
|
|
28
29
|
q = search_query
|
|
29
|
-
@overview_from = q['created_at_gt'].presence
|
|
30
|
-
|
|
30
|
+
@overview_from = q['created_at_gt'].presence
|
|
31
|
+
@overview_from ||= 24.hours.ago unless narrowing_filter?(q)
|
|
32
|
+
q['created_at_gt'] = @overview_from if @overview_from
|
|
31
33
|
@overview = SpreeCmCommissioner::InventoryHoldOverviewQuery.new(holds: scope.ransack(q).result).call
|
|
32
34
|
|
|
33
35
|
render partial: 'overview', layout: false
|
|
@@ -94,6 +96,11 @@ module Spree
|
|
|
94
96
|
end
|
|
95
97
|
end
|
|
96
98
|
|
|
99
|
+
# Status, release reason and an end date alone still match most holds platform-wide.
|
|
100
|
+
def narrowing_filter?(query)
|
|
101
|
+
query.except('s', 'status_eq', 'release_reason_eq', 'created_at_gt', 'created_at_lt').values.any?(&:present?)
|
|
102
|
+
end
|
|
103
|
+
|
|
97
104
|
def back_params
|
|
98
105
|
raw_params = params[:back_params].presence || params
|
|
99
106
|
raw_params = raw_params.to_unsafe_h if raw_params.respond_to?(:to_unsafe_h)
|
|
@@ -30,7 +30,7 @@ module Spree
|
|
|
30
30
|
if result.success?
|
|
31
31
|
flash[:success] = "Successfully reset inventory for #{inventory_item.variant.product.name}"
|
|
32
32
|
else
|
|
33
|
-
flash[:error] = "Failed to reset inventory: #{result.
|
|
33
|
+
flash[:error] = "Failed to reset inventory: #{result.error}"
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
redirect_to action: :index,
|
|
@@ -2,6 +2,8 @@ module SpreeCmCommissioner
|
|
|
2
2
|
module Admin
|
|
3
3
|
module OrdersControllerDecorator
|
|
4
4
|
def self.prepended(base)
|
|
5
|
+
base.helper SpreeCmCommissioner::Admin::TransitLineItemsHelper
|
|
6
|
+
|
|
5
7
|
# spree try to create an empty order in the #new action
|
|
6
8
|
base.around_action :set_writing_role, only: %i[new edit]
|
|
7
9
|
|
|
@@ -50,7 +50,9 @@ module SpreeCmCommissioner
|
|
|
50
50
|
text << "Order Number:\n#{inline_code(order.number)}\n"
|
|
51
51
|
summary = summary_content
|
|
52
52
|
text << "#{summary}\n" if summary.present?
|
|
53
|
-
|
|
53
|
+
line_items = selected_line_items.to_a
|
|
54
|
+
preload_external_refs(line_items)
|
|
55
|
+
text << line_items.map { |item| line_item_content(item) }.compact.join("\n\n")
|
|
54
56
|
text.compact.join("\n")
|
|
55
57
|
end
|
|
56
58
|
|
|
@@ -193,10 +195,22 @@ module SpreeCmCommissioner
|
|
|
193
195
|
text << "Passengers: #{line_item.number_of_guests}"
|
|
194
196
|
text << "Departure: #{departure_text}" if departure_text.present?
|
|
195
197
|
text << seat_labels_text(line_item) if seat_labels_text(line_item).present?
|
|
198
|
+
external_ref = line_item.external_ref
|
|
199
|
+
text << "External Ref: #{inline_code(ERB::Util.html_escape(external_ref))}" if external_ref.present?
|
|
196
200
|
|
|
197
201
|
text.compact.join("\n")
|
|
198
202
|
end
|
|
199
203
|
|
|
204
|
+
# The partner's booking reference is read from each line item's mappings and their integration. Loaded here for
|
|
205
|
+
# all the message's integration line items at once, so it is two queries however many there are, and none for
|
|
206
|
+
# an order without an integration booking (`integration?` is a cached flag, so that check costs no query).
|
|
207
|
+
def preload_external_refs(line_items)
|
|
208
|
+
integration_items = line_items.select { |item| item.transit? && item.integration? }
|
|
209
|
+
return if integration_items.empty?
|
|
210
|
+
|
|
211
|
+
ActiveRecord::Associations::Preloader.new(records: integration_items, associations: { integration_mappings: :integration }).call
|
|
212
|
+
end
|
|
213
|
+
|
|
200
214
|
def transit_route_text(line_item)
|
|
201
215
|
trip = trip_for(line_item)
|
|
202
216
|
return line_item.product.name if trip.blank?
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module SpreeCmCommissioner
|
|
2
|
+
module Admin
|
|
3
|
+
# Trip cards for transit line items on the order's cart page (spree/admin/orders/_transit_line_item).
|
|
4
|
+
module TransitLineItemsHelper
|
|
5
|
+
DIRECTION_BADGES = {
|
|
6
|
+
'outbound' => %w[badge-primary Outbound],
|
|
7
|
+
'inbound' => %w[badge-info Return]
|
|
8
|
+
}.freeze
|
|
9
|
+
|
|
10
|
+
# The order's line items as [transit, other]. Guests, seats and partner bookings only show on the trip cards
|
|
11
|
+
# (plus the partner ref column for other integration rows), so they are loaded for those alone: an event order
|
|
12
|
+
# can carry hundreds of guests the table never displays.
|
|
13
|
+
def split_order_line_items(order)
|
|
14
|
+
line_items = order.line_items.includes(variant: :product).to_a
|
|
15
|
+
transit_items, other_items = line_items.partition { |item| item.trip_id.to_i.positive? }
|
|
16
|
+
|
|
17
|
+
preload_line_item_records(transit_items, [{ guests: %i[block nationality] }, { integration_mappings: :integration }])
|
|
18
|
+
preload_line_item_records(other_items.select(&:integration?), { integration_mappings: :integration })
|
|
19
|
+
|
|
20
|
+
[transit_items, other_items]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A line item keeps only trip, stop and vendor ids, so load every card's records once. Vendors are
|
|
24
|
+
# looked up here because LineItem#vendor (spree_multi_vendor) runs its own find and ignores preloading.
|
|
25
|
+
def transit_line_item_lookups(line_items)
|
|
26
|
+
stop_ids = line_items.flat_map { |item| [item.boarding_trip_stop_id, item.drop_off_trip_stop_id] }.compact.uniq
|
|
27
|
+
|
|
28
|
+
{
|
|
29
|
+
trips: SpreeCmCommissioner::Trip.includes(:vehicle_type).where(id: line_items.map(&:trip_id).compact.uniq).index_by(&:id),
|
|
30
|
+
trip_stops: SpreeCmCommissioner::TripStop.includes(:stop_place, :location_place).where(id: stop_ids).index_by(&:id),
|
|
31
|
+
vendors: Spree::Vendor.unscoped.where(id: line_items.map(&:vendor_id).compact.uniq).index_by(&:id)
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def transit_direction_badge(line_item)
|
|
36
|
+
badge_class, label = DIRECTION_BADGES[line_item.direction]
|
|
37
|
+
return if badge_class.nil?
|
|
38
|
+
|
|
39
|
+
# Namespaced: Spree already translates a bare `return` as the lowercase verb.
|
|
40
|
+
text = I18n.t("spree.admin.transit_line_items.#{line_item.direction}", default: label)
|
|
41
|
+
content_tag(:span, text, class: "badge #{badge_class} text-uppercase mr-2")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def transit_datetime(time)
|
|
45
|
+
return '-' if time.blank?
|
|
46
|
+
|
|
47
|
+
content_tag(:div, time.strftime('%H:%M'), class: 'h5 mb-0') +
|
|
48
|
+
content_tag(:div, time.strftime('%a, %d %b %Y'), class: 'small text-muted')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The stop's own name, with its city when they differ (e.g. "Olympic Stadium · Phnom Penh").
|
|
52
|
+
def transit_stop_label(trip_stop)
|
|
53
|
+
return '-' if trip_stop.nil?
|
|
54
|
+
|
|
55
|
+
stop = trip_stop.stop_name.presence || trip_stop.stop_place&.name.presence
|
|
56
|
+
city = trip_stop.location_place&.name.presence
|
|
57
|
+
[stop, (city if city != stop)].compact.join(' · ').presence || '-'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def transit_passenger_seat_label(guest)
|
|
61
|
+
guest.block&.label.presence || '-'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def transit_passenger_name(guest)
|
|
65
|
+
guest.full_name.presence || Spree.t(:no_details, default: 'No details')
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The picked nationality, else local or foreigner: staff selling at the counter often only choose the group.
|
|
69
|
+
def transit_passenger_fare_label(guest)
|
|
70
|
+
guest.nationality&.name.presence || guest.nationality_group&.humanize.presence || '-'
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def transit_duration(from, to)
|
|
74
|
+
return if from.blank? || to.blank? || to <= from
|
|
75
|
+
|
|
76
|
+
minutes = ((to - from) / 60).round
|
|
77
|
+
[("#{minutes / 60}h" if minutes >= 60), ("#{minutes % 60}m" if (minutes % 60).positive?)].compact.join(' ')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def preload_line_item_records(line_items, associations)
|
|
83
|
+
return if line_items.empty?
|
|
84
|
+
|
|
85
|
+
ActiveRecord::Associations::Preloader.new(records: line_items, associations: associations).call
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -21,7 +21,8 @@ module SpreeCmCommissioner
|
|
|
21
21
|
firebase_id_token_context = FirebaseIdTokenProvider.call(id_token: id_token)
|
|
22
22
|
|
|
23
23
|
if firebase_id_token_context.success?
|
|
24
|
-
|
|
24
|
+
provider = firebase_id_token_context.provider
|
|
25
|
+
build_user_identify_provider(provider.merge(email: FirebaseEmails::Resolve.call(provider: provider).value))
|
|
25
26
|
else
|
|
26
27
|
context.fail!(message: firebase_id_token_context.message)
|
|
27
28
|
end
|
|
@@ -2,7 +2,9 @@ require 'firebase-admin-sdk'
|
|
|
2
2
|
|
|
3
3
|
module SpreeCmCommissioner
|
|
4
4
|
class FirebaseEmailFetcher < BaseInteractor
|
|
5
|
-
|
|
5
|
+
# :user_id, or :sub with :provider_id ('google.com', 'apple.com'), plus :project_id, the
|
|
6
|
+
# Firebase project to look in (the shared one when blank)
|
|
7
|
+
delegate :user_id, :sub, :provider_id, :project_id, to: :context
|
|
6
8
|
|
|
7
9
|
# Firebase response
|
|
8
10
|
# {
|
|
@@ -15,7 +17,7 @@ module SpreeCmCommissioner
|
|
|
15
17
|
# "displayName" => "Sreyleak Deth",
|
|
16
18
|
# "photoUrl" => "https://lh3.googleusercontent.com/a/ACg8ocK88Fm3GhVeCS98vLGE-vmShSi76xZwYYA1QwImuyck7zAqpR0=s96-c",
|
|
17
19
|
# "federatedId" => "109192493976909808585",
|
|
18
|
-
# "email" => "
|
|
20
|
+
# "email" => "jonhdoe@gmail.com",
|
|
19
21
|
# "rawId" => "109192493976909808585"
|
|
20
22
|
# }
|
|
21
23
|
# ],
|
|
@@ -26,21 +28,42 @@ module SpreeCmCommissioner
|
|
|
26
28
|
# }
|
|
27
29
|
|
|
28
30
|
def call
|
|
29
|
-
|
|
31
|
+
context.fail!(message: "No service account for Firebase project #{project_id}") if service_account.blank?
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
user = @manager.get_user_by(uid: context.user_id)
|
|
33
|
-
elsif context.sub.present?
|
|
34
|
-
user = @manager.get_user_by_sub(sub: context.sub)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
context.email = user.provider_data.first&.email
|
|
33
|
+
context.email = email_of(find_user)
|
|
38
34
|
end
|
|
39
35
|
|
|
40
36
|
private
|
|
41
37
|
|
|
38
|
+
# The email is best-effort, but the SDK raises on any HTTP error. A Firebase outage, or a tenant
|
|
39
|
+
# key without the Firebase Authentication Viewer role, must fail this lookup, not the sign-in or
|
|
40
|
+
# the backfill run calling it.
|
|
41
|
+
def find_user
|
|
42
|
+
manager = initialize_firebase_manager
|
|
43
|
+
|
|
44
|
+
if user_id.present?
|
|
45
|
+
manager.get_user_by(uid: user_id)
|
|
46
|
+
elsif sub.present?
|
|
47
|
+
manager.get_user_by_sub(sub: sub, provider_id: provider_id)
|
|
48
|
+
end
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
CmAppLogger.error(label: "#{self.class.name}#find_user", data: { project_id: project_id, error: e.message })
|
|
51
|
+
context.fail!(message: e.message)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Firebase often leaves the user's own email blank and keeps it only per sign-in provider, like
|
|
55
|
+
# the response in call method above: no "email" at the top, but "jonhdoe@gmail.com" under google.com.
|
|
56
|
+
def email_of(user)
|
|
57
|
+
return if user.nil?
|
|
58
|
+
|
|
59
|
+
providers = user.provider_data
|
|
60
|
+
user.email.presence ||
|
|
61
|
+
providers.find { |info| info.provider_id == provider_id }&.email.presence ||
|
|
62
|
+
providers.first&.email.presence
|
|
63
|
+
end
|
|
64
|
+
|
|
42
65
|
def service_account
|
|
43
|
-
@service_account ||=
|
|
66
|
+
@service_account ||= SpreeCmCommissioner::FirebaseProjects.service_account_for(project_id, purpose: :auth)
|
|
44
67
|
end
|
|
45
68
|
|
|
46
69
|
def initialize_firebase_manager
|
|
@@ -1,23 +1,53 @@
|
|
|
1
1
|
module SpreeCmCommissioner
|
|
2
|
+
# Fills blank emails on users and their Google/Apple identities from the shared Firebase project.
|
|
3
|
+
# Run it for a tenant before switching its app to the tenant's own project: Apple shares a user's
|
|
4
|
+
# email only on their first authorization of our team, so the new project may never receive it and
|
|
5
|
+
# the shared project would be the only place left holding it.
|
|
2
6
|
class FirebaseEmailFetcherCronExecutor < BaseInteractor
|
|
7
|
+
FIREBASE_PROVIDER_IDS = { 'google' => 'google.com', 'apple' => 'apple.com' }.freeze
|
|
8
|
+
|
|
9
|
+
# :tenant_id (optional; nil backfills every user)
|
|
3
10
|
def call
|
|
4
|
-
|
|
5
|
-
.joins(:google_user_identity_providers)
|
|
6
|
-
.where(email: nil)
|
|
11
|
+
stats = Hash.new(0)
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
next unless firebase_user.success?
|
|
13
|
+
identities.find_each do |identity|
|
|
14
|
+
email = fetch_email(identity)
|
|
15
|
+
next stats[:not_found] += 1 if email.blank?
|
|
12
16
|
|
|
13
|
-
email
|
|
17
|
+
identity.update(email: email) if identity.email.blank?
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
next if
|
|
19
|
+
user = identity.user
|
|
20
|
+
next if user.email.present?
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
# Email is unique per tenant, so a validation failure here means another account in the same
|
|
23
|
+
# tenant already uses it; leave this one blank rather than guess which account owns it.
|
|
24
|
+
user.update(email: email) ? stats[:users_filled] += 1 : stats[:users_skipped] += 1
|
|
20
25
|
end
|
|
26
|
+
|
|
27
|
+
context.stats = stats
|
|
28
|
+
CmAppLogger.log(label: "#{self.class.name}#call", data: { tenant_id: context.tenant_id, stats: stats })
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def identities
|
|
34
|
+
scope = UserIdentityProvider.joins(:user)
|
|
35
|
+
.includes(:user)
|
|
36
|
+
.where(identity_type: FIREBASE_PROVIDER_IDS.keys)
|
|
37
|
+
.where.not(sub: [nil, ''])
|
|
38
|
+
scope = scope.where(spree_users: { tenant_id: context.tenant_id }) if context.tenant_id.present?
|
|
39
|
+
|
|
40
|
+
scope.where(email: [nil, '']).or(scope.where(spree_users: { email: [nil, ''] }))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def fetch_email(identity)
|
|
44
|
+
result = FirebaseEmailFetcher.call(
|
|
45
|
+
sub: identity.sub,
|
|
46
|
+
provider_id: FIREBASE_PROVIDER_IDS[identity.identity_type],
|
|
47
|
+
project_id: FirebaseProjects.shared_project_id
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
result.email if result.success?
|
|
21
51
|
end
|
|
22
52
|
end
|
|
23
53
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
module SpreeCmCommissioner
|
|
2
2
|
class FirebaseIdTokenProvider < BaseInteractor
|
|
3
|
+
SIGN_IN_PROVIDERS = %w[google.com apple.com facebook.com].freeze
|
|
4
|
+
|
|
3
5
|
# :id_token
|
|
4
6
|
def call
|
|
5
7
|
claim = decode_id_token
|
|
@@ -85,33 +87,30 @@ module SpreeCmCommissioner
|
|
|
85
87
|
return nil if claim.nil?
|
|
86
88
|
|
|
87
89
|
# A non-Firebase token (e.g. a Telegram OIDC id_token, or a crafted non-object payload)
|
|
88
|
-
# has no `firebase` section.
|
|
90
|
+
# has no `firebase` section. Other Firebase providers are refused too: a `password` user can
|
|
91
|
+
# set any email without verifying it, and callers match existing accounts by that email.
|
|
89
92
|
provider_name = claim.dig('firebase', 'sign_in_provider') if claim.is_a?(Hash)
|
|
90
|
-
context.fail!(message: I18n.t('firebase_id_token.failure'))
|
|
93
|
+
context.fail!(message: I18n.t('firebase_id_token.failure')) unless SIGN_IN_PROVIDERS.include?(provider_name)
|
|
91
94
|
|
|
92
95
|
user_id = claim['user_id']
|
|
96
|
+
project_id = claim['aud']
|
|
93
97
|
sub = claim.dig('firebase', 'identities', provider_name)&.first
|
|
94
|
-
email = claim['email'] || get_user_email(user_id)
|
|
95
98
|
name = claim['name']
|
|
96
99
|
|
|
100
|
+
# Only what the token carries. FirebaseEmails::Resolve looks it up when a caller needs it.
|
|
101
|
+
email = claim['email']
|
|
102
|
+
|
|
97
103
|
{
|
|
98
104
|
identity_type: provider_name.split('.').first,
|
|
99
105
|
sub: sub,
|
|
100
106
|
name: name,
|
|
101
|
-
email: email
|
|
107
|
+
email: email,
|
|
108
|
+
sign_in_provider: provider_name,
|
|
109
|
+
firebase_user_id: user_id,
|
|
110
|
+
firebase_project_id: project_id
|
|
102
111
|
}
|
|
103
112
|
end
|
|
104
113
|
|
|
105
|
-
def get_user_email(user_id)
|
|
106
|
-
return nil if user_id.blank?
|
|
107
|
-
|
|
108
|
-
firebase_context = FirebaseEmailFetcher.call(user_id: user_id)
|
|
109
|
-
|
|
110
|
-
return unless firebase_context.success?
|
|
111
|
-
|
|
112
|
-
firebase_context.email
|
|
113
|
-
end
|
|
114
|
-
|
|
115
114
|
delegate :id_token, to: :context
|
|
116
115
|
end
|
|
117
116
|
end
|
|
@@ -22,7 +22,8 @@ module SpreeCmCommissioner
|
|
|
22
22
|
|
|
23
23
|
context.device_token.assign_attributes(
|
|
24
24
|
client_version: context.client_version,
|
|
25
|
-
device_type: context.device_type
|
|
25
|
+
device_type: context.device_type,
|
|
26
|
+
firebase_project_id: context.firebase_project_id.presence
|
|
26
27
|
)
|
|
27
28
|
|
|
28
29
|
return if context.device_token.save
|
|
@@ -2,7 +2,8 @@ module SpreeCmCommissioner
|
|
|
2
2
|
class UserIdTokenAuthenticator < BaseInteractor
|
|
3
3
|
# :id_token, :tenant_id (optional)
|
|
4
4
|
def call
|
|
5
|
-
context.
|
|
5
|
+
context.provider = decode_provider!
|
|
6
|
+
context.user = existing_user || register_user
|
|
6
7
|
context.fail!(message: I18n.t('authenticator.incorrect_login')) if context.user.nil?
|
|
7
8
|
|
|
8
9
|
update_user_email if context.user.email.blank?
|
|
@@ -10,26 +11,43 @@ module SpreeCmCommissioner
|
|
|
10
11
|
reject_unauthenticatable_user!(context.user)
|
|
11
12
|
end
|
|
12
13
|
|
|
14
|
+
# Decoded once and handed to the checker and registration, which would otherwise each decode it again.
|
|
15
|
+
def decode_provider!
|
|
16
|
+
firebase_context = FirebaseIdTokenProvider.call(id_token: context.id_token)
|
|
17
|
+
context.fail!(message: I18n.t('authenticator.incorrect_login')) unless firebase_context.success?
|
|
18
|
+
|
|
19
|
+
firebase_context.provider
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def existing_user
|
|
23
|
+
SpreeCmCommissioner::UserIdTokenChecker.call(
|
|
24
|
+
id_token: context.id_token,
|
|
25
|
+
tenant_id: context.tenant_id,
|
|
26
|
+
provider: context.provider
|
|
27
|
+
).user
|
|
28
|
+
end
|
|
29
|
+
|
|
13
30
|
def register_user
|
|
14
31
|
register_context = SpreeCmCommissioner::UserRegistrationWithIdToken.call(
|
|
15
32
|
id_token: context.id_token,
|
|
16
|
-
tenant_id: context.tenant_id
|
|
33
|
+
tenant_id: context.tenant_id,
|
|
34
|
+
provider: context.provider.merge(email: resolved_email)
|
|
17
35
|
)
|
|
18
36
|
|
|
19
|
-
|
|
37
|
+
# A failed registration may still hold the user it matched by email before linking failed.
|
|
38
|
+
register_context.user if register_context.success?
|
|
20
39
|
end
|
|
21
40
|
|
|
22
41
|
def update_user_email
|
|
23
|
-
|
|
24
|
-
return unless firebase_context.success?
|
|
25
|
-
|
|
26
|
-
email = firebase_context.provider[:email]
|
|
27
|
-
context.user.update(email: email) if email.present?
|
|
42
|
+
context.user.update(email: resolved_email) if resolved_email.present?
|
|
28
43
|
end
|
|
29
44
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
45
|
+
# Only needed to register or to fill a blank email, and looked up at most once per login, so a
|
|
46
|
+
# returning user with an email never waits on Firebase.
|
|
47
|
+
def resolved_email
|
|
48
|
+
return @resolved_email if defined?(@resolved_email)
|
|
49
|
+
|
|
50
|
+
@resolved_email = FirebaseEmails::Resolve.call(provider: context.provider).value
|
|
33
51
|
end
|
|
34
52
|
end
|
|
35
53
|
end
|
|
@@ -3,15 +3,17 @@ module SpreeCmCommissioner
|
|
|
3
3
|
# expects:
|
|
4
4
|
# context.id_token
|
|
5
5
|
# context.tenant_id (optional)
|
|
6
|
+
# context.provider (optional; an already-decoded FirebaseIdTokenProvider#provider, skips decoding)
|
|
6
7
|
|
|
7
8
|
def call
|
|
9
|
+
validate_user_by_provider(context.provider || decode_provider!)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def decode_provider!
|
|
8
13
|
firebase_id_token_context = SpreeCmCommissioner::FirebaseIdTokenProvider.call(id_token: context.id_token)
|
|
14
|
+
context.fail!(message: firebase_id_token_context.message) unless firebase_id_token_context.success?
|
|
9
15
|
|
|
10
|
-
|
|
11
|
-
validate_user_by_provider(firebase_id_token_context.provider)
|
|
12
|
-
else
|
|
13
|
-
context.fail!(message: firebase_id_token_context.message)
|
|
14
|
-
end
|
|
16
|
+
firebase_id_token_context.provider
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
# :identity_type, :sub, :tenant_id
|
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
module SpreeCmCommissioner
|
|
2
2
|
class UserRegistrationWithIdToken < BaseInteractor
|
|
3
|
-
# :id_token
|
|
3
|
+
# :id_token, :tenant_id, :provider (optional; an already-decoded FirebaseIdTokenProvider#provider
|
|
4
|
+
# whose email the caller has resolved, skips decoding and the email lookup)
|
|
4
5
|
def call
|
|
5
|
-
|
|
6
|
-
return if firebase_context.nil?
|
|
6
|
+
provider = context.provider || decode_provider!
|
|
7
7
|
|
|
8
8
|
ActiveRecord::Base.transaction do
|
|
9
|
-
find_or_register_user!(
|
|
10
|
-
link_user_account!(
|
|
9
|
+
find_or_register_user!(provider[:name], provider[:email])
|
|
10
|
+
link_user_account!(provider)
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
private
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
# Resolved before the transaction opens: the lookup is a Firebase HTTP call.
|
|
17
|
+
def decode_provider!
|
|
17
18
|
firebase_context = SpreeCmCommissioner::FirebaseIdTokenProvider.call(id_token: context.id_token)
|
|
18
|
-
|
|
19
|
+
context.fail!(message: firebase_context.message) unless firebase_context.success?
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
provider = firebase_context.provider
|
|
22
|
+
provider.merge(email: SpreeCmCommissioner::FirebaseEmails::Resolve.call(provider: provider).value)
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
def find_or_register_user!(name, email)
|
|
@@ -2,8 +2,9 @@ module SpreeCmCommissioner
|
|
|
2
2
|
class FirebaseEmailFetcherJob < ApplicationJob
|
|
3
3
|
queue_as :default
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
# options: { tenant_id: } to backfill one tenant; omit for every user.
|
|
6
|
+
def perform(options = {})
|
|
7
|
+
SpreeCmCommissioner::FirebaseEmailFetcherCronExecutor.call(tenant_id: options[:tenant_id])
|
|
7
8
|
end
|
|
8
9
|
end
|
|
9
10
|
end
|
|
@@ -23,6 +23,19 @@ module SpreeCmCommissioner
|
|
|
23
23
|
ActiveModel::Type::Boolean.new.cast(private_metadata['integration'])
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
# The partner's booking reference (BookMeBus R-..., Larryta booking codes, VET transaction id), or nil.
|
|
27
|
+
# Kept after a cancel too, since that's when staff most need it. Goes through the mapping's own
|
|
28
|
+
# integration rather than `vendor`, which spree_multi_vendor reloads with a find on every call;
|
|
29
|
+
# preload `integration_mappings: :integration` when listing several line items.
|
|
30
|
+
def external_ref
|
|
31
|
+
return unless integration?
|
|
32
|
+
|
|
33
|
+
mapping = integration_mappings.max_by(&:updated_at)
|
|
34
|
+
mapping&.integration&.external_ref(mapping)
|
|
35
|
+
rescue StandardError
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
26
39
|
private
|
|
27
40
|
|
|
28
41
|
def set_integration_flag
|
|
@@ -2,7 +2,7 @@ module SpreeCmCommissioner
|
|
|
2
2
|
module RouteType
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
|
-
ROUTE_TYPES = %i[bus intercity_taxi airport_shuttle rails ferry].freeze
|
|
5
|
+
ROUTE_TYPES = %i[bus intercity_taxi airport_shuttle rails ferry tours].freeze
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
8
|
enum route_type: ROUTE_TYPES if table_exists? && column_names.include?('route_type')
|
|
@@ -11,7 +11,7 @@ module SpreeCmCommissioner
|
|
|
11
11
|
module ServiceType
|
|
12
12
|
extend ActiveSupport::Concern
|
|
13
13
|
|
|
14
|
-
SERVICE_TYPES = %i[organizer transit intercity_taxi accommodation].freeze
|
|
14
|
+
SERVICE_TYPES = %i[organizer transit intercity_taxi accommodation activity].freeze
|
|
15
15
|
|
|
16
16
|
class_methods do
|
|
17
17
|
def service_type_enum(column_name = :service_type)
|
|
@@ -33,6 +33,13 @@ module SpreeCmCommissioner
|
|
|
33
33
|
false
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# The partner's booking reference for a line item, as staff look it up in the partner's own
|
|
37
|
+
# system. Read from what the booking already saved on the line item mapping, so nothing extra
|
|
38
|
+
# is stored. The mapping's external_id is the booking id for most partners (VET's transaction id).
|
|
39
|
+
def external_ref(line_item_mapping)
|
|
40
|
+
line_item_mapping.external_id
|
|
41
|
+
end
|
|
42
|
+
|
|
36
43
|
def restock_external_inventory!(_order, _line_items)
|
|
37
44
|
raise NotImplementedError, 'Subclasses must implement the restock_external_inventory! method'
|
|
38
45
|
end
|
|
@@ -38,6 +38,14 @@ class SpreeCmCommissioner::Integrations::BookMeBusV1 < SpreeCmCommissioner::Inte
|
|
|
38
38
|
raise SpreeCmCommissioner::Integrations::SyncError, 'Booking cannot be cancelled once confirmed'
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# override
|
|
42
|
+
# BookMeBus staff search by reservation number with an R- prefix; the mapping's external_id is the cart token.
|
|
43
|
+
def external_ref(line_item_mapping)
|
|
44
|
+
reservations = line_item_mapping.external_payload&.dig('reservations')
|
|
45
|
+
numbers = reservations.is_a?(Array) ? reservations.filter_map { |reservation| reservation['reservation_number'] if reservation.is_a?(Hash) } : []
|
|
46
|
+
numbers.any? ? numbers.map { |number| "R-#{number}" }.join(', ') : super
|
|
47
|
+
end
|
|
48
|
+
|
|
41
49
|
# override - Fetch all external inventory items
|
|
42
50
|
def external_inventory_items(order)
|
|
43
51
|
SpreeCmCommissioner::Integrations::BookMeBusV1::Inventory::ExternalInventoryItems.new.call(
|
|
@@ -41,6 +41,13 @@ class SpreeCmCommissioner::Integrations::Larryta < SpreeCmCommissioner::Integrat
|
|
|
41
41
|
true
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
+
# override
|
|
45
|
+
# One Larryta booking code per seat; the mapping's external_id joins them with '-', which codes can contain too.
|
|
46
|
+
def external_ref(line_item_mapping)
|
|
47
|
+
codes = Array(line_item_mapping.external_payload&.dig('booking_codes')).compact_blank
|
|
48
|
+
codes.any? ? codes.join(', ') : super
|
|
49
|
+
end
|
|
50
|
+
|
|
44
51
|
def release_inventory!(order, line_items)
|
|
45
52
|
SpreeCmCommissioner::Integrations::Larryta::Inventory::ReleaseSeats.new.call(
|
|
46
53
|
integration: self,
|