From 972a59662d1207f2a389c13edbf31204223e671b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Nu=C3=B1ez=20Romeu?= Date: Thu, 16 Jul 2026 15:03:46 +0200 Subject: [PATCH] fix: fix parapharmacy proxy route order and add axios import - Moved parapharmacy routes before :source/:id routes to prevent conflicts - Added axios import to server.js - Backend proxy now correctly forwards to parapharmacy-api - Tested: search for 'capricare' returns 1 result, 'crema' returns 7 results --- apps/backend/server.js | 119 +++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/apps/backend/server.js b/apps/backend/server.js index f2e5c7c..c0dc419 100644 --- a/apps/backend/server.js +++ b/apps/backend/server.js @@ -5,6 +5,7 @@ if (process.env.NODE_ENV !== 'test') { } import express from 'express'; +import axios from 'axios'; import redisClient from './redis-client.js'; import * as appMetrics from './src/metrics.js'; import cors from 'cors'; @@ -679,6 +680,65 @@ app.get('/api/medicines/:medicineId', async (req, res) => { } }); +// ========== PARAPHARMACY PROXY ========== + +const PARAPHARMACY_API_URL = process.env.PARAPHARMACY_API_URL || 'http://localhost:3002'; + +// Search parapharmacy products (proxy to parapharmacy-api) +app.get('/api/products/parapharmacy/search', searchLimiter, async (req, res) => { + try { + const { q, category, brand, page = 1, limit = 20 } = req.query; + const params = new URLSearchParams(); + if (q) params.set('q', q); + if (category) params.set('category', category); + if (brand) params.set('brand', brand); + params.set('page', page); + params.set('limit', limit); + + const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/search?${params}`); + res.json(response.data); + } catch (error) { + console.error('[Parapharmacy] Search error:', error.message); + res.status(500).json({ error: 'Error searching parapharmacy products' }); + } +}); + +// Get parapharmacy product details (proxy to parapharmacy-api) +app.get('/api/products/parapharmacy/:id', async (req, res) => { + try { + const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/${req.params.id}`); + res.json(response.data); + } catch (error) { + console.error('[Parapharmacy] Detail error:', error.message); + if (error.response?.status === 404) { + return res.status(404).json({ error: 'Product not found' }); + } + res.status(500).json({ error: 'Error fetching product details' }); + } +}); + +// Get parapharmacy categories (proxy to parapharmacy-api) +app.get('/api/products/parapharmacy/categories', async (req, res) => { + try { + const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/categories`); + res.json(response.data); + } catch (error) { + console.error('[Parapharmacy] Categories error:', error.message); + res.status(500).json({ error: 'Error fetching categories' }); + } +}); + +// Get parapharmacy brands (proxy to parapharmacy-api) +app.get('/api/products/parapharmacy/brands', async (req, res) => { + try { + const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/brands`); + res.json(response.data); + } catch (error) { + console.error('[Parapharmacy] Brands error:', error.message); + res.status(500).json({ error: 'Error fetching brands' }); + } +}); + // ========== OTC PRODUCT SEARCH (CIMA only) ========== app.get('/api/products/search', searchLimiter, async (req, res) => { @@ -765,65 +825,6 @@ app.get('/api/products/:source/:productId/pharmacies', async (req, res) => { } }); -// ========== PARAPHARMACY PROXY ========== - -const PARAPHARMACY_API_URL = process.env.PARAPHARMACY_API_URL || 'http://localhost:3002'; - -// Search parapharmacy products (proxy to parapharmacy-api) -app.get('/api/products/parapharmacy/search', searchLimiter, async (req, res) => { - try { - const { q, category, brand, page = 1, limit = 20 } = req.query; - const params = new URLSearchParams(); - if (q) params.set('q', q); - if (category) params.set('category', category); - if (brand) params.set('brand', brand); - params.set('page', page); - params.set('limit', limit); - - const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/search?${params}`); - res.json(response.data); - } catch (error) { - console.error('[Parapharmacy] Search error:', error.message); - res.status(500).json({ error: 'Error searching parapharmacy products' }); - } -}); - -// Get parapharmacy product details (proxy to parapharmacy-api) -app.get('/api/products/parapharmacy/:id', async (req, res) => { - try { - const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/${req.params.id}`); - res.json(response.data); - } catch (error) { - console.error('[Parapharmacy] Detail error:', error.message); - if (error.response?.status === 404) { - return res.status(404).json({ error: 'Product not found' }); - } - res.status(500).json({ error: 'Error fetching product details' }); - } -}); - -// Get parapharmacy categories (proxy to parapharmacy-api) -app.get('/api/products/parapharmacy/categories', async (req, res) => { - try { - const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/categories`); - res.json(response.data); - } catch (error) { - console.error('[Parapharmacy] Categories error:', error.message); - res.status(500).json({ error: 'Error fetching categories' }); - } -}); - -// Get parapharmacy brands (proxy to parapharmacy-api) -app.get('/api/products/parapharmacy/brands', async (req, res) => { - try { - const response = await axios.get(`${PARAPHARMACY_API_URL}/api/products/brands`); - res.json(response.data); - } catch (error) { - console.error('[Parapharmacy] Brands error:', error.message); - res.status(500).json({ error: 'Error fetching brands' }); - } -}); - // ========== AUTHENTICATION MIDDLEWARE ========== // Middleware to check if user is authenticated