mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-03 02:21:33 +00:00
Merge pull request #10449 from macanudo527/fix_ER
Update OCs after ER Permissions are Revoked
This commit is contained in:
@@ -13,6 +13,7 @@ class EnterpriseRelationship < ApplicationRecord
|
||||
|
||||
after_save :update_permissions_of_child_variant_overrides
|
||||
before_destroy :revoke_all_child_variant_overrides
|
||||
before_destroy :destroy_related_exchanges
|
||||
|
||||
scope :with_enterprises, -> {
|
||||
joins("
|
||||
@@ -102,6 +103,10 @@ class EnterpriseRelationship < ApplicationRecord
|
||||
child_variant_overrides.update_all(permission_revoked_at: Time.zone.now)
|
||||
end
|
||||
|
||||
def destroy_related_exchanges
|
||||
Exchange.where(sender: parent, receiver: child, incoming: true).destroy_all
|
||||
end
|
||||
|
||||
def child_variant_overrides
|
||||
VariantOverride.unscoped.for_hubs(child)
|
||||
.joins(variant: :product).where("spree_products.supplier_id IN (?)", parent)
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class EnterpriseRelationshipPermission < ApplicationRecord
|
||||
belongs_to :enterprise_relationship
|
||||
default_scope { order('name') }
|
||||
before_destroy :destroy_related_exchanges
|
||||
|
||||
def destroy_related_exchanges
|
||||
return if name != "add_to_order_cycle"
|
||||
|
||||
Exchange
|
||||
.where(sender: enterprise_relationship.parent,
|
||||
receiver: enterprise_relationship.child, incoming: true).destroy_all
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,4 +3,9 @@
|
||||
class ExchangeVariant < ApplicationRecord
|
||||
belongs_to :exchange
|
||||
belongs_to :variant, class_name: 'Spree::Variant'
|
||||
after_destroy :destroy_related_outgoing_variants
|
||||
|
||||
def destroy_related_outgoing_variants
|
||||
VariantDeleter.new.destroy_related_outgoing_variants(variant_id, exchange.order_cycle)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,6 +11,15 @@ class VariantDeleter
|
||||
variant.destroy
|
||||
end
|
||||
|
||||
def destroy_related_outgoing_variants(variant_id, order_cycle)
|
||||
internal_variants = ExchangeVariant.where(variant_id: variant_id).
|
||||
joins(:exchange).
|
||||
where(
|
||||
exchanges: { order_cycle: order_cycle, incoming: false }
|
||||
)
|
||||
internal_variants.destroy_all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def only_variant_on_product?(variant)
|
||||
|
||||
Reference in New Issue
Block a user