Skip to content

Commit b593edb

Browse files
Merge pull request #47 from Purple-Stock/staging
Staging
2 parents a2aaaea + 65e2047 commit b593edb

27 files changed

+456
-58
lines changed

app/jobs/order_items_job.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class OrderItemsJob < ApplicationJob
2+
queue_as :default
3+
retry_on StandardError, attempts: 5, wait: :exponentially_longer
4+
5+
def perform(record)
6+
return unless record.items.empty?
7+
8+
account_id = record.account_id
9+
items_attributes = []
10+
order = Services::Bling::FindOrder.call(id: record.bling_order_id, order_command: 'find_order', tenant: account_id)
11+
raise StandardError if order['error'].present?
12+
13+
order['data']['itens'].each do |item|
14+
items_attributes << {
15+
sku: item['codigo'],
16+
unity: item['unidade'],
17+
quantity: item['quantidade'],
18+
discount: item['desconto'],
19+
value: item['valor'],
20+
ipi_tax: item['aliquotaIPI'],
21+
description: item['descricao'],
22+
long_description: item['descricaoDetalhada'],
23+
product_id: item['produto']['id'],
24+
account_id:
25+
}
26+
end
27+
28+
record.items.build(items_attributes)
29+
record.save!
30+
end
31+
end

app/models/bling_order_item.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ class BlingOrderItem < ApplicationRecord
3333
# TODO, refactor me separating the tables
3434
# There are features hard to implement without this separation.
3535

36+
has_many :items, dependent: :destroy
3637
belongs_to :account, optional: true
3738

39+
accepts_nested_attributes_for :items
40+
41+
after_create :synchronize_items
42+
3843
STORE_ID_NAME_KEY_VALUE = {
3944
'204219105' => 'Shein',
4045
'203737982' => 'Shopee',
@@ -93,7 +98,6 @@ class Status
9398
where(store_id: '204061683')
9499
}
95100

