Class: SpreeCmCommissioner::OrderTelegramMessageFactory

Inherits:
TelegramMessageFactory show all
Defined in:
app/factory/spree_cm_commissioner/order_telegram_message_factory.rb

Instance Attribute Summary collapse

Attributes inherited from TelegramMessageFactory

#subtitle, #title

Instance Method Summary collapse

Methods inherited from TelegramMessageFactory

#bold, #header, #inline_code, #italic, #message, #parse_mode, #pretty_date

Constructor Details

#initialize(title:, order:, subtitle: nil, show_details_link: nil, vendor: nil) ⇒ OrderTelegramMessageFactory

Returns a new instance of OrderTelegramMessageFactory.



27
28
29
30
31
32
33
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 27

def initialize(title:, order:, subtitle: nil, show_details_link: nil, vendor: nil)
  @order = order
  @vendor = vendor
  @show_details_link = show_details_link || false

  super(title: title, subtitle: subtitle)
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



25
26
27
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 25

def order
  @order
end

Returns the value of attribute show_details_link.



25
26
27
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 25

def show_details_link
  @show_details_link
end

#vendorObject (readonly)

Returns the value of attribute vendor.



25
26
27
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 25

def vendor
  @vendor
end

Instance Method Details

#bodyObject

override



42
43
44
45
46
47
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 42

def body
  text = []
  text << "Order Number:\n#{inline_code(order.number)}\n"
  text << selected_line_items.map { |item| line_item_content(item) }.compact.join("\n\n")
  text.compact.join("\n")
end

override



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 75

def footer
  text = []

  text << bold('🙍 Customer Info')
  text << "Name: #{order.name}"
  text << "Tel: #{inline_code(order.intel_phone_number || order.phone_number)}"
  text << "Email: #{inline_code(order.email)}" if order.email.present?

  if show_details_link && order.guests.any?
    text << ''
    text << 'View Tickets:'
    text += generate_guests_links(order.guests)
  end

  text.compact.join("\n")
end

Result: | No. A24 | No. A24 | | No. A24 | No. A24 | | No. A24 | No. A24 | | No. A24 | No. A24 | | No. A24 | No. A24 | | No. A24 |



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 99

def generate_guests_links(guests, guests_per_row = 2)
  rows = (guests.size.to_f / guests_per_row).ceil
  formatted_rows = []

  rows.times do |i|
    row_guests = guests.slice(i * guests_per_row, guests_per_row)
    formatted_row = row_guests.map do |guest|
      button_label = guest.seat_number.present? ? "No. #{guest.seat_number}" : "No. #{guest.formatted_bib_number || 'N/A'}"
      link = Rails.application.routes.url_helpers.guest_cards_url(guest.token)
      "| <a href='#{link}'>#{button_label}</a>"
    end.join(' ')

    formatted_rows << ("#{formatted_row} |")
  end

  formatted_rows
end

#line_item_content(line_item) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 49

def line_item_content(line_item)
  text = []

  text << bold(line_item.product.name.to_s)
  text << "Quantity: #{line_item.quantity}"
  text << italic("👉 #{line_item.options_text}") if line_item.options_text.present?
  text << italic(pretty_date_for(line_item)) if pretty_date_for(line_item).present?
  text << italic("🏪 #{line_item.vendor.name}") if line_item.vendor&.name.present? && vendor.blank?

  text.compact.join("\n")
end

#pretty_date_for(line_item) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 61

def pretty_date_for(line_item)
  return nil unless line_item.date_present?

  from_date = pretty_date(line_item.from_date)
  to_date = pretty_date(line_item.to_date)

  if from_date == to_date
    "🗓️ #{from_date}"
  else
    "🗓️ #{from_date} -> #{to_date}"
  end
end

#selected_line_itemsObject



35
36
37
38
39
# File 'app/factory/spree_cm_commissioner/order_telegram_message_factory.rb', line 35

def selected_line_items
  return order.line_items.for_vendor(vendor) if vendor.present?

  order.line_items
end