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

feat: update product_type #11173

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
1 change: 0 additions & 1 deletion lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ $options{categories_exempted_from_nutriscore} = [
en:spices
en:sugar-substitutes
en:vinegars
en:pet-food
en:non-food-products
)
];
Expand Down
48 changes: 48 additions & 0 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ BEGIN {
&compute_completeness_and_missing_tags
&compute_product_history_and_completeness
&compute_languages
&review_product_type
&compute_changes_diff_text
&compute_data_sources
&compute_sort_keys
Expand Down Expand Up @@ -2955,6 +2956,48 @@ sub compute_languages ($product_ref) {
return;
}

=head2 review_product_type ( $product_ref )

Reviews the product type based on the presence of specific tags in the categories field.
Updates the product type if necessary.

=head3 Arguments

=head4 Product reference $product_ref

A reference to a hash containing the product details.

=cut

sub review_product_type ($product_ref) {

my $error;

my $expected_type;
if (has_tag($product_ref, "categories", "en:open-beauty-facts")) {
$expected_type = "beauty";
}
elsif (has_tag($product_ref, "categories", "en:open-food-facts")) {
$expected_type = "food";
}
elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) {
$expected_type = "petfood";
}
elsif (has_tag($product_ref, "categories", "en:open-products-facts")) {
$expected_type = "product";
}

if ($expected_type and ($product_ref->{product_type} ne $expected_type)) {
$error = change_product_type($product_ref, $expected_type);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @benbenben2 that's great.

That said, I would have first computed "expected_product_type" using "has_tag" and if it's defined then, I would have compared it to $product_ref->{product_type}.

It would allow less repetitions in the code.

Are you willing to refactor ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I applied some changes. Is it what you expected?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was more:

	my $expected_type;
	if (has_tag($product_ref, "categories", "en:open-food-facts")) {
		$expected_type = "food";
	}
	elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) {
		$expected_type = "petfood";
	}
	elsif (has_tag($product_ref, "categories", "en:open-products-facts")) {
		$expected_type = "product";
	}
	elsif (has_tag($product_ref, "categories", "en:open-beauty-facts")) {
		$expected_type = "beauty";
	}
	if ($expected_type and ($product_ref->{product_type} ne expected_type)) {
		$error = change_product_type($product_ref, expected_type);
	}


if ($error) {
$log->error("review_product_type - error", {error => $error, product_ref => $product_ref});
}

return;
}

=head2 process_product_edit_rules ($product_ref)

Process the edit_rules (see C<@edit_rules> in in Config file).
Expand Down Expand Up @@ -3678,6 +3721,11 @@ sub analyze_and_enrich_product_data ($product_ref, $response_ref) {

compute_languages($product_ref); # need languages for allergens detection and cleaning ingredients

# change the product type of non-food categorized products (issue #11094)
if (has_tag($product_ref, "categories", "en:incorrect-product-type")) {
review_product_type($product_ref);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something tricky: change_product_type() updates the product_type field and adds a old_product_type, so that store_product() can then remove the product from the old MongoDB collection.

But that means that store_product() must absolutely be called when the product_type is changed.

What you did works for changes done through the website or API, because we call store_product(). But we don't always call store_product(), there is an exception in scripts/update_all_products.pl : when we reprocess ingredients, scores etc.

So we need to change update_all_products.pl so that we do call store_product() if we have a $product_ref->{old_product_type} value. We can use an user like "update-product-type-bot".

See https://github.com/openfoodfacts/openfoodfacts-server/blob/main/scripts/update_all_products.pl#L1405

}

# Run special analysis, score calculations that it specific to the product type

if (($options{product_type} eq "food")) {
Expand Down
Loading
Loading