Class: SpreeCmCommissioner::Guest

Inherits:
Base
  • Object
show all
Includes:
GuestDataFillStageConcern, Integrations::IntegrationMappable, KycBitwise, PhoneNumberSanitizer
Defined in:
app/models/spree_cm_commissioner/guest.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from GuestDataFillStageConcern

SpreeCmCommissioner::GuestDataFillStageConcern::STAGES

Constants included from KycBitwise

KycBitwise::BIT_FIELDS, KycBitwise::KYC_FIELD_ATTRIBUTES, KycBitwise::ORDERED_BIT_FIELDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GuestDataFillStageConcern

#any_fields_exist_for?, #incomplete?, #refresh_data_fill_stage, #stage_completed?

Methods included from KycBitwise

#available_social_contact_platforms, #kyc?, #kyc_fields, #kyc_value_enabled?, #unordered_kyc_fields

Class Method Details

.csv_importable_columns ⇒ Object



88
89
90
91
92
93
94
# File 'app/models/spree_cm_commissioner/guest.rb', line 88

def self.csv_importable_columns
  %i[
    first_name last_name phone_number age dob gender address other_occupation
    entry_type nationality_id other_organization expectation emergency_contact bib_number
    preferred_telegram_user_id seat_number country_code contact
  ]
end

.variants_for_rules(rules) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/models/spree_cm_commissioner/guest.rb', line 356

def self.variants_for_rules(rules)
  variant_ids = rules.flat_map do |rule|
    case rule.ruleable_type
    when 'Spree::Product', 'Spree::OptionValue'
      rule.ruleable&.variants&.pluck(:id)
    when 'Spree::Variant'
      [rule.ruleable_id]
    else
      []
    end
  end.compact.flatten.uniq

  return [] if variant_ids.empty?

  Spree::Variant.where(id: variant_ids).includes(:product, :option_values)
end

Instance Method Details

#allowed_checkout? ⇒ Boolean

no validation for each field as we allow user to save data to model partially.



111
112
113
# File 'app/models/spree_cm_commissioner/guest.rb', line 111

def allowed_checkout?
  kyc_fields.all? { |field| allowed_checkout_for?(field) }
end

#allowed_checkout_for?(field) ⇒ Boolean

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/spree_cm_commissioner/guest.rb', line 115

def allowed_checkout_for?(field) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  return first_name.present? && last_name.present? if field == :guest_name
  return gender.present? if field == :guest_gender
  return dob.present? if field == :guest_dob
  return occupation.present? || other_occupation.present? if field == :guest_occupation
  return nationality.present? if field == :guest_nationality
  return age.present? if field == :guest_age
  return emergency_contact.present? if field == :guest_emergency_contact
  return other_organization.present? if field == :guest_organization
  return expectation.present? if field == :guest_expectation
  return social_contact_platform.present? && social_contact.present? if field == :guest_social_contact
  return upload_later? || (id_card.present? && id_card.allowed_checkout?) if field == :guest_id_card
  return address.present? if field == :guest_address
  return phone_number.present? if field == :guest_phone_number
  return country_code.present? if field == :guest_country_code
  return contact.present? if field == :guest_contact

  false
end

#allows_gift_transfer? ⇒ Boolean



398
399
400
# File 'app/models/spree_cm_commissioner/guest.rb', line 398

def allows_gift_transfer?
  line_item&.product&.allow_gift_transfer?
end

#allows_transfer? ⇒ Boolean

Ticket Transfer validations



394
395
396
# File 'app/models/spree_cm_commissioner/guest.rb', line 394

def allows_transfer?
  line_item&.product&.allow_transfer?
end

#assign_seat_number_with_bib ⇒ Object



182
183
184
185
186
# File 'app/models/spree_cm_commissioner/guest.rb', line 182

def assign_seat_number_with_bib
  return if seat_number.present? # avoid reassign seat to guest

  assign_seat_number_with_bib!
end

#assign_seat_number_with_bib! ⇒ Object



188
189
190
191
192
193
# File 'app/models/spree_cm_commissioner/guest.rb', line 188

def assign_seat_number_with_bib!
  index = bib_number - 1
  positions = line_item.variant.seat_number_positions || []

  self.seat_number = (positions[index] if index >= 0 && index < positions.size)
end

#assign_seat_number_with_block ⇒ Object



178
179
180
# File 'app/models/spree_cm_commissioner/guest.rb', line 178

def assign_seat_number_with_block
  self.seat_number = block.present? ? block.label : nil
end

#bib_display_prefix? ⇒ Boolean



264
265
266
# File 'app/models/spree_cm_commissioner/guest.rb', line 264

def bib_display_prefix?
  line_item.variant.bib_display_prefix?
end

#bib_required? ⇒ Boolean



260
261
262
# File 'app/models/spree_cm_commissioner/guest.rb', line 260

def bib_required?
  line_item.variant.bib_required?
end

#can_hold_block? ⇒ Boolean

