solidus_bling 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/spree/admin/accounts_controller.rb +54 -0
  3. data/app/helpers/solidus_bling/accounts_helper.rb +4 -0
  4. data/app/jobs/solidus_bling/application_job.rb +9 -0
  5. data/app/jobs/solidus_bling/contact_job.rb +22 -0
  6. data/app/jobs/solidus_bling/order_job.rb +22 -0
  7. data/app/jobs/solidus_bling/product_job.rb +13 -0
  8. data/app/models/solidus_bling/account.rb +9 -0
  9. data/app/models/{bling → solidus_bling}/api.rb +5 -5
  10. data/app/models/{bling/contato.rb → solidus_bling/contact.rb} +12 -12
  11. data/app/models/solidus_bling/event.rb +4 -0
  12. data/app/models/{bling/pedido.rb → solidus_bling/order.rb} +13 -18
  13. data/app/models/solidus_bling/payment_method.rb +5 -0
  14. data/app/models/{bling/produto.rb → solidus_bling/product.rb} +11 -8
  15. data/app/models/solidus_bling/seller.rb +5 -0
  16. data/app/models/solidus_bling/shipping_method.rb +5 -0
  17. data/app/subscribers/{bling → solidus_bling}/subscriber.rb +6 -6
  18. data/app/views/spree/admin/accounts/_form.html.erb +68 -0
  19. data/app/views/spree/admin/accounts/edit.html.erb +2 -0
  20. data/app/views/spree/admin/accounts/index.html.erb +13 -0
  21. data/app/views/spree/admin/accounts/new.html.erb +2 -0
  22. data/app/views/spree/admin/accounts/show.html.erb +8 -0
  23. data/config/routes.rb +3 -1
  24. data/db/migrate/{20230906141624_create_erp_accounts.rb → 20230918141419_create_solidus_bling_accounts.rb} +4 -2
  25. data/db/migrate/20230918141505_add_bling_order_id_to_spree_orders.rb +5 -0
  26. data/db/migrate/{20230906142108_create_erp_products.rb → 20230918141700_create_solidus_bling_products.rb} +3 -3
  27. data/db/migrate/{20230906142806_create_erp_events.rb → 20230918142547_create_solidus_bling_events.rb} +2 -2
  28. data/db/migrate/20230918142742_create_solidus_bling_payment_methods.rb +13 -0
  29. data/db/migrate/20230918142953_create_solidus_bling_sellers.rb +11 -0
  30. data/db/migrate/20230918143057_create_solidus_bling_shipping_methods.rb +13 -0
  31. data/lib/generators/solidus_bling/install/templates/initializer.rb +0 -5
  32. data/lib/solidus_bling/configuration.rb +3 -1
  33. data/lib/solidus_bling/engine.rb +17 -4
  34. data/lib/solidus_bling/version.rb +1 -1
  35. data/solidus_bling.gemspec +6 -0
  36. data/spec/helpers/solidus_bling/accounts_helper_spec.rb +17 -0
  37. data/spec/jobs/solidus_bling/contact_job_spec.rb +7 -0
  38. data/spec/jobs/solidus_bling/order_job_spec.rb +7 -0
  39. data/spec/jobs/solidus_bling/product_job_spec.rb +7 -0
  40. data/spec/models/solidus_bling/account_spec.rb +7 -0
  41. data/spec/models/solidus_bling/event_spec.rb +7 -0
  42. data/spec/models/solidus_bling/payment_method_spec.rb +7 -0
  43. data/spec/models/solidus_bling/product_spec.rb +7 -0
  44. data/spec/models/solidus_bling/seller_spec.rb +7 -0
  45. data/spec/models/solidus_bling/shipping_method_spec.rb +7 -0
  46. data/spec/requests/solidus_bling/accounts_spec.rb +7 -0
  47. metadata +55 -32
  48. data/app/jobs/application_job.rb +0 -7
  49. data/app/jobs/erp_contact_job.rb +0 -20
  50. data/app/jobs/erp_order_job.rb +0 -20
  51. data/app/jobs/erp_product_job.rb +0 -11
  52. data/app/models/erp_account.rb +0 -3
  53. data/app/models/erp_event.rb +0 -2
  54. data/app/models/erp_product.rb +0 -3
  55. data/config/initializers/solidus_bling.rb +0 -11
  56. data/db/migrate/20230906141728_add_erp_id_to_spree_orders.rb +0 -5
  57. data/spec/jobs/erp_contact_job_spec.rb +0 -5
  58. data/spec/jobs/erp_order_job_spec.rb +0 -5
  59. data/spec/jobs/erp_product_job_spec.rb +0 -5
  60. data/spec/models/erp_account_spec.rb +0 -5
  61. data/spec/models/erp_event_spec.rb +0 -5
  62. data/spec/models/erp_product_spec.rb +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57eb623fa49a1b9f53dac0ad680791afba17c0b1964ffb68042f3c962362003f
4
- data.tar.gz: e0063409850eccd59f8a7945b0cdc3cf3cf12a9936d65cd08af5bad5f5272867
3
+ metadata.gz: 81e47bc81b103b82f681395e85e63674a0950316711770d0bbe6ca6f854eef01
4
+ data.tar.gz: 3983f4e929e14af2409202589016c7bb330376a2e38e0d1a33e02fcfa5620583
5
5
  SHA512:
6
- metadata.gz: 01ab7448075d61c24cd7b7bd95bc964bc9967616e05248c1db21b7b4f1835bc8340b5152dfdadb2eee9adead2e8923165851c5d23582fe9873f3ac5921f9425b
7
- data.tar.gz: 36fb291c3658ebbe20a303c7a75aea9c58e18a3f37401bbc2131291e3e4fff0150dabc73d677751be7dd52987931196e8f060ccb5a66a03fcb3312fa0475ee4f
6
+ metadata.gz: 885bd2077efb3f7dc2d0b0cad3ba18e92f136c82608da254e35de9b6867ce235a587d11dfdee02d88ff086fff0e96ab19f5acf58c587ce6579b432fde861eb0b
7
+ data.tar.gz: 754480ad38bc954d93577623a3aaee813dead59ee10aede1afed9a5aa48c425b675f62d03a04eb934459eb5e62cb238cb78cd71ea548c88b9c2854318f023a96
@@ -0,0 +1,54 @@
1
+ module Spree
2
+ module Admin
3
+ class AccountsController < ResourceController
4
+ def index
5
+ end
6
+
7
+ def show
8
+ @bling_account = SolidusBling::Account.find(params[:id])
9
+ end
10
+
11
+ def new
12
+ @bling_account = SolidusBling::Account.new
13
+ @payment_methods = Spree::PaymentMethod.all.pluck(:name, :id)
14
+ @shipping_methods = Spree::ShippingMethod.all.pluck(:name, :id)
15
+ @bling_account.payment_methods.build
16
+ @bling_account.shipping_methods.build
17
+ @bling_account.sellers.build
18
+ end
19
+
20
+ def create
21
+ bling_account = SolidusBling::Account.create account_params.merge(name: "Bling")
22
+ redirect_to admin_accounts_path
23
+ end
24
+
25
+ def edit
26
+ @bling_account = SolidusBling::Account.find(params[:id])
27
+ @payment_methods = Spree::PaymentMethod.all.pluck(:name, :id)
28
+ @shipping_methods = Spree::ShippingMethod.all.pluck(:name, :id)
29
+ end
30
+
31
+ def update
32
+ bling_account = SolidusBling::Account.find(params[:id])
33
+ bling_account.update account_params
34
+ redirect_to edit_admin_account_path(bling_account.id)
35
+ end
36
+
37
+ def delete
38
+ bling_account = SolidusBling::Account.find(params[:id])
39
+ bling_account.delete
40
+ redirect_to admin_accounts_path
41
+ end
42
+
43
+ private
44
+
45
+ def model_class
46
+ SolidusBling::Account
47
+ end
48
+
49
+ def account_params
50
+ params.require(:account).permit(:app_name, :client_id, :client_secret, :refresh_token, :api_base_url, :external_store_id, :incoming_category_id, payment_methods_attributes: [:id, :spree_payment_method_id, :external_id], shipping_methods_attributes: [:id, :spree_shipping_method_id, :alias], sellers_attributes: [:id, :name, :external_id])
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module SolidusBling
2
+ module AccountsHelper
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ module SolidusBling
2
+ class ApplicationJob < ActiveJob::Base
3
+ # Automatically retry jobs that encountered a deadlock
4
+ # retry_on ActiveRecord::Deadlocked
5
+
6
+ # Most jobs are safe to ignore if the underlying records are no longer available
7
+ # discard_on ActiveJob::DeserializationError
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module SolidusBling
2
+ class ContactJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform event:, method_name:
6
+ bling_event = SolidusBling::Event.new
7
+ contact = event[:args][:contato]
8
+ order = event[:args][:order]
9
+ bling_event.internal_id = order.id
10
+ bling_event.name = "event.omnes_event_name"
11
+ bling_event.method = method_name
12
+ if contact.include? "error"
13
+ bling_event.status = 'error'
14
+ bling_event.message = contact["error"]["description"]
15
+ else
16
+ bling_event.status = 'success'
17
+ end
18
+ bling_event.body = order.to_json
19
+ bling_event.save!
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module SolidusBling
2
+ class OrderJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform event:, method_name:
6
+ bling_event = SolidusBling::Event.new
7
+ order = event[:order]
8
+ bling_event.internal_id = order.id
9
+ bling_event.method = method_name
10
+ begin
11
+ SolidusBling::Order.new(order).send
12
+ bling_event.status = 'success'
13
+ rescue => e
14
+ bling_event.status = 'error'
15
+ bling_event.message = e.to_json
16
+ ensure
17
+ bling_event.body = order.to_json
18
+ bling_event.save!
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module SolidusBling
2
+ class ProductJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform event:, method_name:
6
+ bling_event = SolidusBling::Event.new
7
+ products = event[:products]
8
+ bling_event.method = method_name
9
+ bling_event.body = products.to_json
10
+ bling_event.save!
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module SolidusBling
2
+ class Account < ApplicationRecord
3
+ has_many :payment_methods
4
+ has_many :shipping_methods
5
+ has_many :products
6
+ has_many :sellers
7
+ accepts_nested_attributes_for :payment_methods, :shipping_methods, :sellers
8
+ end
9
+ end
@@ -1,9 +1,9 @@
1
- module Bling
1
+ module SolidusBling
2
2
  class Api
3
3
 
