Class: Kiosque::Checkin

Inherits:
Object
  • Object
show all
Defined in:
app/services/kiosque/checkin.rb

Overview

typed: ignore frozen_string_literal: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reservation_id = nil) ⇒ Checkin

Returns a new instance of Checkin.



7
8
9
# File 'app/services/kiosque/checkin.rb', line 7

def initialize(reservation_id = nil)
  @reservation = Reservation.find(reservation_id).decorate if reservation_id.present?
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



5
6
7
# File 'app/services/kiosque/checkin.rb', line 5

def reservation
  @reservation
end

Instance Method Details

#custom_menu_id_is_validObject



60
61
62
63
64
65
# File 'app/services/kiosque/checkin.rb', line 60

def custom_menu_id_is_valid
  reservation_data = generate_reservation_data
  kiosque_menu_ids = reservation_data['attributes']['packages'].pluck('kiosque_menu_id')

  !kiosque_menu_ids.include?('')
end

#generate_reservation_dataObject



49
50
51
52
53
54
55
56
57
58
# File 'app/services/kiosque/checkin.rb', line 49

def generate_reservation_data
  reservation_data = Api::Kiosque::V1::ReservationSerializer.new(reservation).as_json

  data = reservation_data['data']
  data_without_packages = data.dup
  data_without_packages['attributes'] = data['attributes'].except('packages')
  data_without_packages['attributes']['packages'] = data_without_packages['attributes'].delete('kiosque_packages')

  data_without_packages
end

#hh_checkinObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/kiosque/checkin.rb', line 11

def hh_checkin
  return false if reservation.nil?

  authorization_kind = 'token'
  refresh_authorization_if_expired(authorization_kind) # Refresh token if it's expired

  authorization_token = KiosqueCode.where(kind: authorization_kind).last

  if authorization_token.present?
    url = "#{kiosque_host}/HHCheckin"
    reservation_data = generate_reservation_data
    payload = {
      access_token: authorization_token.code,
      data: reservation_data,
    }.to_json

    api_response, response_body = api_call(url, payload)

    if api_response.status != 200
      if !custom_menu_id_is_valid
        error_msg = "kiosque_menu_id (Custom Menu ID of package) is empty #{api_response.status}"
      elsif response_body.present?
        error_msg = "#{response_body['error']}, #{response_body['error_description']} [HHCheckin Kiosque]"
      else
        error_msg = "API call failed with status #{api_response.status}"
      end

      handle_error('HHCheckin', error_msg, payload, response_body)
      false
    else
      save_kiosque_reservation(payload, response_body.to_json)
      true
    end
  else
    false
  end
end

#refresh_authorization_if_expired(kind) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/kiosque/checkin.rb', line 67

def refresh_authorization_if_expired(kind)
  authorization = KiosqueCode.where(kind: kind).last

  if authorization.nil?
    send("kiosque_request_#{kind}")
  else
    expire_in = Time.at(authorization.expire_in).in_time_zone('Asia/Bangkok') - 7.hours
    now = Time.zone.now.in_time_zone('Asia/Bangkok')

    if expire_in.to_i < now.to_i
      # remove if the code/token has expired
      authorization.destroy

      send("kiosque_request_#{kind}")
    end
  end
end

#validate_table_numberObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/kiosque/checkin.rb', line 85

def validate_table_number
  reservation.reload
  checkin_setting = self_checkin_setting
  if checkin_setting.present?
    if checkin_setting.require_table_number
      reservation.table.present?
    else
      true
    end
  else
    false
  end
end