Created
March 16, 2018 10:09
-
-
Save trico/9b89847c96c17b3882fd99f9869d0dec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CampaignBlock::ReportManager | |
class ReportIncrementalBudget | |
def self.default_year | |
Date.current.year | |
end | |
def self.default_month | |
Date.current.month | |
end | |
def self.find_or_create(year = default_year, month = default_month) | |
report = Report.find_by(slug: slug, analytic_year: year, analytic_month: month) | |
report = Report.create!(slug: slug, name: "budget #{year} #{month}", analytic_year: year, analytic_month: month) if report.nil? | |
report | |
end | |
def self.slug | |
'campaign_block_incremental_budget' | |
end | |
def self.generate_async(year = default_year, month = default_month) | |
CampaignBlockReportIncrementalBudgetWorker.perform_with_semaphore(year, month) | |
end | |
def self.generate(year = default_year, month = default_month) | |
return if !generable?(year, month) | |
report = find_or_create(year, month) | |
report.report_details. | |
where.not(id: protected_report_detail_ids(report, year, month)). | |
destroy_all | |
CampaignBlock.where.not(id: protected_campaign_block_ids(year, month)).order(type: :asc).each do |block| | |
next if block.steps.empty? || !block.fulfilled? || block.type == 'CampaignBlock::CouponReminder' | |
klass = "CampaignBlock::ReportManager::ReportIncrementalBudget#{block.slug.capitalize}".constantize | |
klass.new(block, report).generate_details! | |
end | |
report.reload | |
report.generate | |
rescue StandardError => e | |
report.update!(status: 'error') if report | |
raise e | |
end | |
def self.generable?(year, month) | |
date = Date.new(year, month, 1) | |
date >= Date.current.beginning_of_month | |
end | |
def self.protected_campaign_block_ids(year, month) | |
date = Date.new(year, month, 1) | |
campaign_block_ids = [] | |
campaign_block_ids << CampaignBlock. | |
where.not(type: 'CampaignBlock::Custom'). | |
select { |block| !block.scheduled_between?(date, date.end_of_month) || !block.history_by_dates(date, date.end_of_month).nil? }. | |
map(&:id) | |
campaign_block_ids << CampaignBlock. | |
where(type: 'CampaignBlock::Custom'). | |
select { |block| block.sent_between?(date, date.end_of_month) }. | |
map(&:id) | |
campaign_block_ids.flatten! | |
end | |
def self.protected_report_detail_ids(report, year, month) | |
date = Date.new(year, month, 1) | |
report_detail_ids = [] | |
if date > Date.current.beginning_of_month | |
sent_campaign_block_ids = protected_campaign_block_ids(year, month) | |
report_details = report.report_details. select do |report_detail| | |
block_data = report_detail.configuration['campaign_block_data'] | |
block_data && sent_campaign_block_ids.include?(block_data['campaign_block_id']) | |
end | |
report_detail_ids = report_details.map(&:id) | |
end | |
report_detail_ids | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment