2022-05-16 08:29:59 +00:00
|
|
|
class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
|
2022-07-11 07:13:24 +00:00
|
|
|
include ::FileTypeHelper
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
before_action :fetch_portal, except: [:index, :create]
|
2022-07-04 14:59:44 +00:00
|
|
|
before_action :check_authorization
|
2022-07-28 08:29:16 +00:00
|
|
|
before_action :set_current_page, only: [:index]
|
2020-09-25 21:02:34 +00:00
|
|
|
|
|
|
|
def index
|
2022-05-16 08:29:59 +00:00
|
|
|
@portals = Current.account.portals
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
|
2022-07-04 14:59:44 +00:00
|
|
|
def add_members
|
|
|
|
agents = Current.account.agents.where(id: portal_member_params[:member_ids])
|
|
|
|
@portal.members << agents
|
|
|
|
end
|
|
|
|
|
2022-10-21 20:43:15 +00:00
|
|
|
def show
|
|
|
|
@all_articles = @portal.articles
|
|
|
|
@articles = @all_articles.search(locale: params[:locale])
|
|
|
|
end
|
2022-04-20 10:30:37 +00:00
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
def create
|
2022-07-11 07:13:24 +00:00
|
|
|
@portal = Current.account.portals.build(portal_params)
|
2022-12-03 15:07:56 +00:00
|
|
|
@portal.custom_domain = parsed_custom_domain
|
2022-07-11 07:13:24 +00:00
|
|
|
@portal.save!
|
|
|
|
process_attached_logo
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2022-07-11 07:13:24 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@portal.update!(portal_params) if params[:portal].present?
|
2022-12-03 15:07:56 +00:00
|
|
|
# @portal.custom_domain = parsed_custom_domain
|
2022-07-11 07:13:24 +00:00
|
|
|
process_attached_logo
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e
|
|
|
|
render json: { error: @portal.errors.messages }.to_json, status: :unprocessable_entity
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2022-03-24 07:58:25 +00:00
|
|
|
@portal.destroy!
|
2020-09-25 21:02:34 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2022-07-05 11:45:38 +00:00
|
|
|
def archive
|
|
|
|
@portal.update(archive: true)
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2022-07-11 07:13:24 +00:00
|
|
|
def process_attached_logo
|
|
|
|
@portal.logo.attach(params[:logo])
|
|
|
|
end
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_portal
|
2022-05-16 08:29:59 +00:00
|
|
|
@portal = Current.account.portals.find_by(slug: permitted_params[:id])
|
2022-04-20 10:30:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_params
|
|
|
|
params.permit(:id)
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def portal_params
|
|
|
|
params.require(:portal).permit(
|
2022-09-01 11:14:06 +00:00
|
|
|
:account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug, :archived, { config: [:default_locale,
|
|
|
|
{ allowed_locales: [] }] }
|
2020-09-25 21:02:34 +00:00
|
|
|
)
|
|
|
|
end
|
2022-07-04 14:59:44 +00:00
|
|
|
|
|
|
|
def portal_member_params
|
|
|
|
params.require(:portal).permit(:account_id, member_ids: [])
|
|
|
|
end
|
2022-07-28 08:29:16 +00:00
|
|
|
|
|
|
|
def set_current_page
|
|
|
|
@current_page = params[:page] || 1
|
|
|
|
end
|
2022-12-03 15:07:56 +00:00
|
|
|
|
|
|
|
def parsed_custom_domain
|
|
|
|
domain = URI.parse(@portal.custom_domain)
|
|
|
|
domain.is_a?(URI::HTTP) ? domain.host : @portal.custom_domain
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|