3429b7e548
- Updated ProductView to use correct endpoint for parapharmacy - Updated getProduct service to handle parapharmacy products - Shows price, category, brand, source for parapharmacy products - Updated labels from 'OFF' to 'Parafarmacia'
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import api from './api';
|
|
|
|
export interface Product {
|
|
id: string;
|
|
_id?: string;
|
|
source: 'cima' | 'promofarma' | 'pharmarket' | 'docmorris' | '1001farma' | 'primor' | 'mifarma';
|
|
name: string;
|
|
brand: string;
|
|
category: string;
|
|
subcategory?: string;
|
|
image_url: string | null;
|
|
source_url?: string;
|
|
price?: number;
|
|
original_price?: number;
|
|
available?: boolean;
|
|
rating?: number;
|
|
review_count?: number;
|
|
active_ingredient?: string;
|
|
dosage?: string;
|
|
form?: string;
|
|
prescription?: string;
|
|
commercialized?: boolean;
|
|
photos?: { tipo: string; url: string }[];
|
|
docs?: { tipo: number; url: string }[];
|
|
}
|
|
|
|
export interface ProductSearchResponse {
|
|
results: Product[];
|
|
total: number;
|
|
page?: number;
|
|
pages?: number;
|
|
sources?: {
|
|
cima: number;
|
|
};
|
|
}
|
|
|
|
export async function searchProducts(query: string): Promise<Product[]> {
|
|
if (!query || query.trim().length < 2) return [];
|
|
try {
|
|
const { data } = await api.get<ProductSearchResponse>('/products/search', {
|
|
params: { q: query }
|
|
});
|
|
return data.results || [];
|
|
} catch (error) {
|
|
console.error('[Products] Search error:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function searchParapharmacy(query: string, options?: {
|
|
category?: string;
|
|
brand?: string;
|
|
page?: number;
|
|
limit?: number;
|
|
}): Promise<ProductSearchResponse> {
|
|
if (!query || query.trim().length < 2) {
|
|
return { results: [], total: 0 };
|
|
}
|
|
try {
|
|
const params: Record<string, string | number> = { q: query };
|
|
if (options?.category) params.category = options.category;
|
|
if (options?.brand) params.brand = options.brand;
|
|
if (options?.page) params.page = options.page;
|
|
if (options?.limit) params.limit = options.limit;
|
|
|
|
const { data } = await api.get<ProductSearchResponse>('/products/parapharmacy/search', {
|
|
params
|
|
});
|
|
return data;
|
|
} catch (error) {
|
|
console.error('[Parapharmacy] Search error:', error);
|
|
return { results: [], total: 0 };
|
|
}
|
|
}
|
|
|
|
export async function getParapharmacyProduct(id: string): Promise<Product | null> {
|
|
try {
|
|
const { data } = await api.get<Product>(`/products/parapharmacy/${id}`);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('[Parapharmacy] Detail error:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getParapharmacyCategories(): Promise<string[]> {
|
|
try {
|
|
const { data } = await api.get<string[]>('/products/parapharmacy/categories');
|
|
return data || [];
|
|
} catch (error) {
|
|
console.error('[Parapharmacy] Categories error:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getParapharmacyBrands(): Promise<string[]> {
|
|
try {
|
|
const { data } = await api.get<string[]>('/products/parapharmacy/brands');
|
|
return data || [];
|
|
} catch (error) {
|
|
console.error('[Parapharmacy] Brands error:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getProduct(source: string, id: string): Promise<Product | null> {
|
|
try {
|
|
// Use different endpoint for parapharmacy products
|
|
const endpoint = source === 'parapharmacy'
|
|
? `/products/parapharmacy/${id}`
|
|
: `/products/${source}/${id}`;
|
|
|
|
const { data } = await api.get<Product>(endpoint);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('[Products] Detail error:', error);
|
|
return null;
|
|
}
|
|
}
|