feat(lifecycle): REST endpoint for lifecycle config singleton
This commit is contained in:
parent
7c17a7cb96
commit
8690a49971
@ -1,5 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::Accounts::Captain::LifecycleConfigsController < Api::V1::Accounts::BaseController
|
||||
def show
|
||||
head :ok
|
||||
before_action :current_account
|
||||
before_action :set_config
|
||||
before_action -> { check_authorization(@config) }
|
||||
|
||||
def show; end
|
||||
|
||||
def update
|
||||
@config.update!(config_params)
|
||||
render 'api/v1/accounts/captain/lifecycle_configs/show'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_config
|
||||
@config = Captain::Lifecycle::Config.for_account(Current.account)
|
||||
end
|
||||
|
||||
def config_params
|
||||
params.require(:config).permit(
|
||||
:quiet_hours_enabled, :quiet_hours_from, :quiet_hours_to,
|
||||
:min_interval_minutes, :pause_on_customer_reply,
|
||||
:pause_on_customer_reply_within_minutes, :opt_out_label_id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/captain/lifecycle_config', resource: @config
|
||||
@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/captain/lifecycle_config', resource: @config
|
||||
@ -0,0 +1,10 @@
|
||||
json.id resource.id
|
||||
json.account_id resource.account_id
|
||||
json.quiet_hours_enabled resource.quiet_hours_enabled
|
||||
json.quiet_hours_from resource.quiet_hours_from&.strftime('%H:%M')
|
||||
json.quiet_hours_to resource.quiet_hours_to&.strftime('%H:%M')
|
||||
json.min_interval_minutes resource.min_interval_minutes
|
||||
json.pause_on_customer_reply resource.pause_on_customer_reply
|
||||
json.pause_on_customer_reply_within_minutes resource.pause_on_customer_reply_within_minutes
|
||||
json.opt_out_label_id resource.opt_out_label_id
|
||||
json.max_per_reservation 5
|
||||
@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Captain::LifecycleConfigs', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
def json_response
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/:account_id/captain/lifecycle_config' do
|
||||
it 'creates a default config on first access' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/lifecycle_config",
|
||||
headers: admin.create_new_auth_token, as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response[:quiet_hours_enabled]).to be(false)
|
||||
expect(json_response[:min_interval_minutes]).to eq(30)
|
||||
expect(json_response[:max_per_reservation]).to eq(5)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PATCH /api/v1/accounts/:account_id/captain/lifecycle_config' do
|
||||
it 'blocks agents' do
|
||||
patch "/api/v1/accounts/#{account.id}/captain/lifecycle_config",
|
||||
params: { config: { quiet_hours_enabled: true } },
|
||||
headers: agent.create_new_auth_token, as: :json
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it 'updates for admin' do
|
||||
patch "/api/v1/accounts/#{account.id}/captain/lifecycle_config",
|
||||
params: { config: { quiet_hours_enabled: true, quiet_hours_from: '22:00', min_interval_minutes: 60 } },
|
||||
headers: admin.create_new_auth_token, as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response[:quiet_hours_enabled]).to be(true)
|
||||
expect(json_response[:min_interval_minutes]).to eq(60)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user