Skip to content

Commit

Permalink
API : Organization Category (#2587)
Browse files Browse the repository at this point in the history
* bundle update

* add category_ids to organization in api

* organization open api

* Orga Categories API

* fix tests (added category to noesya fixture)
  • Loading branch information
SebouChu authored Jan 20, 2025
1 parent e774c3d commit 77e135d
Show file tree
Hide file tree
Showing 27 changed files with 2,284 additions and 395 deletions.
78 changes: 44 additions & 34 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GIT

GIT
remote: https://github.com/noesya/summernote-rails.git
revision: 32fd182c929cdcacaa6e3bd3569871bd025fa669
revision: f7ce423738fa98a1a2bacc509d6906e03b498a0e
specs:
summernote-rails (0.8.20.1)
nokogiri
Expand Down Expand Up @@ -119,20 +119,20 @@ GEM
autoprefixer-rails (10.4.19.0)
execjs (~> 2)
aws-eventstream (1.3.0)
aws-partitions (1.1029.0)
aws-sdk-core (3.214.1)
aws-partitions (1.1040.0)
aws-sdk-core (3.216.0)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.992.0)
aws-sigv4 (~> 1.9)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.96.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-kms (1.97.0)
aws-sdk-core (~> 3, >= 3.216.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.176.1)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-s3 (1.178.0)
aws-sdk-core (~> 3, >= 3.216.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.10.1)
aws-sigv4 (1.11.0)
aws-eventstream (~> 1, >= 1.0.2)
base64 (0.2.0)
bcrypt (3.1.20)
Expand Down Expand Up @@ -171,18 +171,23 @@ GEM
caxlsx_rails (0.6.4)
actionpack (>= 3.1)
caxlsx (>= 3.0)
citeproc (1.0.10)
citeproc (1.1.0)
date
forwardable
json
namae (~> 1.0)
citeproc-ruby (2.1.0)
observer (< 1.0)
open-uri (< 1.0)
citeproc-ruby (2.1.1)
citeproc (~> 1.0, >= 1.0.9)
csl (~> 2.0)
observer (< 1.0)
cocoon (1.2.15)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
countries (7.0.0)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
countries (7.1.0)
unaccent (~> 0.3)
country_select (10.0.0)
country_select (10.0.1)
countries (> 5.0, < 8.0)
crack (1.0.0)
bigdecimal
Expand Down Expand Up @@ -246,12 +251,15 @@ GEM
net-http (>= 0.5.0)
faraday-retry (2.2.1)
faraday (~> 2.0)
fastimage (2.3.1)
ffi (1.17.0)
fastimage (2.4.0)
ffi (1.17.1-arm64-darwin)
ffi (1.17.1-x86_64-darwin)
ffi (1.17.1-x86_64-linux-gnu)
figaro (1.2.0)
thor (>= 0.14.0, < 2)
font-awesome-sass (6.7.2)
sassc (~> 2.0)
forwardable (1.3.3)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
Expand All @@ -278,7 +286,7 @@ GEM
terminal-table (>= 1.5.1)
globalid (1.2.1)
activesupport (>= 6.1)
good_job (4.6.0)
good_job (4.7.0)
activejob (>= 6.1.0)
activerecord (>= 6.1.0)
concurrent-ruby (>= 1.3.1)
Expand Down Expand Up @@ -308,7 +316,7 @@ GEM
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
httpclient (2.8.3)
i18n (1.14.6)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
i18n_data (0.17.1)
simple_po_parser (~> 1.1)
Expand Down Expand Up @@ -361,8 +369,8 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.4)
loofah (2.23.1)
logger (1.6.5)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
lucide-rails (0.5.1)
Expand Down Expand Up @@ -399,7 +407,7 @@ GEM
nesty (1.0.2)
net-http (0.6.0)
uri
net-imap (0.5.4)
net-imap (0.5.5)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -408,11 +416,11 @@ GEM
timeout
net-smtp (0.5.0)
nio4r (2.7.4)
nokogiri (1.18.0-arm64-darwin)
nokogiri (1.18.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.18.0-x86_64-darwin)
nokogiri (1.18.2-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.18.0-x86_64-linux-gnu)
nokogiri (1.18.2-x86_64-linux-gnu)
racc (~> 1.4)
oauth (1.1.0)
oauth-tty (~> 1.0, >= 1.0.1)
Expand Down Expand Up @@ -455,7 +463,7 @@ GEM
pghero (3.6.1)
activerecord (>= 6.1)
popper_js (2.11.8)
psych (5.2.2)
psych (5.2.3)
date
stringio
public_suffix (6.0.1)
Expand All @@ -470,7 +478,8 @@ GEM
base64 (>= 0.1.0)
logger (>= 1.6.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack-session (2.1.0)
base64 (>= 0.1.0)
rack (>= 3.0.0)
rack-test (2.2.0)
rack (>= 1.3)
Expand Down Expand Up @@ -517,11 +526,11 @@ GEM
ffi (~> 1.0)
rbnacl (7.1.2)
ffi (~> 1)
rdoc (6.10.0)
rdoc (6.11.0)
psych (>= 4.0.0)
redis (5.3.0)
redis-client (>= 0.22.0)
redis-client (0.23.0)
redis-client (0.23.2)
connection_pool
regexp_parser (2.10.0)
reline (0.6.0)
Expand Down Expand Up @@ -574,10 +583,10 @@ GEM
ruby-vips (2.2.2)
ffi (~> 1.12)
logger
rubyzip (2.3.2)
sanitize (6.1.3)
rubyzip (2.4.1)
sanitize (7.0.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
nokogiri (>= 1.16.8)
sassc (2.4.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
Expand Down Expand Up @@ -635,15 +644,15 @@ GEM
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.2)
tilt (2.5.0)
tilt (2.6.0)
time (0.4.1)
date
timeout (0.4.3)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2024.2)
tzinfo-data (1.2025.1)
tzinfo (>= 1.0.0)
unaccent (0.4.0)
unicode-display_width (2.6.0)
Expand Down Expand Up @@ -678,7 +687,8 @@ GEM
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket (1.2.11)
websocket-driver (0.7.6)
websocket-driver (0.7.7)
base64
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
class Api::Osuny::University::Organizations::CategoriesController < Api::Osuny::ApplicationController
before_action :build_category, only: :create
before_action :load_category, only: [:show, :update, :destroy]

before_action :load_migration_identifier, only: [:create, :update]
before_action :ensure_same_migration_identifier, only: :update

def index
@categories = current_university.organization_categories.includes(:localizations)
end

def show
end

def create
if @category.save
render :show, status: :created
else
render json: { errors: @category.errors }, status: :unprocessable_entity
end
end

def update
if @category.update(category_params)
render :show
else
render json: { errors: @category.errors }, status: :unprocessable_entity
end
end

def upsert
categories_params = params[:categories] || []
every_category_has_migration_identifier = categories_params.all? { |category_params|
category_params[:migration_identifier].present?
}
unless every_category_has_migration_identifier
render_on_missing_migration_identifier
return
end

permitted_categories_params = categories_params.map { |unpermitted_params|
category_params_for_upsert(unpermitted_params)
}
@successfully_created_categories = []
@successfully_updated_categories = []
@invalid_categories_with_index = []
permitted_categories_params.each_with_index do |permitted_category_params, index|
category = current_university.organization_categories.find_by(migration_identifier: permitted_category_params[:migration_identifier])
if category.present?
if category.update(permitted_category_params)
@successfully_updated_categories << category
else
@invalid_categories_with_index << { category: category, index: index }
end
else
category = current_university.organization_categories.build(permitted_category_params)
if category.save
@successfully_created_categories << category
else
@invalid_categories_with_index << { category: category, index: index }
end
end
end

status = @invalid_categories_with_index.any? ? :unprocessable_entity : :ok
render 'upsert', status: status
end

def destroy
@category.destroy
head :no_content
end

protected

def build_category
@category = current_university.organization_categories.build
@category.assign_attributes(category_params)
end

def load_category
@category = current_university.organization_categories.find(params[:id])
end

def load_migration_identifier
@migration_identifier = category_params[:migration_identifier]
render_on_missing_migration_identifier unless @migration_identifier.present?
end

def ensure_same_migration_identifier
if @category.migration_identifier != @migration_identifier
render json: { error: 'Migration identifier does not match' }, status: :unprocessable_entity
end
end

def l10n_permitted_keys
[
:migration_identifier, :language, :name, :meta_description,
:path, :slug, :summary, :_destroy,
featured_image: [:url, :alt, :credit, :_destroy],
blocks: [:migration_identifier, :template_kind, :title, :position, :published, :html_class, data: {}]
]
end

def category_params
@category_params ||= begin
permitted_params = params.require(:category)
.permit(
:migration_identifier, :parent_id, :position, :is_taxonomy, localizations: {}
).merge(
university_id: current_university.id
)
set_l10n_attributes(permitted_params, @category) if permitted_params[:localizations].present?
permitted_params
end
end

def category_params_for_upsert(category_params)
permitted_params = category_params
.permit(
:migration_identifier, :parent_id, :position, :is_taxonomy, localizations: {}
).merge(
university_id: current_university.id
)
category = current_university.organization_categories.find_by(migration_identifier: permitted_params[:migration_identifier])
permitted_params[:id] = category.id if category.present?
set_l10n_attributes(permitted_params, category) if permitted_params[:localizations].present?
permitted_params
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def organization_params
.permit(
:migration_identifier, :kind, :active, :email, :phone,
:address, :zipcode, :city, :country, :nic, :siren,
localizations: {}
category_ids: [], localizations: {}
).merge(university_id: current_university.id)
set_l10n_attributes(permitted_params, @organization) if permitted_params[:localizations].present?
permitted_params
Expand All @@ -121,7 +121,7 @@ def organization_params_for_upsert(organization_params)
.permit(
:migration_identifier, :kind, :active, :email, :phone,
:address, :zipcode, :city, :country, :nic, :siren,
localizations: {}
category_ids: [], localizations: {}
).merge(university_id: current_university.id)
organization = current_university.organizations.find_by(migration_identifier: permitted_params[:migration_identifier])
permitted_params[:id] = organization.id if organization.present?
Expand Down
16 changes: 9 additions & 7 deletions app/models/university/organization/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#
# Table name: university_organization_categories
#
# id :uuid not null, primary key
# is_taxonomy :boolean default(FALSE)
# position :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
# parent_id :uuid indexed
# university_id :uuid not null, indexed
# id :uuid not null, primary key
# is_taxonomy :boolean default(FALSE)
# migration_identifier :string
# position :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
# parent_id :uuid indexed
# university_id :uuid not null, indexed
#
# Indexes
#
Expand All @@ -24,6 +25,7 @@ class University::Organization::Category < ApplicationRecord
include AsCategory
include AsIndirectObject
include Localizable
include WithOpenApi
include WithUniversity

has_and_belongs_to_many :organizations,
Expand Down
Loading

0 comments on commit 77e135d

Please sign in to comment.