Class: SyncReservationPackageWorker

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

Overview

untuk membuat relasi antara table reservation ke table restaurant package

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#reservation_propertyObject (readonly)

Returns the value of attribute reservation_property.



3
4
5
# File 'app/workers/sync_reservation_package_worker.rb', line 3

def reservation_property
  @reservation_property
end

Instance Method Details

#package_column_to_reservation_packagesObject

selama ini kita menyimpan data package ke dalam kolom ReservationProperty#package cara tersebut tidak bagus jika kita ingin mengetahui package mana saja yang telah dibeli oleh customer, karena MySQL tidak punya fitur referensi data untuk tipe data JSON maka kita buat table baru yang bernama ReservationPackage untuk menghubungkan data package yang dibeli untuk setiap booking



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/workers/sync_reservation_package_worker.rb', line 55

def package_column_to_reservation_packages
  return [] if reservation_property.package.blank? || reservation_property.package['package_data'].blank?

  reservation_property.package['package_data'].map do |pd|
    next if pd['restaurant_package_id'].nil?

    data_from_json = pd.slice('type', 'price_cents', 'price_currency',
                              'commision', 'quantity', 'restaurant_package_id',
                              'use_custom_price', 'prepayment_percent')

    default_values = { 'reservation_id' => reservation_property.reservation_id,
                       'custom_price' => nil,
                       'use_custom_price' => nil,
                       'prepayment_percent' => nil }
    default_values.merge(data_from_json)
  end.compact
end

#perform(reservation_property_id, event) ⇒ Object



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
32
33
# File 'app/workers/sync_reservation_package_worker.rb', line 5

def perform(reservation_property_id, event)
  @reservation_property = ReservationProperty.find_by(id: reservation_property_id)
  return if reservation_property.nil?

  case event.to_s.to_sym
  when :create
    sync_packages_on_create
  when :update
    sync_packages_on_update
  when :destroy
    sync_packages_on_destroy
  else
    raise NotImplementedError
  end

  trigger_package_sync
rescue ActiveRecord::InvalidForeignKey
  # nothing to do
  # admin might has deleted the restaurant package record from DB
rescue ActiveRecord::Deadlocked => e
  # Retry the job if deadlock occurs
  APMErrorHandler.report('SyncReservationPackageWorker deadlock, retrying', {
                           reservation_property_id: reservation_property_id,
                           event: event,
                           error_message: e.message,
                           exception: e,
                         })
  self.class.perform_in(5.seconds, reservation_property_id, event)
end

#sync_packages_on_createObject

see #package_column_to_reservation_packages doc



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/workers/sync_reservation_package_worker.rb', line 36

def sync_packages_on_create
  reservation_packages = package_column_to_reservation_packages
  return if reservation_packages.blank?

  ReservationPackage.transaction do
    ReservationPackage.import! reservation_packages, on_duplicate_key_update: %i[
      quantity price_cents commision use_custom_price custom_price
      prepayment_percent
    ], raise_error: true
  end
rescue ActiveRecord::RecordInvalid => e
  APMErrorHandler.report('failed to import reservation packages', attributes: reservation_property.attributes)
end

#sync_packages_on_destroyObject



78
79
80
# File 'app/workers/sync_reservation_package_worker.rb', line 78

def sync_packages_on_destroy
  reservation_property.reservation&.reservation_packages&.delete_all
end

#sync_packages_on_updateObject



73
74
75
76
# File 'app/workers/sync_reservation_package_worker.rb', line 73

def sync_packages_on_update
  sync_packages_on_destroy
  sync_packages_on_create
end