From af60077fe074613e2dcd931533847aa5b36a403a Mon Sep 17 00:00:00 2001 From: Kosuke Tanabe Date: Mon, 11 Apr 2022 00:45:34 +0900 Subject: [PATCH] implement InventoryFile#import --- app/models/inventory_file.rb | 98 ++++++++++++++++++++++++------ spec/models/inventory_file_spec.rb | 38 ++++++++++++ spec/models/inventory_spec.rb | 20 ++++++ 3 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 spec/models/inventory_file_spec.rb create mode 100644 spec/models/inventory_spec.rb diff --git a/app/models/inventory_file.rb b/app/models/inventory_file.rb index aa5ac52c3b..508be079e8 100644 --- a/app/models/inventory_file.rb +++ b/app/models/inventory_file.rb @@ -23,24 +23,88 @@ class InventoryFile < ApplicationRecord paginates_per 10 def import - self.reload - file = File.open(self.inventory.path) - reader = file.read - reader.split.each do |row| - identifier = row.to_s.strip - item = Item.find_by(item_identifier: identifier) - next unless item - next if self.items.where(id: item.id).select('items.id').first - - Inventory.create( - inventory_file: self, - item: item, - current_shelf_name: shelf.name, - item_identifier: identifier - ) + if ENV['ENJU_STORAGE'] == 's3' + body = Faraday.get(inventory.expiring_url(10)).body.force_encoding('UTF-8') + else + body = File.open(inventory.path).read + end + + CSV.parse(body, headers: true, col_sep: "\t") do |row| + # 電子書籍を除外 + next if row['shelf'] == 'web' + item_identifier = row['item_identifier'].to_s.strip + next if item_identifier.blank? + + Item.transaction do + item = Item.find_by(item_identifier: item_identifier) + Inventory.create( + inventory_file: self, + item: item, + current_shelf_name: shelf.name, + item_identifier: item_identifier + ) + + next unless item + + # 配架場所の変更を反映 + unless row['current_shelf'] == row['shelf'] + unless row['current_shelf'] == '-' or row['current_shelf'].blank? + shelf = Shelf.find_by(name: row['current_shelf']) + if item && shelf + item.update!(shelf: Shelf.find_by(name: row['current_shelf'])) + Rails.logger.info "Inventory: #{item.item_identifier}: shelf updated to #{shelf.name}" + end + end + end + next + + case row['circulation_status'] + when 'On Loan' + # 書架にないと報告されたが、確認したら貸出中の場合、不明日を削除 + if item.missing_since.present? + item.update!(missing_since: nil) + Rails.logger.info "Inventory: #{row['item_identifier']}: wrong report (rent)" + end + when 'Available On Shelf' + # 書架にないと報告されたが、確認したら書架にあった本を「利用可能」に変更 + if row['current_shelf'] == '-' or row['current_shelf'].blank? + if item.missing_since.present? && !item.rent? + item.update!(missing_since: nil, circulation_status: CirculationStatus.find_by(name: row['circulation_status'])) + Rails.logger.info "Inventory: #{row['item_identifier']}: wrong report (on shelf)" + end + end + + # 発見された不明本の状態を変更 + unless row['current_shelf'] == '-' or row['current_shelf'].blank? + if row['found_at'].present? + if item.rent? + item.update!(missing_since: nil) + else + item.update!( + missing_since: nil, + circulation_status: CirculationStatus.find_by(name: row['circulation_status']) + ) + end + Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to found" + end + end + + if item.circulation_status.name == 'In Process' + item.update!(circulation_status: CirculationStatus.find_by(name: 'Available On Shelf')) + Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to available" + end + when 'Missing' + item.circulation_status = CirculationStatus.find_by(name: 'Missing') unless item.circulation_status == 'Missing' + if row['missing_since'].blank? + item.update!(missing_since: Date.today) + Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to missing" + else + item.update!(missing_since: Date.parse(row['missing_since'])) + Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to missing" + end + end + end end - file.close - true end def export(col_sep: "\t") diff --git a/spec/models/inventory_file_spec.rb b/spec/models/inventory_file_spec.rb new file mode 100644 index 0000000000..e99a3084f2 --- /dev/null +++ b/spec/models/inventory_file_spec.rb @@ -0,0 +1,38 @@ +require 'rails_helper' + +describe InventoryFile do + fixtures :users + + before(:each) do + @file = InventoryFile.create( + user: users(:admin), + shelf: Shelf.find_by(name: 'first_shelf'), + inventory: File.new("#{Rails.root.to_s}/spec/fixtures/files/inventory_file_sample.tsv") + ) + end + + it "should be imported" do + expect(@file.import).to be_truthy + end + + it "should export results" do + expect(@file.export).to be_truthy + end +end + +# == Schema Information +# +# Table name: inventory_files +# +# id :bigint not null, primary key +# user_id :bigint +# note :text +# created_at :datetime not null +# updated_at :datetime not null +# inventory_file_name :string +# inventory_content_type :string +# inventory_file_size :integer +# inventory_updated_at :datetime +# inventory_fingerprint :string +# shelf_id :bigint not null +# diff --git a/spec/models/inventory_spec.rb b/spec/models/inventory_spec.rb new file mode 100644 index 0000000000..b49da4ea4a --- /dev/null +++ b/spec/models/inventory_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +describe Inventory do + # pending "add some examples to (or delete) #{__FILE__}" + +end + +# == Schema Information +# +# Table name: inventories +# +# id :bigint not null, primary key +# item_id :bigint +# inventory_file_id :bigint +# note :text +# created_at :datetime not null +# updated_at :datetime not null +# item_identifier :string not null +# current_shelf_name :string not null +#