Skip to content

Commit 9f9b932

Browse files
Merge pull request #70 from Purple-Stock/fix/68-under-count-pendings
refactor bling_order_item.rb: task 68
2 parents 2c86fb2 + 4fd24c9 commit 9f9b932

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

app/jobs/bling_order_job_updater.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ def perform(record, account_id)
66
result = Services::Bling::FindOrder.call(id: record.bling_order_id, order_command: 'find_order', tenant: account_id)
77
raise StandardError if result['error'].present?
88

9-
record.update({ value: result['data']['total'], situation_id: result['data']['situacao']['id'] })
9+
record.situation_id = result['data']['situacao']['id']
10+
record.value = result['data']['total']
11+
record.alteration_date = Time.zone.today
12+
return unless record.situation_id_changed?
13+
14+
record.save!
1015
end
1116
end

app/models/bling_order_item.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@ def self.group_order_items(base_query)
119119
def self.update_yourself(self_collection)
120120
account_id = self_collection.first.account_id
121121

122-
self_collection.each do |record|
123-
BlingOrderJobUpdater.perform_later(record, account_id)
122+
self_collection.find_in_batches(batch_size: 100) do |sub_collection|
123+
GoodJob::Bulk.enqueue do
124+
sub_collection.each do |record|
125+
BlingOrderJobUpdater.perform_later(record, account_id)
126+
end
127+
end
128+
sleep 10 if Rails.env.eql?('production') || Rails.env.eql?('staging')
124129
end
125130
end
126131

spec/models/bling_order_item_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@
243243
expect(collection.first.reload.value.to_f).to eq(69.9)
244244
end
245245
end
246+
247+
it 'updates status situation' do
248+
VCR.use_cassette('find_checked_order', erb: true) do
249+
described_class.update_yourself(collection)
250+
expect(collection.first.reload.situation_id.to_i).to eq(BlingOrderItem::Status::IN_PROGRESS)
251+
end
252+
end
253+
254+
it 'updates alteration date' do
255+
VCR.use_cassette('find_checked_order', erb: true) do
256+
described_class.update_yourself(collection)
257+
expect(collection.first.reload.alteration_date.strftime('%Y-%m-%d')).to eq(Time.zone.today.strftime)
258+
end
259+
end
246260
end
247261

248262
context 'when there are too many requests error in the response' do

0 commit comments

Comments
 (0)