Class: ScheduleWorkers::ActivePackageType

Inherits:
ApplicationWorker show all
Defined in:
app/workers/schedule_workers/active_package_type.rb

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#performObject



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/workers/schedule_workers/active_package_type.rb', line 6

def perform
  PackageType.exclude_add_on.find_each do |package_type|
    package_type.update(active: false)
    package_type.cities.delete_all
  end

  City.all.each do |city|
    today = Date.current_date
    active_restaurant_package_types =
      HhPackage::RestaurantPackage.active_scope.joins(restaurant: :city).
        where(cities: { id: city.id }, restaurants: { active: true }).
        where('date(restaurants.expiry_date) >= ?', today).
        select('DISTINCT(package_type)').map(&:package_type)

    active_restaurant_package_types.each do |klass_type|
      long_package_type = klass_type.partition('::').last.partition('::').last
      short_code = HhPackage::PACKAGE_SHORT_TO_LIST.invert[long_package_type]
      package_type = PackageType.find_by(package_type_code: short_code)
      next unless package_type.present?

      package_type.active = true
      package_type.cities << city
      package_type.save
    end
  end
end