Class: BBMB::Util::Server

Inherits:
SBSM::AdminServer
  • Object
show all
Defined in:
lib/bbmb/util/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(persistence, app) ⇒ Server

Returns a new instance of Server.



29
30
31
32
33
# File 'lib/bbmb/util/server.rb', line 29

def initialize(persistence, app)
  @persistence = persistence
  @app = app
  super(app: app)
end

Instance Method Details

#inject_order(customer_id, products, infos, opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bbmb/util/server.rb', line 45

def inject_order(customer_id, products, infos, opts={})
  customer = Model::Customer.find_by_customer_id(customer_id) \
    || Model::Customer.find_by_ean13(customer_id)
  needed_create = false
  unless customer
    if idtype = opts[:create_missing_customer] && !customer_id.empty?
      customer = Model::Customer.new(customer_id)
      if idtype.to_s == 'ean13'
        customer.ean13 = customer_id
      end
      BBMB.persistence.save(customer)
      needed_create = true
    else
      raise "Unknown Customer #{customer_id}"
    end
  end
  order = Model::Order.new(customer)
  products.each { |info|
    if(product = Model::Product.find_by_pcode(info[:pcode]) \
       || Model::Product.find_by_ean13(info[:ean13]) \
       || Model::Product.find_by_article_number(info[:article_number]))
      order.add(info[:quantity], product)
      [:article_number, :backorder].each do |key|
        info.store key, product.send(key)
      end
      info.store :description, product.description.de
      info[:deliverable] = info[:quantity]
    else
      info[:deliverable] = 0
    end
  }
  infos.each { |key, value|
    order.send("#{key}=", value)
  }
  customer.inject_order(order)
  if opts[:deliver]
    @app.send_order order, customer
  end
  if needed_create
    BBMB::Util::Mail.notify_inject_error(order, opts)
  end
  SBSM.info "inject_order #{order.order_id} for customer_id #{customer_id} done at #{Time.now}"
  { :order_id => order.order_id, :products => products }
end

#invoice(range) ⇒ Object



34
35
36
37
38
39
# File 'lib/bbmb/util/server.rb', line 34

def invoice(range)
  SBSM.info "invoice started at #{Time.now} for #{range}"
  Invoicer.run(range)
rescue Exception => e
  Mail.notify_error(e)
end

#rename_user(customer_id, old_name, new_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bbmb/util/server.rb', line 89

def rename_user(customer_id, old_name, new_name)
  return if old_name.eql?(new_name)
  BBMB.auth.autosession(BBMB.config.auth_domain) do |session|
    if (old_name.nil?)
      begin
        session.create_entity(new_name)
      rescue => error
        raise error if Model::Customer.odba_extent.find {|x| x.email && x.email.index(new_name) }
        SBSM.info("Skip session.create_entity for customer #{customer_id} as we found no customer with e-mail #{new_name}")
      end
    else
      session.rename(old_name, new_name)
    end
  end
rescue => error
  SBSM.info(msg = "#{error}: Cannot rename email '#{old_name}' to '#{new_name}'")
  raise Yus::YusError
end

#run_invoicerObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bbmb/util/server.rb', line 107

def run_invoicer
  run_only_once_at_startup = false
  @invoicer ||= Thread.new {
    Thread.current.abort_on_exception = true
    loop {
      today = Date.today
      day = today >> 1
      start = Time.local(today.year, today.month)
      now = Time.now
      at = Time.local(day.year, day.month)
      secs = at - now
      SBSM.debug("run_invoicer sleeping %.2f seconds. run_only_once_at_startup #{run_only_once_at_startup}" % secs)
      if run_only_once_at_startup then puts "Skipped sleeping #{secs}" else sleep(secs) end
      SBSM.debug("invoice starting")
      invoice(start...at)
      SBSM.debug("invoice finished")
    }
  }
end

#run_updaterObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bbmb/util/server.rb', line 126

def run_updater
  run_only_once_at_startup = false
  SBSM.debug("run_only_once_at_startup? #{run_only_once_at_startup} hour: #{BBMB.config.update_hour}")
  @updater ||= Thread.new {
    loop {
      day = Date.today
      now = Time.now
      if(now.hour >= BBMB.config.update_hour)
        day += 1
      end
      at = Time.local(day.year, day.month, day.day, BBMB.config.update_hour)
      secs = at - now
      SBSM.debug("updater sleeping %.2f seconds. run_only_once_at_startup #{run_only_once_at_startup}" % secs)
      if run_only_once_at_startup then puts "Skipped sleeping #{secs}" else sleep(secs) end
      SBSM.debug("update starting")
      update
      SBSM.debug("update finished")
      Thread.abort if run_only_once_at_startup
    }
  }
end

#updateObject



40
41
42
43
44
# File 'lib/bbmb/util/server.rb', line 40

def update
  Updater.run
rescue Exception => e
  Mail.notify_error(e)
end