Skip to content

Commit

Permalink
fix: pro platform product writes to the public platform MongoDB datab…
Browse files Browse the repository at this point in the history
…ase (#11065)

fixes #11064

debug notes in the issue:
#11064 (comment)
  • Loading branch information
stephanegigandet authored Nov 28, 2024
1 parent 9048011 commit f77eb82
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 20 deletions.
6 changes: 5 additions & 1 deletion cgi/product_jqm_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ =head1 DESCRIPTION
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server
# unless we are on the pro platform

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
if ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{
redirect_to_url($request_ref, 307,
format_subdomain($subdomain, $product_ref->{product_type}) . '/cgi/product_jqm.pl?code=' . $code);
}
Expand Down
6 changes: 5 additions & 1 deletion cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,12 @@ ($product_ref)
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server
# unless we are on the pro platform
# We use a 302 redirect so that browsers issue a GET request to display the form (even if we received a POST request)
if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
if ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{
redirect_to_url($request_ref, 302,
format_subdomain($subdomain, $product_ref->{product_type}) . '/cgi/product.pl?code=' . $code);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ProductOpener/APIProductRead.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,14 @@ sub read_product_api ($request_ref) {
);
$response_ref->{result} = {id => "product_not_found"};
}
elsif ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
elsif ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{

# If the product has a product_type and it is not the product_type of the server,
# redirect to the correct server if the request includes a matching product_type parameter (or the "all" product type)
# unless we are on the pro platform

my $requested_product_type = single_param("product_type");
if ( (defined $requested_product_type)
Expand Down
6 changes: 5 additions & 1 deletion lib/ProductOpener/APIProductWrite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,12 @@ sub write_product_api ($request_ref) {
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server
# unless we are on the pro platform

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
if ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{
redirect_to_url($request_ref, 307,
format_subdomain($subdomain, $product_ref->{product_type}) . '/api/v3/product/' . $code);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ProductOpener/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@ $ProductOpener::Config::options{other_servers} = {
domain => "openproductsfacts.org",
},
opff => {
prefix => "opff",
name => "Open Pet Food Facts",
mongodb => "opff",
domain => "openpetfoodfacts.org",
},
'off-pro' => {
name => "Open Food Facts Pro",
mongodb => "off-pro",
domain => "pro.openfoodfacts.org",
}
};

Expand Down
19 changes: 16 additions & 3 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7925,8 +7925,12 @@ JS
}

# If the product has a product_type and it is not the product_type of the server, redirect to the correct server
# unless we are in the pro platform

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
if ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{
redirect_to_url($request_ref, 302,
format_subdomain($subdomain, $product_ref->{product_type}) . product_url($product_ref));
}
Expand Down Expand Up @@ -10614,16 +10618,25 @@ sub display_product_api ($request_ref) {
$response{jqm} .= $html;
}
}
elsif ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
elsif ( (not $server_options{private_products})
and (defined $product_ref->{product_type})
and ($product_ref->{product_type} ne $options{product_type}))
{

# If the product has a product_type and it is not the product_type of the server,
# redirect to the correct server if the request includes a matching product_type parameter (or the "all" product type)
# If we are on the producers platform, don't redirect as we have only one server for all flavors

my $requested_product_type = single_param("product_type");
if ( (defined $requested_product_type)
and (($requested_product_type eq "all") or ($requested_product_type eq $product_ref->{product_type})))
{
redirect_to_url($request_ref, 302,
my $status_code = 302;
# If the method is POST, PUT, PATCH or DELETE, return a 307 status code
if ($request_ref->{api_method} =~ /^(POST|PUT|PATCH|DELETE)$/) {
$status_code = 307;
}
redirect_to_url($request_ref, $status_code,
format_subdomain($subdomain, $product_ref->{product_type}) . "/"
. $request_ref->{original_query_string});
}
Expand Down
50 changes: 38 additions & 12 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ The product id.

sub product_id_for_owner ($ownerid, $code) {

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if ($server_options{private_products}) {
if (defined $ownerid) {
return $ownerid . "/" . $code;
}
Expand Down Expand Up @@ -561,8 +561,7 @@ The relative path for the product.

sub product_path_from_id ($product_id) {

if ( (defined $server_options{private_products})
and ($server_options{private_products})
if ( ($server_options{private_products})
and ($product_id =~ /\//))
{
return $` . "/" . split_code($');
Expand Down Expand Up @@ -596,7 +595,7 @@ sub product_path ($product_ref) {
die("Argument of product_path() must be a reference to the product hash object, not a scalar: $product_ref\n");
}

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if ($server_options{private_products}) {
return $product_ref->{owner} . "/" . split_code($product_ref->{code});
}
else {
Expand Down Expand Up @@ -642,7 +641,7 @@ sub product_exists ($product_id) {

sub get_owner_id ($userid, $orgid, $ownerid) {

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if ($server_options{private_products}) {

if (not defined $ownerid) {
if (defined $orgid) {
Expand Down Expand Up @@ -704,7 +703,7 @@ sub init_product ($userid, $orgid, $code, $countryid) {
$product_ref->{server} = $server;
}

if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if ($server_options{private_products}) {
my $ownerid = get_owner_id($userid, $orgid, $Owner_id);

$product_ref->{owner} = $ownerid;
Expand Down Expand Up @@ -1034,6 +1033,36 @@ sub compute_sort_keys ($product_ref) {
return;
}

=head2 get_server_for_product ( $product_ref )
Return the MongoDB database for the product: off, obf, opf, opff or off-pro
If we are on the producers platform, we currently have only one server: off-pro
=cut

sub get_server_for_product ($product_ref) {

my $server;

# On the pro platform, we currently have only one server
if ($server_options{private_products}) {
$server = $mongodb; # off-pro
}
else {
# In case we need to move a product from OFF to OBF etc.
# we will have a old_product_type field

$server
= $options{product_types_flavors}{$product_ref->{old_product_type}
|| $product_ref->{product_type}
|| $options{product_type}};

}

return $server;
}

=head2 store_product ($user_id, $product_ref, $comment)
Save changes of a product:
Expand Down Expand Up @@ -1084,10 +1113,7 @@ sub store_product ($user_id, $product_ref, $comment) {
# we will have a old_product_type field

# Get the previous server and collection for the product
my $previous_server
= $options{product_types_flavors}{$product_ref->{old_product_type}
|| $product_ref->{product_type}
|| $options{product_type}};
my $previous_server = get_server_for_product($product_ref);

# We use the was_obsolete flag so that we can remove the product from its old collection
# (either products or products_obsolete) if its obsolete status has changed
Expand All @@ -1104,7 +1130,7 @@ sub store_product ($user_id, $product_ref, $comment) {
}

# Get the server and collection for the product that we will write
my $new_server = $options{product_types_flavors}{$product_ref->{product_type} || $options{product_type}};
my $new_server = get_server_for_product($product_ref);
my $new_products_collection = get_products_collection(
{database => $options{other_servers}{$new_server}{mongodb}, obsolete => $product_ref->{obsolete}});

Expand Down Expand Up @@ -1699,7 +1725,7 @@ sub compute_completeness_and_missing_tags ($product_ref, $current_ref, $previous
}

# On the producers platform, keep track of which products have changes to be exported
if ((defined $server_options{private_products}) and ($server_options{private_products})) {
if ($server_options{private_products}) {
if ( (defined $product_ref->{last_exported_t})
and ($product_ref->{last_exported_t} > $product_ref->{last_modified_t}))
{
Expand Down

0 comments on commit f77eb82

Please sign in to comment.