diff --git a/app/packs/src/components/ChemicalTab.js b/app/packs/src/components/ChemicalTab.js index da45f726f9..747790cec9 100644 --- a/app/packs/src/components/ChemicalTab.js +++ b/app/packs/src/components/ChemicalTab.js @@ -219,19 +219,23 @@ export default class ChemicalTab extends React.Component { // eslint-disable-next-line class-methods-use-this stylePhrases = (str) => { const HazardPhrases = []; - // eslint-disable-next-line no-restricted-syntax - for (const [key, value] of Object.entries(str.h_statements)) { - // eslint-disable-next-line react/jsx-one-expression-per-line - const st =
{key}:{value}
; - HazardPhrases.push(st); + if (str && str.h_statements && str.h_statements.length !== 0) { + // eslint-disable-next-line no-restricted-syntax + for (const [key, value] of Object.entries(str.h_statements)) { + // eslint-disable-next-line react/jsx-one-expression-per-line + const st ={key}:{value}
; + HazardPhrases.push(st); + } } const precautionaryPhrases = []; - // eslint-disable-next-line no-restricted-syntax - for (const [key, value] of Object.entries(str?.p_statements || {})) { - // eslint-disable-next-line react/jsx-one-expression-per-line - const st ={key}:{value}
; - precautionaryPhrases.push(st); + if (str && str.p_statements && str?.p_statements?.length !== 0) { + // eslint-disable-next-line no-restricted-syntax + for (const [key, value] of Object.entries(str?.p_statements || {})) { + // eslint-disable-next-line react/jsx-one-expression-per-line + const st ={key}:{value}
; + precautionaryPhrases.push(st); + } } const pictogramsArray = str.pictograms?.map((i) => ( @@ -582,7 +586,7 @@ export default class ChemicalTab extends React.Component { vendor_product: vendorProduct }; ChemicalFetcher.saveSafetySheets(params).then((result) => { - if (result || result === 'file is already saved') { + if (result) { const value = `/safety_sheets/${productInfo.productNumber}_${productInfo.vendor}.pdf`; const chemicalData = chemical._chemical_data; const pathArr = []; diff --git a/lib/chemotion/chemicals_service.rb b/lib/chemotion/chemicals_service.rb index 54f5bde6fa..65558c249d 100644 --- a/lib/chemotion/chemicals_service.rb +++ b/lib/chemotion/chemicals_service.rb @@ -4,10 +4,14 @@ module Chemotion class ChemicalsService def self.request_options { headers: { - 'Access-Control-Request-Method' => 'GET', - 'Accept' => '*/*', - 'User-Agent': 'Google Chrome', - } } + 'Access-Control-Request-Method' => 'GET', + 'Accept' => '*/*', + 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0', + 'Accept-Encoding': 'gzip, deflate, br', + Connection: 'keep-alive', + }, + timeout: 15, + follow_redirects: true } end def self.merck_request(name) @@ -50,21 +54,10 @@ def self.alfa(name, language) 'Could not find safety data sheet from Thermofisher' end - def self.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names) - saved = false - unless safety_sheet_files_names.empty? - safety_sheet_files_names.each do |file| - if file == file_name - saved = true - break - end - end - end - saved - end - def self.write_file(file_path, link) - req_safety_sheet = HTTParty.get(link, request_options) + options = request_options.dup + options[:headers]['Origin'] = 'https://www.sigmaaldrich.com' + req_safety_sheet = HTTParty.get(link, options) file_name = "public/safety_sheets/#{file_path}" if req_safety_sheet.headers['Content-Type'] == 'application/pdf' File.binwrite(file_name, req_safety_sheet) @@ -72,15 +65,15 @@ def self.write_file(file_path, link) else 'there is no file to save' end + rescue HTTParty::RedirectionTooDeep => e + "Redirection limit exceeded: #{e}" + rescue HTTParty::TimeoutError => e + "Request timed out: #{e}" end def self.create_sds_file(file_path, link) - safety_sheet_files_names = Dir.children('public/safety_sheets') - if check_if_safety_sheet_already_saved(file_path, safety_sheet_files_names) == false - write_file(file_path, link) - else - 'file is already saved' - end + write_file(file_path, link) + sleep 1 rescue StandardError 'could not save safety data sheet' end diff --git a/lib/import/import_chemicals.rb b/lib/import/import_chemicals.rb index 03b2f66571..ed88f15754 100644 --- a/lib/import/import_chemicals.rb +++ b/lib/import/import_chemicals.rb @@ -106,7 +106,9 @@ def self.extract_product_number(url) match[1] else path = url.split('/') - path.last + filter = path.last + # extract digits from the string + filter.match(/.*?(?=[$%&?]|$)/)&.[](0) end end @@ -115,7 +117,7 @@ def self.create_safety_sheet_path(vendor, value, product_number, chemical) chemical['chemical_data'][0]['safetySheetPath'] ||= [] sheet_path = { "#{vendor}_link" => "#{SAFETY_SHEET_PATH}#{file_path}" } is_created = Chemotion::ChemicalsService.create_sds_file(file_path, value) - result = [true, 'file is already saved'].include?(is_created) + result = [true].include?(is_created) chemical['chemical_data'][0]['safetySheetPath'] << sheet_path if result chemical end diff --git a/spec/lib/chemotion/chemicals_service_spec.rb b/spec/lib/chemotion/chemicals_service_spec.rb index 8be85b6de8..60d36c3afd 100644 --- a/spec/lib/chemotion/chemicals_service_spec.rb +++ b/spec/lib/chemotion/chemicals_service_spec.rb @@ -4,26 +4,6 @@ describe Chemotion::ChemicalsService do describe Chemotion::ChemicalsService do - context 'when check_if_safety_sheet_already_saved is called' do - it 'returns true when file is already saved' do - file_name = 'safety_file.pdf' - safety_sheet_files_names = ['safety_file.pdf', 'other_file.pdf'] - expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_truthy - end - - it 'returns false when file is not already saved' do - file_name = 'safety_file.pdf' - safety_sheet_files_names = ['other_file.pdf', 'another_file.pdf'] - expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_falsey - end - - it 'returns false when safety_sheet_files_names is empty' do - file_name = 'safety_file.pdf' - safety_sheet_files_names = [] - expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_falsey - end - end - context 'when write_file is called' do let(:link) { 'https://www.sigmaaldrich.com/DE/en/sds/sigald/383112' } let(:file_path) { '252549_Merck.pdf' } @@ -55,15 +35,6 @@ result = described_class.create_sds_file(file_path, link) expect(result).to be_truthy end - - it 'returns file is already saved if already saved' do - file_path = '252549_Merck.pdf' - link = 'https://www.sigmaaldrich.com/US/en/sds/sial/252549' - allow(described_class).to receive(:check_if_safety_sheet_already_saved) - .with(file_path, anything).and_return(true) - result = described_class.create_sds_file(file_path, link) - expect(result).to eq('file is already saved') - end end context 'when health_section is called' do