Skip to content

Commit

Permalink
Merge pull request #217 from monsieurtanuki/feature/#191
Browse files Browse the repository at this point in the history
feature/#191 - added and computed possible product improvements
  • Loading branch information
monsieurtanuki authored Sep 16, 2021
2 parents c9f629b + 41e7cea commit 89c4e12
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
82 changes: 82 additions & 0 deletions lib/model/Product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,49 @@ import 'Nutriments.dart';

part 'Product.g.dart';

/// Possible improvements on a [Product] given its current data
enum ProductImprovement {
/// Possible message:
/// “The Eco-Score takes into account the origins of the ingredients.
/// Please take them into a photo (ingredient list and/or any geographic claim
/// or edit the product so that they can be taken into account. If it is not
/// clear, you can contact the food producer.”
ORIGINS_TO_BE_COMPLETED,

/// We could not compute an Nutri-Score for this product. It might be that the
/// category is an exception. If you believe this is an error,
/// you can email [email protected]
/// List of exceptions:
/// https://www.santepubliquefrance.fr/content/download/150262/file/QR_scientifique_technique_150421.pdf
/// You can get states with
/// https://world.openfoodfacts.org/api/v0/product/3414280980209.json?fields=ecoscore_grade,states_tags
/// Add a message if we have a category but no nutrition
CATEGORIES_BUT_NO_NUTRISCORE,

/// Possible message: “Add nutrition facts to compute the Nutri-Score”
/// Add a one-click option to indicate no nutrition facts on the packaging
/// This product doesn't have nutrition facts
/// Add a message if we have nutrition but no category
ADD_NUTRITION_FACTS,

/// Possible message: “Add a category to compute the Nutri-Score”
/// Help the user add the category if it is missing
/// You can use our Robotoff API to get your users to validate a prediction
/// Robotoff Questions
/// Add a message if we have no category and no nutrition
ADD_CATEGORY,

/// Prompt: “Add nutrition facts and a category to compute the Nutri-Score”
/// Add a one-click option to indicate no nutrition facts on the packaging
/// This product doesn't have nutrition facts
/// Add a message if the nutrition image is missing
ADD_NUTRITION_FACTS_AND_CATEGORY,

/// Add a message if the nutrition image is obsolete using the image refresh API
/// https://github.com/openfoodfacts/api-documentation/issues/15
OBSOLETE_NUTRITION_IMAGE,
}

/// This class contains most of the data about a specific product.
///
/// Please read the language mechanics explanation if you intend to display
Expand Down Expand Up @@ -461,4 +504,43 @@ class Product extends JsonObject {
}
return result;
}

/// Returns all the potential improvements given the quality of the data
///
/// For apps with contribution mode.
/// A typical use-case is to alert the end-users that they can improve
/// the quality of the OFF data by taking pictures (or something like that),
/// when displaying a [Product].
Set<ProductImprovement> getProductImprovements() {
final Set<ProductImprovement> result = {};
if (statesTags == null) {
return result;
}
if (statesTags!.contains('en:origins-to-be-completed')) {
result.add(ProductImprovement.ORIGINS_TO_BE_COMPLETED);
}
if (statesTags!.contains('en:categories-completed')) {
if (nutriscore == null) {
result.add(ProductImprovement.CATEGORIES_BUT_NO_NUTRISCORE);
}
if (statesTags!.contains('en:nutrition-facts-to-be-completed')) {
result.add(ProductImprovement.ADD_NUTRITION_FACTS);
}
}
if (statesTags!.contains('en:categories-to-be-completed')) {
if (statesTags!.contains('en:nutrition-facts-completed')) {
result.add(ProductImprovement.ADD_CATEGORY);
}
if (statesTags!.contains('en:nutrition-facts-to-be-completed')) {
result.add(ProductImprovement.ADD_NUTRITION_FACTS_AND_CATEGORY);
}
}
if (statesTags!.contains('en:nutrition-photo-to-be-selected') ||
statesTags!.contains('en:photos-to-be-uploaded')) {
result.add(ProductImprovement.OBSOLETE_NUTRITION_IMAGE);
}

// TODO (Optional) Add Nutri-Score disclaimers cf. https://github.com/openfoodfacts/openfoodfacts-dart/issues/193
return result;
}
}
10 changes: 10 additions & 0 deletions test/api_getProduct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ void main() {

group = result.product!.attributeGroups!
.singleWhere((element) => element.id == 'labels');

final Set<ProductImprovement> improvements =
result.product!.getProductImprovements();
expect(improvements.contains(ProductImprovement.ORIGINS_TO_BE_COMPLETED),
false);
});

test('get product without setting OpenFoodFactsLanguage or ProductField; ',
Expand Down Expand Up @@ -861,6 +866,11 @@ void main() {
image.language == OpenFoodFactsLanguage.GERMAN)
.url,
'https://static.openfoodfacts.net/images/products/500/011/254/8167/ingredients_de.7.400.jpg');

final Set<ProductImprovement> improvements =
result.product!.getProductImprovements();
expect(improvements.contains(ProductImprovement.ORIGINS_TO_BE_COMPLETED),
true);
});

test(
Expand Down

0 comments on commit 89c4e12

Please sign in to comment.