Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-canonicalize packaging component properties #8246

Merged
merged 1 commit into from
Mar 24, 2023
Merged
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
24 changes: 24 additions & 0 deletions lib/ProductOpener/Packaging.pm
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,27 @@ sub migrate_old_number_and_quantity_fields_202211 ($product_ref) {
return;
}

=head2 canonicalize_packaging_components_properties ($product_ref) {

Re-canonicalize the shape, material and recycling properties of packaging components.
This is useful in particular if the corresponding taxonomies have changed.

=cut

sub canonicalize_packaging_components_properties ($product_ref) {

foreach my $packaging_ref (@{$product_ref->{packagings}}) {
foreach my $property ("shape", "material", "recycling") {
if (defined $packaging_ref->{$property}) {
my $tagtype = $packaging_taxonomies{$property};
$packaging_ref->{$property}
= canonicalize_taxonomy_tag($product_ref->{lc}, $tagtype, $packaging_ref->{$property});
}
}
}
return;
}

=head2 set_packaging_misc_tags($product_ref)

Set some tags in the /misc/ facet so that we can track the products that have
Expand Down Expand Up @@ -867,6 +888,9 @@ sub analyze_and_combine_packaging_data ($product_ref, $response_ref) {
# TODO: remove once all products have been migrated
migrate_old_number_and_quantity_fields_202211($product_ref);

# Re-canonicalize the packaging components properties, in case the corresponding taxonomies have changed
canonicalize_packaging_components_properties($product_ref);

# The packaging text field (populated by OCR of the packaging image and/or contributors or producers)
# is used as input only if the packagings structure is empty
if ((scalar @{$product_ref->{packagings}} == 0) and (defined $product_ref->{packaging_text})) {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/packaging.t
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,30 @@ foreach my $test_ref (@tests) {
}
}

my $product_ref = {
lc => "fr",
packagings => [
{
shape => 'fr:bouteille',
material => 'en:pet',
recycling => 'à jeter'
},
],
};

ProductOpener::Packaging::canonicalize_packaging_components_properties($product_ref);

is_deeply(
$product_ref->{packagings},
[
{
'material' => 'en:pet-1-polyethylene-terephthalate',
'recycling' => 'en:discard',
'shape' => 'en:bottle'
},
]
) or diag explain $product_ref->{packagings};

#

done_testing();