Class: SchedulerBooking

Inherits:
ApplicationRecord show all
Defined in:
app/models/scheduler_booking.rb

Overview

== Schema Information

Table name: scheduler_bookings
Database name: primary

id :bigint not null, primary key
answers :jsonb
ends_at :datetime not null
guest_attendees :jsonb
guest_email :string not null
guest_name :string not null
guest_phone :string
location_type :string
notes :text
rescheduled_at :datetime
rescheduled_count :integer default(0), not null
starts_at :datetime not null
status :string default("confirmed"), not null
timezone :string default("America/Chicago"), not null
uuid :uuid not null
zoom_join_url :string
created_at :datetime not null
updated_at :datetime not null
employee_id :bigint not null
google_event_id :string
party_id :integer
scheduler_booking_page_id :bigint not null
zoom_meeting_id :string

Indexes

index_scheduler_bookings_on_employee_id_and_starts_at (employee_id,starts_at)
index_scheduler_bookings_on_party_id (party_id)
index_scheduler_bookings_on_scheduler_booking_page_id (scheduler_booking_page_id)
index_scheduler_bookings_on_starts_at (starts_at)
index_scheduler_bookings_on_status (status)
index_scheduler_bookings_on_uuid (uuid) UNIQUE

Foreign Keys

fk_rails_... (employee_id => parties.id) ON DELETE => cascade
fk_rails_... (scheduler_booking_page_id => scheduler_booking_pages.id) ON DELETE => cascade

Constant Summary

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#ends_atObject (readonly)



55
# File 'app/models/scheduler_booking.rb', line 55

validates :guest_name, :guest_email, :starts_at, :ends_at, :timezone, presence: true

#guest_emailObject (readonly)



55
# File 'app/models/scheduler_booking.rb', line 55

validates :guest_name, :guest_email, :starts_at, :ends_at, :timezone, presence: true

#guest_nameObject (readonly)



55
# File 'app/models/scheduler_booking.rb', line 55

validates :guest_name, :guest_email, :starts_at, :ends_at, :timezone, presence: true

#location_typeObject (readonly)



58
# File 'app/models/scheduler_booking.rb', line 58

validates :location_type, inclusion: { in: %w[phone zoom] }, allow_nil: true

#starts_atObject (readonly)



55
# File 'app/models/scheduler_booking.rb', line 55

validates :guest_name, :guest_email, :starts_at, :ends_at, :timezone, presence: true

#timezoneObject (readonly)



55
# File 'app/models/scheduler_booking.rb', line 55

validates :guest_name, :guest_email, :starts_at, :ends_at, :timezone, presence: true

#uuidObject (readonly)



57
# File 'app/models/scheduler_booking.rb', line 57

validates :uuid, uniqueness: true

Class Method Details

.cancelledActiveRecord::Relation<SchedulerBooking>

A relation of SchedulerBookings that are cancelled. Active Record Scope

Returns:

See Also:



62
# File 'app/models/scheduler_booking.rb', line 62

scope :cancelled, -> { where(status: 'cancelled') }

.confirmedActiveRecord::Relation<SchedulerBooking>

A relation of SchedulerBookings that are confirmed. Active Record Scope

Returns:

See Also:



61
# File 'app/models/scheduler_booking.rb', line 61

scope :confirmed, -> { where(status: 'confirmed') }

.for_employee_on_dateActiveRecord::Relation<SchedulerBooking>

A relation of SchedulerBookings that are for employee on date. Active Record Scope

Returns:

See Also:



65
66
67
68
69
# File 'app/models/scheduler_booking.rb', line 65

scope :for_employee_on_date, ->(employee, date) {
  confirmed
    .where(employee: employee)
    .where(starts_at: date.in_time_zone('America/Chicago').all_day)
}

.pastActiveRecord::Relation<SchedulerBooking>

A relation of SchedulerBookings that are past. Active Record Scope

Returns:

See Also:



64
# File 'app/models/scheduler_booking.rb', line 64

scope :past, -> { confirmed.where(starts_at: ..Time.current) }

.upcomingActiveRecord::Relation<SchedulerBooking>

A relation of SchedulerBookings that are upcoming. Active Record Scope

Returns:

See Also:



63
# File 'app/models/scheduler_booking.rb', line 63

scope :upcoming, -> { confirmed.where('starts_at > ?', Time.current) }

Instance Method Details

#activitiesActiveRecord::Relation<Activity>

Returns:

See Also:



51
# File 'app/models/scheduler_booking.rb', line 51

has_many :activities, as: :resource, dependent: :nullify

#customer_sales_rep_invite_emailObject

Email to add as a Google Calendar attendee when the booking page opts in and we resolve
an existing customer with a primary sales rep. Availability is not checked; the rep may accept or decline.



121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/scheduler_booking.rb', line 121

def customer_sales_rep_invite_email
  return unless scheduler_booking_page.invite_customer_sales_rep?

  customer = SchedulerBookingLeadProcessor.customer_for_sales_rep_invite(self)
  rep = customer&.primary_sales_rep
  return if rep&.email.blank?

  return if rep.id == employee_id

  rep.email.to_s.strip.downcase
end

#duration_minutesObject



87
88
89
# File 'app/models/scheduler_booking.rb', line 87

def duration_minutes
  ((ends_at - starts_at) / 60).to_i
end

#effective_location_typeObject



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

def effective_location_type
  location_type || scheduler_booking_page.location_type
end

#employeeEmployee

Returns:

See Also:



48
# File 'app/models/scheduler_booking.rb', line 48

belongs_to :employee

#ends_at_in_guest_tzObject



95
96
97
# File 'app/models/scheduler_booking.rb', line 95

def ends_at_in_guest_tz
  ends_at.in_time_zone(timezone)
end


115
116
117
# File 'app/models/scheduler_booking.rb', line 115

def link_party_ids
  [party_id].compact
end

#partyParty

Returns:

See Also:



49
# File 'app/models/scheduler_booking.rb', line 49

belongs_to :party, optional: true

#reschedule!(new_starts_at, new_ends_at, new_timezone) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/scheduler_booking.rb', line 103

def reschedule!(new_starts_at, new_ends_at, new_timezone)
  raise "Cannot reschedule a #{status} booking" unless confirmed?

  update!(
    starts_at: new_starts_at,
    ends_at: new_ends_at,
    timezone: new_timezone,
    rescheduled_at: Time.current,
    rescheduled_count: rescheduled_count + 1
  )
end

#scheduler_booking_pageSchedulerBookingPage



47
# File 'app/models/scheduler_booking.rb', line 47

belongs_to :scheduler_booking_page

#starts_at_in_guest_tzObject



91
92
93
# File 'app/models/scheduler_booking.rb', line 91

def starts_at_in_guest_tz
  starts_at.in_time_zone(timezone)
end