Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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+**
Expand Down
28 changes: 19 additions & 9 deletions app/services/trade/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -167,21 +169,29 @@ 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
end

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
Expand Down
7 changes: 7 additions & 0 deletions app/services/trade/shipments_export.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# 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

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
Expand All @@ -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' }
Expand Down