Class: Bsale::Buyers
- Inherits:
-
Object
- Object
- Bsale::Buyers
- Defined in:
- lib/sale/buyers.rb
Defined Under Namespace
Classes: BuyerNotFoundError
Constant Summary collapse
- EMAIL_REGEX =
/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
Instance Method Summary collapse
- #create(firstName:, lastName:, email:, phone:, address:, municipality:, city:, code: nil, note: nil, activity: nil) ⇒ Object
- #find_all_by_code(code) ⇒ Object
- #find_all_by_email(email) ⇒ Object
- #find_by_email_and_code(email, code = nil) ⇒ Object
- #find_or_create(data) ⇒ Object
-
#initialize(root) ⇒ Buyers
constructor
A new instance of Buyers.
- #update(id, data) ⇒ Object
- #update_points(buyer, difference, order_code) ⇒ Object
Constructor Details
Instance Method Details
#create(firstName:, lastName:, email:, phone:, address:, municipality:, city:, code: nil, note: nil, activity: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sale/buyers.rb', line 45 def create(firstName:, lastName:, email:, phone:, address:, municipality:, city:, code: nil, note: nil, activity: nil) raise "Invalid email: #{email}" unless email.match(EMAIL_REGEX) # is_foreign = buyer.code.to_s.gsub(/[^0-9]/, '').to_i == 55_555_555 data = { accumulatePoints: 1, firstName: firstName, lastName: lastName, code: code && code != '' ? code : FakeRUT.for(email), address: address, email: email, phone: phone, city: city, municipality: municipality, note: note, # gender activity: activity # date of birth } root.post("/clients.json", data) end |
#find_all_by_code(code) ⇒ Object
31 32 33 |
# File 'lib/sale/buyers.rb', line 31 def find_all_by_code(code) get_buyers_with(code: code) end |
#find_all_by_email(email) ⇒ Object
26 27 28 29 |
# File 'lib/sale/buyers.rb', line 26 def find_all_by_email(email) return nil unless email && email.match(EMAIL_REGEX) get_buyers_with(email: email) end |
#find_by_email_and_code(email, code = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sale/buyers.rb', line 12 def find_by_email_and_code(email, code = nil) if list = find_all_by_email(email) and list.any? with_code = list.select { |b| code.nil? ? (b.code || '').size > 0 : (code.to_s == b.code.to_s) } if with_code.count != 1 raise BuyerNotFoundError.new("#{with_code.count} clients under #{email} and code #{code}") end return with_code.first # else # raise BuyerNotFoundError.new("No matches with email #{email}, code #{code}") end end |
#find_or_create(data) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/sale/buyers.rb', line 35 def find_or_create(data) matches = if data[:code].present? # has rut, either from order contact info or company record find_all_by_code(data[:code]) else find_all_by_code(FakeRUT.for(data[:email])) end matches.any ? matches.first : create(data) end |
#update(id, data) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/sale/buyers.rb', line 66 def update(id, data) if [:email, :code].include?(data.symbolize_keys.keys) raise 'Invalid request, cannot update email nor code!' end # puts "Updating client #{id} with data: #{data.inspect}" root.put("/clients/#{id}.json", data.merge(id: id)) end |
#update_points(buyer, difference, order_code) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sale/buyers.rb', line 75 def update_points(buyer, difference, order_code) raise 'No point in modifying 0 points' if difference == 0 data = { email: buyer.email, points: difference.abs, type: difference > 0 ? 0 : 1, # 0 to add, 1 to subtract description: "Apply #{difference} points from order #{order_code}", orderId: order_code } resp = root.put('/clients/points.json', data) # resp.points end |