feat: Business hour Inbox APIs (#1821)

* feat: Business hour Inbox APIs
This commit is contained in:
Sojan Jose
2021-02-23 12:11:15 +05:30
committed by GitHub
parent a3b0de63de
commit 0e721653e5
14 changed files with 86 additions and 23 deletions

View File

@@ -23,6 +23,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
def update
@inbox.update(inbox_update_params.except(:channel))
@inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours]
return unless @inbox.channel.is_a?(Channel::WebWidget) && inbox_update_params[:channel].present?
@inbox.channel.update!(inbox_update_params[:channel])
@@ -80,7 +81,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
def inbox_update_params
params.permit(:enable_auto_assignment, :name, :avatar, :greeting_message, :greeting_enabled,
:working_hours_enabled, :out_of_office_message,
:working_hours_enabled, :out_of_office_message, :timezone,
channel: [
:website_url,
:widget_color,

View File

@@ -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
#

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -5,7 +5,8 @@ class MessageTemplates::HookExecutionService
return if inbox.agent_bot_inbox&.active?
::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
# TODO: let's see whether this is needed and remove this and related logic if not
#::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if should_send_email_collect?
end

View File

@@ -4,6 +4,10 @@ json.name resource.name
json.channel_type resource.channel_type
json.greeting_enabled resource.greeting_enabled
json.greeting_message resource.greeting_message
json.working_hours_enabled resource.working_hours_enabled
json.out_of_office_message resource.out_of_office_message
json.working_hours resource.weekly_schedule
json.timezone resource.timezone
json.avatar_url resource.try(:avatar_url)
json.page_id resource.channel.try(:page_id)
json.widget_color resource.channel.try(:widget_color)