feat: Added portal logo and the updated the JSON (#4996)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
|
||||
include ::FileTypeHelper
|
||||
|
||||
before_action :fetch_portal, except: [:index, :create]
|
||||
before_action :check_authorization
|
||||
|
||||
@@ -14,11 +16,21 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@portal = Current.account.portals.create!(portal_params)
|
||||
@portal = Current.account.portals.build(portal_params)
|
||||
render json: { error: @portal.errors.messages }, status: :unprocessable_entity and return unless @portal.valid?
|
||||
|
||||
@portal.save!
|
||||
process_attached_logo
|
||||
end
|
||||
|
||||
def update
|
||||
@portal.update!(portal_params)
|
||||
ActiveRecord::Base.transaction do
|
||||
@portal.update!(portal_params) if params[:portal].present?
|
||||
process_attached_logo
|
||||
rescue StandardError => e
|
||||
Rails.logger.error e
|
||||
render json: { error: @portal.errors.messages }.to_json, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@@ -31,6 +43,10 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
|
||||
head :ok
|
||||
end
|
||||
|
||||
def process_attached_logo
|
||||
@portal.logo.attach(params[:logo])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_portal
|
||||
@@ -43,7 +59,7 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController
|
||||
|
||||
def portal_params
|
||||
params.require(:portal).permit(
|
||||
:account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug, :archived
|
||||
:account_id, :color, :custom_domain, :header_text, :homepage_link, :name, :page_title, :slug, :archived, config: { allowed_locales: [] }
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ class Category < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :slug, presence: true
|
||||
validates :name, presence: true
|
||||
validate :allowed_locales
|
||||
validates :locale, uniqueness: { scope: %i[slug portal_id],
|
||||
message: 'should be unique in the category and portal' }
|
||||
accepts_nested_attributes_for :related_categories
|
||||
@@ -80,4 +81,14 @@ class Category < ApplicationRecord
|
||||
def ensure_account_id
|
||||
self.account_id = portal&.account_id
|
||||
end
|
||||
|
||||
def allowed_locales
|
||||
return if portal.blank?
|
||||
|
||||
allowed_locales = portal.config['allowed_locales']
|
||||
|
||||
return true if allowed_locales.include?(locale)
|
||||
|
||||
errors.add(:locale, "#{locale} of category is not part of portal's #{allowed_locales}.")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
# index_portals_on_slug (slug) UNIQUE
|
||||
#
|
||||
class Portal < ApplicationRecord
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
belongs_to :account
|
||||
has_many :categories, dependent: :destroy_async
|
||||
has_many :folders, through: :categories
|
||||
@@ -33,12 +35,35 @@ class Portal < ApplicationRecord
|
||||
class_name: :User,
|
||||
dependent: :nullify,
|
||||
source: :user
|
||||
has_one_attached :logo
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :name, presence: true
|
||||
validates :slug, presence: true, uniqueness: true
|
||||
validate :config_json_format
|
||||
|
||||
accepts_nested_attributes_for :members
|
||||
|
||||
scope :active, -> { where(archived: false) }
|
||||
|
||||
CONFIG_JSON_KEYS = %w[allowed_locales].freeze
|
||||
|
||||
def file_base_data
|
||||
{
|
||||
id: logo.id,
|
||||
portal_id: id,
|
||||
file_type: logo.content_type,
|
||||
account_id: account_id,
|
||||
file_url: url_for(logo),
|
||||
blob_id: logo.blob_id,
|
||||
filename: logo.filename.to_s
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def config_json_format
|
||||
denied_keys = config.keys - CONFIG_JSON_KEYS
|
||||
errors.add(:cofig, "in portal on #{denied_keys.join(',')} is not supported.") if denied_keys.any?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ json.page_title portal.page_title
|
||||
json.slug portal.slug
|
||||
json.archived portal.archived
|
||||
json.config portal.config
|
||||
json.logo portal.file_base_data if portal.logo.present?
|
||||
|
||||
json.portal_members do
|
||||
if portal.members.any?
|
||||
|
||||
Reference in New Issue
Block a user