Skip to content

Commit

Permalink
feat: update new form to capture data day-by-day from sevendays
Browse files Browse the repository at this point in the history
  • Loading branch information
mewthu2 committed May 3, 2024
1 parent 93c2ef6 commit 8b5f7e0
Showing 1 changed file with 46 additions and 67 deletions.
113 changes: 46 additions & 67 deletions app/jobs/update_product_sales_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,62 +51,24 @@ def update_thirty_days_sales(month)
def update_seven_days_sales(month = nil)
products = Product.where(status: 'Active')

if month.present?
month_start = Date.new(Date.today.year, Date.today.month, 1)
weeks_in_month = (month_start..month_start.end_of_month).each_slice(7)

week_number = 1

weeks_in_month.each do |week|
start_date = week.first.strftime('%Y-%m-%dT00:00:00Z')
end_date = week.last.strftime('%Y-%m-%dT%H:%M:%SZ')
date_range = "#{start_date}--#{end_date}"

products.each do |prd|
product_sale = ProductSale.where(product_id: prd.id,
week_refference: week_number,
kind: 'seven_days',
interval: date_range,
month_refference: month_start.strftime('%B'),
year_refference: Date.today.year)
next if product_sale.present?

p('Sleeping for 1 second...')
sleep(1.seconds)

request_params = {
granularity: 'total',
interval: date_range,
marketplaceIds: ENV['MARKETPLACE_ID'],
sku: prd.seller_sku
}

endpoint = 'https://sellingpartnerapi-na.amazon.com/sales/v1/orderMetrics'

response = HTTParty.get(endpoint, query: request_params,
headers: { 'x-amz-access-token' => @access_token })
data = response['payload']&.first

next unless response['payload'].present?
update_or_create_product_sale(prd, week_number, date_range, data, 'seven_days', month_start)
end
week_number += 1
end
else
# Calcula a data de início e fim da semana passada
last_week_start = (Date.today - 1.week).beginning_of_week
last_week_end = (Date.today - 1.week).end_of_week
month_start = Date.new(Date.today.year, month.present? ? month : Date.today.month, 1)
weeks_in_month = (month_start..month_start.end_of_month).each_slice(7)

week_number = 1
week_number = 1

date_range = "#{last_week_start.strftime('%Y-%m-%dT00:00:00Z')}--#{last_week_end.strftime('%Y-%m-%dT%H:%M:%SZ')}"
weeks_in_month.each do |week|
start_date = week.first.strftime('%Y-%m-%dT00:00:00Z')
end_date = week.last.strftime('%Y-%m-%dT%H:%M:%SZ')
date_range = "#{start_date}--#{end_date}"

products.each do |prd|
next if ProductSale.where(product_id: prd.id,
week_refference: week_number,
kind: 'seven_days',
month_refference: Date.today.month,
year_refference: Date.today.year).present?
product_sale = ProductSale.where(product_id: prd.id,
week_refference: week_number,
kind: 'seven_days',
interval: date_range,
month_refference: month_start.strftime('%B'),
year_refference: Date.today.year)
next if product_sale.present?

p('Sleeping for 1 second...')
sleep(1.seconds)
Expand All @@ -125,28 +87,45 @@ def update_seven_days_sales(month = nil)
data = response['payload']&.first

next unless response['payload'].present?

update_or_create_product_sale(prd, week_number, date_range, data, 'seven_days', last_week_start)
update_or_create_product_sale(prd, week_number, date_range, data, 'seven_days', month_start)
end
week_number += 1
end
end

private

def update_or_create_product_sale(prd, week_of_month, date_range, data, kind, data_reference)
ProductSale.find_or_create_by(product_id: prd.id,
kind:,
month_refference: data_reference.strftime('%B'),
week_refference: week_of_month,
year_refference: Date.today.year,
interval: date_range,
unit_count: data['unitCount'],
order_item_count: data['orderItemCount'],
order_count: data['orderCount'],
average_unit_price: data['averageUnitPrice']['amount'],
average_unit_price_currency: data['averageUnitPrice']['currencyCode'],
total_sales: data['totalSales']['amount'],
total_sales_currency: data['totalSales']['currencyCode'])
product_sale = ProductSale.find_by(product_id: prd.id,
kind:,
month_refference: data_reference.strftime('%B'),
week_refference: week_of_month,
year_refference: Date.today.year,
interval: date_range)

if product_sale
product_sale.update(unit_count: data['unitCount'],
order_item_count: data['orderItemCount'],
order_count: data['orderCount'],
average_unit_price: data['averageUnitPrice']['amount'],
average_unit_price_currency: data['averageUnitPrice']['currencyCode'],
total_sales: data['totalSales']['amount'],
total_sales_currency: data['totalSales']['currencyCode'])
else
ProductSale.create(product_id: prd.id,
kind:,
month_refference: data_reference.strftime('%B'),
week_refference: week_of_month,
year_refference: Date.today.year,
interval: date_range,
unit_count: data['unitCount'],
order_item_count: data['orderItemCount'],
order_count: data['orderCount'],
average_unit_price: data['averageUnitPrice']['amount'],
average_unit_price_currency: data['averageUnitPrice']['currencyCode'],
total_sales: data['totalSales']['amount'],
total_sales_currency: data['totalSales']['currencyCode'])
end
end

def obtain_acess_token
Expand Down

0 comments on commit 8b5f7e0

Please sign in to comment.