Only allow holding a block if it represents a seat. Other block types, like standing, don’t need a hold. Use the variant's inventory item to track availability instead.



99
100
101
# File 'app/models/spree_cm_commissioner/guest.rb', line 99

def can_hold_block?
  block.present? && block.seatable?
end

#can_reserve_block? ⇒ Boolean

Only allow holding a block if it represents a seat. Other block types, like standing, don’t need a hold. Use the variant's inventory item to track availability instead.



106
107
108
# File 'app/models/spree_cm_commissioner/guest.rb', line 106

def can_reserve_block?
  block.present? && block.seatable?
end

#cancel_reserved_block! ⇒ Object



379
380
381
382
383
384
385
386
# File 'app/models/spree_cm_commissioner/guest.rb', line 379

def cancel_reserved_block!
  reserved_block.update!(
    status: :canceled,
    expired_at: nil,
    updated_by: nil,
    guest_id: nil
  )
end

#current_age ⇒ Object



254
255
256
257
258
# File 'app/models/spree_cm_commissioner/guest.rb', line 254

def current_age
  return nil if dob.nil?

  ((Time.zone.now - dob.to_time) / 1.year.seconds).floor
end

#display_name_with_bib ⇒ Object



199
200
201
# File 'app/models/spree_cm_commissioner/guest.rb', line 199

def display_name_with_bib
  [full_name, formatted_bib_number, seat_number].compact.join(' | ')
end

#eligible_check_in_session_ids ⇒ Object



306
307
308
309
310
311
312
# File 'app/models/spree_cm_commissioner/guest.rb', line 306

def eligible_check_in_session_ids
  # Ensure public_metadata is a hash
  self. = JSON.parse() if .is_a?(String)

  # Return as array of integers for consistency, even if stored as strings
  Array(['eligible_check_in_session_ids']).map(&:to_i)
end

#eligible_check_in_session_ids=(ids) ⇒ Object



319
320
321
322
323
324
325
# File 'app/models/spree_cm_commissioner/guest.rb', line 319

def eligible_check_in_session_ids=(ids)
  # Ensure public_metadata is a hash
  self. = {} if .nil?
  self. = JSON.parse() if .is_a?(String)

  ['eligible_check_in_session_ids'] = ids
end

#eligible_check_in_sessions ⇒ Object



373
374
375
376
377
# File 'app/models/spree_cm_commissioner/guest.rb', line 373

def eligible_check_in_sessions
  return SpreeCmCommissioner::CheckInSession.none if eligible_check_in_session_ids.blank?

  SpreeCmCommissioner::CheckInSession.where(id: eligible_check_in_session_ids)
end

#eligible_for_session?(check_in_session) ⇒ Boolean



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'app/models/spree_cm_commissioner/guest.rb', line 338

def eligible_for_session?(check_in_session)
  rules = check_in_session.check_in_rules
  return true if rules.empty?

  rules.any? do |rule|
    case rule.ruleable_type
    when 'Spree::Product'
      line_item.product_id == rule.ruleable_id
    when 'Spree::Variant'
      line_item.variant_id == rule.ruleable_id
    when 'Spree::OptionValue'
      line_item.variant.option_value_ids.include?(rule.ruleable_id)
    else
      false
    end
  end
end

#event_ended? ⇒ Boolean

A ticket stays transferable while the event is ongoing (e.g. a multi-day festival) and is only blocked once the event has ended.



404
405
406
407
# File 'app/models/spree_cm_commissioner/guest.rb', line 404

def event_ended?
  event_to_date = event&.to_date
  event_to_date.present? && event_to_date <= Time.current
end

#external_qr_data ⇒ Object

QR data for check-in. If external_wins? is true, use their QR data (they have their own check-in system). Otherwise, use our system's QR data. Only applicable to models with QR support (e.g., line_item, guest).



247
248
249
250
251
252
# File 'app/models/spree_cm_commissioner/guest.rb', line 247

def external_qr_data
  return nil if line_item.nil? || !line_item.integration?

  mapping = external_wins_integration_mappings.first
  mapping.external_qr_data if mapping.present?
end

#formatted_bib_number ⇒ Object

bib_number: 345, bib_prefix: 5KM, bib_zerofill: 5 => return 5KM00345 bib_number: 345, bib_prefix: 5KM, bib_zerofill: 2 => return 5KM345



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'app/models/spree_cm_commissioner/guest.rb', line 270

def formatted_bib_number
  return nil if line_item.blank? || line_item.variant.blank?
  return nil if bib_prefix.blank?
  return nil if bib_number.blank?

  filled_bib_number = bib_number.to_s.rjust(line_item.variant.bib_zerofill.to_i, '0')

  if bib_display_prefix?
    "#{bib_prefix}#{filled_bib_number}"
  else
    filled_bib_number
  end
end

#from_transfer? ⇒ Boolean

Set by TicketTransfers::TransferGuest when this guest was created to receive a transferred ticket



315
316
317
# File 'app/models/spree_cm_commissioner/guest.rb', line 315

def from_transfer?
  ActiveModel::Type::Boolean.new.cast(['from_transfer']) || false
end

#full_name ⇒ Object



195
196
197
# File 'app/models/spree_cm_commissioner/guest.rb', line 195

def full_name
  [first_name, last_name].compact_blank.join(' ')
end

#generate_png_qr(size = 120) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/models/spree_cm_commissioner/guest.rb', line 217

def generate_png_qr(size = 120)
  return nil if qr_data.blank?

  qrcode = RQRCode::QRCode.new(qr_data)
  qrcode.as_png(
    bit_depth: 1,
    border_modules: 1,
    color_mode: ChunkyPNG::COLOR_GRAYSCALE,
    color: 'black',
    file: nil,
    fill: 'white',
    module_px_size: 4,
    resize_exactly_to: false,
    resize_gte_to: false,
    size: size
  )
end

#generate_svg_qr ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/spree_cm_commissioner/guest.rb', line 203

def generate_svg_qr
  return nil if qr_data.blank?

  qrcode = RQRCode::QRCode.new(qr_data)
  qrcode.as_svg(
    color: '000',
    shape_rendering: 'crispEdges',
    module_size: 5,
    standalone: true,
    use_path: true,
    viewbox: '0 0 20 10'
  )
end

#preload_eligible_check_in_session_ids ⇒ Object



327
328
329
330
331
332
333
334
335
336
# File 'app/models/spree_cm_commissioner/guest.rb', line 327

def preload_eligible_check_in_session_ids
  event = self.event || line_item&.event
  return if event.blank?

  # only preload eligible and active check-in sessions, otherwise should not checkinable.
  self.eligible_check_in_session_ids = event.active_check_in_sessions
                                            .select { |session| eligible_for_session?(session) }
                                            .map(&:id)
                                            .uniq
end

#preload_order_block_ids ⇒ Object



298
299
300
301
302
303
304
# File 'app/models/spree_cm_commissioner/guest.rb', line 298

def preload_order_block_ids
  return if line_item.blank?
  return if line_item.order.blank?

  block_ids = line_item.order.blocks.pluck(:id)
  line_item.order.update(preload_block_ids: block_ids)
end

#qr_data ⇒ Object

Transferred guests no longer own the ticket, so they must not receive any scannable QR data. The ownership moved to the guest created by SpreeCmCommissioner::TicketTransfers::TransferGuest.



238
239
240
241
242
243
# File 'app/models/spree_cm_commissioner/guest.rb', line 238

def qr_data
  return nil if transferred?
  return external_qr_data if external_qr_data.present?

  token
end

#reject_guest_dynamic_field?(attributes) ⇒ Boolean



388
389
390
391
# File 'app/models/spree_cm_commissioner/guest.rb', line 388

def reject_guest_dynamic_field?(attributes)
  # Reject if dynamic_field_id is blank or if _destroy is true
  attributes[:dynamic_field_id].blank? || ActiveModel::Type::Boolean.new.cast(attributes[:_destroy])
end

#require_kyc_field? ⇒ Boolean

rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/spree_cm_commissioner/guest.rb', line 135

def require_kyc_field? # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
  kyc_fields.any? do |field|
    case field
    when :guest_name
      first_name.blank? || last_name.blank?
    when :guest_gender
      gender.blank?
    when :guest_dob
      dob.blank?
    when :guest_age
      age.blank?
    when :guest_occupation
      occupation.blank? && other_occupation.blank?
    when :guest_nationality
      nationality.blank?
    when :guest_emergency_contact
      emergency_contact.blank?
    when :guest_organization
      other_organization.blank?
    when :guest_expectation
      expectation.blank?
    when :guest_social_contact
      social_contact_platform.blank? || social_contact.blank?
    when :guest_id_card
      id_card.blank?
    when :guest_address
      address.blank?
    when :guest_phone_number
      phone_number.blank?
    when :guest_country_code
      country_code.blank?
    when :guest_contact
      contact.blank?
    else
      false
    end
  end
end

#set_event_id ⇒ Object



174
175
176
# File 'app/models/spree_cm_commissioner/guest.rb', line 174

def set_event_id
  self.event_id ||= line_item&.event_id
end

#should_validate_block? ⇒ Boolean

validate only if block_id is present AND (new record OR block_id is changing) goal: don't bother re-validating when block_id stays the same



286
287
288
# File 'app/models/spree_cm_commissioner/guest.rb', line 286

def should_validate_block?
  block_id.present? && (new_record? || will_save_change_to_block_id?)
end

#validate_block ⇒ Object



290
291
292
293
294
295
296
# File 'app/models/spree_cm_commissioner/guest.rb', line 290

def validate_block
  return if block_id.blank?
  return if line_item.blank? || line_item.variant.blank?
  return if line_item.variant.blocks.exists?(id: block_id)

  errors.add(:block, "does not exist in variant #{line_item.variant_id}")
end