Class: SpreeCmCommissioner::Guest

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from KycBitwise

KycBitwise::BIT_FIELDS, KycBitwise::ORDERED_BIT_FIELDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KycBitwise

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

Class Method Details

.csv_importable_columnsObject



75
76
77
78
79
80
81
# File 'app/models/spree_cm_commissioner/guest.rb', line 75

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

Instance Method Details

#all_required_dynamic_fields_completed?(stage) ⇒ Boolean

Returns:

  • (Boolean)


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

def all_required_dynamic_fields_completed?(stage)
  missing_dynamic_fields_for_stage(stage).blank?
end

#allowed_checkout?Boolean

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

Returns:

  • (Boolean)


84
85
86
# File 'app/models/spree_cm_commissioner/guest.rb', line 84

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

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/spree_cm_commissioner/guest.rb', line 88

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

#assign_seat_number_with_bibObject



155
156
157
158
159
# File 'app/models/spree_cm_commissioner/guest.rb', line 155

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



161
162
163
164
165
166
# File 'app/models/spree_cm_commissioner/guest.rb', line 161

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_blockObject



151
152
153
# File 'app/models/spree_cm_commissioner/guest.rb', line 151

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

#bib_display_prefix?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'app/models/spree_cm_commissioner/guest.rb', line 214

def bib_display_prefix?
  line_item.variant.bib_display_prefix?
end

#bib_required?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'app/models/spree_cm_commissioner/guest.rb', line 210

def bib_required?
  line_item.variant.bib_prefix.present?
end

#can_check_in?(session) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_check_in?(session)
  eligible_check_in_sessions.include?(session)
end

#cancel_reserved_block!Object



276
277
278
279
280
281
282
283
# File 'app/models/spree_cm_commissioner/guest.rb', line 276

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

#check_in_completed?Boolean

Returns:

  • (Boolean)


355
356
357
358
359
360
361
362
# File 'app/models/spree_cm_commissioner/guest.rb', line 355

def check_in_completed?
  return false unless line_item&.order&.completed?
  return true unless line_item&.product&.dynamic_fields&.during_check_in&.any?

  required_fields = line_item.product.dynamic_fields.during_check_in.pluck(:id)
  filled_fields = guest_dynamic_fields.where(dynamic_field_id: required_fields).pluck(:dynamic_field_id)
  (required_fields - filled_fields).empty?
end

#cm_check_in_rulesObject



376
377
378
# File 'app/models/spree_cm_commissioner/guest.rb', line 376

def cm_check_in_rules
  SpreeCmCommissioner::CheckInRule.where(ruleable_id: line_item.product_id)
end

#completed_phasesObject



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

def completed_phases
  phases = []
  phases << 'pre_registration' if pre_registration_completed?
  phases << 'post_registration' if post_registration_completed?
  phases << 'during_check_in' if check_in_completed?
  phases
end

#current_ageObject



204
205
206
207
208
# File 'app/models/spree_cm_commissioner/guest.rb', line 204

def current_age
  return nil if dob.nil?

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

#during_check_in_fields?Boolean

Returns:

  • (Boolean)


372
373
374
# File 'app/models/spree_cm_commissioner/guest.rb', line 372

def during_check_in_fields?
  line_item&.product&.dynamic_fields&.during_check_in&.any?
end

#eligible_check_in_sessionsObject



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'app/models/spree_cm_commissioner/guest.rb', line 380

def eligible_check_in_sessions
  check_in_session_ids = []

  cm_check_in_rules.includes(:check_in_session).find_each do |rule|
    case rule.ruleable_type
    when 'Spree::Product'
      check_in_session_ids << rule.check_in_session_id if line_item.product_id == rule.ruleable_id
    when 'Spree::Variant'
      check_in_session_ids << rule.check_in_session_id if line_item.variant_id == rule.ruleable_id
    when 'Spree::OptionValue'
      check_in_session_ids << rule.check_in_session_id if line_item.variant.option_value_ids.include?(rule.ruleable_id)
    end
  end

  SpreeCmCommissioner::CheckInSession.where(id: check_in_session_ids.uniq)
end

#formatted_bib_numberObject

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



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/models/spree_cm_commissioner/guest.rb', line 240

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

#full_nameObject



168
169
170
# File 'app/models/spree_cm_commissioner/guest.rb', line 168

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

#generate_bibObject



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

def generate_bib
  return if bib_prefix.present?
  return unless bib_required?
  return if event_id.blank?

  self.bib_prefix = line_item.variant.bib_prefix

  last_bib_number = event.guests
                         .where(bib_prefix: bib_prefix)
                         .maximum(:bib_number) || 0

  self.bib_number = last_bib_number + 1
  self.bib_index = "#{event_id}-#{bib_prefix}-#{bib_number}"
end

#generate_bib!Object



233
234
235
236
# File 'app/models/spree_cm_commissioner/guest.rb', line 233

def generate_bib!
  generate_bib
  save!
end

#generate_png_qr(size = 120) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/spree_cm_commissioner/guest.rb', line 184

def generate_png_qr(size = 120)
  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_qrObject



172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/spree_cm_commissioner/guest.rb', line 172

def generate_svg_qr
  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

#guest_incomplete?(guest) ⇒ Boolean

Returns:

  • (Boolean)


447
448
449
450
451
# File 'app/models/spree_cm_commissioner/guest.rb', line 447

