Skip to content

Commit

Permalink
Merge pull request #39 from Purple-Stock/feat/add-items-to-count
Browse files Browse the repository at this point in the history
Feat/add items to count
  • Loading branch information
Pauloparakleto authored Jan 3, 2024
2 parents aa679a9 + 28040e6 commit 6255c74
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 2 deletions.
26 changes: 25 additions & 1 deletion app/models/bling_order_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class BlingOrderItem < ApplicationRecord
# TODO, refactor me separating the tables
# There are features hard to implement without this separation.

has_many :items, dependent: :destroy
belongs_to :account, optional: true

accepts_nested_attributes_for :items

STORE_ID_NAME_KEY_VALUE = {
'204219105' => 'Shein',
'203737982' => 'Shopee',
Expand Down Expand Up @@ -93,7 +96,6 @@ class Status
where(store_id: '204061683')
}


scope :date_range, lambda { |initial_date, final_date|
initial_date = initial_date.try(:to_date).try(:beginning_of_day)
final_date = final_date.try(:to_date).try(:end_of_day)
Expand Down Expand Up @@ -127,4 +129,26 @@ def store_name
def value
super || 0.0
end

def synchronize_items
items_attributes = []
order = Services::Bling::FindOrder.new(id: bling_order_id, order_command: 'find_order', tenant: account.id).call
order['data']['itens'].each do |item|
items_attributes << {
sku: item['codigo'],
unity: item['unidade'],
quantity: item['quantidade'],
discount: item['desconto'],
value: item['valor'],
ipi_tax: item['aliquotaIPI'],
description: item['descricao'],
long_description: item['descricaoDetalhada'],
product_id: item['produto']['id'],
account_id: account.id
}
end

items.build(items_attributes)
save
end
end
24 changes: 24 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: items
#
# id :bigint not null, primary key
# description :string
# discount :decimal(, )
# ipi_tax :decimal(, )
# long_description :string
# quantity :integer
# sku :string
# unity :integer
# value :decimal(, )
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer
# bling_order_item_id :bigint
# product_id :bigint
#
class Item < ApplicationRecord
belongs_to :account
belongs_to :product
belongs_to :bling_order_item
end
19 changes: 19 additions & 0 deletions db/migrate/20240103204520_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateItems < ActiveRecord::Migration[7.0]
def change
create_table :items do |t|
t.string :sku
t.integer :unity
t.integer :quantity
t.decimal :discount
t.decimal :value
t.decimal :ipi_tax
t.string :description
t.string :long_description
t.bigint :product_id
t.integer :account_id
t.bigint :bling_order_item_id

t.timestamps
end
end
end
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

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

create_table "items", force: :cascade do |t|
t.string "sku"
t.integer "unity"
t.integer "quantity"
t.decimal "discount"
t.decimal "value"
t.decimal "ipi_tax"
t.string "description"
t.string "long_description"
t.bigint "product_id"
t.integer "account_id"
t.bigint "bling_order_item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "post_data", force: :cascade do |t|
t.string "client_name"
t.string "cep"
Expand Down
30 changes: 30 additions & 0 deletions spec/models/bling_order_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@
require 'rails_helper'

RSpec.describe BlingOrderItem, type: :model do
describe 'associations' do
it { is_expected.to have_many(:items).dependent(:destroy) }
end

describe 'nested attributes' do
it { is_expected.to accept_nested_attributes_for(:items) }
end

describe '#synchronize_items' do
subject(:synchronize_items) { order.synchronize_items }

include_context 'with bling token'

let(:order) { FactoryBot.create(:bling_order_item, bling_order_id: 19_178_587_026, account_id: user.account.id) }

before do
VCR.use_cassette('find_order', erb: true) do
synchronize_items
end
end

it 'counts 1 item' do
expect(order.items.length).to eq(1)
end

it 'has quantity 1' do
expect(order.items.first.quantity).to eq(1)
end
end

describe '#store_name' do
it 'has name' do
subject.store_id = '204219105'
Expand Down
35 changes: 35 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# == Schema Information
#
# Table name: items
#
# id :bigint not null, primary key
# description :string
# discount :decimal(, )
# ipi_tax :decimal(, )
# long_description :string
# quantity :integer
# sku :string
# unity :integer
# value :decimal(, )
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer
# bling_order_item_id :bigint
# product_id :bigint
#
require 'rails_helper'

RSpec.describe Item, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:product) }
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:bling_order_item) }
end

describe 'db columns' do
it { is_expected.to have_db_column(:sku) }
it { is_expected.to have_db_column(:unity) }
it { is_expected.to have_db_column(:value) }
it { is_expected.to have_db_column(:quantity) }
end
end
69 changes: 69 additions & 0 deletions spec/vcr/find_order.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6255c74

Please sign in to comment.