Files
FarmaFinder/apps/backend/off-service.js
T
Antoni Nuñez Romeu f1b0eab11d
Run Tests on Branches / Detect Changes (push) Successful in 11s
Run Tests on Branches / Frontend Tests (push) Has been cancelled
Run Tests on Branches / Frontend Mobile Tests (push) Has been cancelled
Run Tests on Branches / Backend Tests (push) Has been cancelled
feat(backend): add Open Food Facts authentication support
- Add OFF_USERNAME and OFF_PASSWORD env vars
- Use Basic Auth when credentials are provided
- Improves rate limits for authenticated requests
- Auth is optional (works without credentials)
2026-07-13 16:28:34 +02:00

169 lines
4.8 KiB
JavaScript

import axios from 'axios';
import redisClient from './redis-client.js';
const OFF_API_BASE = 'https://world.openfoodfacts.org';
const CACHE_TTL = 3600;
// Open Food Facts authentication (optional, improves rate limits)
const OFF_USERNAME = process.env.OFF_USERNAME;
const OFF_PASSWORD = process.env.OFF_PASSWORD;
function getOffAuth() {
if (OFF_USERNAME && OFF_PASSWORD) {
return {
username: OFF_USERNAME,
password: OFF_PASSWORD,
};
}
return null;
}
export function transformOFFProduct(offProduct) {
if (!offProduct || !offProduct._id || !offProduct.product_name) {
return null;
}
const categoriesTags = offProduct.categories_tags || [];
let category = 'baby_food';
for (const tag of categoriesTags) {
const lower = tag.toLowerCase();
if (lower.includes('baby-milk') || lower.includes('infant-milk')) {
category = 'baby_milk';
break;
}
if (lower.includes('baby-cereal') || lower.includes('infant-cereal')) {
category = 'baby_cereal';
break;
}
}
return {
id: offProduct._id,
source: 'openfoodfacts',
name: offProduct.product_name,
brand: offProduct.brands || '',
category,
image_url: offProduct.image_url || null,
nutriscore: offProduct.nutriscore_grade || null,
ingredients: offProduct.ingredients_text || null,
nova_group: offProduct.nova_group || null,
eco_score: offProduct.ecoscore_grade || null,
};
}
export async function searchBabyProducts(query) {
if (!query || query.trim().length < 2) {
return [];
}
const searchTerm = query.trim().toLowerCase();
const cacheKey = `off:baby:${searchTerm}`;
try {
const cachedData = await redisClient.get(cacheKey);
if (cachedData) {
console.log(`Cache hit for OFF search: ${searchTerm}`);
return JSON.parse(cachedData);
}
console.log(`Fetching from OFF API: ${searchTerm}`);
const auth = getOffAuth();
const response = await axios.get(`${OFF_API_BASE}/cgi/search.pl`, {
params: {
search_terms: searchTerm,
json: true,
fields: 'product_name,brands,image_url,nutriscore_grade,ingredients_text,nova_group,ecoscore_grade,categories_tags',
page_size: 20,
action: 'process',
},
timeout: 10000,
validateStatus: (status) => status === 200,
...(auth && { auth }),
});
// Check if response is actually JSON (OFF sometimes returns HTML on error)
if (typeof response.data === 'string' || !response.data.products) {
console.warn(`[OFF] Invalid response for "${searchTerm}": API may be down`);
return [];
}
const products = response.data.products
.map(transformOFFProduct)
.filter(Boolean);
// Only cache non-empty results to avoid caching API failures
if (products.length > 0) {
try {
await redisClient.setEx(cacheKey, CACHE_TTL, JSON.stringify(products));
console.log(`Cached ${products.length} OFF products for: ${searchTerm}`);
} catch (cacheErr) {
// cache write failed, still return the data
}
} else {
console.log(`[OFF] No products found for "${searchTerm}", not caching empty result`);
}
return products;
} catch (error) {
console.error('Error searching baby products from OFF:', error.message);
return [];
}
}
export async function getBabyProductDetails(barcode) {
if (!barcode) {
return null;
}
const cacheKey = `off:product:${barcode}`;
try {
const cachedData = await redisClient.get(cacheKey);
if (cachedData) {
console.log(`Cache hit for OFF product: ${barcode}`);
return JSON.parse(cachedData);
}
console.log(`Fetching OFF product details: ${barcode}`);
const auth = getOffAuth();
const response = await axios.get(`${OFF_API_BASE}/api/v2/product/${barcode}.json`, {
timeout: 10000,
validateStatus: (status) => status === 200,
...(auth && { auth }),
});
if (response.data && response.data.product) {
const product = transformOFFProduct(response.data.product);
if (product) {
try {
await redisClient.setEx(cacheKey, CACHE_TTL, JSON.stringify(product));
console.log(`Cached OFF product: ${barcode}`);
} catch (cacheErr) {
// cache write failed, still return the data
}
return product;
}
}
return null;
} catch (error) {
console.error(`Error fetching OFF product ${barcode}:`, error.message);
return null;
}
}
export async function clearCache(pattern = 'off:*') {
try {
const keys = await redisClient.keys(pattern);
if (keys.length > 0) {
await redisClient.del(keys);
console.log(`Cleared ${keys.length} OFF cache entries`);
return keys.length;
}
return 0;
} catch (error) {
console.error('Error clearing OFF cache:', error);
return 0;
}
}