Class: Spree::Admin::VendorAuthorizedUsersController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/vendor_authorized_users_controller.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



10
11
12
13
14
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 10

def collection
  load_vendor

  @users = @vendor.users
end

#collection_url(options = {}) ⇒ Object



63
64
65
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 63

def collection_url(options = {})
  admin_vendor_vendor_authorized_users_url(options)
end

#load_vendorObject



6
7
8
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 6

def load_vendor
  @vendor ||= Spree::Vendor.by_vendor_id!(params[:vendor_id])
end

#model_classObject



53
54
55
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 53

def model_class
  Spree::User
end

#object_nameObject



58
59
60
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 58

def object_name
  'user'
end

#submitted_alert_channels_maskObject

OR the bits, never sum them. Sum is not idempotent: if the same bit arrives twice (duplicated checkbox, double-submit, hand-crafted post) 1 + 1 = 2, which silently means google_form instead of spree. ["0","1","1","16"].sum => 18 => [google_form, offline_sell]; the same input OR'd => 17 => [spree, offline_sell].

Masking against the known bits also drops junk from a crafted post (999 => 7).



45
46
47
48
49
50
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 45

def 
  known = SpreeCmCommissioner::OrderChannelBitwise::CHANNEL_ALERT_BITS.values.reduce(:|)

  Array(params.dig(:vendor, :telegram_alert_channels))
    .reduce(0) { |acc, value| acc | value.to_i } & known
end

#update_telegram_chatObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/spree/admin/vendor_authorized_users_controller.rb', line 16

def update_telegram_chat
  @vendor.update(preferred_telegram_alert_channels: )

  context = ::SpreeCmCommissioner::TelegramChatsFinder.call(
    telegram_chat_name: params['vendor']['preferred_telegram_chat_name'],
    telegram_chat_type: params['vendor']['preferred_telegram_chat_type']
  )

  if context.chats.present? && context.chats.is_a?(Hash)
    vendor_attributes = {
      preferred_telegram_chat_name: params['vendor']['preferred_telegram_chat_name'],
      preferred_telegram_chat_type: params['vendor']['preferred_telegram_chat_type'],
      preferred_telegram_chat_id: context.chats[:id]
    }

    flash[:error] = @vendor.errors.full_messages.to_sentence unless @vendor.update(vendor_attributes)
  else
    flash[:error] = I18n.t('vendor.validation.could_not_find_telegram_chat')
  end

  redirect_to collection_url(@vendor)
end