From e5c5bc29f1097bf5d482486675b071de15e5b1b8 Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Fri, 17 Apr 2026 14:23:42 +0100 Subject: [PATCH] fix: CITES Trade DB: Fixes an issue affecting EU imports based on accession/exit date https://unep-wcmc.codebasehq.com/projects/cites-support-maintenance/tickets/463 --- CHANGELOG.md | 6 ++++++ app/services/trade/filter.rb | 28 +++++++++++++++++--------- app/services/trade/shipments_export.rb | 7 +++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c88f2f20..4ba5571b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +### 1.21.3 + +**CITES Trade DB** + +* Fixes another issue affecting filtering on EU as an importer/exporter + ### 1.21.2 **Species+** diff --git a/app/services/trade/filter.rb b/app/services/trade/filter.rb index 74f3a5d46..fa43ad1b5 100644 --- a/app/services/trade/filter.rb +++ b/app/services/trade/filter.rb @@ -87,15 +87,15 @@ def initialize_query draft_query = draft_query.where(term_id: @terms_ids) end - unless @importers_ids.empty? + unless importers_ids.empty? draft_query = draft_query.where( - importer_id: resolve_eu(@importers_ids) + importer_id: resolve_eu(importers_ids) ) end - unless @exporters_ids.empty? + unless exporters_ids.empty? draft_query = draft_query.where( - exporter_id: resolve_eu(@exporters_ids) + exporter_id: resolve_eu(exporters_ids) ) end @@ -144,11 +144,13 @@ def initialize_query if importer_eu_country_ids.present? sub_query = eu_country_date_query(@time_range_start, @time_range_end, 'importer', importer_eu_country_ids) - draft_query = draft_query.where.not(sub_query) if date_query.present? + + draft_query = draft_query.where.not(sub_query) if sub_query.present? end if exporter_eu_country_ids.present? sub_query = eu_country_date_query(@time_range_start, @time_range_end, 'exporter', exporter_eu_country_ids) + draft_query = draft_query.where.not(sub_query) if sub_query.present? end @@ -167,12 +169,20 @@ def eu_country_ids EuCountryDate.pluck(:geo_entity_id) end + def importers_ids + @importers_ids&.presence&.map(&:to_i) || [] + end + + def exporters_ids + @exporters_ids&.presence&.map(&:to_i) || [] + end + # this is to collect only eu country IDs to apply EU rules query to # e.g. EU + Austria we don't have to apply EU rules to Austria def importer_eu_country_ids @importer_eu_country_ids ||= - if @importer_ids&.presence&.include?(eu_id) - eu_country_ids - @importer_ids + if importers_ids.include?(eu_id) + eu_country_ids - exporters_ids else [] end @@ -180,8 +190,8 @@ def importer_eu_country_ids def exporter_eu_country_ids @exporter_eu_country_ids ||= - if @exporter_ids&.presence&.include?(eu_id) - eu_country_ids - @exporter_ids + if exporters_ids.include?(eu_id) + eu_country_ids - exporters_ids else [] end diff --git a/app/services/trade/shipments_export.rb b/app/services/trade/shipments_export.rb index 326b59744..9cafe287c 100644 --- a/app/services/trade/shipments_export.rb +++ b/app/services/trade/shipments_export.rb @@ -1,9 +1,12 @@ # Implements "raw" shipments export class Trade::ShipmentsExport < Species::CsvCopyExport include Trade::ShipmentReportQueries + PUBLIC_CSV_LIMIT = 1000000 PUBLIC_WEB_LIMIT = 50000 + include ActiveModel::SerializerSupport + delegate :report_type, to: :@search delegate :page, to: :@search delegate :per_page, to: :@search @@ -11,6 +14,7 @@ class Trade::ShipmentsExport < Species::CsvCopyExport def initialize(filters) @search = Trade::Filter.new(filters) @filters = @search.options.merge(csv_separator: filters['csv_separator']) + initialize_csv_separator(filters[:csv_separator]) initialize_file_name end @@ -19,12 +23,15 @@ def export unless csv_cached? to_csv end + unless csv_created? Rails.logger.error('Unable to generate output') return false end + ctime = File.ctime(@file_name).strftime('%Y-%m-%d %H:%M') @public_file_name = "#{resource_name}_#{ctime}_#{@csv_separator}_separated.csv" + [ @file_name, { filename: public_file_name, type: 'text/csv' }