Skip to content

Commit 98c3dcf

Browse files
Merge pull request #21 from Purple-Stock/feat/canceled-alteration-date-filter
Feat/canceled alteration date filter
2 parents 532d2ee + 7fd940f commit 98c3dcf

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed

app/jobs/daily_canceled_order_job.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# DailyCanceledOrderJob perform against created orders set previously
4+
# with another status.
5+
# For this reason, we should call this job as an updater job.
6+
# The crucial difference here is the absence of the alteration date in the bling API response.
7+
# It is possible to filter by alteration date, however the alteration date (dataAlteraçãoInicial) is not
8+
# present in the response. Our solution is querying by alteration date assuming it is the same in bling API
9+
# setting this assumed value directly on our database.
10+
# Motives to run this job:
11+
# There are orders status over counted. There are possibilities they became canceled on bling API
12+
class DailyCanceledOrderJob < BlingOrderItemCreatorBaseJob
13+
STATUS = BlingOrderItem::Status::CANCELED.freeze
14+
15+
attr_accessor :account_id
16+
17+
def perform(account_id, initial_alteration_date = nil)
18+
@status = STATUS
19+
@account_id = account_id
20+
@initial_date = initial_alteration_date || Date.today
21+
@final_date = Date.today
22+
date_range = (@initial_date..@final_date)
23+
date_range.each do |alteration_date|
24+
@alteration_date = alteration_date
25+
final_alteration_date = (alteration_date + 1.day).strftime
26+
options = { dataAlteracaoInicial: @alteration_date.strftime, dataAlteracaoFinal: final_alteration_date }
27+
orders = Services::Bling::Order.call(order_command: 'find_orders', tenant: account_id,
28+
situation: STATUS, options: options)
29+
orders = orders['data']
30+
31+
create_orders(orders)
32+
end
33+
end
34+
end

config/environments/development.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@
131131
description: "Create Order Items statuses are canceled" # optional description that appears in Dashboard
132132
},
133133

134+
daily_canceled_order_task: {
135+
cron: "*/10 * * * *",
136+
class: "DailyCanceledOrderJob",
137+
args: [1, Date.today],
138+
set: { priority: 1 },
139+
description: "Create Order Items statuses are canceled at current day"
140+
},
141+
134142
checked_order_items_task: {
135143
cron: "@weekly",
136144
class: "CheckedBlingOrderItemsJob",

config/environments/production.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@
157157
description: "Create Order Items statuses are canceled" # optional description that appears in Dashboard
158158
},
159159

160+
daily_canceled_order_task: {
161+
cron: "*/10 * * * *",
162+
class: "DailyCanceledOrderJob",
163+
args: [1, Date.today],
164+
set: { priority: 1 },
165+
description: "Create Order Items statuses are canceled at current day"
166+
},
167+
160168
checked_order_items_task: {
161169
cron: "@weekly",
162170
class: "CheckedBlingOrderItemsJob",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe DailyCanceledOrderJob, type: :job do
6+
let(:user) { FactoryBot.create(:user) }
7+
let(:date) { Date.new(2023, 11, 22) }
8+
let(:alteration_date) { Date.new(2023, 12, 14) }
9+
let(:situation_id) { BlingOrderItem::Status::PENDING }
10+
let(:bling_order_id) { '19341921284' }
11+
12+
describe '#perform' do
13+
before do
14+
allow(Date).to receive(:today).and_return Date.new(2023, 12, 14)
15+
FactoryBot.create(:bling_datum, account_id: user.account.id, expires_at: Time.zone.now + 2.days)
16+
FactoryBot.create(:bling_order_item, bling_order_id:, date:, situation_id:)
17+
end
18+
19+
it 'has status canceled' do
20+
VCR.use_cassette('canceled_by_initial_alteration_date', erb: true) do
21+
subject.perform(user.account.id)
22+
expect(BlingOrderItem.find_by(bling_order_id:).situation_id.to_i)
23+
.to eq(BlingOrderItem::Status::CANCELED)
24+
end
25+
end
26+
27+
it 'is equal to alteration_date' do
28+
VCR.use_cassette('canceled_by_initial_alteration_date', erb: true) do
29+
subject.perform(user.account.id)
30+
expect(BlingOrderItem.find_by(bling_order_id:).alteration_date.to_date)
31+
.to eq(alteration_date)
32+
end
33+
end
34+
end
35+
end

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
# https://github.com/vcr/vcr/discussions/887 ways to hide sensitive data.
1313
interaction.request.headers['Authorization'][0] = "Bearer <%= ENV['ACCESS_TOKEN'] %>"
1414
interaction.response.body.gsub!(/\d{3}[.]\d{3}[.]\d{3}[-]\d{2}/, '999.999.999-99')
15+
# Some names with latin punctuation had escaped
16+
interaction.response.body.gsub!(/\\u00ed/, '')
17+
interaction.response.body.gsub!(/\\u00f3/, '')
18+
interaction.response.body.gsub!(/\\u00fa/, '')
19+
interaction.response.body.gsub!(/\\u00e3/, '')
20+
interaction.response.body.gsub!(/\\u00e7/, '')
21+
interaction.response.body.gsub!('******', 'Aa Bb')
22+
interaction.response.body.gsub!('****', 'Aa Bb')
1523
interaction.response.body.gsub!(/"nome\":["^\"][a-z-A-Z]+[\s][a-z-A-Z | \s]+/, "\"nome\":\"Faker Name Souza")
1624
end
1725
end

0 commit comments

Comments
 (0)