Add external_id field to repository list and checklist items [SCI-9454] (#6702)

This commit is contained in:
Alex Kriuchykhin 2023-11-24 14:21:11 +01:00 committed by GitHub
parent 39f4637351
commit 9e9f8ab402
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 6 deletions

View file

@ -111,7 +111,7 @@ class RepositoryStockValue < ApplicationRecord
self.repository_stock_unit_item = repository_cell
.repository_column
.repository_stock_unit_items
.find(new_data[:unit_item_id])
.find_by(id: new_data[:unit_item_id])
self.last_modified_by = user
new_amount = new_data[:amount].to_d
delta = new_amount - amount.to_d
@ -157,7 +157,7 @@ class RepositoryStockValue < ApplicationRecord
value.repository_stock_unit_item = value.repository_cell
.repository_column
.repository_stock_unit_items
.find(payload['unit_item_id'])
.find_by(id: payload['unit_item_id'])
value
else
raise ActiveRecord::RecordInvalid, 'Missing amount value'

View file

@ -144,6 +144,8 @@ class Extends
RepositoryStockValue
)
STI_PRELOAD_CLASSES = %w(LinkedRepository)
# Array of preload relations used in search query for repository rows
REPOSITORY_ROWS_PRELOAD_RELATIONS = []

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
unless Rails.application.config.eager_load
Rails.application.config.to_prepare do
Extends::STI_PRELOAD_CLASSES.each(&:constantize)
end
end

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require File.expand_path('app/helpers/database_helper')
class AddExternalIdToRepositoryColumnItems < ActiveRecord::Migration[6.1]
include DatabaseHelper
def up
add_column :repository_list_items, :external_id, :string, null: true
add_column :repository_checklist_items, :external_id, :string, null: true
add_index :repository_list_items,
:external_id,
unique: true,
name: 'unique_index_repository_list_items_on_external_id'
add_index :repository_checklist_items,
:external_id,
unique: true,
name: 'unique_index_repository_checklist_items_on_external_id'
add_gin_index_without_tags(:repository_list_items, :external_id)
add_gin_index_without_tags(:repository_checklist_items, :external_id)
end
def down
remove_index :repository_rows, name: 'index_repository_checklist_items_on_external_id'
remove_index :repository_rows, name: 'index_repository_list_items_on_external_id'
remove_index :repository_checklist_items,
:external_id,
name: 'unique_index_repository_checklist_items_on_external_id'
remove_index :repository_list_items,
:external_id,
name: 'unique_index_repository_list_items_on_external_id'
remove_column :repository_checklist_items, :external_id, :string, null: true
remove_column :repository_list_items, :external_id, :string, null: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_10_23_091358) do
ActiveRecord::Schema[7.0].define(version: 2023_11_07_163821) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
enable_extension "pg_trgm"
@ -657,8 +657,11 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_23_091358) do
t.bigint "last_modified_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "external_id"
t.index "trim_html_tags((data)::text) gin_trgm_ops", name: "index_repository_checklist_items_on_data", using: :gin
t.index "trim_html_tags((external_id)::text) gin_trgm_ops", name: "index_repository_checklist_items_on_external_id", using: :gin
t.index ["created_by_id"], name: "index_repository_checklist_items_on_created_by_id"
t.index ["external_id"], name: "unique_index_repository_checklist_items_on_external_id", unique: true
t.index ["last_modified_by_id"], name: "index_repository_checklist_items_on_last_modified_by_id"
t.index ["repository_column_id"], name: "index_repository_checklist_items_on_repository_column_id"
end
@ -750,8 +753,11 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_23_091358) do
t.bigint "last_modified_by_id"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "external_id"
t.index "trim_html_tags((external_id)::text) gin_trgm_ops", name: "index_repository_list_items_on_external_id", using: :gin
t.index "trim_html_tags(data) gin_trgm_ops", name: "index_repository_list_items_on_data", using: :gin
t.index ["created_by_id"], name: "index_repository_list_items_on_created_by_id"
t.index ["external_id"], name: "unique_index_repository_list_items_on_external_id", unique: true
t.index ["last_modified_by_id"], name: "index_repository_list_items_on_last_modified_by_id"
t.index ["repository_column_id"], name: "index_repository_list_items_on_repository_column_id"
end

View file

@ -749,7 +749,7 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
)
end
it 'Response with inventory cell, stock cell, missing stock unit' do
it 'Response with inventory cell, stock cell, empty stock unit' do
hash_body = nil
empty_item = create(:repository_row, repository: @valid_inventory)
invalid_file_body = @valid_stock_body.deep_dup
@ -759,9 +759,15 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
inventory_id: @valid_inventory.id,
item_id: empty_item.id
), params: invalid_file_body.to_json, headers: @valid_headers
expect(response).to have_http_status 404
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body['errors'][0]).to include('status': 404)
expect(hash_body[:data]).to match(
JSON.parse(
ActiveModelSerializers::SerializableResource
.new(RepositoryCell.last, serializer: Api::V1::InventoryCellSerializer)
.to_json
)['data']
)
end
it 'Response with inventory cell, stock cell, missing amount' do