def guest_incomplete?(guest)
  (guest.pre_registration_fields? && !guest.pre_registration_completed?) ||
    (guest.post_registration_fields? && !guest.post_registration_completed?) ||
    (guest.during_check_in_fields? && !guest.check_in_completed?)
end

#missing_dynamic_fields_for_stage(stage) ⇒ Object



405
406
407
408
409
410
411
412
413
414
# File 'app/models/spree_cm_commissioner/guest.rb', line 405

def missing_dynamic_fields_for_stage(stage)
  return [] if line_item.blank? || line_item.product.blank?

  # Cache product dynamic fields to avoid multiple queries
  @product_dynamic_fields ||= line_item.product.dynamic_fields
  required_fields = @product_dynamic_fields.where(data_fill_stage: stage)
  filled_ids = guest_dynamic_fields&.pluck(:dynamic_field_id) || []

  required_fields.reject { |field| filled_ids.include?(field.id) }
end

#order_has_incomplete_guests?(order) ⇒ Boolean

Returns:

  • (Boolean)


439
440
441
442
443
444
445
# File 'app/models/spree_cm_commissioner/guest.rb', line 439

def order_has_incomplete_guests?(order)
  order.line_items.any? do |line_item|
    line_item.guests.any? do |guest|
      guest_incomplete?(guest)
    end
  end
end

#post_registration_completed?Boolean

Returns:

  • (Boolean)


346
347
348
349
350
351
352
353
# File 'app/models/spree_cm_commissioner/guest.rb', line 346

def post_registration_completed?
  return false unless line_item&.order&.completed?
  return true unless line_item&.product&.dynamic_fields&.post_registration&.any?

  required_fields = line_item.product.dynamic_fields.post_registration.pluck(:id)
  filled_fields = guest_dynamic_fields.where(dynamic_field_id: required_fields).pluck(:dynamic_field_id)
  (required_fields - filled_fields).empty?
end

#post_registration_fields?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'app/models/spree_cm_commissioner/guest.rb', line 368

def post_registration_fields?
  line_item&.product&.dynamic_fields&.post_registration&.any?
end

#pre_registration_completed?Boolean

Returns:

  • (Boolean)


337
338
339
340
341
342
343
344
# File 'app/models/spree_cm_commissioner/guest.rb', line 337

def pre_registration_completed?
  return false unless line_item&.order&.completed?
  return true unless line_item&.product&.dynamic_fields&.pre_registration&.any?

  required_fields = line_item.product.dynamic_fields.pre_registration.pluck(:id)
  filled_fields = guest_dynamic_fields.where(dynamic_field_id: required_fields).pluck(:dynamic_field_id)
  (required_fields - filled_fields).empty?
end

#pre_registration_fields?Boolean

Returns:

  • (Boolean)


364
365
366
# File 'app/models/spree_cm_commissioner/guest.rb', line 364

def pre_registration_fields?
  line_item&.product&.dynamic_fields&.pre_registration&.any?
end

#preload_order_block_idsObject



268
269
270
271
272
273
274
# File 'app/models/spree_cm_commissioner/guest.rb', line 268

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

#product_dynamic_fieldsObject



401
402
403
# File 'app/models/spree_cm_commissioner/guest.rb', line 401

def product_dynamic_fields
  @product_dynamic_fields ||= line_item.product.dynamic_fields
end

#qr_dataObject



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

def qr_data
  token
end

#reject_guest_dynamic_field?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


416
417
418
419
# File 'app/models/spree_cm_commissioner/guest.rb', line 416

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

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/spree_cm_commissioner/guest.rb', line 108

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

#save_and_move_to_next_stage(_transition = nil) ⇒ Object



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

def save_and_move_to_next_stage(_transition = nil)
  # Trigger state machine transitions based on completion status
  case data_fill_stage_phase
  when 'pre_registration'
    complete_pre_registration! if pre_registration_completed?
  when 'post_registration'
    complete_post_registration! if post_registration_completed?
  when 'during_check_in'
    complete_check_in! if check_in_completed?
  end
end

#set_event_idObject



147
148
149
# File 'app/models/spree_cm_commissioner/guest.rb', line 147

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

#should_update_incomplete_guest_info?Boolean

Returns:

  • (Boolean)


453
454
455
456
# File 'app/models/spree_cm_commissioner/guest.rb', line 453

def should_update_incomplete_guest_info?
  saved_change_to_data_fill_stage_phase? ||
    (guest_dynamic_fields.any? && (guest_dynamic_fields.any?(&:saved_changes?) || guest_dynamic_fields.any?(&:destroyed?)))
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

Returns:

  • (Boolean)


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

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

#update_user_incomplete_guest_info_statusObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'app/models/spree_cm_commissioner/guest.rb', line 421

def update_user_incomplete_guest_info_status
  return unless user
  return unless line_item&.order&.completed?

  user.reload

  has_incomplete = user.orders.complete.any? do |order|
    order_has_incomplete_guests?(order)
  end

  current_status = user.['has_incomplete_guest_info'] || false

  return unless current_status != has_incomplete

  user.['has_incomplete_guest_info'] = has_incomplete
  user.save!
end

#validate_blockObject



260
261
262
263
264
265
266
# File 'app/models/spree_cm_commissioner/guest.rb', line 260

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