Class: Item::AvailabilityDateChecker

Inherits:
BaseService show all
Defined in:
app/services/item/availability_date_checker.rb

Overview

Service object: availability date checker.

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#processObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/item/availability_date_checker.rb', line 4

def process
  # Examine all store items with availability date set and toggle their inactive status
  relation = StoreItem.where("store_items.availability_start_date IS NOT NULL OR store_items.availability_end_date IS NOT NULL").joins(:item).merge(Item.active)
  publication_results = []
  publications_relation = relation.merge(Item.publications)

  non_publications_results = []
  non_publications_relation = relation.merge(Item.non_publications)

  publications_relation.find_each do |store_item|
    publication_results << { store_item: store_item.to_s, item_id: store_item.item_id }.merge(store_item.update_availability)
    publication_results = publication_results.reject { |v| v[:result] == :unchanged }
  end

  non_publications_relation.find_each do |store_item|
    non_publications_results << { store_item: store_item.to_s, item_id: store_item.item_id }.merge(store_item.update_availability)
    non_publications_results = non_publications_results.reject { |v| v[:result] == :unchanged }
  end

  InventoryMailer.store_item_availability_timed_update(publication_results, :publications).deliver_later if publication_results.present?

  InventoryMailer.store_item_availability_timed_update(non_publications_results, :non_publications).deliver_later if non_publications_results.present?

  {
    publications: publication_results,
    non_publications: non_publications_results
  }.freeze
end