96-
97101
scope :date_range, lambda { |initial_date, final_date|
98102
initial_date = initial_date.try(:to_date).try(:beginning_of_day)
99103
final_date = final_date.try(:to_date).try(:end_of_day)
@@ -127,4 +131,8 @@ def store_name
127131
def value
128132
super || 0.0
129133
end
134+
135+
def synchronize_items
136+
OrderItemsJob.perform_later(self)
137+
end
130138
end

app/models/item.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# == Schema Information
2+
#
3+
# Table name: items
4+
#
5+
# id :bigint not null, primary key
6+
# description :string
7+
# discount :decimal(, )
8+
# ipi_tax :decimal(, )
9+
# long_description :string
10+
# quantity :integer
11+
# sku :string
12+
# unity :integer
13+
# value :decimal(, )
14+
# created_at :datetime not null
15+
# updated_at :datetime not null
16+
# account_id :integer
17+
# bling_order_item_id :bigint
18+
# product_id :bigint
19+
#
20+
class Item < ApplicationRecord
21+
belongs_to :account
22+
belongs_to :bling_order_item
23+
end

app/models/stock.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def self.filter_by_total_balance_situation(balance_situation = nil)
3030
def self.filter_by_status(status_number = nil)
3131
return all if status_number.blank?
3232

33-
joins(:product).where(product: { active: status_number })
33+
joins(:product).where(products: { active: status_number })
3434
end
3535

3636
def self.only_positive_price(query = false)

app/views/products/show.html.erb

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,62 @@
1-
<%- model_class = Product -%>
2-
<div class="section-header">
3-
<div class="page-header">
4-
<h1><%= t 'products.two', :default => model_class.model_name.human.titleize %></h1>
5-
</div>
6-
</div>
7-
8-
<div class="section-body">
9-
<div class="card">
10-
<div class="card-body">
11-
12-
<dl class="dl-horizontal">
13-
<dt><strong><%= model_class.human_attribute_name(:custom_id) %>:</strong></dt>
14-
<dd><%= @product.custom_id %></dd>
15-
<dt><strong><%= model_class.human_attribute_name(:sku) %>:</strong></dt>
16-
<dd><%= @product.sku %></dd>
17-
<dt><strong><%= t('products.name') %>:</strong></dt>
18-
<dd><%= @product.name %></dd>
19-
<dt><strong><%= t('products.price') %>:</strong></dt>
20-
<dd><%= @product.price %></dd>
21-
<dt><strong><%= t('products.entry') %>:</strong></dt>
22-
<dd><%= Services::Product::CountQuantity.call(product: @product, product_command: 'purchase_product') %></dd>
23-
<dt><strong><%= t('products.exit') %>:</strong></dt>
24-
<dd><%= Services::Product::CountQuantity.call(product: @product, product_command: 'sale_product') %></dd>
25-
<dt><strong><%= t('products.balance') %>:</strong></dt>
26-
<dd><%= Services::Product::CountQuantity.call(product: @product, product_command: 'balance_product') %></dd>
27-
<dt><strong><%= t('products.rq_code') %></strong></dt>
28-
<dd><%= image_tag(Services::Product::GenerateQrCode.call(product: @product)) %></dd>
29-
<dt><strong><%= t('products.image') %>:</strong></dt>
30-
<dd><%= image_tag(@product.image.attached? ? @product.image : "https://purple-stock.s3-sa-east-1.amazonaws.com/images.png", size: "150") %></dd>
31-
<dt><strong><%= t('products.highlight') %>:</strong></dt>
32-
<dd><%= @product.highlight %></dd>
33-
<dt><strong><%= t('products.category') %>:</strong></dt>
34-
<dd><%= @product.category.try(:name) %></dd>
35-
<dt><strong><%= t('products.active') %>:</strong></dt>
36-
<dd><%= @product.active %></dd>
37-
38-
</dl>
39-
40-
<%= link_to t('.back', :default => t("helpers.links.back")),
41-
products_path, :class => 'btn btn-default' %>
1+
<section class="main-content">
2+
<div class="row">
3+
<div class="col-md-12">
4+
<h1 class="display-4"><%= @product.name %></h1>
5+
<hr>
6+
<table>
7+
<!-- What to do with image? <dd class="col-sm-9"><%= image_tag(@product.image.attached? ? @product.image : "https://purple-stock.s3-sa-east-1.amazonaws.com/images.png", size: "150") %></dd> -->
8+
<tr class="row">
9+
<td><%= image_tag(Services::Product::GenerateQrCode.call(product: @product)) %></td>
10+
<td class="mt-md-5">
11+
<table>
12+
<tr>
13+
<th class="col"><%= t('activerecord.attributes.product.custom_id') %></th>
14+
<td class="col"><%= @product.custom_id %></td>
15+
</tr>
16+
<tr>
17+
<th class="col"><%= t('activerecord.attributes.product.sku') %></th>
18+
<td class="col"><%= @product.sku %></td>
19+
</tr>
20+
<tr>
21+
<th class="col"><%= t('products.price') %></th>
22+
<td class="col"><%= @product.price %></td>
23+
</tr>
24+
<tr>
25+
<th class="col"><%= t('products.entry') %></th>
26+
<td class="col"><%= Services::Product::CountQuantity.call(product: @product, product_command: 'purchase_product') %></td>
27+
</tr>
28+
<tr>
29+
<th class="col"><%= t('products.exit') %></th>
30+
<td class="col"><%= Services::Product::CountQuantity.call(product: @product, product_command: 'sale_product') %></td>
31+
</tr>
32+
<tr>
33+
<th class="col"><%= t('products.balance') %></th>
34+
<td class="col"><%= Services::Product::CountQuantity.call(product: @product, product_command: 'balance_product') %></td>
35+
</tr>
36+
<tr>
37+
<th class="col"><%= t('products.highlight') %></th>
38+
<td class="col"><%= @product.highlight %></td>
39+
</tr>
40+
<tr>
41+
<th class="col"><%= t('products.category') %></th>
42+
<td class="col"><%= @product.category.try(:name) %></td>
43+
</tr>
44+
<tr>
45+
<th class="col"><%= t('products.active') %>:</th>
46+
<td class="col"><%= @product.active %></td>
47+
</tr>
48+
</table>
49+
</td>
50+
</tr>
51+
</table>
52+
<%= link_to t('helpers.links.back'), products_path, class: 'btn btn-primary btn-lg' %>
4253
<%= link_to t('.edit', :default => t("helpers.links.edit")),
43-
edit_product_path(@product), :class => 'btn btn-default' %>
54+
edit_product_path(@product), :class => 'btn btn-dark btn-lg' %>
4455
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
45-
product_path(@product),
46-
title: t('destroy'), class: 'btn btn-danger',
47-
method: :delete,
56+
product_path(@product),
57+
title: t('destroy'), class: 'btn btn-danger btn-lg',
58+
method: :delete,
4859
data: { toggle: 'tooltip', turbo: true, turbo_method: :delete, turbo_confirm: t('confirm') } %>
4960
</div>
5061
</div>
51-
</div>
62+
</section>

config/environments/development.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
# config.action_cable.disable_request_forgery_protection = true
7474
config.good_job.smaller_number_is_higher_priority = true
7575

76-
config.good_job.enable_cron = false
76+
config.good_job.enable_cron = ENV['ENABLE_CRON'] || false
7777
config.good_job.cron = {
7878
product_sync_job: {
7979
cron: "*/10 * * * *",

config/environments/staging.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
# Use a real queuing backend for Active Job (and separate queues per environment).
6464
# config.active_job.queue_adapter = :resque
6565
# config.active_job.queue_name_prefix = "awesome_rails_production"
66-
config.active_job.queue_adapter = :async
6766
# config.action_controller.asset_host = ENV['CLOUDFRONT_URL']
6867
config.cache_store = :redis_cache_store, { url: ENV['REDISCLOUD_URL'] }
6968

@@ -96,7 +95,10 @@
9695
# Do not dump schema after migrations.
9796
config.active_record.dump_schema_after_migration = false
9897

99-
config.good_job.enable_cron = false
98+
config.good_job.smaller_number_is_higher_priority = true
99+
config.good_job.execution_mode = :async
100+
101+
config.good_job.enable_cron = ENV['ENABLE_CRON'] || false
100102
config.good_job.cron = {
101103
product_sync_job: {
102104
cron: "*/10 * * * *",
@@ -119,7 +121,7 @@
119121
class: "PendingOrderItemsJob",
120122
args: [1, { dataInicial: (Date.today - 3.weeks).strftime, dataFinal: Date.today.strftime }],
121123
set: { priority: 1 },
122-
description: "Create Order Items with pending status from current week"
124+
description: "Create Order Items with pending status considering 3 week ago"
123125
},
124126

125127
general_pending_order_items_task: {
@@ -145,6 +147,7 @@
145147
set: { priority: 1 }, # additional Active Job properties; can also be a lambda/proc e.g. `-> { { priority: [1,2].sample } }`
146148
description: "Create Order Items statuses are checked and verified" # optional description that appears in Dashboard
147149
},
150+
148151
general_canceled_order_items_task: { # each recurring job must have a unique key
149152
cron: "@monthly", # cron-style scheduling format by fugit gem
150153
class: "CanceledBlingOrderItemsJob", # name of the job class as a String; must reference an Active Job job class
@@ -174,7 +177,7 @@
174177
class: "CheckedBlingOrderItemsJob",
175178
args: [1],
176179
set: { priority: 3 },
177-
description: "Create Order Items statuses are checked"
180+
description: "Create Order Items whose statuses are checked"
178181
},
179182

180183
frequent_checked_order_items_task: {

config/locales/pt-BR.models.products.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pt-BR:
2323
bling:
2424
true: Sim
2525
false: Não
26+
custom_id: ID do Cliente
2627

2728
products:
2829
one: Produto
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CreateItems < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :items do |t|
4+
t.string :sku
5+
t.integer :unity
6+
t.integer :quantity
7+
t.decimal :discount
8+
t.decimal :value
9+
t.decimal :ipi_tax
10+
t.string :description
11+
t.string :long_description
12+
t.bigint :product_id
13+
t.integer :account_id
14+
t.bigint :bling_order_item_id
15+
16+
t.timestamps
17+
end
18+
end
19+
end

db/schema.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2023_12_21_234141) do
13+
ActiveRecord::Schema[7.0].define(version: 2024_01_03_204520) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pgcrypto"
1616
enable_extension "plpgsql"
@@ -211,6 +211,22 @@
211211
t.integer "months", default: 1
212212
end
213213

214+
create_table "items", force: :cascade do |t|
215+
t.string "sku"
216+
t.integer "unity"
217+
t.integer "quantity"
218+
t.decimal "discount"
219+
t.decimal "value"
220+
t.decimal "ipi_tax"
221+
t.string "description"
222+
t.string "long_description"
223+
t.bigint "product_id"
224+
t.integer "account_id"
225+
t.bigint "bling_order_item_id"
226+
t.datetime "created_at", null: false
227+
t.datetime "updated_at", null: false
228+
end
229+
214230
create_table "post_data", force: :cascade do |t|
215231
t.string "client_name"
216232
t.string "cep"

spec/factories/items.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# == Schema Information
2+
#
3+
# Table name: items
4+
#
5+
# id :bigint not null, primary key
6+
# description :string
7+
# discount :decimal(, )
8+
# ipi_tax :decimal(, )
9+
# long_description :string
10+
# quantity :integer
11+
# sku :string
12+
# unity :integer
13+
# value :decimal(, )
14+
# created_at :datetime not null
15+
# updated_at :datetime not null
16+
# account_id :integer
17+
# bling_order_item_id :bigint
18+
# product_id :bigint
19+
#
20+
FactoryBot.define do
21+
factory :item do
22+
sku { "MyString" }
23+
unity { 1 }
24+
quantity { 1 }
25+
discount { "9.99" }
26+
value { "9.99" }
27+
ipi_tax { "9.99" }
28+
description { "MyString" }
29+
long_description { "MyString" }
30+
product_id { 1 }
31+
account_id { 1 }
32+
end
33+
end

spec/jobs/bling_order_item_creator_job_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
describe '#perform_now' do
99
before do
10+
allow_any_instance_of(BlingOrderItem).to receive(:synchronize_items).and_return(true)
1011
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.now + 2.day)
1112
allow(subject).to receive(:list_status_situation).and_return([15])
1213
end

spec/jobs/canceled_bling_order_item_job_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
describe '#perform_now' do
99
before do
10+
allow_any_instance_of(BlingOrderItem).to receive(:synchronize_items).and_return(true)
1011
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.now + 2.day)
1112
end
1213

spec/jobs/checked_bling_order_items_job_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
describe '#perform' do
1010
before do
11+
allow_any_instance_of(BlingOrderItem).to receive(:synchronize_items).and_return(true)
1112
allow(Date).to receive(:today).and_return Date.new(2023, 11, 15)
1213
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.zone.now + 2.days)
1314
end

spec/jobs/current_done_bling_order_item_job_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
describe '#perform_now' do
99
before do
10+
allow_any_instance_of(BlingOrderItem).to receive(:synchronize_items).and_return(true)
1011
allow(Date).to receive(:today).and_return Date.new(2023, 11, 9)
1112
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.now + 2.day)
1213
end

spec/jobs/daily_canceled_order_job_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
describe '#perform' do
1313
before do
14+
allow_any_instance_of(BlingOrderItem).to receive(:synchronize_items).and_return(true)
1415
allow(Date).to receive(:today).and_return Date.new(2023, 12, 14)
1516
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.zone.now + 2.days)
1617
FactoryBot.create(:bling_order_item, bling_order_id:, date:, situation_id:)

0 commit comments

Comments
 (0)