4
- def initialize
5
- app_name = SolidusBling.config.app_name
6
- @account = find_account(app_name)
4
+ attr_reader :account, :headers
5
+ def initialize account
6
+ @account = account
7
7
  verify_token
8
8
  @headers = {
9
9
  "Content-Type": "application/json",
@@ -48,7 +48,7 @@ module Bling
48
48
  private
49
49
 
50
50
  def find_account app_name
51
- account = ErpAccount.find_by(app_name: app_name)
51
+ account = SolidusBling::Account.find_by(app_name: app_name)
52
52
  raise 'Conta de aplicativo inexistente' if account.nil?
53
53
  account
54
54
  end
@@ -1,16 +1,16 @@
1
- module Bling
2
- class Contato < Bling::Api
1
+ module SolidusBling
2
+ class Contact < SolidusBling::Api
3
3
 
4
4
  attr_reader :info
5
5
 
6
- def initialize order
7
- super()
6
+ def initialize order, account
7
+ super(account)
8
8
  @info = find_or_create(order)
9
9
  end
10
10
 
11
11
  def find_or_create order
12
- raise "Order com CPF/CNPJ vazio" if order.cpf_cnpj.nil?
13
- contato = find_by_cpf(order.cpf_cnpj)
12
+ raise "Order com CPF/CNPJ vazio" if order.tax_id.nil?
13
+ contato = find_by_cpf(order.tax_id)
14
14
  if contato.nil?
15
15
  contato_external_id = create(order)
16
16
  contato = {"id" => contato_external_id}
@@ -18,8 +18,8 @@ module Bling
18
18
  contato
19
19
  end
20
20
 
21
- def find_by_cpf cpf_cnpj
22
- req = Typhoeus.get("#{@account.api_base_url}/contatos?numeroDocumento=#{cpf_cnpj.gsub(/\D/, "")}", headers: @headers)
21
+ def find_by_cpf tax_id
22
+ req = Typhoeus.get("#{@account.api_base_url}/contatos?numeroDocumento=#{tax_id.gsub(/\D/, "")}", headers: @headers)
23
23
  json = JSON.parse(req.body)
24
24
  response_has_error?(json)
25
25
  user = {}
@@ -39,7 +39,7 @@ module Bling
39
39
  ensure
40
40
  json = JSON.parse(req.body)
41
41
  response_has_error?(json)
42
- Spree::Bus.publish :erp_contato_created, args: {order: order, contato: json}
42
+ Spree::Bus.publish :bling_contact_created, args: {order: order, contato: json}
43
43
  return json.dig("data", "id")
44
44
  end
45
45
  end
@@ -47,15 +47,15 @@ module Bling
47
47
  private
48
48
 
49
49
  def tipo_pessoa doc
50
- doc.length > 11 ? "J" : "F"
50
+ doc.gsub(/\D/, "").length > 11 ? "J" : "F"
51
51
  end
52
52
 
53
53
  def build_hash_contato order
54
54
  {
55
55
  "nome": order.ship_address.name,
56
56
  "telefone": order.ship_address.phone,
57
- "tipo": tipo_pessoa(order.cpf_cnpj),
58
- "numeroDocumento": order.cpf_cnpj,
57
+ "tipo": tipo_pessoa(order.tax_id),
58
+ "numeroDocumento": order.tax_id,
59
59
  "email": order.email,
60
60
  "endereco": {
61
61
  "geral": {
@@ -0,0 +1,4 @@
1
+ module SolidusBling
2
+ class Event < ApplicationRecord
3
+ end
4
+ end
@@ -1,19 +1,14 @@
1
- module Bling
2
- class Pedido < Bling::Api
3
-
4
- ID_LOJA_BLING = SolidusBling.config.store_id.to_i || 204601505
5
- ID_VENDEDOR_BLING = SolidusBling.config.seller_id.to_i || 15596272829
6
- ID_FORMA_PGTO_BLING = SolidusBling.config.payment_type_id.to_i || 4484076
7
- ID_CAT_RECEITA_BLING = SolidusBling.config.category_id.to_i || 14653794318
1
+ module SolidusBling
2
+ class Order < SolidusBling::Api
8
3
 
9
4
  def initialize order
10
- super()
5
+ super(SolidusBling::Account.first)
11
6
  @order = order
12
7
  end
13
8
 
14
9
  def send
15
10
  if @order.present?
16
- contato = Bling::Contato.new(@order)
11
+ contato = SolidusBling::Contact.new(@order, @account)
17
12
  hash_order = build_hash_order(@order, contato.info)
18
13
  begin
19
14
  attempts ||= 1
@@ -25,7 +20,7 @@ module Bling
25
20
  json = JSON.parse(req.body)
26
21
  response_has_error?(json)
27
22
  if json.include? "data"
28
- @order.erp_order_id = json["data"]["id"]
23
+ @order.bling_order_id = json["data"]["id"]
29
24
  @order.save
30
25
  end
31
26
  end
@@ -39,14 +34,14 @@ module Bling
39
34
  "numeroLoja": order.number,
40
35
  "data": order.completed_at.strftime("%Y-%m-%d"),
41
36
  "loja": {
42
- "id": ID_LOJA_BLING
37
+ "id": @account.external_store_id
43
38
  },
44
39
  "contato": {
45
40
  "id": contato["id"]
46
41
  },
47
42
  "itens": build_array_items(order.line_items),
48
43
  "vendedor": {
49
- "id": ID_VENDEDOR_BLING
44
+ "id": @account.sellers.first.external_id
50
45
  },
51
46
  "transporte": build_hash_transporte(order),
52
47
  "parcelas": build_array_parcelas(order),
@@ -59,20 +54,20 @@ module Bling
59
54
  "observacoes": " Nº Pedido Loja: #{order.number}",
60
55
  "observacoesInternas": "Pagamento: #{@parcelas}x R$ #{(order.total.to_f / @parcelas).round(2)}\nForma de envio: #{order.shipments.last.selected_shipping_rate.name}\n#{build_cupons(order.promotions)}",
61
56
  "categoria": {
62
- "id": ID_CAT_RECEITA_BLING
57
+ "id": @account.incoming_category_id
63
58
  }
64
59
  }
65
60
  end
66
61
 
67
62
  def build_array_items items
68
- items.map do |item|
69
- erp_product = ErpProduct.find_by(sku: item.sku)
63
+ items.map do |item|
64
+ bling_product = SolidusBling::Product.find_by(sku: item.sku)
70
65
  {
71
- "id": erp_product.external_id,
66
+ "id": bling_product.external_id,
72
67
  "quantidade": item.quantity,
73
68
  "valor": item.price.to_f,
74
69
  "produto": {
75
- "id": erp_product.external_id
70
+ "id": bling_product.external_id
76
71
  }
77
72
  }
78
73
  end
@@ -89,7 +84,7 @@ module Bling
89
84
  "dataVencimento": (order.completed_at + parcela.months).strftime("%Y-%m-%d"),
90
85
  "valor": valor_parcela,
91
86
  "formaPagamento": {
92
- "id": ID_FORMA_PGTO_BLING
87
+ "id": @account.payment_methods.find_by(spree_payment_method_id: order.payments.last.payment_method_id).external_id
93
88
  }
94
89
  }
95
90
  end
@@ -0,0 +1,5 @@
1
+ module SolidusBling
2
+ class PaymentMethod < ApplicationRecord
3
+ belongs_to :account
4
+ end
5
+ end
@@ -1,20 +1,22 @@
1
- module Bling
2
- class Produto < Bling::Api
1
+ module SolidusBling
2
+ class Product < ApplicationRecord
3
+ belongs_to :account
3
4
 
4
- def update
5
+ def self.update bling_account
6
+ bling_api = SolidusBling::Api.new(bling_account)
5
7
  skus = Spree::Variant.pluck(:sku)
6
8
  hash_products = {}
7
9
  products = skus.map do |sku|
8
10
  begin
9
11
  attempts ||= 1
10
- req = Typhoeus.get("#{@account.api_base_url}/produtos?codigo=#{sku}", headers: @headers)
12
+ req = Typhoeus.get("#{bling_api.account.api_base_url}/produtos?codigo=#{sku}", headers: bling_api.headers)
11
13
  res_json = JSON.parse(req.body)
12
14
  p "#{sku} => #{res_json}"
13
15
  next if res_json["data"] == []
14
16
  product = res_json["data"].first
15
17
  hash_products[sku] = product["id"]
16
18
  {
17
- erp_account_id: @account.id,
19
+ account_id: bling_api.account.id,
18
20
  sku: sku,
19
21
  external_id: product["id"].to_s,
20
22
  name: product["nome"]
@@ -26,8 +28,9 @@ module Bling
26
28
  retry if (attempts += 1) < 5
27
29
  end
28
30
  end.compact.uniq
29
- ErpProduct.upsert_all(products, unique_by: :sku)
30
- Spree::Bus.publish :erp_products_updated, products: hash_products
31
+ SolidusBling::Product.upsert_all(products, unique_by: :sku)
32
+ Spree::Bus.publish :bling_products_updated, products: hash_products
31
33
  end
34
+
32
35
  end
33
- end
36
+ end
@@ -0,0 +1,5 @@
1
+ module SolidusBling
2
+ class Seller < ApplicationRecord
3
+ belongs_to :account
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module SolidusBling
2
+ class ShippingMethod < ApplicationRecord
3
+ belongs_to :account
4
+ end
5
+ end
@@ -1,4 +1,4 @@
1
- module Bling
1
+ module SolidusBling
2
2
  class Subscriber
3
3
  include Omnes::Subscriber
4
4
 
@@ -6,27 +6,27 @@ module Bling
6
6
  with: :send_order_to_bling,
7
7
  id: :send_order_to_bling
8
8
 
9
- handle :erp_products_updated,
9
+ handle :bling_products_updated,
10
10
  with: :products_updated,
11
11
  id: :products_updated
12
12
 
13
- handle :erp_contato_created,
13
+ handle :bling_contact_created,
14
14
  with: :contato_created,
15
15
  id: :contato_created
16
16
 
17
17
  def send_order_to_bling event, debug_info = {}
18
18
  method = __method__.to_s
19
- ErpOrderJob.perform_later(event: event.payload, method_name: method)
19
+ SolidusBling::OrderJob.perform_later(event: event.payload, method_name: method)
20
20
  end
21
21
 
22
22
  def contato_created event, debug_info = {}
23
23
  method = __method__.to_s
24
- ErpContactJob.perform_later(event: event.payload, method_name: method)
24
+ SolidusBling::ContactJob.perform_later(event: event.payload, method_name: method)
25
25
  end
26
26
 
27
27
  def products_updated event, debug_info = {}
28
28
  method = __method__.to_s
29
- ErpProductJob.perform_later(event: event.payload, method_name: method)
29
+ SolidusBling::ProductJob.perform_later(event: event.payload, method_name: method)
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,68 @@
1
+ <%= form_with model: bling_account, url: form_url do |form| %>
2
+ <div>
3
+ <%= form.label :app_name, "Nome do Aplicativo: " %>
4
+ <%= form.text_field :app_name %>
5
+ </div>
6
+
7
+ <div>
8
+ <%= form.label :client_id, "Client ID: " %>
9
+ <%= form.text_field :client_id %>
10
+ </div>
11
+
12
+ <div>
13
+ <%= form.label :client_secret, "Client Secret: " %>
14
+ <%= form.text_field :client_secret %>
15
+ </div>
16
+
17
+ <div>
18
+ <%= form.label :refresh_token, "Refresh Token: " %>
19
+ <%= form.text_field :refresh_token %>
20
+ </div>
21
+
22
+ <div>
23
+ <%= form.label :api_base_url, "API URL: " %>
24
+ <%= form.text_field :api_base_url %>
25
+ </div>
26
+
27
+ <div>
28
+ <%= form.label :external_store_id, "ID da Loja no Bling: " %>
29
+ <%= form.text_field :external_store_id %>
30
+ </div>
31
+
32
+ <div>
33
+ <%= form.label :incoming_category_id, "ID da Categoria de Receita: " %>
34
+ <%= form.text_field :incoming_category_id %>
35
+ </div>
36
+
37
+ <div>
38
+ <% bling_account.payment_methods.each do |payment_method| %>
39
+ <%= form.fields_for :payment_methods do |payment_form| %>
40
+ <%= payment_form.select :spree_payment_method_id, @payment_methods %>
41
+ <%= payment_form.text_field :external_id %>
42
+ <% end %>
43
+ <% end %>
44
+ </div>
45
+
46
+ <div>
47
+ <% bling_account.shipping_methods.each do |shipping_method| %>
48
+ <%= form.fields_for :shipping_methods do |shipping_form| %>
49
+ <%= shipping_form.select :spree_shipping_method_id, @shipping_methods %>
50
+ <%= shipping_form.text_field :alias %>
51
+ <% end %>
52
+ <% end %>
53
+ </div>
54
+
55
+ <div>
56
+ <% bling_account.sellers.each do |seller| %>
57
+ <%= form.fields_for :sellers do |seller_form| %>
58
+ <%= seller_form.label :name, "Nome do Vendedor: " %>
59
+ <%= seller_form.text_field :name %>
60
+
61
+ <%= seller_form.label :external_id, "ID do Vendedor: " %>
62
+ <%= seller_form.text_field :external_id %>
63
+ <% end %>
64
+ <% end %>
65
+ </div>
66
+
67
+ <%= form.submit "Salvar" %>
68
+ <% end %>
@@ -0,0 +1,2 @@
1
+
2
+ <%= render "form", bling_account: @bling_account, form_url: admin_account_path(@bling_account.id) %>
@@ -0,0 +1,13 @@
1
+ <%= link_to "Criar", new_admin_account_path %>
2
+ <div>
3
+ <% @accounts.each do |account| %>
4
+ <div>
5
+ <div>
6
+ Nome do aplicativo:
7
+ <span><%= account.app_name %></span>
8
+ </div>
9
+ <%= link_to "Editar", edit_admin_account_path(account.id) %>
10
+ <%= link_to "Excluir", admin_account_path(account.id), method: :delete %>
11
+ </div>
12
+ <% end %>
13
+ </div>
@@ -0,0 +1,2 @@
1
+
2
+ <%= render "form", bling_account: @bling_account, form_url: admin_accounts_path %>
@@ -0,0 +1,8 @@
1
+ <div>
2
+ <div>
3
+ Nome do aplicativo:
4
+ <span><%= @bling_account.app_name %></span>
5
+ </div>
6
+ <%= link_to "Editar", edit_admin_account_path(@bling_account.id) %>
7
+ <%= link_to "Excluir", admin_account_path(@bling_account.id), method: :delete %>
8
+ </div>
data/config/routes.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Spree::Core::Engine.routes.draw do
4
- # Add your extension routes here
4
+ namespace :admin do
5
+ resources :accounts
6
+ end
5
7
  end
@@ -1,6 +1,6 @@
1
- class CreateErpAccounts < ActiveRecord::Migration[7.0]
1
+ class CreateSolidusBlingAccounts < ActiveRecord::Migration[7.0]
2
2
  def change
3
- create_table :erp_accounts do |t|
3
+ create_table :solidus_bling_accounts do |t|
4
4
  t.string :name
5
5
  t.string :app_name
6
6
  t.string :api_base_url
@@ -9,6 +9,8 @@ class CreateErpAccounts < ActiveRecord::Migration[7.0]
9
9
  t.string :client_secret
10
10
  t.string :refresh_token
11
11
  t.string :access_token
12
+ t.string :external_store_id
13
+ t.string :incoming_category_id
12
14
  t.datetime :token_expires_in
13
15
 
14
16
  t.timestamps
@@ -0,0 +1,5 @@
1
+ class AddBlingOrderIdToSpreeOrders < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :spree_orders, :bling_order_id, :string, default: nil
4
+ end
5
+ end
@@ -1,10 +1,10 @@
1
- class CreateErpProducts < ActiveRecord::Migration[7.0]
1
+ class CreateSolidusBlingProducts < ActiveRecord::Migration[7.0]
2
2
  def change
3
- create_table :erp_products do |t|
3
+ create_table :solidus_bling_products do |t|
4
4
  t.string :external_id, null: false, index: { unique: true }
5
5
  t.string :name, null: false
6
6
  t.string :sku, null: false, index: { unique: true }
7
- t.references :erp_account, null: false, foreign_key: true
7
+ t.integer :account_id, null: false, index: true
8
8
 
9
9
  t.timestamps
10
10
  end
@@ -1,6 +1,6 @@
1
- class CreateErpEvents < ActiveRecord::Migration[7.0]
1
+ class CreateSolidusBlingEvents < ActiveRecord::Migration[7.0]
2
2
  def change
3
- create_table :erp_events do |t|
3
+ create_table :solidus_bling_events do |t|
4
4
  t.string :internal_id
5
5
  t.string :status
6
6
  t.string :message
@@ -0,0 +1,13 @@
1
+ class CreateSolidusBlingPaymentMethods < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :solidus_bling_payment_methods do |t|
4
+ t.string :external_id
5
+ t.integer :account_id, null: false, index: true
6
+ t.integer :spree_payment_method_id, null: false, index: true
7
+
8
+ t.timestamps
9
+
10
+ t.foreign_key :spree_payment_methods, column: :spree_payment_method_id
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSolidusBlingSellers < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :solidus_bling_sellers do |t|
4
+ t.string :name
5
+ t.string :external_id
6
+ t.integer :account_id, null: false, index: true
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ class CreateSolidusBlingShippingMethods < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :solidus_bling_shipping_methods do |t|
4
+ t.string :alias
5
+ t.integer :account_id, null: false, index: true, index: { name: "solidus_bling_shipping_account_index" }
6
+ t.integer :spree_shipping_method_id, null: false, index: true, index: { name: "solidus_bling_spree_shipping_index" }
7
+
8
+ t.timestamps
9
+
10
+ t.foreign_key :spree_shipping_methods, column: :spree_shipping_method_id
11
+ end
12
+ end
13
+ end
@@ -3,9 +3,4 @@
3
3
  SolidusBling.configure do |config|
4
4
  # TODO: Remember to change this with the actual preferences you have implemented!
5
5
  # config.sample_preference = 'sample_value'
6
- config.app_name = ''
7
- config.store_id = ''
8
- config.seller_id = ''
9
- config.payment_type_id = ''
10
- config.category_id = ''
11
6
  end
@@ -2,7 +2,9 @@
2
2
 
3
3
  module SolidusBling
4
4
  class Configuration
5
- attr_accessor :app_name, :store_id, :seller_id, :payment_type_id, :category_id
5
+ # Define here the settings for this extension, e.g.:
6
+ #
7
+ # attr_accessor :my_setting
6
8
  end
7
9
 
8
10
  class << self
@@ -7,19 +7,32 @@ module SolidusBling
7
7
  class Engine < Rails::Engine
8
8
  include SolidusSupport::EngineExtensions
9
9
 
10
- isolate_namespace ::Spree
10
+ isolate_namespace SolidusBling
11
11
 
12
12
  engine_name 'solidus_bling'
13
13
 
14
+ initializer 'solidus_bling.configure_backend' do
15
+ next unless ::Spree::Backend::Config.respond_to?(:menu_items)
16
+
17
+ ::Spree::Backend::Config.configure do |config|
18
+ config.menu_items << config.class::MenuItem.new(
19
+ [:bling_account],
20
+ 'exclamation',
21
+ url: :admin_accounts_path,
22
+ condition: -> { can?(:manage, Spree::Store) }
23
+ )
24
+ end
25
+ end
26
+
14
27
  # use rspec for tests
15
28
  config.generators do |g|
16
29
  g.test_framework :rspec
17
30
  end
18
31
 
19
32
  config.to_prepare do
20
- Spree::Bus.register(:erp_products_updated)
21
- Spree::Bus.register(:erp_contato_created)
22
- Bling::Subscriber.new.subscribe_to(Spree::Bus)
33
+ Spree::Bus.register(:bling_products_updated)
34
+ Spree::Bus.register(:bling_contact_created)
35
+ SolidusBling::Subscriber.new.subscribe_to(Spree::Bus)
23
36
  end
24
37
 
25
38
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusBling
4
- VERSION = '1.0.3'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -34,4 +34,10 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency 'solidus_brazilian_adaptations'
35
35
 
36
36
  spec.add_development_dependency 'solidus_dev_support', '~> 2.7'
37
+
38
+ spec.post_install_message = <<-MESSAGE
39
+ To complete the installation, please run:
40
+ bin/rails g solidus_brazilian_adaptations:install
41
+ MESSAGE
42
+
37
43
  end
@@ -0,0 +1,17 @@
1
+ require 'rails_helper'
2
+
3
+ # Specs in this file have access to a helper object that includes
4
+ # the AccountsHelper. For example:
5
+ #
6
+ # describe AccountsHelper do
7
+ # describe "string concat" do
8
+ # it "concats two strings with spaces" do
9
+ # expect(helper.concat_strings("this","that")).to eq("this that")
10
+ # end
11
+ # end
12
+ # end
13
+ module SolidusBling
14
+ RSpec.describe AccountsHelper, type: :helper do
15
+ pending "add some examples to (or delete) #{__FILE__}"
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe ContactJob, type: :job do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe OrderJob, type: :job do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe ProductJob, type: :job do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe Account, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe Event, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe PaymentMethod, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe Product, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe Seller, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusBling
4
+ RSpec.describe ShippingMethod, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "Accounts", type: :request do
4
+ describe "GET /index" do
5
+ pending "add some examples (or delete) #{__FILE__}"
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_bling
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ulysses-bull
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-12 00:00:00.000000000 Z
11
+ date: 2023-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -108,18 +108,27 @@ files:
108
108
  - app/assets/javascripts/spree/frontend/solidus_bling.js
109
109
  - app/assets/stylesheets/spree/backend/solidus_bling.css
110
110
  - app/assets/stylesheets/spree/frontend/solidus_bling.css
111
- - app/jobs/application_job.rb
112
- - app/jobs/erp_contact_job.rb
113
- - app/jobs/erp_order_job.rb
114
- - app/jobs/erp_product_job.rb
115
- - app/models/bling/api.rb
116
- - app/models/bling/contato.rb
117
- - app/models/bling/pedido.rb
118
- - app/models/bling/produto.rb
119
- - app/models/erp_account.rb
120
- - app/models/erp_event.rb
121
- - app/models/erp_product.rb
122
- - app/subscribers/bling/subscriber.rb
111
+ - app/controllers/spree/admin/accounts_controller.rb
112
+ - app/helpers/solidus_bling/accounts_helper.rb
113
+ - app/jobs/solidus_bling/application_job.rb
114
+ - app/jobs/solidus_bling/contact_job.rb
115
+ - app/jobs/solidus_bling/order_job.rb
116
+ - app/jobs/solidus_bling/product_job.rb
117
+ - app/models/solidus_bling/account.rb
118
+ - app/models/solidus_bling/api.rb
119
+ - app/models/solidus_bling/contact.rb
120
+ - app/models/solidus_bling/event.rb
121
+ - app/models/solidus_bling/order.rb
122
+ - app/models/solidus_bling/payment_method.rb
123
+ - app/models/solidus_bling/product.rb
124
+ - app/models/solidus_bling/seller.rb
125
+ - app/models/solidus_bling/shipping_method.rb
126
+ - app/subscribers/solidus_bling/subscriber.rb
127
+ - app/views/spree/admin/accounts/_form.html.erb
128
+ - app/views/spree/admin/accounts/edit.html.erb
129
+ - app/views/spree/admin/accounts/index.html.erb
130
+ - app/views/spree/admin/accounts/new.html.erb
131
+ - app/views/spree/admin/accounts/show.html.erb
123
132
  - bin/console
124
133
  - bin/rails
125
134
  - bin/rails-engine
@@ -127,13 +136,15 @@ files:
127
136
  - bin/rake
128
137
  - bin/sandbox
129
138
  - bin/setup
130
- - config/initializers/solidus_bling.rb
131
139
  - config/locales/en.yml
132
140
  - config/routes.rb
133
- - db/migrate/20230906141624_create_erp_accounts.rb
134
- - db/migrate/20230906141728_add_erp_id_to_spree_orders.rb
135
- - db/migrate/20230906142108_create_erp_products.rb
136
- - db/migrate/20230906142806_create_erp_events.rb
141
+ - db/migrate/20230918141419_create_solidus_bling_accounts.rb
142
+ - db/migrate/20230918141505_add_bling_order_id_to_spree_orders.rb
143
+ - db/migrate/20230918141700_create_solidus_bling_products.rb
144
+ - db/migrate/20230918142547_create_solidus_bling_events.rb
145
+ - db/migrate/20230918142742_create_solidus_bling_payment_methods.rb
146
+ - db/migrate/20230918142953_create_solidus_bling_sellers.rb
147
+ - db/migrate/20230918143057_create_solidus_bling_shipping_methods.rb
137
148
  - lib/generators/solidus_bling/install/install_generator.rb
138
149
  - lib/generators/solidus_bling/install/templates/initializer.rb
139
150
  - lib/solidus_bling.rb
@@ -142,12 +153,17 @@ files:
142
153
  - lib/solidus_bling/testing_support/factories.rb
143
154
  - lib/solidus_bling/version.rb
144
155
  - solidus_bling.gemspec
145
- - spec/jobs/erp_contact_job_spec.rb
146
- - spec/jobs/erp_order_job_spec.rb
147
- - spec/jobs/erp_product_job_spec.rb
148
- - spec/models/erp_account_spec.rb
149
- - spec/models/erp_event_spec.rb
150
- - spec/models/erp_product_spec.rb
156
+ - spec/helpers/solidus_bling/accounts_helper_spec.rb
157
+ - spec/jobs/solidus_bling/contact_job_spec.rb
158
+ - spec/jobs/solidus_bling/order_job_spec.rb
159
+ - spec/jobs/solidus_bling/product_job_spec.rb
160
+ - spec/models/solidus_bling/account_spec.rb
161
+ - spec/models/solidus_bling/event_spec.rb
162
+ - spec/models/solidus_bling/payment_method_spec.rb
163
+ - spec/models/solidus_bling/product_spec.rb
164
+ - spec/models/solidus_bling/seller_spec.rb
165
+ - spec/models/solidus_bling/shipping_method_spec.rb
166
+ - spec/requests/solidus_bling/accounts_spec.rb
151
167
  - spec/spec_helper.rb
152
168
  homepage: https://github.com/ulysses-bull/solidus_bling#readme
153
169
  licenses:
@@ -156,7 +172,9 @@ metadata:
156
172
  homepage_uri: https://github.com/ulysses-bull/solidus_bling#readme
157
173
  source_code_uri: https://github.com/ulysses-bull/solidus_bling
158
174
  changelog_uri: https://github.com/ulysses-bull/solidus_bling/blob/master/CHANGELOG.md
159
- post_install_message:
175
+ post_install_message: |2
176
+ To complete the installation, please run:
177
+ bin/rails g solidus_brazilian_adaptations:install
160
178
  rdoc_options: []
161
179
  require_paths:
162
180
  - lib
@@ -179,10 +197,15 @@ signing_key:
179
197
  specification_version: 4
180
198
  summary: Solidus extension to integrate with the Bling
181
199
  test_files:
182
- - spec/jobs/erp_contact_job_spec.rb
183
- - spec/jobs/erp_order_job_spec.rb
184
- - spec/jobs/erp_product_job_spec.rb
185
- - spec/models/erp_account_spec.rb
186
- - spec/models/erp_event_spec.rb
187
- - spec/models/erp_product_spec.rb
200
+ - spec/helpers/solidus_bling/accounts_helper_spec.rb
201
+ - spec/jobs/solidus_bling/contact_job_spec.rb
202
+ - spec/jobs/solidus_bling/order_job_spec.rb
203
+ - spec/jobs/solidus_bling/product_job_spec.rb
204
+ - spec/models/solidus_bling/account_spec.rb
205
+ - spec/models/solidus_bling/event_spec.rb
206
+ - spec/models/solidus_bling/payment_method_spec.rb
207
+ - spec/models/solidus_bling/product_spec.rb
208
+ - spec/models/solidus_bling/seller_spec.rb
209
+ - spec/models/solidus_bling/shipping_method_spec.rb
210
+ - spec/requests/solidus_bling/accounts_spec.rb
188
211
  - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- class ApplicationJob < ActiveJob::Base
2
- # Automatically retry jobs that encountered a deadlock
3
- # retry_on ActiveRecord::Deadlocked
4
-
5
- # Most jobs are safe to ignore if the underlying records are no longer available
6
- # discard_on ActiveJob::DeserializationError
7
- end
@@ -1,20 +0,0 @@
1
- class ErpContactJob < ApplicationJob
2
- queue_as :default
3
-
4
- def perform event:, method_name:
5
- erp_event = ErpEvent.new
6
- contact = event[:args][:contato]
7
- order = event[:args][:order]
8
- erp_event.internal_id = order.id
9
- erp_event.name = "event.omnes_event_name"
10
- erp_event.method = method_name
11
- if contact.include? "error"
12
- erp_event.status = 'error'
13
- erp_event.message = contact["error"]["description"]
14
- else
15
- erp_event.status = 'success'
16
- end
17
- erp_event.body = order.to_json
18
- erp_event.save!
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- class ErpOrderJob < ApplicationJob
2
- queue_as :default
3
-
4
- def perform event:, method_name:
5
- erp_event = ErpEvent.new
6
- order = event[:order]
7
- erp_event.internal_id = order.id
8
- erp_event.method = method_name
9
- begin
10
- Bling::Pedido.new(order).send
11
- erp_event.status = 'success'
12
- rescue => e
13
- erp_event.status = 'error'
14
- erp_event.message = e.to_json
15
- ensure
16
- erp_event.body = order.to_json
17
- erp_event.save!
18
- end
19
- end
20
- end
@@ -1,11 +0,0 @@
1
- class ErpProductJob < ApplicationJob
2
- queue_as :default
3
-
4
- def perform event:, method_name:
5
- erp_event = ErpEvent.new
6
- products = event[:products]
7
- erp_event.method = method_name
8
- erp_event.body = products.to_json
9
- erp_event.save!
10
- end
11
- end
@@ -1,3 +0,0 @@
1
- class ErpAccount < ApplicationRecord
2
- has_many :products, foreign_key: 'erp_account_id', class_name: 'ErpProduct'
3
- end
@@ -1,2 +0,0 @@
1
- class ErpEvent < ApplicationRecord
2
- end
@@ -1,3 +0,0 @@
1
- class ErpProduct < ApplicationRecord
2
- belongs_to :erp_account
3
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- SolidusBling.configure do |config|
4
- # TODO: Remember to change this with the actual preferences you have implemented!
5
- # config.sample_preference = 'sample_value'
6
- config.app_name = ''
7
- config.store_id = ''
8
- config.seller_id = ''
9
- config.payment_type_id = ''
10
- config.category_id = ''
11
- end
@@ -1,5 +0,0 @@
1
- class AddErpIdToSpreeOrders < ActiveRecord::Migration[7.0]
2
- def change
3
- add_column :spree_orders, :erp_order_id, :string, default: nil
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpContactJob, type: :job do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpOrderJob, type: :job do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpProductJob, type: :job do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpAccount, type: :model do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpEvent, type: :model do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end
@@ -1,5 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe ErpProduct, type: :model do
4
- pending "add some examples to (or delete) #{__FILE__}"
5
- end