Class: SolidusTiny::Seller

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/solidus_tiny/seller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activesObject



30
31
32
# File 'app/models/solidus_tiny/seller.rb', line 30

def self.actives
  where(active: true)
end

.defaultObject



26
27
28
# File 'app/models/solidus_tiny/seller.rb', line 26

def self.default
  find_by(default: true)
end

.inactivate_sellers(sellers_tiny) ⇒ Object



34
35
36
# File 'app/models/solidus_tiny/seller.rb', line 34

def self.inactivate_sellers sellers_tiny
  where.not(external_id: sellers_tiny.pluck(:id)).update_all(active: false)
end

.upsert_sellers(account) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/solidus_tiny/seller.rb', line 10

def self.upsert_sellers 
  TinyErpApi.configure { |config| config.access_token = .api_key }
  sellers = TinyErpApi::Seller.get_all
  upsert_sellers = sellers.map do |seller|
    situacao = seller[:situacao] == "Ativo" ? true : false
    { 
      name: seller[:nome], 
      external_id: seller[:id], 
      account_id: .id, 
      active: situacao
    }
  end
  SolidusTiny::Seller.upsert_all(upsert_sellers, unique_by: :external_id)
  inactivate_sellers(sellers)
end

Instance Method Details

#falsify_all_othersObject



38
39
40
41
42
# File 'app/models/solidus_tiny/seller.rb', line 38

def falsify_all_others
  if default
    self.class.where('id != ?', self.id).update_all(default: false)
  end
end