Filter already connected items on repository item relationship modal [SCI-9916] (#6934)

This commit is contained in:
ajugo 2024-01-15 09:41:52 +01:00 committed by GitHub
parent 81fbd38b0c
commit 98ee391d03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 29 deletions

View file

@ -1,10 +1,10 @@
# frozen_string_literal: true
class RepositoryRowConnectionsController < ApplicationController
before_action :load_repository, except: %i(repositories)
before_action :load_repository, except: :repositories
before_action :load_create_vars, only: :create
before_action :check_read_permissions, except: :repositories
before_action :load_repository_row, except: %i(repositories repository_rows)
before_action :load_repository_row, except: :repositories
before_action :check_manage_permissions, only: %i(create destroy)
def index
@ -97,11 +97,16 @@ class RepositoryRowConnectionsController < ApplicationController
end
def repository_rows
repository_rows = @repository.repository_rows
.search_by_name_and_id(current_user, current_user.teams, params[:query])
.order(name: :asc)
.page(params[:page] || 1)
.per(Constants::SEARCH_LIMIT)
selected_repository = Repository.accessible_by_teams(current_team).find(params[:selected_repository_id])
repository_rows = selected_repository.repository_rows
.where.not(id: @repository_row.id)
.where.not(id: @repository_row.parent_connections.select(:parent_id))
.where.not(id: @repository_row.child_connections.select(:child_id))
.search_by_name_and_id(current_user, current_user.teams, params[:query])
.order(name: :asc)
.page(params[:page] || 1)
.per(Constants::SEARCH_LIMIT)
render json: {
data: repository_rows.select(:id, :name, :archived, :repository_id)
.preload(:repository)

View file

@ -130,15 +130,13 @@
import SelectSearch from '../shared/select_search.vue';
import ChecklistSearch from '../shared/checklist_search.vue';
import Select from '../shared/select.vue';
import ChecklistSelect from '../shared/checklist_select.vue';
export default {
name: 'RepositoryItemRelationshipsModal',
components: {
'select-search': SelectSearch,
ChecklistSearch,
Select,
'checklist-select': ChecklistSelect
Select
},
created() {
window.repositoryItemRelationshipsModal = this;
@ -174,7 +172,10 @@ export default {
this.loadingInventories = true;
$.ajax({
url: `${this.inventoriesUrl}?page=${this.nextInventoriesPage}`,
url: this.inventoriesUrl,
data: {
page: this.nextInventoriesPage
},
success: (result) => {
this.inventoryOptions = this.inventoryOptions.concat(result.data.map((val) => [val.id, val.name]));
this.loadingInventories = false;
@ -187,7 +188,11 @@ export default {
this.loadingItems = true;
$.ajax({
url: `${this.inventoryItemsUrl}/?page=${this.nextItemsPage}&repository_id=${inventoryValue}`,
url: this.inventoryItemsUrl,
data: {
page: this.nextItemsPage,
selected_repository_id: inventoryValue
},
success: (result) => {
this.itemOptions = this.itemOptions.concat(result.data.map((val) => ({ id: val.id, label: val.name })));
this.loadingItems = false;
@ -237,7 +242,7 @@ export default {
this.nextItemsPage = 1;
if (value) {
this.loadingItems = true;
this.itemParams = [`repository_id=${value}`];
this.itemParams = { selected_repository_id: value };
}
this.$nextTick(() => {
this.fetchInventoryItems(value);

View file

@ -81,7 +81,7 @@ export default {
return;
}
if (this.optionsUrlValue) {
if (this.optionsUrl) {
// reset current options and next page when lazy loading is enabled
if (this.lazyLoadEnabled) {
this.currentOptions = [];
@ -106,14 +106,6 @@ export default {
return this.currentOptions.find(({ id }) => id === this.selectedValues[0])?.label;
}
return `${this.selectedValues.length} ${this.i18n.t('general.options_selected')}`;
},
optionsUrlValue() {
if (!this.optionsUrl) return '';
let url = `${this.optionsUrl}?query=${this.query || ''}`;
if (this.lazyLoadEnabled && this.nextPage) url = `${url}&page=${this.nextPage}`;
if (this.params.length) url = `${url}&${this.params.join('&')}`;
return url;
}
},
methods: {
@ -128,7 +120,12 @@ export default {
if (!this.nextPage || !this.optionsUrl) return;
$.ajax({
url: this.optionsUrlValue,
url: this.optionsUrl,
data: {
...(this.query && { query: this.query }),
...(this.lazyLoadEnabled && this.nextPage && { page: this.nextPage }),
...this.params
},
success: (result) => {
if (this.lazyLoadEnabled) {
this.nextPage = result.next_page;

View file

@ -31,7 +31,8 @@ json.actions do
end
json.row_connections do
json.inventories_url repository_row_connections_repositories_url
json.inventory_items_url repository_row_connections_repository_rows_url
json.inventory_items_url repository_rows_repository_repository_row_repository_row_connections_path(@repository,
@repository_row)
json.create_url repository_repository_row_repository_row_connections_url(@repository, @repository_row)
end
end

View file

@ -735,7 +735,11 @@ Rails.application.routes.draw do
get :actions_toolbar
end
resources :repository_row_connections, only: %i(index create destroy)
resources :repository_row_connections, only: %i(index create destroy) do
collection do
get :repository_rows
end
end
member do
get 'repository_stock_value/new', to: 'repository_stock_values#new', as: 'new_repository_stock'
get 'repository_stock_value/edit', to: 'repository_stock_values#edit', as: 'edit_repository_stock'
@ -792,7 +796,6 @@ Rails.application.routes.draw do
namespace :repository_row_connections do
get :repositories
get :repository_rows
end
resources :connected_devices, controller: 'users/connected_devices', only: %i(destroy)

View file

@ -123,14 +123,44 @@ describe RepositoryRowConnectionsController, type: :controller do
end
describe 'GET #repository_rows' do
let!(:child_repository_row) { create :repository_row, repository: repository }
let!(:parent_repository_row) { create :repository_row, repository: repository }
let!(:child_connection) do
create :repository_row_connection,
parent: repository_row,
child: child_repository_row,
created_by: user,
last_modified_by: user
end
let!(:parent_connection) do
create :repository_row_connection,
child: repository_row,
parent: parent_repository_row,
created_by: user,
last_modified_by: user
end
it 'returns a successful response' do
get :repository_rows, format: :json, params: { repository_id: repository.id }
get :repository_rows, format: :json, params: {
repository_id: repository.id,
repository_row_id: repository_row.id,
selected_repository_id: repository.id
}
expect(response).to have_http_status(:success)
end
it 'returns the correct data structure' do
get :repository_rows, format: :json, params: { repository_id: repository.id }
expect(response.body).to include(repository_row.name)
get :repository_rows, format: :json, params: {
repository_id: repository.id,
repository_row_id: repository_row.id,
selected_repository_id: repository.id
}
expect(response.body).not_to include(repository_row.name)
expect(response.body).not_to include(child_repository_row.name)
expect(response.body).not_to include(parent_repository_row.name)
expect(response.body).to include(other_repository_row.name)
end
end
end