mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Add read inventories and inventory items endpoints [SCI-2614][SCI-2616][SCI-2617]
This commit is contained in:
parent
938ec5acb8
commit
8d11e03d47
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
class ApiController < ActionController::API
|
||||
attr_reader :iss
|
||||
|
@ -50,7 +52,7 @@ module Api
|
|||
def authenticate
|
||||
if auth_params[:grant_type] == 'password'
|
||||
user = User.find_by_email(auth_params[:email])
|
||||
unless user && user.valid_password?(auth_params[:password])
|
||||
unless user&.valid_password?(auth_params[:password])
|
||||
raise StandardError, 'Default: Wrong user password'
|
||||
end
|
||||
payload = { user_id: user.id }
|
||||
|
|
31
app/controllers/api/v1/inventories_controller.rb
Normal file
31
app/controllers/api/v1/inventories_controller.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class InventoriesController < BaseController
|
||||
before_action :load_team, only: %i(show index)
|
||||
before_action :load_inventory, only: %i(show)
|
||||
|
||||
def index
|
||||
inventories =
|
||||
@team.repositories.page(params[:page]).per(params[:page_size])
|
||||
render json: inventories, each_serializer: InventorySerializer
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @inventory, serializer: InventorySerializer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_team
|
||||
@team = Team.find(params.require(:team_id))
|
||||
return render json: {}, status: :forbidden unless can_read_team?(@team)
|
||||
end
|
||||
|
||||
def load_inventory
|
||||
@inventory = @team.repositories.find(params.require(:id))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
app/controllers/api/v1/inventory_items_controller.rb
Normal file
32
app/controllers/api/v1/inventory_items_controller.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Api
|
||||
module V1
|
||||
class InventoryItemsController < BaseController
|
||||
before_action :load_team
|
||||
before_action :load_inventory
|
||||
|
||||
def index
|
||||
items =
|
||||
@inventory.repository_rows
|
||||
.includes(repository_cells: :repository_column)
|
||||
.includes(
|
||||
repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES
|
||||
).page(params[:page])
|
||||
.per(params[:page_size])
|
||||
render json: items,
|
||||
each_serializer: InventoryItemSerializer,
|
||||
include: :inventory_cells
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_team
|
||||
@team = Team.find(params.require(:team_id))
|
||||
return render json: {}, status: :forbidden unless can_read_team?(@team)
|
||||
end
|
||||
|
||||
def load_inventory
|
||||
@inventory = @team.repositories.find(params.require(:inventory_id))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -16,7 +16,6 @@ module Api
|
|||
|
||||
def load_team
|
||||
@team = Team.find(params.require(:id))
|
||||
return render json: {}, status: :not_found unless @team
|
||||
return render json: {}, status: :forbidden unless can_read_team?(@team)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,22 +8,26 @@ class RepositoryCell < ActiveRecord::Base
|
|||
dependent: :destroy
|
||||
belongs_to :repository_text_value,
|
||||
(lambda do
|
||||
where(repository_cells: { value_type: 'RepositoryTextValue' })
|
||||
joins(:repository_cell)
|
||||
.where(repository_cells: { value_type: 'RepositoryTextValue' })
|
||||
end),
|
||||
optional: true, foreign_key: :value_id
|
||||
belongs_to :repository_date_value,
|
||||
(lambda do
|
||||
where(repository_cells: { value_type: 'RepositoryDateValue' })
|
||||
joins(:repository_cell)
|
||||
.where(repository_cells: { value_type: 'RepositoryDateValue' })
|
||||
end),
|
||||
optional: true, foreign_key: :value_id
|
||||
belongs_to :repository_list_value,
|
||||
(lambda do
|
||||
where(repository_cells: { value_type: 'RepositoryListValue' })
|
||||
joins(:repository_cell)
|
||||
.where(repository_cells: { value_type: 'RepositoryListValue' })
|
||||
end),
|
||||
optional: true, foreign_key: :value_id
|
||||
belongs_to :repository_asset_value,
|
||||
(lambda do
|
||||
where(repository_cells: { value_type: 'RepositoryAssetValue' })
|
||||
joins(:repository_cell)
|
||||
.where(repository_cells: { value_type: 'RepositoryAssetValue' })
|
||||
end),
|
||||
optional: true, foreign_key: :value_id
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ class RepositoryRow < ApplicationRecord
|
|||
foreign_key: :last_modified_by_id,
|
||||
class_name: 'User',
|
||||
optional: true
|
||||
has_many :repository_cells, dependent: :destroy
|
||||
has_many :repository_cells, -> { order(:id) }, dependent: :destroy
|
||||
has_many :repository_columns, through: :repository_cells
|
||||
has_many :my_module_repository_rows,
|
||||
inverse_of: :repository_row, dependent: :destroy
|
||||
|
|
35
app/serializers/api/v1/inventory_cell_serializer.rb
Normal file
35
app/serializers/api/v1/inventory_cell_serializer.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
module Api
|
||||
module V1
|
||||
class InventoryCellSerializer < ActiveModel::Serializer
|
||||
attribute :id
|
||||
attribute :repository_column_id, key: :column_id
|
||||
attribute :data_type
|
||||
attribute :data
|
||||
|
||||
def data_type
|
||||
type_id = RepositoryColumn
|
||||
.data_types[object.repository_column.data_type]
|
||||
I18n.t("api.v1.inventory_data_types.t#{type_id}")
|
||||
end
|
||||
|
||||
def data
|
||||
value =
|
||||
case object.value_type
|
||||
when 'RepositoryTextValue'
|
||||
object.repository_text_value
|
||||
when 'RepositoryDateValue'
|
||||
object.repository_date_value
|
||||
when 'RepositoryListValue'
|
||||
object.repository_list_value
|
||||
when 'RepositoryAssetValue'
|
||||
object.repository_list_value
|
||||
end
|
||||
ActiveModelSerializers::SerializableResource.new(
|
||||
value,
|
||||
namespace: Api::V1,
|
||||
adapter: :attribute
|
||||
).as_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
12
app/serializers/api/v1/inventory_item_serializer.rb
Normal file
12
app/serializers/api/v1/inventory_item_serializer.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class InventoryItemSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
has_many :repository_cells, key: :inventory_cells,
|
||||
serializer: InventoryCellSerializer,
|
||||
class_name: 'RepositoryCell'
|
||||
end
|
||||
end
|
||||
end
|
7
app/serializers/api/v1/inventory_serializer.rb
Normal file
7
app/serializers/api/v1/inventory_serializer.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module Api
|
||||
module V1
|
||||
class InventorySerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
end
|
||||
end
|
||||
end
|
32
app/serializers/api/v1/repository_asset_value_serializer.rb
Normal file
32
app/serializers/api/v1/repository_asset_value_serializer.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class RepositoryAssetValueSerializer < ActiveModel::Serializer
|
||||
attributes :file_id, :file_name, :file_size, :url
|
||||
|
||||
def file_id
|
||||
object.asset&.id
|
||||
end
|
||||
|
||||
def file_name
|
||||
object.asset&.file_file_name
|
||||
end
|
||||
|
||||
def file_size
|
||||
object.asset&.file_file_size
|
||||
end
|
||||
|
||||
def url
|
||||
if !object.asset&.file_present
|
||||
nil
|
||||
elsif object.asset&.file&.is_stored_on_s3?
|
||||
object.asset.presigned_url(download: true)
|
||||
else
|
||||
# separate api endpoint for local files download is needed
|
||||
download_asset_path(object.asset.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
module Api
|
||||
module V1
|
||||
class RepositoryListValueSerializer < ActiveModel::Serializer
|
||||
attribute :formatted, key: :value
|
||||
attribute :repository_list_item_id, key: :list_item_id
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
module Api
|
||||
module V1
|
||||
class RepositoryTextValueSerializer < ActiveModel::Serializer
|
||||
attribute :formatted, key: :value
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1943,6 +1943,12 @@ en:
|
|||
status_ok: "Ok"
|
||||
expired_token: "Token is expired"
|
||||
invalid_token: "Token is invalid"
|
||||
v1:
|
||||
inventory_data_types:
|
||||
t0: "text"
|
||||
t1: "date"
|
||||
t2: "list"
|
||||
t3: "file"
|
||||
|
||||
Add: "Add"
|
||||
Asset: "File"
|
||||
|
|
|
@ -542,6 +542,9 @@ Rails.application.routes.draw do
|
|||
post 'auth/token', to: 'api#authenticate'
|
||||
namespace :v1 do
|
||||
resources :teams, only: %i(index show) do
|
||||
resources :inventories, only: %i(index show) do
|
||||
get 'items', to: 'inventory_items#index'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,6 +71,14 @@ RSpec.configure do |config|
|
|||
DatabaseCleaner.clean
|
||||
end
|
||||
|
||||
config.before(:all) do
|
||||
DatabaseCleaner.start
|
||||
end
|
||||
|
||||
config.after(:all) do
|
||||
DatabaseCleaner.clean
|
||||
end
|
||||
|
||||
config.around(:each, type: :background_job) do |example|
|
||||
run_background_jobs_immediately do
|
||||
example.run
|
||||
|
|
93
spec/requests/api/v1/inventories_controller_spec.rb
Normal file
93
spec/requests/api/v1/inventories_controller_spec.rb
Normal file
|
@ -0,0 +1,93 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "Api::V1::InventoriesController", type: :request do
|
||||
before :all do
|
||||
@user = create(:user)
|
||||
@teams = create_list(:team, 2, created_by: @user)
|
||||
create(:user_team, user: @user, team: @teams.first, role: 2)
|
||||
|
||||
# valid_inventories
|
||||
create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.first)
|
||||
create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.first)
|
||||
|
||||
# unaccessable_inventories
|
||||
create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.second)
|
||||
create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.second)
|
||||
|
||||
@valid_headers =
|
||||
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
|
||||
end
|
||||
|
||||
describe 'GET inventories, #index' do
|
||||
it 'Response with correct inventories' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventories_path(team_id: @teams.first.id),
|
||||
headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.repositories,
|
||||
each_serializer: Api::V1::InventorySerializer)
|
||||
.as_json[:data]
|
||||
)
|
||||
end
|
||||
|
||||
it 'When invalid request, user in not member of the team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventories_path(team_id: @teams.second.id),
|
||||
headers: @valid_headers
|
||||
expect(response).to have_http_status(403)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET inventory, #show' do
|
||||
it 'When valid request, user is member of the team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_path(team_id: @teams.first.id,
|
||||
id: @teams.first.repositories.first.id),
|
||||
headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.repositories.first,
|
||||
serializer: Api::V1::InventorySerializer)
|
||||
.as_json[:data]
|
||||
)
|
||||
end
|
||||
|
||||
it 'When invalid request, user in not member of the team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_path(team_id: @teams.second.id,
|
||||
id: @teams.second.repositories.first.id),
|
||||
headers: @valid_headers
|
||||
expect(response).to have_http_status(403)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
|
||||
it 'When invalid request, non existing inventory' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_path(team_id: @teams.first.id, id: 123),
|
||||
headers: @valid_headers
|
||||
expect(response).to have_http_status(404)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
|
||||
it 'When invalid request, repository from another team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_path(team_id: @teams.first.id,
|
||||
id: @teams.second.repositories.first.id),
|
||||
headers: @valid_headers
|
||||
expect(response).to have_http_status(404)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
end
|
||||
end
|
127
spec/requests/api/v1/inventory_items_controller_spec.rb
Normal file
127
spec/requests/api/v1/inventory_items_controller_spec.rb
Normal file
|
@ -0,0 +1,127 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "Api::V1::InventoryItemsController", type: :request do
|
||||
before :all do
|
||||
@user = create(:user)
|
||||
@teams = create_list(:team, 2, created_by: @user)
|
||||
create(:user_team, user: @user, team: @teams.first, role: 2)
|
||||
|
||||
# valid_inventory
|
||||
@valid_inventory = create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.first)
|
||||
|
||||
# unaccessable_inventory
|
||||
create(:repository, name: Faker::Name.unique.name,
|
||||
created_by: @user, team: @teams.second)
|
||||
|
||||
text_column = create(:repository_column, name: Faker::Name.unique.name,
|
||||
repository: @valid_inventory, data_type: :RepositoryTextValue)
|
||||
list_column = create(:repository_column, name: Faker::Name.unique.name,
|
||||
repository: @valid_inventory, data_type: :RepositoryListValue)
|
||||
list_item =
|
||||
create(:repository_list_item, repository: @valid_inventory,
|
||||
repository_column: list_column, data: Faker::Name.unique.name)
|
||||
file_column = create(:repository_column, name: Faker::Name.unique.name,
|
||||
repository: @valid_inventory, data_type: :RepositoryAssetValue)
|
||||
asset = create(:asset)
|
||||
|
||||
create_list(:repository_row, 100, repository: @valid_inventory)
|
||||
|
||||
@valid_inventory.repository_rows.each do |row|
|
||||
create(:repository_text_value,
|
||||
data: Faker::Name.name,
|
||||
repository_cell_attributes:
|
||||
{ repository_row: row, repository_column: text_column })
|
||||
create(:repository_list_value, repository_list_item: list_item,
|
||||
repository_cell_attributes:
|
||||
{ repository_row: row, repository_column: list_column })
|
||||
create(:repository_asset_value, asset: asset,
|
||||
repository_cell_attributes:
|
||||
{ repository_row: row, repository_column: file_column })
|
||||
end
|
||||
|
||||
@valid_headers =
|
||||
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
|
||||
end
|
||||
|
||||
describe 'GET inventory_items, #index' do
|
||||
it 'Response with correct inventory items, default per page' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_items_path(
|
||||
team_id: @teams.first.id,
|
||||
inventory_id: @teams.first.repositories.first.id
|
||||
), headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_inventory.repository_rows.limit(10),
|
||||
each_serializer: Api::V1::InventoryItemSerializer,
|
||||
include: :inventory_cells)
|
||||
.as_json[:data]
|
||||
)
|
||||
expect(hash_body[:included]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_inventory.repository_rows.limit(10),
|
||||
each_serializer: Api::V1::InventoryItemSerializer,
|
||||
include: :inventory_cells)
|
||||
.as_json[:included]
|
||||
)
|
||||
end
|
||||
|
||||
it 'Response with correct inventory items, 30 per page' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_items_path(
|
||||
team_id: @teams.first.id,
|
||||
inventory_id: @teams.first.repositories.first.id
|
||||
), params: { page_size: 100 }, headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_inventory.repository_rows.limit(100),
|
||||
each_serializer: Api::V1::InventoryItemSerializer,
|
||||
include: :inventory_cells)
|
||||
.as_json[:data]
|
||||
)
|
||||
expect(hash_body[:included]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_inventory.repository_rows.limit(100),
|
||||
each_serializer: Api::V1::InventoryItemSerializer,
|
||||
include: :inventory_cells)
|
||||
.as_json[:included]
|
||||
)
|
||||
end
|
||||
|
||||
it 'When invalid request, user in not member of the team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_items_path(
|
||||
team_id: @teams.second.id,
|
||||
inventory_id: @teams.second.repositories.first.id
|
||||
), headers: @valid_headers
|
||||
expect(response).to have_http_status(403)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
|
||||
it 'When invalid request, non existing inventory' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_items_path(
|
||||
team_id: @teams.first.id,
|
||||
inventory_id: 123
|
||||
), headers: @valid_headers
|
||||
expect(response).to have_http_status(404)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
|
||||
it 'When invalid request, repository from another team' do
|
||||
hash_body = nil
|
||||
get api_v1_team_inventory_items_path(
|
||||
team_id: @teams.first.id,
|
||||
inventory_id: @teams.second.repositories.first.id
|
||||
), headers: @valid_headers
|
||||
expect(response).to have_http_status(404)
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match({})
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue