feat: Business hour Inbox APIs (#1821)
* feat: Business hour Inbox APIs
This commit is contained in:
@@ -10,7 +10,6 @@
|
||||
# name :string not null
|
||||
# settings_flags :integer default(0), not null
|
||||
# support_email :string(100)
|
||||
# timezone :string default("UTC")
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
module OutOfOffisable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
OFFISABLE_ATTRS = %w[day_of_week closed_all_day open_hour open_minutes close_hour close_minutes].freeze
|
||||
|
||||
included do
|
||||
has_many :working_hours, dependent: :destroy
|
||||
after_create :create_default_working_hours
|
||||
@@ -16,15 +18,35 @@ module OutOfOffisable
|
||||
!out_of_office?
|
||||
end
|
||||
|
||||
def weekly_schedule
|
||||
working_hours.select(*OFFISABLE_ATTRS).as_json(except: :id)
|
||||
end
|
||||
|
||||
# accepts an array of hashes similiar to the format of weekly_schedule
|
||||
# [
|
||||
# { "day_of_week"=>1,
|
||||
# "closed_all_day"=>false,
|
||||
# "open_hour"=>9,
|
||||
# "open_minutes"=>0,
|
||||
# "close_hour"=>17,
|
||||
# "close_minutes"=>0},...]
|
||||
def update_working_hours(params)
|
||||
ActiveRecord::Base.transaction do
|
||||
params.each do |working_hour|
|
||||
working_hours.find_by(day_of_week: working_hour['day_of_week']).update(working_hour.slice(*OFFISABLE_ATTRS))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_default_working_hours
|
||||
working_hours.create!(day_of_week: 0, closed_all_day: true)
|
||||
working_hours.create!(day_of_week: 1, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
|
||||
working_hours.create!(day_of_week: 2, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
|
||||
working_hours.create!(day_of_week: 3, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
|
||||
working_hours.create!(day_of_week: 4, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
|
||||
working_hours.create!(day_of_week: 5, open_hour: 9, open_minutes: 0, close_hour: 17, close_minutes: 0)
|
||||
working_hours.create!(day_of_week: 6, closed_all_day: true)
|
||||
working_hours.create!(day_of_week: 7, closed_all_day: true)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
# greeting_message :string
|
||||
# name :string not null
|
||||
# out_of_office_message :string
|
||||
# timezone :string default("UTC")
|
||||
# working_hours_enabled :boolean default(FALSE)
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
@@ -29,6 +30,7 @@ class Inbox < ApplicationRecord
|
||||
include OutOfOffisable
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :timezone, inclusion: { in: TZInfo::Timezone.all_identifiers }
|
||||
|
||||
belongs_to :account
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class WorkingHour < ApplicationRecord
|
||||
validate :close_after_open, unless: :closed_all_day?
|
||||
|
||||
def self.today
|
||||
find_by(day_of_week: Date.current.cwday)
|
||||
find_by(day_of_week: Date.current.wday)
|
||||
end
|
||||
|
||||
def open_at?(time)
|
||||
|
||||
Reference in New Issue
Block a user